| @@ -1,45 +1,47 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace AcyMailing\Helpers; |
| 4 | 4 | |
| 5 | +use AcyMailing\Classes\FieldClass; | |
| 5 | 6 | use AcyMailing\Classes\MailClass; |
| 7 | +use AcyMailing\Classes\UrlClickClass; | |
| 6 | 8 | use AcyMailing\Classes\UserClass; |
| 9 | +use AcyMailing\Classes\UserStatClass; | |
| 7 | 10 | use AcyMailing\Core\AcymObject; |
| 8 | 11 | |
| 9 | 12 | class UserHelper extends AcymObject |
| 10 | 13 | { |
| 11 | - public function exportdata(int $id): void | |
| 14 | + public function getUserData(object $user): array | |
| 12 | 15 | { |
| 13 | - if (empty($id)) { | |
| 14 | - die(acym_translation('ACYM_USER_NOT_FOUND')); | |
| 15 | - } | |
| 16 | + $userData = []; | |
| 17 | + $this->prepareProfileData($user, $userData); | |
| 18 | + $this->prepareSubscriptionData($user, $userData); | |
| 19 | + $this->prepareStatisticsData($user, $userData); | |
| 20 | + $this->prepareClickStatisticsData($user, $userData); | |
| 16 | 21 | |
| 22 | + return $userData; | |
| 23 | + } | |
| 24 | + | |
| 25 | + private function prepareProfileData(object $user, array &$exportedData): void | |
| 26 | + { | |
| 17 | 27 | $userClass = new UserClass(); |
| 18 | - $user = $userClass->getOneByIdWithCustomFields($id); | |
| 19 | - if (empty($user)) { | |
| 20 | - die(acym_translation('ACYM_USER_NOT_FOUND')); | |
| 21 | - } | |
| 28 | + $userClass->getAllUserFields($user); | |
| 22 | 29 | |
| 23 | - acym_enqueueMessage(acym_translation('ACYM_ERROR'), 'error'); | |
| 30 | + $profileData = []; | |
| 24 | 31 | |
| 25 | - $dateFields = ['creation_date', 'userstats_open_date', 'userstats_send_date', 'urlclick_date_click']; | |
| 26 | - $excludedFields = ['id', 'cms_id', 'key', 'source']; | |
| 32 | + $fieldsClass = new FieldClass(); | |
| 33 | + $fields = $fieldsClass->getAll('namekey'); | |
| 27 | 34 | |
| 28 | - $exportFiles = []; | |
| 35 | + $uploadFolder = acym_rootURI().trim(acym_cleanPath(html_entity_decode(acym_getFilesFolder(true))), DS.' ').DS; | |
| 36 | + $coreFields = ['confirmation_date', 'confirmation_ip', 'language']; | |
| 29 | 37 | |
| 30 | - $xml = new \SimpleXMLElement('<xml/>'); | |
| 31 | - $userNode = $xml->addChild('user'); | |
| 32 | - | |
| 33 | - $fields = acym_loadObjectList('SELECT name, field.option as options, type, value FROM #__acym_field as field', 'name'); | |
| 34 | - $uploadFolder = trim(acym_cleanPath(html_entity_decode(acym_getFilesFolder(true))), DS.' ').DS; | |
| 35 | - | |
| 36 | - | |
| 37 | 38 | foreach ($user as $column => $value) { |
| 38 | - if (in_array($column, $excludedFields) || strlen($value) == 0) { | |
| 39 | + if (is_null($value) || strlen($value) === 0) { | |
| 39 | 40 | continue; |
| 40 | 41 | } |
| 41 | - if (in_array($column, $dateFields)) { | |
| 42 | + | |
| 43 | + if ($column === 'confirmation_date') { | |
| 42 | 44 | if (empty($value)) { |
| 43 | 45 | continue; |
| 44 | 46 | } |
| 45 | 47 | $value = acym_getDate($value, '%Y-%m-%d %H:%M:%S'); |
| @@ -44,153 +46,177 @@ | ||
| 44 | 46 | } |
| 45 | 47 | $value = acym_getDate($value, '%Y-%m-%d %H:%M:%S'); |
| 46 | 48 | } |
| 47 | 49 | |
| 48 | - if (empty($fields[$column])) { | |
| 49 | - $userNode->addChild($column, acym_escape($value)); | |
| 50 | + if (empty($fields[$column]) || $fields[$column]->core == 1) { | |
| 51 | + if (in_array($column, $coreFields)) { | |
| 52 | + $profileData[] = [ | |
| 53 | + 'name' => ucfirst(str_replace('_', ' ', $column)), | |
| 54 | + 'value' => $value, | |
| 55 | + ]; | |
| 56 | + } | |
| 50 | 57 | continue; |
| 51 | 58 | } |
| 52 | 59 | |
| 53 | - if ($fields[$column]->type == 'date') { | |
| 54 | - $valueTmp = is_array(json_decode($value)) ? json_decode($value) : $value; | |
| 60 | + if ($fields[$column]->type === 'date') { | |
| 61 | + $decodedValue = @json_decode($value, true); | |
| 62 | + $valueTmp = is_array($decodedValue) ? $decodedValue : $value; | |
| 63 | + | |
| 55 | 64 | $value = is_array($valueTmp) ? implode('/', $valueTmp) : $valueTmp; |
| 56 | 65 | } |
| 57 | 66 | |
| 58 | - if (in_array($fields[$column]->type, ["multiple_dropdown", "single_dropdown", "radio", "checkbox"])) { | |
| 59 | - $selectedValues = []; | |
| 60 | - if (!empty($fields[$column]->value)) { | |
| 61 | - $options = json_decode($fields[$column]->value); | |
| 62 | - } | |
| 63 | - $valueTmp = is_array(json_decode($value)) || is_object(json_decode($value)) ? json_decode($value) : $value; | |
| 64 | - if (!is_array($valueTmp) && !is_string($valueTmp)) { | |
| 65 | - foreach ($valueTmp as $key => $one) { | |
| 66 | - $selectedValues[] = $key; | |
| 67 | - } | |
| 67 | + if (in_array($fields[$column]->type, ['gravatar', 'file'])) { | |
| 68 | + $userFilePath = $uploadFolder.'userfiles'.DS.$user->id.DS.$value; | |
| 69 | + if (file_exists(ACYM_ROOT.$userFilePath)) { | |
| 70 | + $value = $userFilePath; | |
| 68 | 71 | } else { |
| 69 | - foreach ($options as $key => $optionVal) { | |
| 70 | - if (is_string($valueTmp) && $optionVal->value == $valueTmp) { | |
| 71 | - $selectedValues[] = !empty($optionVal->title) ? $optionVal->title : $optionVal->value; | |
| 72 | - } elseif (!is_string($valueTmp) && in_array($optionVal->value, $valueTmp)) { | |
| 73 | - $selectedValues[] = !empty($optionVal->title) ? $optionVal->title : $optionVal->value; | |
| 74 | - } | |
| 75 | - } | |
| 72 | + $value = $uploadFolder.'userfiles'.DS.$value; | |
| 76 | 73 | } |
| 74 | + } | |
| 77 | 75 | |
| 78 | - $value = implode(',', $selectedValues); | |
| 79 | - } elseif (in_array($fields[$column]->type, ['gravatar', 'file'])) { | |
| 80 | - $data = acym_fileGetContent(ACYM_ROOT.$uploadFolder.'userfiles'.DS.$value); | |
| 81 | - $value = str_replace('_', ' ', substr($value, strpos($value, '_'))); | |
| 82 | - $exportFiles[] = ['name' => $value, 'data' => $data]; | |
| 83 | - continue; | |
| 84 | - } | |
| 76 | + $profileData[] = [ | |
| 77 | + 'name' => $fields[$column]->name, | |
| 78 | + 'value' => $value, | |
| 79 | + ]; | |
| 80 | + } | |
| 85 | 81 | |
| 86 | - $userNode->addChild($column, acym_escape($value)); | |
| 82 | + $exportedData[] = [ | |
| 83 | + 'group_id' => 'acyprofile', | |
| 84 | + 'group_label' => 'Newsletter profile', | |
| 85 | + 'item_id' => 'user-'.$user->id, | |
| 86 | + 'data' => $profileData, | |
| 87 | + ]; | |
| 88 | + } | |
| 89 | + | |
| 90 | + private function prepareSubscriptionData(object $user, array &$exportedData): void | |
| 91 | + { | |
| 92 | + $userClass = new UserClass(); | |
| 93 | + $subscription = $userClass->getUserSubscriptionById($user->id); | |
| 94 | + | |
| 95 | + if (empty($subscription)) { | |
| 96 | + return; | |
| 87 | 97 | } |
| 88 | 98 | |
| 89 | - $subscription = acym_loadObjectList( | |
| 90 | - 'SELECT list.name, list.id, user_list.subscription_date, user_list.unsubscribe_date, user_list.status FROM #__acym_user_has_list AS user_list JOIN #__acym_list AS list ON list.id = user_list.list_id WHERE user_list.user_id = '.intval( | |
| 91 | - $id | |
| 92 | - ) | |
| 93 | - ); | |
| 94 | - if (!empty($subscription)) { | |
| 95 | - $dateFields = ['subscription_date', 'unsubscribe_date']; | |
| 96 | - $subscriptionNode = $xml->addChild('subscription'); | |
| 99 | + $subscriptionData = []; | |
| 97 | 100 | |
| 98 | - foreach ($subscription as $oneSubscription) { | |
| 99 | - $list = $subscriptionNode->addChild('list'); | |
| 101 | + foreach ($subscription as $oneSubscription) { | |
| 102 | + $listName = empty($oneSubscription->display_name) ? $oneSubscription->name : $oneSubscription->display_name; | |
| 100 | 103 | |
| 101 | - $oneSubscription = get_object_vars($oneSubscription); | |
| 102 | - foreach ($oneSubscription as $column => $value) { | |
| 103 | - if (strlen($value) == 0) { | |
| 104 | - continue; | |
| 105 | - } | |
| 106 | - if (in_array($column, $dateFields)) { | |
| 107 | - if (empty($value)) { | |
| 108 | - continue; | |
| 109 | - } | |
| 110 | - $value = acym_getDate($value, '%Y-%m-%d %H:%M:%S'); | |
| 111 | - } | |
| 112 | - if ($column == 'status') { | |
| 113 | - $value = str_replace(['-1', '1', '2'], ['Unsubscribed', 'Subscribed', 'Waiting for confirmation'], $value); | |
| 114 | - } | |
| 115 | - $list->addChild($column, acym_escape($value)); | |
| 116 | - } | |
| 104 | + $value = acym_translation('ACYM_SUBSCRIPTION_DATE').' '.acym_getDate($oneSubscription->subscription_date, '%Y-%m-%d %H:%M:%S'); | |
| 105 | + | |
| 106 | + if (!empty($oneSubscription->unsubscribe_date) && $oneSubscription->status == 0) { | |
| 107 | + $value .= ' / '.acym_translation('ACYM_UNSUBSCRIPTION_DATE').' '.acym_getDate($oneSubscription->unsubscribe_date, '%Y-%m-%d %H:%M:%S'); | |
| 117 | 108 | } |
| 109 | + | |
| 110 | + $subscriptionData[] = [ | |
| 111 | + 'name' => $listName, | |
| 112 | + 'value' => $value, | |
| 113 | + ]; | |
| 118 | 114 | } |
| 119 | 115 | |
| 116 | + $exportedData[] = [ | |
| 117 | + 'group_id' => 'acysubscription', | |
| 118 | + 'group_label' => acym_translation('ACYM_LISTS'), | |
| 119 | + 'item_id' => 'subscription-'.$user->id, | |
| 120 | + 'data' => $subscriptionData, | |
| 121 | + ]; | |
| 122 | + } | |
| 123 | + | |
| 124 | + private function prepareStatisticsData(object $user, array &$exportedData): void | |
| 125 | + { | |
| 120 | 126 | $mailClass = new MailClass(); |
| 121 | - $statistics = $mailClass->decode( | |
| 122 | - acym_loadObjectList( | |
| 123 | - 'SELECT mail.subject, user_stats.* FROM #__acym_user_stat AS user_stats JOIN #__acym_mail AS mail ON mail.id = user_stats.mail_id WHERE user_stats.user_id = '.intval( | |
| 124 | - $id | |
| 125 | - ) | |
| 126 | - ) | |
| 127 | - ); | |
| 128 | - if (!empty($statistics)) { | |
| 129 | - $dateFields = ['send_date', 'open_date']; | |
| 130 | - $excludedFields = ['user_id']; | |
| 131 | - $statisticsNode = $xml->addChild('statistics'); | |
| 127 | + $userStatClass = new UserStatClass(); | |
| 128 | + $statistics = $userStatClass->getAllUserStatByUserId($user->id); | |
| 129 | + if (empty($statistics)) { | |
| 130 | + return; | |
| 131 | + } | |
| 132 | + $statistics = $mailClass->decode($statistics); | |
| 132 | 133 | |
| 133 | - foreach ($statistics as $oneStat) { | |
| 134 | - $detailedStat = $statisticsNode->addChild('email'); | |
| 134 | + foreach ($statistics as $oneStatistics) { | |
| 135 | + if (empty($oneStatistics->sent)) { | |
| 136 | + continue; | |
| 137 | + } | |
| 135 | 138 | |
| 136 | - $oneStat = get_object_vars($oneStat); | |
| 137 | - foreach ($oneStat as $column => $value) { | |
| 138 | - if (in_array($column, $excludedFields) || strlen($value) == 0) { | |
| 139 | - continue; | |
| 140 | - } | |
| 141 | - if (in_array($column, $dateFields)) { | |
| 142 | - if (empty($value)) { | |
| 143 | - continue; | |
| 144 | - } | |
| 145 | - $value = acym_getDate($value, '%Y-%m-%d %H:%M:%S'); | |
| 146 | - } | |
| 139 | + $statisticsData = []; | |
| 140 | + $statisticsData[] = [ | |
| 141 | + 'name' => acym_translation('ACYM_EMAIL_SUBJECT'), | |
| 142 | + 'value' => strlen($oneStatistics->subject) > 50 ? substr($oneStatistics->subject, 0, 50).'...' : $oneStatistics->subject, | |
| 143 | + ]; | |
| 147 | 144 | |
| 148 | - $detailedStat->addChild($column, acym_escape($value)); | |
| 149 | - } | |
| 145 | + $statisticsData[] = [ | |
| 146 | + 'name' => acym_translation('ACYM_SEND_DATE'), | |
| 147 | + 'value' => acym_getDate($oneStatistics->send_date, 'Y-m-d H:i:s'), | |
| 148 | + ]; | |
| 149 | + | |
| 150 | + $statisticsData[] = [ | |
| 151 | + 'name' => acym_translation('ACYM_OPENED'), | |
| 152 | + 'value' => acym_translation(empty($oneStatistics->open) ? 'ACYM_NO' : 'ACYM_YES'), | |
| 153 | + ]; | |
| 154 | + | |
| 155 | + if (!empty($oneStatistics->open_date)) { | |
| 156 | + $statisticsData[] = [ | |
| 157 | + 'name' => acym_translation('ACYM_OPEN_DATE'), | |
| 158 | + 'value' => acym_getDate($oneStatistics->open_date, 'Y-m-d H:i:s'), | |
| 159 | + ]; | |
| 150 | 160 | } |
| 151 | - } | |
| 152 | 161 | |
| 153 | - $clickStats = acym_loadObjectList( | |
| 154 | - 'SELECT url.url, url_click.date_click | |
| 155 | - FROM #__acym_url_click AS url_click | |
| 156 | - JOIN #__acym_url AS url ON url.id = url_click.url_id | |
| 157 | - WHERE url_click.user_id = '.intval($id) | |
| 158 | - ); | |
| 159 | - if (!empty($clickStats)) { | |
| 160 | - $dateFields = ['date_click']; | |
| 161 | - $clickStatsNode = $xml->addChild('click_statistics'); | |
| 162 | + if (!empty($oneStatistics->device)) { | |
| 163 | + $statisticsData[] = [ | |
| 164 | + 'name' => acym_translation('ACYM_DEVICE_COLUMN_STAT'), | |
| 165 | + 'value' => $oneStatistics->device, | |
| 166 | + ]; | |
| 167 | + } | |
| 162 | 168 | |
| 163 | - foreach ($clickStats as $oneClick) { | |
| 164 | - $click = $clickStatsNode->addChild('click'); | |
| 169 | + if (!empty($oneStatistics->opened_with)) { | |
| 170 | + $statisticsData[] = [ | |
| 171 | + 'name' => acym_translation('ACYM_OPENED_WITH_COLUMN_STAT'), | |
| 172 | + 'value' => $oneStatistics->opened_with, | |
| 173 | + ]; | |
| 174 | + } | |
| 165 | 175 | |
| 166 | - $oneClick = get_object_vars($oneClick); | |
| 167 | - foreach ($oneClick as $column => $value) { | |
| 168 | - if (strlen($value) == 0) { | |
| 169 | - continue; | |
| 170 | - } | |
| 171 | - if (in_array($column, $dateFields)) { | |
| 172 | - if (empty($value)) { | |
| 173 | - continue; | |
| 174 | - } | |
| 175 | - $value = acym_getDate($value, '%Y-%m-%d %H:%M:%S'); | |
| 176 | - } | |
| 176 | + $exportedData[] = [ | |
| 177 | + 'group_id' => 'acystatistics', | |
| 178 | + 'group_label' => acym_translation('ACYM_STATISTICS'), | |
| 179 | + 'item_id' => 'statistics-'.$user->id.'-'.$oneStatistics->mail_id, | |
| 180 | + 'data' => $statisticsData, | |
| 181 | + ]; | |
| 182 | + } | |
| 183 | + } | |
| 177 | 184 | |
| 178 | - $click->addChild($column, acym_escape($value)); | |
| 179 | - } | |
| 185 | + private function prepareClickStatisticsData(object $user, array &$exportedData): void | |
| 186 | + { | |
| 187 | + $urlClickClass = new UrlClickClass(); | |
| 188 | + $statistics = $urlClickClass->getClicksByUserId($user->id); | |
| 189 | + if (empty($statistics)) { | |
| 190 | + return; | |
| 191 | + } | |
| 192 | + | |
| 193 | + foreach ($statistics as $oneStatistics) { | |
| 194 | + if (empty($oneStatistics->click)) { | |
| 195 | + continue; | |
| 180 | 196 | } |
| 181 | - } | |
| 182 | 197 | |
| 183 | - $exportFiles[] = ['name' => 'user_data.xml', 'data' => $xml->asXML()]; | |
| 198 | + $statisticsData = []; | |
| 199 | + $statisticsData[] = [ | |
| 200 | + 'name' => acym_translation('ACYM_URL'), | |
| 201 | + 'value' => strlen($oneStatistics->url) > 200 ? substr($oneStatistics->url, 0, 197).'...' : $oneStatistics->url, | |
| 202 | + ]; | |
| 184 | 203 | |
| 185 | - $tempFolder = ACYM_MEDIA.'tmp'.DS; | |
| 186 | - acym_createArchive($tempFolder.'export_data_user_'.$id, $exportFiles); | |
| 204 | + $statisticsData[] = [ | |
| 205 | + 'name' => acym_translation('ACYM_CLICK_DATE'), | |
| 206 | + 'value' => acym_getDate($oneStatistics->date_click, 'Y-m-d H:i:s'), | |
| 207 | + ]; | |
| 187 | 208 | |
| 188 | - $exportHelper = new ExportHelper(); | |
| 189 | - $exportHelper->setDownloadHeaders('export_data_user_'.$id, 'zip'); | |
| 190 | - readfile($tempFolder.'export_data_user_'.$id.'.zip'); | |
| 209 | + $statisticsData[] = [ | |
| 210 | + 'name' => acym_translation('ACYM_TOTAL_CLICKS'), | |
| 211 | + 'value' => $oneStatistics->click, | |
| 212 | + ]; | |
| 191 | 213 | |
| 192 | - ignore_user_abort(true); | |
| 193 | - unlink($tempFolder.'export_data_user_'.$id.'.zip'); | |
| 194 | - exit; | |
| 214 | + $exportedData[] = [ | |
| 215 | + 'group_id' => 'acyclickstatistics', | |
| 216 | + 'group_label' => acym_translation('ACYM_USER_CLICK_DETAILS'), | |
| 217 | + 'item_id' => 'clickstatistics-'.$user->id.'-'.$oneStatistics->url_id, | |
| 218 | + 'data' => $statisticsData, | |
| 219 | + ]; | |
| 220 | + } | |
| 195 | 221 | } |
| 196 | 222 | } |