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

248 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()
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()
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 return $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 return $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 return $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
147 $newConfig = new \stdClass();
148 $newConfig->export_separator = $separator;
149 $newConfig->export_charset = $charset;
150 $newConfig->export_excelsecurity = $excelSecurity;
151 $newConfig->export_fields = implode(',', array_merge($fieldsToExport, array_keys($customFieldsToExport)));
152 if (empty($selectedUsers)) {
153 $newConfig->export_lists = implode(',', $listsToExport);
154 }
155 $this->config->save($newConfig);
156
157 foreach ($fieldsToExport as $oneField) {
158 acym_secureDBColumn($oneField);
159 }
160 $query = 'SELECT DISTINCT user.`id`, user.`'.implode('`, user.`', $fieldsToExport).'` FROM #__acym_user AS user';
161
162 $where = [];
163 $flagSegment = 0;
164
165 if (!empty($selectedUsersArray)) {
166 acym_arrayToInteger($selectedUsersArray);
167 $where[] = 'user.id IN ('.implode(',', $selectedUsersArray).')';
168 } else {
169 if ($usersToExport == 'list' && !empty($listsToExport)) {
170 acym_arrayToInteger($listsToExport);
171
172 $listJoin = '#__acym_user_has_list AS userlist ON userlist.user_id = user.id AND userlist.list_id IN ('.implode(',', $listsToExport).')';
173
174 if ($exportUsersType == 'none') {
175 $query .= ' LEFT JOIN '.$listJoin;
176 $where[] = 'userlist.status IS NULL';
177 } else {
178 $query .= ' JOIN '.$listJoin;
179 }
180 if ($exportUsersType == 'sub') $where[] = 'userlist.status = 1';
181 if ($exportUsersType == 'unsub') $where[] = 'userlist.status = 0';
182 }
183
184 $segmentChosen = acym_getVar('string', 'export_segment', '');
185 if ('' !== $segmentChosen) {
186 $segmentClass = new SegmentClass();
187 $segment = $segmentClass->getOneById($segmentChosen);
188 if (!empty($segment)) {
189 $automationHelpers = [];
190 foreach ($segment->filters as $or => $orValues) {
191 if (empty($orValues)) continue;
192 $automationHelpers[$or] = new AutomationHelper();
193 foreach ($orValues as $and => $andValues) {
194 $and = intval($and);
195 foreach ($andValues as $filterName => $options) {
196 acym_trigger('onAcymProcessFilter_'.$filterName, [&$automationHelpers[$or], &$options, $and.'_'.$or]);
197 }
198 }
199 }
200 $flagSegment = SegmentsController::FLAG_EXPORT_USERS;
201 foreach ($automationHelpers as $automationHelper) {
202 $automationHelper->addFlag($flagSegment);
203 }
204
205 $where[] = 'user.automation LIKE "%a'.intval($flagSegment).'a%"';
206 }
207 }
208 }
209
210 $filtersListingSearch = $this->getVarFiltersListing('string', 'users_search', '');
211 $filtersListingStatus = $this->getVarFiltersListing('string', 'users_status', '');
212
213 if (!empty($filtersListingSearch)) {
214 $search = acym_escapeDB('%'.$filtersListingSearch.'%');
215 $where[] = 'user.name LIKE '.$search.' OR user.email LIKE '.$search.' OR user.id LIKE '.$search;
216 }
217
218 if (!empty($filtersListingStatus)) {
219 if ($filtersListingStatus == 'active') {
220 $where[] = 'user.active = 1';
221 } elseif ($filtersListingStatus == 'inactive') {
222 $where[] = 'user.active = 0';
223 } elseif ($filtersListingStatus == 'confirmed') {
224 $where[] = 'user.confirmed = 1';
225 } else {
226 $where[] = 'user.confirmed = 0';
227 }
228 }
229
230 if (!empty($where)) {
231 $query .= ' WHERE ('.implode(') AND (', $where).')';
232 }
233
234 $exportHelper = new ExportHelper();
235 $exportHelper->exportCSV($query, $fieldsToExport, $customFieldsToExport, $specialFieldsToExport, $realSeparators[$separator], $charset, null, $flagSegment);
236
237 exit;
238 }
239
240 private function exportError($message)
241 {
242 acym_enqueueMessage($message, 'error', 0);
243 acym_setNoTemplate(false);
244
245 return acym_redirect(acym_completeLink('users&task=export', false, true));
246 }
247 }
248