| @@ -1,0 +1,369 @@ | ||
| 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_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[$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_getDefaultUserLanguage(); | |
| 65 | + } | |
| 66 | + | |
| 67 | + return true; | |
| 68 | + } | |
| 69 | + | |
| 70 | + private function prepareEntitySelectEdit(array &$data, int $userId): void | |
| 71 | + { | |
| 72 | + if (empty($userId)) { | |
| 73 | + return; | |
| 74 | + } | |
| 75 | + | |
| 76 | + $entityHelper = new EntitySelectHelper(); | |
| 77 | + | |
| 78 | + $data['entityselectContent'] = $entityHelper->entitySelect( | |
| 79 | + [ | |
| 80 | + 'entity' => 'list', | |
| 81 | + 'entityParams' => ['join' => 'join_user-'.$userId], | |
| 82 | + 'columnsToDisplay' => $entityHelper->getColumnsForList('userlist.list_id'), | |
| 83 | + 'buttonSubmit' => ['text' => acym_translation('ACYM_CONFIRM'), 'action' => 'apply'], | |
| 84 | + ] | |
| 85 | + ); | |
| 86 | + } | |
| 87 | + | |
| 88 | + private function prepareUserFieldsEdit(array &$data, int $userId): void | |
| 89 | + { | |
| 90 | + $data['fieldsValues'] = []; | |
| 91 | + | |
| 92 | + if (empty($userId)) return; | |
| 93 | + | |
| 94 | + $fieldClass = new FieldClass(); | |
| 95 | + $fieldsValues = $fieldClass->getFieldsValueByUserId($userId); | |
| 96 | + foreach ($fieldsValues as $one) { | |
| 97 | + $data['fieldsValues'][$one->field_id] = $one->value; | |
| 98 | + } | |
| 99 | + } | |
| 100 | + | |
| 101 | + private function prepareSubscriptionsEdit(array &$data, int $userId): void | |
| 102 | + { | |
| 103 | + $data['subscriptionsIds'] = []; | |
| 104 | + $data['subscriptions'] = []; | |
| 105 | + $data['unsubscribe'] = []; | |
| 106 | + | |
| 107 | + if (empty($userId)) return; | |
| 108 | + | |
| 109 | + $userClass = new UserClass(); | |
| 110 | + $data['allSubscriptions'] = $userClass->getUserSubscriptionById($userId); | |
| 111 | + | |
| 112 | + foreach ($data['allSubscriptions'] as $sub) { | |
| 113 | + if ($sub->status == 1) { | |
| 114 | + $data['subscriptions'][] = $sub; | |
| 115 | + } else { | |
| 116 | + $data['unsubscribe'][] = $sub; | |
| 117 | + } | |
| 118 | + } | |
| 119 | + | |
| 120 | + $data['subscriptionsIds'] = []; | |
| 121 | + | |
| 122 | + if (!empty($data['subscriptions'])) { | |
| 123 | + $data['subscriptionsIds'] = []; | |
| 124 | + foreach ($data['subscriptions'] as $list) { | |
| 125 | + $data['subscriptionsIds'][] = $list->id; | |
| 126 | + } | |
| 127 | + | |
| 128 | + acym_arrayToInteger($data['subscriptionsIds']); | |
| 129 | + } | |
| 130 | + } | |
| 131 | + | |
| 132 | + private function prepareStatsEdit(array &$data, int $userId): void | |
| 133 | + { | |
| 134 | + $data['percentageOpen'] = 0; | |
| 135 | + $data['percentageClick'] = 0; | |
| 136 | + $data['anonymousStats'] = !empty($this->config->get('anonymous_stats', 0)); | |
| 137 | + | |
| 138 | + // Individual open/click rates are an individual statistic: don't compute them when anonymizing | |
| 139 | + if (empty($userId) || $data['anonymousStats']) return; | |
| 140 | + | |
| 141 | + $userStatClass = new UserStatClass(); | |
| 142 | + $userStatFromDB = $userStatClass->getAllUserStatByUserId($userId); | |
| 143 | + | |
| 144 | + if (empty($userStatFromDB)) return; | |
| 145 | + | |
| 146 | + $userStat = new \stdClass(); | |
| 147 | + $userStat->totalSent = 0; | |
| 148 | + $userStat->open = 0; | |
| 149 | + $userStat->click = 0; | |
| 150 | + | |
| 151 | + foreach ($userStatFromDB as $oneStat) { | |
| 152 | + if ($oneStat->sent > 0) $userStat->totalSent++; | |
| 153 | + if ($oneStat->open > 0) $userStat->open++; | |
| 154 | + } | |
| 155 | + | |
| 156 | + $urlClickClass = new UrlClickClass(); | |
| 157 | + $clickStats = $urlClickClass->getUserMailsClicked($userId); | |
| 158 | + | |
| 159 | + foreach ($clickStats as $oneStat) { | |
| 160 | + if (!empty($oneStat->click)) { | |
| 161 | + $userStat->click++; | |
| 162 | + } | |
| 163 | + } | |
| 164 | + | |
| 165 | + $userStat->percentageOpen = empty($userStat->open) || empty($userStat->totalSent) ? 0 : intval(($userStat->open * 100) / $userStat->totalSent); | |
| 166 | + $userStat->percentageClick = empty($userStat->click) || empty($userStat->totalSent) ? 0 : intval(($userStat->click * 100) / $userStat->totalSent); | |
| 167 | + | |
| 168 | + $data['percentageOpen'] = number_format($userStat->percentageOpen, 2); | |
| 169 | + $data['percentageClick'] = number_format($userStat->percentageClick, 2); | |
| 170 | + } | |
| 171 | + | |
| 172 | + private function prepareMailHistory(array &$data, int $userId): void | |
| 173 | + { | |
| 174 | + if (empty($userId)) { | |
| 175 | + return; | |
| 176 | + } | |
| 177 | + | |
| 178 | + $userClass = new UserClass(); | |
| 179 | + $data['userMailHistory'] = $userClass->getMailHistory($userId); | |
| 180 | + foreach ($data['userMailHistory'] as $mailId => $oneMailHistory) { | |
| 181 | + if (!empty($oneMailHistory->bounce_rule) && preg_match('/(.*) \[(.*)]/Us', $oneMailHistory->bounce_rule, $matches)) { | |
| 182 | + $ruleName = acym_translation($matches[1]).' ['.$matches[2].']'; | |
| 183 | + } else { | |
| 184 | + $ruleName = $data['userMailHistory'][$mailId]->bounce_rule; | |
| 185 | + } | |
| 186 | + $data['userMailHistory'][$mailId]->ruleName = $ruleName; | |
| 187 | + } | |
| 188 | + } | |
| 189 | + | |
| 190 | + private function prepareHistoryEdit(array &$data, int $userId): void | |
| 191 | + { | |
| 192 | + if (empty($userId)) { | |
| 193 | + return; | |
| 194 | + } | |
| 195 | + | |
| 196 | + $historyClass = new HistoryClass(); | |
| 197 | + $data['userHistory'] = $historyClass->getHistoryOfOneById($userId); | |
| 198 | + $data['unsubReasons'] = $historyClass->getAllMainLanguageUnsubReasons(); | |
| 199 | + $data['historyClass'] = $historyClass; | |
| 200 | + } | |
| 201 | + | |
| 202 | + protected function prepareFieldsEdit(array &$data, string $fieldVisibility = 'backend_edition'): void | |
| 203 | + { | |
| 204 | + $data['allFields'] = []; | |
| 205 | + | |
| 206 | + $fieldClass = new FieldClass(); | |
| 207 | + $fieldsElements = $fieldClass->getMatchingElements(); | |
| 208 | + $allFields = $fieldsElements['elements']; | |
| 209 | + $languageFieldId = $fieldClass->getLanguageFieldId(); | |
| 210 | + | |
| 211 | + foreach ($allFields as $one) { | |
| 212 | + $one->option = empty($one->option) ? new \stdClass() : json_decode($one->option); | |
| 213 | + $one->value = empty($one->value) ? '' : json_decode($one->value); | |
| 214 | + $fieldDB = empty($one->option->fieldDB) ? '' : json_decode($one->option->fieldDB); | |
| 215 | + | |
| 216 | + // Keep this code, search for data-display-optional for more info | |
| 217 | + //$displayIf = empty($one->option->display) ? '' : 'data-display-optional="'.esc_attr($one->option->display).'"'; | |
| 218 | + | |
| 219 | + $valuesArray = []; | |
| 220 | + if (!empty($one->value)) { | |
| 221 | + foreach ($one->value as $value) { | |
| 222 | + $valueTmp = new \stdClass(); | |
| 223 | + $valueTmp->text = $value->title; | |
| 224 | + $valueTmp->value = $value->value; | |
| 225 | + if ($value->disabled == 'y') $valueTmp->disable = true; | |
| 226 | + $valuesArray[$value->value] = $valueTmp; | |
| 227 | + } | |
| 228 | + } | |
| 229 | + | |
| 230 | + if (!empty($fieldDB) && !empty($fieldDB->value)) { | |
| 231 | + $fromDB = $fieldClass->getValueFromDB($fieldDB); | |
| 232 | + foreach ($fromDB as $value) { | |
| 233 | + $valuesArray[$value->value] = $value->title; | |
| 234 | + } | |
| 235 | + } | |
| 236 | + | |
| 237 | + $one->display = empty($one->option->display) ? '' : json_decode($one->option->display); | |
| 238 | + | |
| 239 | + if ($one->id == 1) { | |
| 240 | + $defaultValue = empty($data['user-information']->id) ? '' : $data['user-information']->name; | |
| 241 | + } elseif ($one->id == 2) { | |
| 242 | + $defaultValue = empty($data['user-information']->id) ? '' : $data['user-information']->email; | |
| 243 | + } elseif ($one->id == $languageFieldId) { | |
| 244 | + $defaultValue = empty($data['user-information']->id) ? acym_getDefaultUserLanguage() : $data['user-information']->language; | |
| 245 | + } elseif ( | |
| 246 | + isset($data['fieldsValues'][$one->id]) | |
| 247 | + && ( | |
| 248 | + ( | |
| 249 | + ( | |
| 250 | + is_array($data['fieldsValues'][$one->id]) | |
| 251 | + || $data['fieldsValues'][$one->id] instanceof \Countable | |
| 252 | + ) | |
| 253 | + && count($data['fieldsValues'][$one->id]) > 0 | |
| 254 | + ) | |
| 255 | + || ( | |
| 256 | + is_string($data['fieldsValues'][$one->id]) | |
| 257 | + && strlen($data['fieldsValues'][$one->id]) > 0 | |
| 258 | + ) | |
| 259 | + ) | |
| 260 | + ) { | |
| 261 | + $decoded = json_decode($data['fieldsValues'][$one->id]); | |
| 262 | + $defaultValue = is_array($decoded) ? $decoded : $data['fieldsValues'][$one->id]; | |
| 263 | + } else { | |
| 264 | + $defaultValue = $one->default_value; | |
| 265 | + } | |
| 266 | + | |
| 267 | + $data['allFields'][$one->id] = [ | |
| 268 | + 'field' => $one, | |
| 269 | + 'defaultValue' => $defaultValue, | |
| 270 | + 'values' => $valuesArray, | |
| 271 | + 'user' => $data['user-information'], | |
| 272 | + 'showField' => $one->$fieldVisibility != 0, | |
| 273 | + ]; | |
| 274 | + } | |
| 275 | + } | |
| 276 | + | |
| 277 | + public function save(): void | |
| 278 | + { | |
| 279 | + $this->apply(true); | |
| 280 | + } | |
| 281 | + | |
| 282 | + public function apply(bool $listing = false): void | |
| 283 | + { | |
| 284 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 285 | + | |
| 286 | + $userInformation = acym_getVar('array', 'user'); | |
| 287 | + | |
| 288 | + $user = new \stdClass(); | |
| 289 | + $user->name = $userInformation['name']; | |
| 290 | + $user->email = $userInformation['email']; | |
| 291 | + if (!empty($userInformation['language'])) { | |
| 292 | + $user->language = $userInformation['language']; | |
| 293 | + } | |
| 294 | + $user->active = $userInformation['active']; | |
| 295 | + if ($this->config->get('require_confirmation', '0') == '1') { | |
| 296 | + $user->confirmed = $userInformation['confirmed']; | |
| 297 | + } | |
| 298 | + $user->tracking = $userInformation['tracking']; | |
| 299 | + $customFields = acym_getVar('array', 'customField', []); | |
| 300 | + | |
| 301 | + preg_match('/'.acym_getEmailRegex().'/i', $user->email, $matches); | |
| 302 | + | |
| 303 | + if (empty($matches)) { | |
| 304 | + $this->edit(); | |
| 305 | + acym_enqueueMessage(acym_translationSprintf('ACYM_VALID_EMAIL', $user->email), 'error'); | |
| 306 | + | |
| 307 | + return; | |
| 308 | + } | |
| 309 | + | |
| 310 | + $userClass = new UserClass(); | |
| 311 | + $frontCreation = false; | |
| 312 | + $existingUser = $userClass->getOneByEmail($user->email); | |
| 313 | + $userId = acym_getVar('int', 'userId'); | |
| 314 | + | |
| 315 | + if (empty($userId)) { | |
| 316 | + if (!empty($existingUser) && acym_isAdmin()) { | |
| 317 | + acym_enqueueMessage(acym_translationSprintf('ACYM_X_ALREADY_EXIST', $user->email), 'error'); | |
| 318 | + | |
| 319 | + $this->edit(); | |
| 320 | + | |
| 321 | + return; | |
| 322 | + } elseif (!empty($existingUser)) { | |
| 323 | + $userId = $existingUser->id; | |
| 324 | + } else { | |
| 325 | + $user->creation_date = acym_date('now', 'Y-m-d H:i:s', false); | |
| 326 | + $userClass->checkVisitor = false; | |
| 327 | + $userId = $userClass->save($user, $customFields); | |
| 328 | + if (empty($userId)) { | |
| 329 | + acym_enqueueMessage(empty($userClass->errors) ? acym_translation('ACYM_ERROR') : $userClass->errors, 'error'); | |
| 330 | + $this->edit(); | |
| 331 | + | |
| 332 | + return; | |
| 333 | + } | |
| 334 | + $frontCreation = true; | |
| 335 | + } | |
| 336 | + acym_setVar('userId', $userId); | |
| 337 | + } else { | |
| 338 | + if (!empty($existingUser) && $existingUser->id != $userId) { | |
| 339 | + acym_enqueueMessage(acym_translationSprintf('ACYM_X_ALREADY_EXIST', $user->email), 'error'); | |
| 340 | + $this->edit(); | |
| 341 | + | |
| 342 | + return; | |
| 343 | + } | |
| 344 | + | |
| 345 | + if (!$userClass->hasUserAccess($userId)) { | |
| 346 | + die('Access denied for the modification of this user'); | |
| 347 | + } | |
| 348 | + | |
| 349 | + $user->id = $userId; | |
| 350 | + $userClass->save($user, $customFields); | |
| 351 | + } | |
| 352 | + | |
| 353 | + $listsToAdd = json_decode(acym_getVar('string', 'acym__entity_select__selected', '[]'), true); | |
| 354 | + if (!empty($listsToAdd)) { | |
| 355 | + $this->subscribeUser(false, $listsToAdd, $frontCreation); | |
| 356 | + } | |
| 357 | + | |
| 358 | + $listsToUnsub = json_decode(acym_getVar('string', 'acym__entity_select__unselected', '[]'), true); | |
| 359 | + if (!empty($listsToUnsub)) { | |
| 360 | + $userClass->unsubscribeOnSubscriptions($userId, $listsToUnsub); | |
| 361 | + } | |
| 362 | + | |
| 363 | + if ($listing) { | |
| 364 | + $this->listing(); | |
| 365 | + } else { | |
| 366 | + $this->edit(); | |
| 367 | + } | |
| 368 | + } | |
| 369 | +} | |