PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.1.4
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.1.4
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 / Edition.php

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

400 lines 15.3 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\HistoryClass;
7 use AcyMailing\Classes\UrlClickClass;
8 use AcyMailing\Classes\UserClass;
9 use AcyMailing\Classes\UserStatClass;
10 use AcyMailing\Helpers\EntitySelectHelper;
11 use AcyMailing\Helpers\TabHelper;
12
13 trait Edition
14 {
15 public function edit(): void
16 {
17 acym_setVar('layout', 'edit');
18
19 $data = [];
20 $data['tab'] = new TabHelper();
21
22 $userId = acym_getVar('int', 'userId', 0);
23
24 if (!$this->prepareUserEdit($data, $userId)) return;
25 $this->prepareEntitySelectEdit($data, $userId);
26 $this->prepareUserFieldsEdit($data, $userId);
27 $this->prepareSubscriptionsEdit($data, $userId);
28 $this->prepareStatsEdit($data, $userId);
29 $this->prepareHistoryEdit($data, $userId);
30 $this->prepareMailHistory($data, $userId);
31 $this->prepareFieldsEdit($data);
32
33 parent::display($data);
34 }
35
36 private function prepareUserEdit(array &$data, int $userId): bool
37 {
38 if (empty($userId)) {
39 $data['user-information'] = new \stdClass();
40 $data['user-information']->name = '';
41 $data['user-information']->email = '';
42 $data['user-information']->active = '1';
43 $data['user-information']->confirmed = '1';
44 $data['user-information']->cms_id = null;
45 $data['user-information']->tracking = 1;
46 $data['user-information']->language = '';
47
48 $this->breadcrumb[acym_escape(acym_translation('ACYM_NEW_SUBSCRIBER'))] = acym_completeLink('users&task=edit');
49 } else {
50 $userClass = new UserClass();
51 $data['user-information'] = $userClass->getOneById($userId);
52
53 if (empty($data['user-information']) || !$userClass->hasUserAccess($userId)) {
54 acym_enqueueMessage(acym_translation('ACYM_USER_NOT_FOUND'), 'error');
55 $this->listing();
56
57 return false;
58 }
59
60 $this->breadcrumb[acym_escape($data['user-information']->email)] = acym_completeLink('users&task=edit&userId='.$userId);
61 }
62
63 if (empty($data['user-information']->language)) {
64 $data['user-information']->language = acym_getLanguageTag();
65 }
66
67 return true;
68 }
69
70 private function prepareEntitySelectEdit(array &$data, int $userId): void
71 {
72 if (empty($userId)) return;
73
74 $entityHelper = new EntitySelectHelper();
75
76 $columnsToDisplay = $entityHelper->getColumnsForList('userlist.list_id');
77
78 $data['entityselect'] = acym_modal(
79 acym_translation('ACYM_MANAGE_SUBSCRIPTION'),
80 $entityHelper->entitySelect('list', ['join' => 'join_user-'.$userId], $columnsToDisplay, ['text' => acym_translation('ACYM_CONFIRM'), 'action' => 'apply']),
81 null,
82 '',
83 'class="cell medium-6 large-shrink button button-secondary"'
84 );
85 }
86
87 private function prepareUserFieldsEdit(array &$data, int $userId): void
88 {
89 $data['fieldsValues'] = [];
90
91 if (empty($userId)) return;
92
93 $fieldClass = new FieldClass();
94 $fieldsValues = $fieldClass->getFieldsValueByUserId($userId);
95 foreach ($fieldsValues as $one) {
96 $data['fieldsValues'][$one->field_id] = $one->value;
97 }
98 }
99
100 private function prepareSubscriptionsEdit(array &$data, int $userId): void
101 {
102 $data['subscriptionsIds'] = [];
103 $data['subscriptions'] = [];
104 $data['unsubscribe'] = [];
105
106 if (empty($userId)) return;
107
108 $userClass = new UserClass();
109 $data['allSubscriptions'] = $userClass->getUserSubscriptionById($userId);
110
111 foreach ($data['allSubscriptions'] as $sub) {
112 if ($sub->status == 1) {
113 $data['subscriptions'][] = $sub;
114 } else {
115 $data['unsubscribe'][] = $sub;
116 }
117 }
118
119 $data['subscriptionsIds'] = [];
120
121 if (!empty($data['subscriptions'])) {
122 $data['subscriptionsIds'] = [];
123 foreach ($data['subscriptions'] as $list) {
124 $data['subscriptionsIds'][] = $list->id;
125 }
126
127 acym_arrayToInteger($data['subscriptionsIds']);
128 }
129 }
130
131 private function prepareStatsEdit(array &$data, int $userId): void
132 {
133 $data['percentageOpen'] = 0;
134 $data['percentageClick'] = 0;
135
136 if (empty($userId)) return;
137
138 $userStatClass = new UserStatClass();
139 $userStatFromDB = $userStatClass->getAllUserStatByUserId($userId);
140
141 if (empty($userStatFromDB)) return;
142
143 $userStat = new \stdClass();
144 $userStat->totalSent = 0;
145 $userStat->open = 0;
146 $userStat->click = 0;
147
148 foreach ($userStatFromDB as $oneStat) {
149 if ($oneStat->sent > 0) $userStat->totalSent++;
150 if ($oneStat->open > 0) $userStat->open++;
151 }
152
153 $urlClickClass = new UrlClickClass();
154 $clickStats = $urlClickClass->getUserMailsClicked($userId);
155
156 foreach ($clickStats as $oneStat) {
157 if (!empty($oneStat->click)) {
158 $userStat->click++;
159 }
160 }
161
162 $userStat->percentageOpen = empty($userStat->open) || empty($userStat->totalSent) ? 0 : intval(($userStat->open * 100) / $userStat->totalSent);
163 $userStat->percentageClick = empty($userStat->click) || empty($userStat->totalSent) ? 0 : intval(($userStat->click * 100) / $userStat->totalSent);
164
165 $data['percentageOpen'] = number_format($userStat->percentageOpen, 2);
166 $data['percentageClick'] = number_format($userStat->percentageClick, 2);
167 }
168
169 private function prepareMailHistory(array &$data, int $userId): void
170 {
171 if (empty($userId)) {
172 return;
173 }
174
175 $userClass = new UserClass();
176 $data['userMailHistory'] = $userClass->getMailHistory($userId);
177 foreach ($data['userMailHistory'] as $mailId => $oneMailHistory) {
178 if (!empty($oneMailHistory->bounce_rule) && preg_match('/(.*) \[(.*)]/Us', $oneMailHistory->bounce_rule, $matches)) {
179 $ruleName = acym_translation($matches[1]).' ['.$matches[2].']';
180 } else {
181 $ruleName = $data['userMailHistory'][$mailId]->bounce_rule;
182 }
183 $data['userMailHistory'][$mailId]->ruleName = $ruleName;
184 }
185 }
186
187 private function prepareHistoryEdit(array &$data, int $userId): void
188 {
189 if (empty($userId)) return;
190
191 $historyClass = new HistoryClass();
192 $data['userHistory'] = $historyClass->getHistoryOfOneById($userId);
193 $data['unsubReasons'] = $historyClass->getAllMainLanguageUnsubReasons();
194 foreach ($data['userHistory'] as &$oneHistory) {
195 if (!empty($oneHistory->data)) {
196 $historyData = explode("\n", $oneHistory->data);
197 $details = '<div><h5>'.acym_translation('ACYM_DETAILS').'</h5><br />';
198 if (!empty($oneHistory->mail_id)) {
199 $details .= '<b>'.acym_translation('NEWSLETTER').' : </b>';
200 $details .= acym_escape($oneHistory->subject).' ( '.acym_translation('ACYM_ID').' : '.$oneHistory->mail_id.' )<br />';
201 }
202
203 foreach ($historyData as $value) {
204 if (!strpos($value, '::')) {
205 $details .= $value.'<br />';
206 continue;
207 }
208 [$part1, $part2] = explode('::', $value);
209 if (preg_match('#^[A-Z_]*$#', $part2)) $part2 = acym_translation($part2);
210 $details .= '<b>'.acym_escape(acym_translation($part1)).' : </b>'.acym_escape($part2).'<br />';
211 }
212 if ($oneHistory->action === 'unsubscribed') {
213
214 $details .= acym_translation('ACYM_UNSUBSCRIBE_REASON');
215 if (empty(acym_escape($oneHistory->unsubscribe_reason))) {
216 $details .= ' '.acym_translation('ACYM_NO_REASON_SET_BY_USER');
217 } else {
218 $reason = $historyClass->getUnsubscribeReasonText($oneHistory->unsubscribe_reason);
219 $details .= ' '.acym_escape($reason);
220 }
221 }
222
223 $details .= '</div>';
224
225 $oneHistory->data = acym_modal(
226 acym_translation('ACYM_VIEW_DETAILS'),
227 $details,
228 null,
229 'style="word-break: break-word;"',
230 'class="history_details"',
231 true,
232 false
233 );
234 }
235
236 if (!empty($oneHistory->source)) {
237 $source = explode("\n", $oneHistory->source);
238 $details = '<div><h5>'.acym_translation('ACYM_SOURCE').'</h5><br />';
239 foreach ($source as $value) {
240 if (!strpos($value, '::')) continue;
241 [$part1, $part2] = explode('::', $value);
242 $details .= '<b>'.acym_escape($part1).' : </b>'.acym_escape($part2).'<br />';
243 }
244 $details .= '</div>';
245
246 $oneHistory->source = acym_modal(
247 acym_translation('ACYM_VIEW_SOURCE'),
248 $details,
249 null,
250 'style="word-break: break-word;"',
251 'class="history_details"'
252 );
253 }
254 }
255 }
256
257 protected function prepareFieldsEdit(array &$data, string $fieldVisibility = 'backend_edition'): void
258 {
259 $data['allFields'] = [];
260
261 $fieldClass = new FieldClass();
262 $fieldsElements = $fieldClass->getMatchingElements();
263 $allFields = $fieldsElements['elements'];
264 $languageFieldId = $fieldClass->getLanguageFieldId();
265
266 foreach ($allFields as $one) {
267 $one->option = empty($one->option) ? new \stdClass() : json_decode($one->option);
268 $one->value = empty($one->value) ? '' : json_decode($one->value);
269 $fieldDB = empty($one->option->fieldDB) ? '' : json_decode($one->option->fieldDB);
270
271
272 $valuesArray = [];
273 if (!empty($one->value)) {
274 foreach ($one->value as $value) {
275 $valueTmp = new \stdClass();
276 $valueTmp->text = $value->title;
277 $valueTmp->value = $value->value;
278 if ($value->disabled == 'y') $valueTmp->disable = true;
279 $valuesArray[$value->value] = $valueTmp;
280 }
281 }
282 if (!empty($fieldDB) && !empty($fieldDB->value)) {
283 $fromDB = $fieldClass->getValueFromDB($fieldDB);
284 foreach ($fromDB as $value) {
285 $valuesArray[$value->value] = $value->title;
286 }
287 }
288
289 $one->display = empty($one->option->display) ? '' : json_decode($one->option->display);
290 $data['allFields'][$one->id] = $one;
291 if ($one->id == 1) {
292 $defaultValue = empty($data['user-information']->id) ? '' : $data['user-information']->name;
293 } elseif ($one->id == 2) {
294 $defaultValue = empty($data['user-information']->id) ? '' : $data['user-information']->email;
295 } elseif ($one->id == $languageFieldId) {
296 $defaultValue = empty($data['user-information']->id) ? acym_getLanguageTag() : $data['user-information']->language;
297 } elseif (isset($data['fieldsValues'][$one->id]) && (((is_array($data['fieldsValues'][$one->id]) || $data['fieldsValues'][$one->id] instanceof Countable) && count(
298 $data['fieldsValues'][$one->id]
299 ) > 0) || (is_string($data['fieldsValues'][$one->id]) && strlen($data['fieldsValues'][$one->id]) > 0))) {
300 $decoded = json_decode($data['fieldsValues'][$one->id]);
301 $defaultValue = is_null($decoded) ? $data['fieldsValues'][$one->id] : $decoded;
302 } else {
303 $defaultValue = $one->default_value;
304 }
305 $size = empty($one->option->size) ? '' : 'width:'.$one->option->size.'px';
306
307 $data['allFields'][$one->id]->html = $fieldClass->displayField($one, $defaultValue, $size, $valuesArray, true, !acym_isAdmin(), null, $one->$fieldVisibility);
308 }
309 }
310
311 public function save(): void
312 {
313 $this->apply(true);
314 }
315
316 public function apply(bool $listing = false): void
317 {
318 $userInformation = acym_getVar('array', 'user');
319 $userId = acym_getVar('int', 'userId');
320 $listsToAdd = json_decode(acym_getVar('string', 'acym__entity_select__selected', '{}'));
321 $listsToUnsub = json_decode(acym_getVar('string', 'acym__entity_select__unselected', '{}'));
322
323 $user = new \stdClass();
324 $user->name = $userInformation['name'];
325 $user->email = $userInformation['email'];
326 if (!empty($userInformation['language'])) {
327 $user->language = $userInformation['language'];
328 }
329 $user->active = $userInformation['active'];
330 if ($this->config->get('require_confirmation', '0') == '1') {
331 $user->confirmed = $userInformation['confirmed'];
332 }
333 $user->tracking = $userInformation['tracking'];
334 $customFields = acym_getVar('array', 'customField');
335
336 preg_match('/'.acym_getEmailRegex().'/i', $user->email, $matches);
337
338 if (empty($matches)) {
339 $this->edit();
340 acym_enqueueMessage(acym_translationSprintf('ACYM_VALID_EMAIL', $user->email), 'error');
341
342 return;
343 }
344
345 $userClass = new UserClass();
346 $frontCreation = false;
347 $existingUser = $userClass->getOneByEmail($user->email);
348 if (empty($userId)) {
349 if (!empty($existingUser) && acym_isAdmin()) {
350 acym_enqueueMessage(acym_translationSprintf('ACYM_X_ALREADY_EXIST', $user->email), 'error');
351
352 $this->edit();
353
354 return;
355 } elseif (!empty($existingUser)) {
356 $userId = $existingUser->id;
357 } else {
358 $user->creation_date = acym_date('now', 'Y-m-d H:i:s', false);
359 $userClass->checkVisitor = false;
360 $userId = $userClass->save($user, $customFields);
361 if (empty($userId)) {
362 acym_enqueueMessage(empty($userClass->errors) ? acym_translation('ACYM_ERROR') : $userClass->errors, 'error');
363 $this->edit();
364
365 return;
366 }
367 $frontCreation = true;
368 }
369 acym_setVar('userId', $userId);
370 } else {
371 if (!empty($existingUser) && $existingUser->id != $userId) {
372 acym_enqueueMessage(acym_translationSprintf('ACYM_X_ALREADY_EXIST', $user->email), 'error');
373 $this->edit();
374
375 return;
376 }
377
378 if (!$userClass->hasUserAccess($userId)) {
379 die('Access denied for the modification of this user');
380 }
381
382 $user->id = $userId;
383 $userClass->save($user, $customFields);
384 }
385
386 if (!empty($listsToAdd)) {
387 $this->subscribeUser(false, $frontCreation);
388 }
389 if (!empty($listsToUnsub)) {
390 $userClass->unsubscribeOnSubscriptions($userId, $listsToUnsub);
391 }
392
393 if ($listing) {
394 $this->listing();
395 } else {
396 $this->edit();
397 }
398 }
399 }
400