| @@ -1,0 +1,402 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\Controllers\Campaigns; | |
| 4 | + | |
| 5 | +use AcyMailing\Classes\FollowupClass; | |
| 6 | +use AcyMailing\Classes\MailClass; | |
| 7 | +use AcyMailing\Classes\ListClass; | |
| 8 | +use AcyMailing\Classes\SegmentClass; | |
| 9 | +use AcyMailing\Helpers\WorkflowHelper; | |
| 10 | +use stdClass; | |
| 11 | + | |
| 12 | + | |
| 13 | +trait Followup | |
| 14 | +{ | |
| 15 | + public function followup(): void | |
| 16 | + { | |
| 17 | + acym_setVar('layout', 'followup'); | |
| 18 | + | |
| 19 | + $data = [ | |
| 20 | + 'campaign_type' => MailClass::TYPE_FOLLOWUP, | |
| 21 | + 'element_to_display' => lcfirst(acym_translation('ACYM_FOLLOW_UP')), | |
| 22 | + ]; | |
| 23 | + $this->getAllParamsRequest($data); | |
| 24 | + $this->prepareEmailsListing($data, $data['campaign_type'], 'followup'); | |
| 25 | + $this->prepareToolbar($data); | |
| 26 | + $this->prepareListingClasses($data); | |
| 27 | + $this->prepareFollowupListing($data); | |
| 28 | + | |
| 29 | + parent::display($data); | |
| 30 | + } | |
| 31 | + | |
| 32 | + private function prepareFollowupListing(array &$data): void | |
| 33 | + { | |
| 34 | + $followupClass = new FollowupClass(); | |
| 35 | + $triggers = []; | |
| 36 | + acym_trigger('getFollowupTriggers', [&$triggers]); | |
| 37 | + $data['allTriggers'] = $triggers; | |
| 38 | + | |
| 39 | + foreach ($data['allCampaigns'] as $key => $oneFollowup) { | |
| 40 | + if (!empty($triggers[$oneFollowup->trigger])) { | |
| 41 | + $oneFollowup->condition = json_decode($oneFollowup->condition, true); | |
| 42 | + $data['allCampaigns'][$key]->condition = $followupClass->getConditionSummary($oneFollowup); | |
| 43 | + $data['allCampaigns'][$key]->mail_ids = $followupClass->getEmailsByIds([$oneFollowup->id]); | |
| 44 | + } | |
| 45 | + } | |
| 46 | + } | |
| 47 | + | |
| 48 | + public function deleteFollowup(): void | |
| 49 | + { | |
| 50 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 51 | + $ids = acym_getVar('array', 'elements_checked', []); | |
| 52 | + $allChecked = acym_getVar('string', 'checkbox_all'); | |
| 53 | + $currentPage = explode('_', acym_getVar('string', 'page')); | |
| 54 | + $pageNumber = $this->getVarFiltersListing('int', end($currentPage).'_pagination_page', 1); | |
| 55 | + | |
| 56 | + if (!empty($ids)) { | |
| 57 | + $followupClass = new FollowupClass(); | |
| 58 | + $followupClass->delete($ids); | |
| 59 | + if ($allChecked == 'on') { | |
| 60 | + $this->setVarFiltersListing(end($currentPage).'_pagination_page', $pageNumber - 1); | |
| 61 | + } | |
| 62 | + } | |
| 63 | + | |
| 64 | + $this->listing(); | |
| 65 | + } | |
| 66 | + | |
| 67 | + public function followupTrigger(): void | |
| 68 | + { | |
| 69 | + acym_setVar('layout', 'followup_trigger'); | |
| 70 | + | |
| 71 | + $id = acym_getVar('int', 'id', 0); | |
| 72 | + | |
| 73 | + if (!empty($id)) { | |
| 74 | + $followupClass = new FollowupClass(); | |
| 75 | + $followup = $followupClass->getOneById($id); | |
| 76 | + } else { | |
| 77 | + $followup = new stdClass(); | |
| 78 | + } | |
| 79 | + | |
| 80 | + $this->setTaskListing('followup'); | |
| 81 | + | |
| 82 | + $data = [ | |
| 83 | + 'workflowHelper' => new WorkflowHelper(), | |
| 84 | + 'followup' => $followup, | |
| 85 | + ]; | |
| 86 | + | |
| 87 | + $linkId = empty($id) ? '' : '&id='.$id; | |
| 88 | + | |
| 89 | + $this->breadcrumb[empty($id) ? acym_translation('ACYM_NEW_FOLLOW_UP') : $followup->name] = acym_completeLink('campaigns&task=edit&step=followupTrigger'.$linkId); | |
| 90 | + | |
| 91 | + parent::display($data); | |
| 92 | + } | |
| 93 | + | |
| 94 | + public function followupCondition(): void | |
| 95 | + { | |
| 96 | + acym_setVar('layout', 'followup_condition'); | |
| 97 | + | |
| 98 | + $id = acym_getVar('int', 'id', 0); | |
| 99 | + $trigger = acym_getVar('string', 'trigger', ''); | |
| 100 | + | |
| 101 | + if (!empty($id)) { | |
| 102 | + $followupClass = new FollowupClass(); | |
| 103 | + $followup = $followupClass->getOneById($id); | |
| 104 | + } else { | |
| 105 | + $followup = new stdClass(); | |
| 106 | + } | |
| 107 | + | |
| 108 | + if (empty($trigger) && empty($followup->trigger)) { | |
| 109 | + acym_enqueueMessage(acym_translation('ACYM_COULD_NOT_LOAD_DATA'), 'error'); | |
| 110 | + $this->listing(); | |
| 111 | + | |
| 112 | + return; | |
| 113 | + } | |
| 114 | + | |
| 115 | + $listClass = new ListClass(); | |
| 116 | + $lists = $listClass->getAllForSelect(false); | |
| 117 | + | |
| 118 | + $segmentClass = new SegmentClass(); | |
| 119 | + $segments = $segmentClass->getAllForSelect(false); | |
| 120 | + | |
| 121 | + $statusArray = [ | |
| 122 | + 'is' => acym_strtolower(acym_translation('ACYM_IS')), | |
| 123 | + 'is_not' => acym_strtolower(acym_translation('ACYM_IS_NOT')), | |
| 124 | + ]; | |
| 125 | + | |
| 126 | + $additionalCondition = []; | |
| 127 | + acym_trigger('getAcymAdditionalConditionFollowup', [&$additionalCondition, empty($trigger) ? $followup->trigger : $trigger, $followup, $statusArray]); | |
| 128 | + | |
| 129 | + $actualTrigger = empty($trigger) ? $followup->trigger : $trigger; | |
| 130 | + | |
| 131 | + $data = [ | |
| 132 | + 'workflowHelper' => new WorkflowHelper(), | |
| 133 | + 'followup' => $followup, | |
| 134 | + 'additionalCondition' => $additionalCondition, | |
| 135 | + 'trigger' => $actualTrigger, | |
| 136 | + 'lists_multiselect' => '<span class="cell large-4 medium-6 acym__followup__condition__select__in-text">'.acym_selectMultiple( | |
| 137 | + $lists, | |
| 138 | + 'followup[condition][lists]', | |
| 139 | + !empty($followup->condition) && !empty($followup->condition['lists']) ? $followup->condition['lists'] : [], | |
| 140 | + ['class' => 'acym__select'] | |
| 141 | + ).'</span>', | |
| 142 | + 'segments_multiselect' => '<span class="cell large-4 medium-6 acym__followup__condition__select__in-text">'.acym_selectMultiple( | |
| 143 | + $segments, | |
| 144 | + 'followup[condition][segments]', | |
| 145 | + !empty($followup->condition) && !empty($followup->condition['segments']) ? $followup->condition['segments'] : [], | |
| 146 | + ['class' => 'acym__select'] | |
| 147 | + ).'</span>', | |
| 148 | + 'select_status_lists' => '<span class="cell xxlarge-1 medium-2 acym__followup__condition__select__in-text">'.acym_select( | |
| 149 | + $statusArray, | |
| 150 | + 'followup[condition][lists_status]', | |
| 151 | + !empty($followup->condition) && !empty($followup->condition['lists_status']) ? $followup->condition['lists_status'] : '', | |
| 152 | + ['class' => 'acym__select'] | |
| 153 | + ).'</span>', | |
| 154 | + 'select_status_segments' => '<span class="cell xxlarge-1 medium-2 acym__followup__condition__select__in-text">'.acym_select( | |
| 155 | + $statusArray, | |
| 156 | + 'followup[condition][segments_status]', | |
| 157 | + !empty($followup->condition) && !empty($followup->condition['segments_status']) ? $followup->condition['segments_status'] : '', | |
| 158 | + ['class' => 'acym__select'] | |
| 159 | + ).'</span>', | |
| 160 | + 'lists_subscribe_translation' => $actualTrigger == 'user_subscribe' ? 'ACYM_FOLLOW_UP_CONDITION_USER_SUBSCRIBING' : 'ACYM_FOLLOW_UP_CONDITION_USER_SUBSCRIBE', | |
| 161 | + ]; | |
| 162 | + | |
| 163 | + $linkId = empty($id) ? '&trigger='.$trigger : '&id='.$id; | |
| 164 | + | |
| 165 | + $this->breadcrumb[empty($followup->name) ? acym_translation('ACYM_NEW_FOLLOW_UP') : $followup->name] = acym_completeLink( | |
| 166 | + 'campaigns&task=edit&step=followupCondition'.$linkId | |
| 167 | + ); | |
| 168 | + | |
| 169 | + parent::display($data); | |
| 170 | + } | |
| 171 | + | |
| 172 | + public function followupEmail(): void | |
| 173 | + { | |
| 174 | + acym_setVar('layout', 'followup_email'); | |
| 175 | + | |
| 176 | + $id = acym_getVar('int', 'id', 0); | |
| 177 | + | |
| 178 | + if (empty($id)) { | |
| 179 | + acym_enqueueMessage(acym_translation('ACYM_COULD_NOT_LOAD_DATA'), 'error'); | |
| 180 | + $this->listing(); | |
| 181 | + | |
| 182 | + return; | |
| 183 | + } | |
| 184 | + | |
| 185 | + $followupClass = new FollowupClass(); | |
| 186 | + $followup = $followupClass->getOneByIdWithMails($id); | |
| 187 | + | |
| 188 | + if (empty($followup)) { | |
| 189 | + acym_enqueueMessage(acym_translation('ACYM_COULD_NOT_LOAD_DATA'), 'error'); | |
| 190 | + $this->listing(); | |
| 191 | + | |
| 192 | + return; | |
| 193 | + } | |
| 194 | + | |
| 195 | + $mailClass = new MailClass(); | |
| 196 | + $favoriteTemplate = $mailClass->getFavoriteTemplateId(); | |
| 197 | + $startFrom = empty($favoriteTemplate) ? '' : '&from='.$favoriteTemplate; | |
| 198 | + $data = [ | |
| 199 | + 'workflowHelper' => new WorkflowHelper(), | |
| 200 | + 'followup' => $followup, | |
| 201 | + 'linkNewEmail' => acym_completeLink( | |
| 202 | + 'mails&task=edit&step=editEmail&type=followup&followup_id='.$id.'&return='.urlencode( | |
| 203 | + acym_completeLink('campaigns&task=edit&step=followupEmail&id='.$id) | |
| 204 | + ).$startFrom, | |
| 205 | + false, | |
| 206 | + true | |
| 207 | + ), | |
| 208 | + ]; | |
| 209 | + | |
| 210 | + $this->breadcrumb[empty($followup->name) ? acym_translation('ACYM_NEW_FOLLOW_UP') : $followup->name] = acym_completeLink( | |
| 211 | + 'campaigns&task=edit&step=followupEmail&id='.$followup->id | |
| 212 | + ); | |
| 213 | + | |
| 214 | + // Ask the user if he wants to add the new email to the queue for the users that already triggered the followup | |
| 215 | + if (intval($followup->active) === 1) { | |
| 216 | + $newlyCreatedEmail = acym_getVar('int', 'newEmailId'); | |
| 217 | + if (!empty($newlyCreatedEmail)) { | |
| 218 | + $numberOfSubscribers = $followupClass->getNumberSubscribersByListId($followup->list_id, true); | |
| 219 | + if (!empty($numberOfSubscribers)) { | |
| 220 | + $message = '<button type="button" class="acym__followup__add_queue" data-acym-email-id="'.intval($newlyCreatedEmail).'">'; | |
| 221 | + $message .= acym_translationSprintf('ACYM_FOLLOWUP_ADD_QUEUE', $numberOfSubscribers); | |
| 222 | + $message .= '</button>'; | |
| 223 | + acym_enqueueMessage($message, 'info', false); | |
| 224 | + } | |
| 225 | + } | |
| 226 | + } | |
| 227 | + | |
| 228 | + parent::display($data); | |
| 229 | + } | |
| 230 | + | |
| 231 | + public function followupDuplicateMail(): void | |
| 232 | + { | |
| 233 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 234 | + | |
| 235 | + $mailId = acym_getVar('int', 'action_mail_id', 0); | |
| 236 | + $id = acym_getVar('int', 'id', 0); | |
| 237 | + $followupClass = new FollowupClass(); | |
| 238 | + if (!$followupClass->duplicateMail($mailId, $id)) { | |
| 239 | + acym_enqueueMessage(acym_translation('ACYM_COULD_NOT_DUPLICATE_MAIL'), 'error'); | |
| 240 | + } | |
| 241 | + | |
| 242 | + $this->followupEmail(); | |
| 243 | + } | |
| 244 | + | |
| 245 | + public function followupDeleteMail(): void | |
| 246 | + { | |
| 247 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 248 | + | |
| 249 | + $mailId = acym_getVar('int', 'action_mail_id', 0); | |
| 250 | + $followupClass = new FollowupClass(); | |
| 251 | + if (!$followupClass->deleteMail($mailId)) { | |
| 252 | + acym_enqueueMessage(acym_translation('ACYM_COULD_NOT_DELETE_MAIL'), 'error'); | |
| 253 | + } | |
| 254 | + | |
| 255 | + $step = acym_getVar('cmd', 'step', 'followupEmail'); | |
| 256 | + if (!in_array($step, ['followupEmail', 'followupSummary'], true)) { | |
| 257 | + $step = 'followupEmail'; | |
| 258 | + } | |
| 259 | + $this->$step(); | |
| 260 | + } | |
| 261 | + | |
| 262 | + public function followupDraft(): void | |
| 263 | + { | |
| 264 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 265 | + | |
| 266 | + $this->followupFinalize(0); | |
| 267 | + } | |
| 268 | + | |
| 269 | + public function followupActivate(): void | |
| 270 | + { | |
| 271 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 272 | + | |
| 273 | + $this->followupFinalize(1); | |
| 274 | + } | |
| 275 | + | |
| 276 | + public function followupFinalize(int $status): void | |
| 277 | + { | |
| 278 | + $followupId = acym_getVar('int', 'id', 0); | |
| 279 | + $followupClass = new FollowupClass(); | |
| 280 | + $followup = $followupClass->getOneById($followupId); | |
| 281 | + $followup->active = $status; | |
| 282 | + $followupClass->save($followup); | |
| 283 | + | |
| 284 | + $this->followup(); | |
| 285 | + } | |
| 286 | + | |
| 287 | + public function saveFollowupCondition(): void | |
| 288 | + { | |
| 289 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 290 | + | |
| 291 | + if (!acym_isAdmin() || !acym_isAllowed('campaigns')) { | |
| 292 | + die('Access denied for follow-ups'); | |
| 293 | + } | |
| 294 | + | |
| 295 | + $id = acym_getVar('int', 'id', 0); | |
| 296 | + $trigger = acym_getVar('string', 'trigger', ''); | |
| 297 | + $followupData = acym_getVar('array', 'followup', []); | |
| 298 | + | |
| 299 | + $followupClass = new FollowupClass(); | |
| 300 | + | |
| 301 | + if (!empty($id)) { | |
| 302 | + $followup = $followupClass->getOneById($id); | |
| 303 | + $followup->condition = json_encode($followupData['condition']); | |
| 304 | + } else { | |
| 305 | + $followup = new stdClass(); | |
| 306 | + $followup->name = ''; | |
| 307 | + $followup->display_name = ''; | |
| 308 | + $followup->creation_date = gmdate('Y-m-d H:i:s', time()); | |
| 309 | + $followup->trigger = $trigger; | |
| 310 | + $followup->condition = json_encode($followupData['condition']); | |
| 311 | + $followup->active = 0; | |
| 312 | + $followup->send_once = 1; | |
| 313 | + } | |
| 314 | + | |
| 315 | + $followup->id = $followupClass->save($followup); | |
| 316 | + if (empty($followup->id)) { | |
| 317 | + acym_enqueueMessage(acym_translation('ACYM_ERROR_SAVING').': '.acym_getDBError(), 'error'); | |
| 318 | + $this->listing(); | |
| 319 | + | |
| 320 | + return; | |
| 321 | + } | |
| 322 | + | |
| 323 | + acym_setVar('id', $followup->id); | |
| 324 | + | |
| 325 | + $this->edit(); | |
| 326 | + } | |
| 327 | + | |
| 328 | + public function saveFollowupEmail(bool $redirect = true): void | |
| 329 | + { | |
| 330 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 331 | + | |
| 332 | + if (!acym_isAdmin() || !acym_isAllowed('campaigns')) { | |
| 333 | + die('Access denied for follow-ups'); | |
| 334 | + } | |
| 335 | + | |
| 336 | + $id = acym_getVar('int', 'id', 0); | |
| 337 | + $followupData = acym_getVar('array', 'followup', []); | |
| 338 | + if (empty($id) || empty($followupData)) { | |
| 339 | + return; | |
| 340 | + } | |
| 341 | + | |
| 342 | + $followupClass = new FollowupClass(); | |
| 343 | + $followup = $followupClass->getOneById($id); | |
| 344 | + | |
| 345 | + if (empty($followup)) { | |
| 346 | + return; | |
| 347 | + } | |
| 348 | + | |
| 349 | + foreach ($followupData as $key => $data) { | |
| 350 | + if (!isset($followup->$key)) continue; | |
| 351 | + $followup->$key = $data; | |
| 352 | + } | |
| 353 | + | |
| 354 | + $followup->id = $followupClass->save($followup); | |
| 355 | + acym_setVar('id', $followup->id); | |
| 356 | + | |
| 357 | + if ($redirect) { | |
| 358 | + $this->edit(); | |
| 359 | + } | |
| 360 | + } | |
| 361 | + | |
| 362 | + public function followupSummary(): void | |
| 363 | + { | |
| 364 | + acym_setVar('layout', 'followup_summary'); | |
| 365 | + | |
| 366 | + $id = acym_getVar('int', 'id', 0); | |
| 367 | + | |
| 368 | + if (empty($id)) { | |
| 369 | + acym_enqueueMessage(acym_translation('ACYM_FOLLOWUP_NOT_FOUND'), 'error'); | |
| 370 | + $this->listing(); | |
| 371 | + | |
| 372 | + return; | |
| 373 | + } | |
| 374 | + | |
| 375 | + $followupClass = new FollowupClass(); | |
| 376 | + $followup = $followupClass->getOneByIdWithMails($id); | |
| 377 | + | |
| 378 | + $data = [ | |
| 379 | + 'workflowHelper' => new WorkflowHelper(), | |
| 380 | + 'followup' => $followup, | |
| 381 | + 'condition' => $followupClass->getConditionSummary($followup), | |
| 382 | + ]; | |
| 383 | + | |
| 384 | + $this->breadcrumb[empty($followup->name) ? acym_translation('ACYM_NEW_FOLLOW_UP') : $followup->name] = acym_completeLink( | |
| 385 | + 'campaigns&task=edit&step=followupCondition'.$followup->id | |
| 386 | + ); | |
| 387 | + | |
| 388 | + parent::display($data); | |
| 389 | + } | |
| 390 | + | |
| 391 | + public function createNewFollowupMail(): void | |
| 392 | + { | |
| 393 | + $this->saveFollowupEmail(false); | |
| 394 | + $linkNewEmail = acym_getVar('string', 'linkNewEmail', ''); | |
| 395 | + | |
| 396 | + if (empty($linkNewEmail)) { | |
| 397 | + $this->edit(); | |
| 398 | + } else { | |
| 399 | + acym_redirect($linkNewEmail); | |
| 400 | + } | |
| 401 | + } | |
| 402 | +} | |