PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.2.2
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.2.2
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.2.2, at back/Controllers/Users/Export.php

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