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 / Edition.php

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

422 lines 15.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\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
213 if ($oneHistory->action === 'unsubscribed') {
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 (
298 isset($data['fieldsValues'][$one->id])
299 && (
300 (
301 (
302 is_array($data['fieldsValues'][$one->id])
303 || $data['fieldsValues'][$one->id] instanceof \Countable
304 )
305 && count($data['fieldsValues'][$one->id]) > 0
306 )
307 || (
308 is_string($data['fieldsValues'][$one->id])
309 && strlen($data['fieldsValues'][$one->id]) > 0
310 )
311 )
312 ) {
313 $decoded = json_decode($data['fieldsValues'][$one->id]);
314 $defaultValue = is_array($decoded) ? $decoded : $data['fieldsValues'][$one->id];
315 } else {
316 $defaultValue = $one->default_value;
317 }
318
319 $data['allFields'][$one->id]->html = $fieldClass->displayField(
320 $one,
321 $defaultValue,
322 $valuesArray,
323 true,
324 !acym_isAdmin(),
325 $data['user-information'],
326 $one->$fieldVisibility != 0
327 );
328 }
329 }
330
331 public function save(): void
332 {
333 $this->apply(true);
334 }
335
336 public function apply(bool $listing = false): void
337 {
338 $userInformation = acym_getVar('array', 'user');
339
340 $user = new \stdClass();
341 $user->name = $userInformation['name'];
342 $user->email = $userInformation['email'];
343 if (!empty($userInformation['language'])) {
344 $user->language = $userInformation['language'];
345 }
346 $user->active = $userInformation['active'];
347 if ($this->config->get('require_confirmation', '0') == '1') {
348 $user->confirmed = $userInformation['confirmed'];
349 }
350 $user->tracking = $userInformation['tracking'];
351 $customFields = acym_getVar('array', 'customField', []);
352
353 preg_match('/'.acym_getEmailRegex().'/i', $user->email, $matches);
354
355 if (empty($matches)) {
356 $this->edit();
357 acym_enqueueMessage(acym_translationSprintf('ACYM_VALID_EMAIL', $user->email), 'error');
358
359 return;
360 }
361
362 $userClass = new UserClass();
363 $frontCreation = false;
364 $existingUser = $userClass->getOneByEmail($user->email);
365 $userId = acym_getVar('int', 'userId');
366
367 if (empty($userId)) {
368 if (!empty($existingUser) && acym_isAdmin()) {
369 acym_enqueueMessage(acym_translationSprintf('ACYM_X_ALREADY_EXIST', $user->email), 'error');
370
371 $this->edit();
372
373 return;
374 } elseif (!empty($existingUser)) {
375 $userId = $existingUser->id;
376 } else {
377 $user->creation_date = acym_date('now', 'Y-m-d H:i:s', false);
378 $userClass->checkVisitor = false;
379 $userId = $userClass->save($user, $customFields);
380 if (empty($userId)) {
381 acym_enqueueMessage(empty($userClass->errors) ? acym_translation('ACYM_ERROR') : $userClass->errors, 'error');
382 $this->edit();
383
384 return;
385 }
386 $frontCreation = true;
387 }
388 acym_setVar('userId', $userId);
389 } else {
390 if (!empty($existingUser) && $existingUser->id != $userId) {
391 acym_enqueueMessage(acym_translationSprintf('ACYM_X_ALREADY_EXIST', $user->email), 'error');
392 $this->edit();
393
394 return;
395 }
396
397 if (!$userClass->hasUserAccess($userId)) {
398 die('Access denied for the modification of this user');
399 }
400
401 $user->id = $userId;
402 $userClass->save($user, $customFields);
403 }
404
405 $listsToAdd = json_decode(acym_getVar('string', 'acym__entity_select__selected', '[]'), true);
406 if (!empty($listsToAdd)) {
407 $this->subscribeUser(false, $listsToAdd, $frontCreation);
408 }
409
410 $listsToUnsub = json_decode(acym_getVar('string', 'acym__entity_select__unselected', '[]'), true);
411 if (!empty($listsToUnsub)) {
412 $userClass->unsubscribeOnSubscriptions($userId, $listsToUnsub);
413 }
414
415 if ($listing) {
416 $this->listing();
417 } else {
418 $this->edit();
419 }
420 }
421 }
422