PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.11.1
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.11.1
11.1.1 11.1.0 11.0.5 11.0.4 11.0.3 11.0.2 11.0.1 11.0.0 10.11.1 10.11.0 10.10.2 10.10.1 10.10.0 10.9.1 trunk 10.0.0 10.0.1 10.1.0 10.1.1 10.1.2 10.1.3 10.1.4 10.2.0 10.2.1 10.2.2 All 62 releases
acymailing / back / Controllers / Users / Export.php

Export.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 10.11.1, at back/Controllers/Users/Export.php

252 lines 9.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AcyMailing\Controllers\Users;
4
5 use AcyMailing\Classes\FieldClass;
6 use AcyMailing\Classes\ListClass;
7 use AcyMailing\Classes\SegmentClass;
8 use AcyMailing\Classes\UserClass;
9 use AcyMailing\Controllers\SegmentsController;
10 use AcyMailing\Helpers\AutomationHelper;
11 use AcyMailing\Helpers\EncodingHelper;
12 use AcyMailing\Helpers\EntitySelectHelper;
13 use AcyMailing\Helpers\ExportHelper;
14
15 trait Export
16 {
17 public function export(): void
18 {
19 acym_setVar('layout', 'export');
20 $this->breadcrumb[acym_translation('ACYM_EXPORT_SUBSCRIBERS')] = acym_completeLink('users&task=export');
21
22 $listClass = new ListClass();
23 $lists = $listClass->getAll();
24
25 $preselectList = acym_getVar('boolean', 'preselectList', false);
26 $checkedElements = acym_getVar('array', 'elements_checked', []);
27
28 $filtersListing = [];
29
30 $filtersListing['list'] = $this->getVarFiltersListing('int', 'users_list', 0);
31 $filtersListing['list_status'] = $this->getVarFiltersListing('string', 'list_status', 'all');
32
33 $filtersSegment = $this->getVarFiltersListing('int', 'segment', 0);
34 $segmentClass = new SegmentClass();
35 $availableSegments = $segmentClass->getAllForSelect();
36
37 $list = acym_getVar('int', 'users_list', 0);
38 if (!empty($list)) {
39 $preselectList = true;
40 $checkedElements = [$list];
41 }
42
43 if (!empty($filtersListing['list'])) {
44 $preselectList = true;
45 $checkedElements = [$filtersListing['list']];
46 }
47
48 $fields = acym_getColumns('user');
49
50 $fields = array_diff($fields, ['key', 'id']);
51
52 $fieldClass = new FieldClass();
53 $customFields = $fieldClass->getAll();
54
55 $entityHelper = new EntitySelectHelper();
56 $encodingHelper = new EncodingHelper();
57 $userClass = new UserClass();
58
59 if ($preselectList) {
60 $entitySelect = $entityHelper->entitySelect('list', ['join' => 'join_lists-'.implode(',', $checkedElements)], $entityHelper->getColumnsForList('lists.list_id', true));
61 } else {
62 $entitySelect = $entityHelper->entitySelect('list', ['join' => ''], $entityHelper->getColumnsForList('', true));
63 }
64
65 $data = [
66 'lists' => $lists,
67 'checkedElements' => $checkedElements,
68 'fields' => $fields,
69 'customfields' => $customFields,
70 'coreFields' => [1, 2, $fieldClass->getLanguageFieldId()],
71 'isPreselectedList' => $preselectList,
72 'entitySelect' => $entitySelect,
73 'exportListStatus' => $filtersListing['list_status'],
74 'encodingHelper' => $encodingHelper,
75 'userClass' => $userClass,
76 'segments' => $availableSegments,
77 'preselectedSegment' => $filtersSegment,
78 ];
79
80 parent::display($data);
81 }
82
83 public function doexport(): void
84 {
85 acym_checkToken();
86 acym_increasePerf();
87
88 $usersToExport = acym_getVar('string', 'export_users-to-export', 'all');
89 $selectedLists = acym_getVar('string', 'acym__entity_select__selected', '[]');
90 $listsToExport = json_decode(empty($selectedLists) ? '[]' : $selectedLists, true);
91 if ($usersToExport === 'list' && empty($listsToExport)) {
92 acym_enqueueMessage(acym_translation('ACYM_EXPORT_SELECT_LIST'), 'error');
93
94 $this->exportError(acym_translation('ACYM_EXPORT_SELECT_LIST'));
95 }
96 acym_arrayToInteger($listsToExport);
97
98 $exportUsersType = 'all';
99 if ($usersToExport == 'list') {
100 $exportUsersType = acym_getVar('string', 'export_list', 'all');
101 }
102
103 $selectedUsers = acym_getVar('string', 'selected_users', null);
104
105 if (!empty($selectedUsers)) {
106 $selectedUsersArray = explode(',', $selectedUsers);
107 acym_arrayToInteger($selectedUsersArray);
108 }
109
110 $fieldsToExport = acym_getVar('array', 'export_fields', []);
111 if (empty($fieldsToExport)) {
112 acym_setVar('elements_checked', empty($selectedUsersArray) ? [] : $selectedUsersArray);
113
114 $this->exportError(acym_translation('ACYM_EXPORT_SELECT_FIELD'));
115 }
116
117 $tableFields = acym_getColumns('user');
118 $fieldClass = new FieldClass();
119 $customFields = $fieldClass->getAll();
120
121 $forbiddenFields = ['key', 'id'];
122 $fieldsToExport = array_diff($fieldsToExport, $forbiddenFields);
123 $tableFields = array_diff($tableFields, $forbiddenFields);
124
125 $customFieldsToExport = [];
126 $specialFieldsToExport = [];
127 foreach ($fieldsToExport as $i => $oneField) {
128 if (in_array($oneField, ['subscribe_date', 'unsubscribe_date'])) {
129 $specialFieldsToExport[] = $oneField;
130 unset($fieldsToExport[$i]);
131 } elseif (!empty($customFields[$oneField])) {
132 $customFieldsToExport[$oneField] = $customFields[$oneField]->namekey;
133 unset($fieldsToExport[$i]);
134 }
135 }
136
137 $notAllowedFields = array_diff($fieldsToExport, $tableFields);
138 if (!empty($notAllowedFields)) {
139 $this->exportError(acym_translationSprintf('ACYM_NOT_ALLOWED_FIELDS', implode(', ', $notAllowedFields), implode(', ', $tableFields)));
140 }
141
142 $charset = acym_getVar('string', 'export_charset', 'UTF-8');
143 $excelSecurity = acym_getVar('string', 'export_excelsecurity', 0);
144 $separator = acym_getVar('string', 'export_separator', 'comma');
145 $realSeparators = ['comma' => ',', 'semicol' => ';'];
146 if (!in_array($separator, array_keys($realSeparators))) {
147 $separator = 'comma';
148 }
149
150 $newConfig = [
151 'export_separator' => $separator,
152 'export_charset' => $charset,
153 'export_excelsecurity' => $excelSecurity,
154 'export_fields' => implode(',', array_merge($fieldsToExport, array_keys($customFieldsToExport))),
155 ];
156 if (empty($selectedUsers)) {
157 $newConfig['export_lists'] = implode(',', $listsToExport);
158 }
159 $this->config->saveConfig($newConfig);
160
161 foreach ($fieldsToExport as $oneField) {
162 acym_secureDBColumn($oneField);
163 }
164 $query = 'SELECT DISTINCT user.`id`, user.`'.implode('`, user.`', $fieldsToExport).'` FROM #__acym_user AS user';
165
166 $where = [];
167 $flagSegment = 0;
168
169 if (!empty($selectedUsersArray)) {
170 acym_arrayToInteger($selectedUsersArray);
171 $where[] = 'user.id IN ('.implode(',', $selectedUsersArray).')';
172 } else {
173 if ($usersToExport == 'list' && !empty($listsToExport)) {
174 acym_arrayToInteger($listsToExport);
175
176 $listJoin = '#__acym_user_has_list AS userlist ON userlist.user_id = user.id AND userlist.list_id IN ('.implode(',', $listsToExport).')';
177
178 if ($exportUsersType == 'none') {
179 $query .= ' LEFT JOIN '.$listJoin;
180 $where[] = 'userlist.status IS NULL';
181 } else {
182 $query .= ' JOIN '.$listJoin;
183 }
184 if ($exportUsersType == 'sub') $where[] = 'userlist.status = 1';
185 if ($exportUsersType == 'unsub') $where[] = 'userlist.status = 0';
186 }
187
188 $segmentChosen = acym_getVar('string', 'export_segment', '');
189 if ('' !== $segmentChosen) {
190 $segmentClass = new SegmentClass();
191 $segment = $segmentClass->getOneById($segmentChosen);
192 if (!empty($segment)) {
193 $automationHelpers = [];
194 foreach ($segment->filters as $or => $orValues) {
195 if (empty($orValues)) continue;
196 $automationHelpers[$or] = new AutomationHelper();
197 foreach ($orValues as $and => $andValues) {
198 $and = intval($and);
199 foreach ($andValues as $filterName => $options) {
200 acym_trigger('onAcymProcessFilter_'.$filterName, [&$automationHelpers[$or], &$options, $and.'_'.$or]);
201 }
202 }
203 }
204 $flagSegment = SegmentsController::FLAG_EXPORT_USERS;
205 foreach ($automationHelpers as $automationHelper) {
206 $automationHelper->addFlag($flagSegment);
207 }
208
209 $where[] = 'user.automation LIKE "%a'.intval($flagSegment).'a%"';
210 }
211 }
212 }
213
214 $filtersListingSearch = $this->getVarFiltersListing('string', 'users_search', '');
215 $filtersListingStatus = $this->getVarFiltersListing('string', 'users_status', '');
216
217 if (!empty($filtersListingSearch)) {
218 $search = acym_escapeDB('%'.$filtersListingSearch.'%');
219 $where[] = 'user.name LIKE '.$search.' OR user.email LIKE '.$search.' OR user.id LIKE '.$search;
220 }
221
222 if (!empty($filtersListingStatus)) {
223 if ($filtersListingStatus == 'active') {
224 $where[] = 'user.active = 1';
225 } elseif ($filtersListingStatus == 'inactive') {
226 $where[] = 'user.active = 0';
227 } elseif ($filtersListingStatus == 'confirmed') {
228 $where[] = 'user.confirmed = 1';
229 } else {
230 $where[] = 'user.confirmed = 0';
231 }
232 }
233
234 if (!empty($where)) {
235 $query .= ' WHERE ('.implode(') AND (', $where).')';
236 }
237
238 $exportHelper = new ExportHelper();
239 $exportHelper->exportCSV($query, $fieldsToExport, $customFieldsToExport, $specialFieldsToExport, $realSeparators[$separator], $charset, '', $flagSegment);
240
241 exit;
242 }
243
244 private function exportError(string $message): void
245 {
246 acym_enqueueMessage($message, 'error', 0);
247 acym_setNoTemplate(false);
248
249 acym_redirect(acym_completeLink('users&task=export', false, true));
250 }
251 }
252