| 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 |
acym_checkToken(); |
| 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 |
$favoriteTemplate = $this->config->get('favorite_template', 0); |
| 196 |
$startFrom = empty($favoriteTemplate) ? '' : '&from='.$favoriteTemplate; |
| 197 |
$data = [ |
| 198 |
'workflowHelper' => new WorkflowHelper(), |
| 199 |
'followup' => $followup, |
| 200 |
'linkNewEmail' => acym_completeLink( |
| 201 |
'mails&task=edit&step=editEmail&type=followup&followup_id='.$id.'&return='.urlencode( |
| 202 |
acym_completeLink('campaigns&task=edit&step=followupEmail&id='.$id) |
| 203 |
).$startFrom, |
| 204 |
false, |
| 205 |
true |
| 206 |
), |
| 207 |
]; |
| 208 |
|
| 209 |
$this->breadcrumb[empty($followup->name) ? acym_translation('ACYM_NEW_FOLLOW_UP') : $followup->name] = acym_completeLink( |
| 210 |
'campaigns&task=edit&step=followupEmail&id='.$followup->id |
| 211 |
); |
| 212 |
|
| 213 |
if (intval($followup->active) === 1) { |
| 214 |
$newlyCreatedEmail = acym_getVar('int', 'newEmailId'); |
| 215 |
if (!empty($newlyCreatedEmail)) { |
| 216 |
$numberOfSubscribers = $followupClass->getNumberSubscribersByListId($followup->list_id, true); |
| 217 |
if (!empty($numberOfSubscribers)) { |
| 218 |
$message = '<span class="acym__followup__add_queue" data-acym-email-id="'.intval($newlyCreatedEmail).'">'; |
| 219 |
$message .= acym_translationSprintf('ACYM_FOLLOWUP_ADD_QUEUE', $numberOfSubscribers); |
| 220 |
$message .= '</span>'; |
| 221 |
acym_enqueueMessage($message, 'info', false); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
parent::display($data); |
| 227 |
} |
| 228 |
|
| 229 |
public function followupDuplicateMail(): void |
| 230 |
{ |
| 231 |
$mailId = acym_getVar('int', 'action_mail_id', 0); |
| 232 |
$id = acym_getVar('int', 'id', 0); |
| 233 |
$followupClass = new FollowupClass(); |
| 234 |
if (!$followupClass->duplicateMail($mailId, $id)) { |
| 235 |
acym_enqueueMessage(acym_translation('ACYM_COULD_NOT_DUPLICATE_MAIL'), 'error'); |
| 236 |
} |
| 237 |
|
| 238 |
$this->followupEmail(); |
| 239 |
} |
| 240 |
|
| 241 |
public function followupDeleteMail(): void |
| 242 |
{ |
| 243 |
$mailId = acym_getVar('int', 'action_mail_id', 0); |
| 244 |
$followupClass = new FollowupClass(); |
| 245 |
if (!$followupClass->deleteMail($mailId)) { |
| 246 |
acym_enqueueMessage(acym_translation('ACYM_COULD_NOT_DELETE_MAIL'), 'error'); |
| 247 |
} |
| 248 |
|
| 249 |
$step = acym_getVar('cmd', 'step', 'followupEmail'); |
| 250 |
$this->$step(); |
| 251 |
} |
| 252 |
|
| 253 |
public function followupDraft(): void |
| 254 |
{ |
| 255 |
$this->followupFinalize(0); |
| 256 |
} |
| 257 |
|
| 258 |
public function followupActivate(): void |
| 259 |
{ |
| 260 |
$this->followupFinalize(1); |
| 261 |
} |
| 262 |
|
| 263 |
public function followupFinalize(int $status): void |
| 264 |
{ |
| 265 |
$followupId = acym_getVar('int', 'id', 0); |
| 266 |
$followupClass = new FollowupClass(); |
| 267 |
$followup = $followupClass->getOneById($followupId); |
| 268 |
$followup->active = $status; |
| 269 |
$followupClass->save($followup); |
| 270 |
|
| 271 |
$this->followup(); |
| 272 |
} |
| 273 |
|
| 274 |
public function saveFollowupCondition(): void |
| 275 |
{ |
| 276 |
if (!acym_isAdmin()) { |
| 277 |
die('Access denied for follow-ups'); |
| 278 |
} |
| 279 |
|
| 280 |
$id = acym_getVar('int', 'id', 0); |
| 281 |
$trigger = acym_getVar('string', 'trigger', ''); |
| 282 |
$followupData = acym_getVar('array', 'followup', []); |
| 283 |
|
| 284 |
$followupClass = new FollowupClass(); |
| 285 |
|
| 286 |
if (!empty($id)) { |
| 287 |
$followup = $followupClass->getOneById($id); |
| 288 |
$followup->condition = json_encode($followupData['condition']); |
| 289 |
} else { |
| 290 |
$followup = new stdClass(); |
| 291 |
$followup->name = ''; |
| 292 |
$followup->display_name = ''; |
| 293 |
$followup->creation_date = date('Y-m-d H:i:s', time() - date('Z')); |
| 294 |
$followup->trigger = $trigger; |
| 295 |
$followup->condition = json_encode($followupData['condition']); |
| 296 |
$followup->active = 0; |
| 297 |
$followup->send_once = 1; |
| 298 |
} |
| 299 |
|
| 300 |
$followup->id = $followupClass->save($followup); |
| 301 |
if (empty($followup->id)) { |
| 302 |
acym_enqueueMessage(acym_translation('ACYM_ERROR_SAVING').': '.acym_getDBError(), 'error'); |
| 303 |
$this->listing(); |
| 304 |
|
| 305 |
return; |
| 306 |
} |
| 307 |
|
| 308 |
acym_setVar('id', $followup->id); |
| 309 |
|
| 310 |
$this->edit(); |
| 311 |
} |
| 312 |
|
| 313 |
public function saveFollowupEmail(bool $redirect = true): void |
| 314 |
{ |
| 315 |
if (!acym_isAdmin()) { |
| 316 |
die('Access denied for follow-ups'); |
| 317 |
} |
| 318 |
|
| 319 |
$id = acym_getVar('int', 'id', 0); |
| 320 |
$followupData = acym_getVar('array', 'followup', []); |
| 321 |
if (empty($id) || empty($followupData)) { |
| 322 |
return; |
| 323 |
} |
| 324 |
|
| 325 |
$followupClass = new FollowupClass(); |
| 326 |
$followup = $followupClass->getOneById($id); |
| 327 |
|
| 328 |
if (empty($followup)) { |
| 329 |
return; |
| 330 |
} |
| 331 |
|
| 332 |
foreach ($followupData as $key => $data) { |
| 333 |
if (!isset($followup->$key)) continue; |
| 334 |
$followup->$key = $data; |
| 335 |
} |
| 336 |
|
| 337 |
$followup->id = $followupClass->save($followup); |
| 338 |
acym_setVar('id', $followup->id); |
| 339 |
|
| 340 |
if ($redirect) { |
| 341 |
$this->edit(); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
public function followupSummary(): void |
| 346 |
{ |
| 347 |
acym_setVar('layout', 'followup_summary'); |
| 348 |
|
| 349 |
$id = acym_getVar('int', 'id', 0); |
| 350 |
|
| 351 |
if (empty($id)) { |
| 352 |
acym_enqueueMessage(acym_translation('ACYM_FOLLOWUP_NOT_FOUND'), 'error'); |
| 353 |
$this->listing(); |
| 354 |
|
| 355 |
return; |
| 356 |
} |
| 357 |
|
| 358 |
$followupClass = new FollowupClass(); |
| 359 |
$followup = $followupClass->getOneByIdWithMails($id); |
| 360 |
|
| 361 |
$data = [ |
| 362 |
'workflowHelper' => new WorkflowHelper(), |
| 363 |
'followup' => $followup, |
| 364 |
'condition' => $followupClass->getConditionSummary($followup), |
| 365 |
]; |
| 366 |
|
| 367 |
$this->breadcrumb[empty($followup->name) ? acym_translation('ACYM_NEW_FOLLOW_UP') : $followup->name] = acym_completeLink( |
| 368 |
'campaigns&task=edit&step=followupCondition'.$followup->id |
| 369 |
); |
| 370 |
|
| 371 |
parent::display($data); |
| 372 |
} |
| 373 |
|
| 374 |
public function createNewFollowupMail(): void |
| 375 |
{ |
| 376 |
$this->saveFollowupEmail(false); |
| 377 |
$linkNewEmail = acym_getVar('string', 'linkNewEmail', ''); |
| 378 |
|
| 379 |
if (empty($linkNewEmail)) { |
| 380 |
$this->edit(); |
| 381 |
} else { |
| 382 |
acym_redirect($linkNewEmail); |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
|