| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\Classes; |
| 4 |
|
| 5 |
use AcyMailing\Helpers\PaginationHelper; |
| 6 |
use AcyMailing\Core\AcymClass; |
| 7 |
|
| 8 |
class FollowupClass extends AcymClass |
| 9 |
{ |
| 10 |
const DEFAULT_DELAY_UNIT = 86400; |
| 11 |
const DELAY_UNIT = [ |
| 12 |
60 => 'ACYM_MINUTES', |
| 13 |
3600 => 'ACYM_HOURS', |
| 14 |
86400 => 'ACYM_DAYS', |
| 15 |
604800 => 'ACYM_WEEKS', |
| 16 |
2628000 => 'ACYM_MONTHS', |
| 17 |
]; |
| 18 |
|
| 19 |
public function __construct() |
| 20 |
{ |
| 21 |
parent::__construct(); |
| 22 |
|
| 23 |
$this->table = 'followup'; |
| 24 |
$this->pkey = 'id'; |
| 25 |
} |
| 26 |
|
| 27 |
public function getDelayUnits() |
| 28 |
{ |
| 29 |
$return = self::DELAY_UNIT; |
| 30 |
foreach ($return as $key => $value) { |
| 31 |
$return[$key] = acym_translation($value); |
| 32 |
} |
| 33 |
|
| 34 |
return $return; |
| 35 |
} |
| 36 |
|
| 37 |
public function getMatchingElements(array $settings = []): array |
| 38 |
{ |
| 39 |
$query = 'SELECT `followup`.*, COUNT(`mails`.`mail_id`) AS nbEmails |
| 40 |
FROM #__acym_followup AS followup |
| 41 |
LEFT JOIN #__acym_followup_has_mail AS mails |
| 42 |
ON followup.id = mails.followup_id'; |
| 43 |
|
| 44 |
$queryCount = 'SELECT COUNT(*) AS total FROM #__acym_followup AS followup '; |
| 45 |
|
| 46 |
$filters = []; |
| 47 |
|
| 48 |
if (!empty($settings['search'])) { |
| 49 |
$filters[] = 'followup.name LIKE '.acym_escapeDB('%'.$settings['search'].'%'); |
| 50 |
} |
| 51 |
|
| 52 |
if (!empty($filters)) { |
| 53 |
$query .= ' WHERE ('.implode(') AND (', $filters).')'; |
| 54 |
$queryCount .= ' WHERE ('.implode(') AND (', $filters).')'; |
| 55 |
} |
| 56 |
|
| 57 |
$query .= ' GROUP BY `followup`.`id` '; |
| 58 |
|
| 59 |
if (empty($settings['ordering'])) $settings['ordering'] = $this->pkey; |
| 60 |
$query .= ' ORDER BY `'.acym_secureDBColumn($settings['ordering']).'`'; |
| 61 |
if (!empty($settings['ordering_sort_order'])) $query .= ' '.acym_secureDBColumn(strtoupper($settings['ordering_sort_order'])); |
| 62 |
|
| 63 |
if (empty($settings['offset']) || $settings['offset'] < 0) { |
| 64 |
$settings['offset'] = 0; |
| 65 |
} |
| 66 |
|
| 67 |
if (empty($settings['elementsPerPage']) || $settings['elementsPerPage'] < 1) { |
| 68 |
$pagination = new PaginationHelper(); |
| 69 |
$settings['elementsPerPage'] = $pagination->getListLimit(); |
| 70 |
} |
| 71 |
|
| 72 |
$results = []; |
| 73 |
$results['elements'] = acym_loadObjectList($query, '', $settings['offset'], $settings['elementsPerPage']); |
| 74 |
$urlClickClass = new UrlClickClass(); |
| 75 |
foreach ($results['elements'] as $key => $oneFollowup) { |
| 76 |
$results['elements'][$key]->subscribers = $this->getNumberSubscribersByListId($oneFollowup->list_id); |
| 77 |
$this->getGlobalStats($results['elements'][$key], $urlClickClass); |
| 78 |
} |
| 79 |
|
| 80 |
$results['total'] = acym_loadObject($queryCount); |
| 81 |
|
| 82 |
return $results; |
| 83 |
} |
| 84 |
|
| 85 |
public function getNumberSubscribersByListId($listId = 0, $onlySubscribed = false) |
| 86 |
{ |
| 87 |
if (empty($listId)) return 0; |
| 88 |
|
| 89 |
$query = 'SELECT COUNT(*) |
| 90 |
FROM #__acym_user_has_list |
| 91 |
WHERE `list_id` = '.intval($listId); |
| 92 |
|
| 93 |
if ($onlySubscribed) { |
| 94 |
$query .= ' AND `status` = 1'; |
| 95 |
} |
| 96 |
|
| 97 |
return acym_loadResult($query); |
| 98 |
} |
| 99 |
|
| 100 |
private function getGlobalStats(&$element, $urlClickClass) |
| 101 |
{ |
| 102 |
$mailsStats = acym_loadObjectList( |
| 103 |
'SELECT mail_stat.* |
| 104 |
FROM #__acym_mail_stat AS mail_stat |
| 105 |
JOIN #__acym_followup_has_mail AS fhm ON fhm.mail_id = mail_stat.mail_id |
| 106 |
WHERE fhm.followup_id = '.intval($element->id) |
| 107 |
); |
| 108 |
|
| 109 |
$element->open = 0; |
| 110 |
$element->click = 0; |
| 111 |
$element->income = 0; |
| 112 |
$numberMailSent = 0; |
| 113 |
if (empty($mailsStats)) return; |
| 114 |
|
| 115 |
foreach ($mailsStats as $key => $mailsStat) { |
| 116 |
$element->open += $mailsStat->open_unique; |
| 117 |
$element->click += $urlClickClass->getNumberUsersClicked($mailsStat->mail_id); |
| 118 |
$numberMailSent += $mailsStat->sent; |
| 119 |
} |
| 120 |
|
| 121 |
if (!empty($numberMailSent)) { |
| 122 |
$element->open = number_format($element->open / $numberMailSent * 100, 2); |
| 123 |
$element->click = number_format($element->click / $numberMailSent * 100, 2); |
| 124 |
} |
| 125 |
|
| 126 |
if (!acym_isTrackingSalesActive()) return; |
| 127 |
|
| 128 |
$trackingSales = acym_loadObject( |
| 129 |
'SELECT SUM(user_stat.tracking_sale) AS sale, user_stat.currency |
| 130 |
FROM #__acym_user_stat AS user_stat |
| 131 |
JOIN #__acym_followup_has_mail AS fhm ON fhm.mail_id = user_stat.mail_id |
| 132 |
WHERE fhm.followup_id = '.intval($element->id) |
| 133 |
); |
| 134 |
|
| 135 |
$element->sale = $trackingSales->sale; |
| 136 |
if (empty($element->currency)) $element->currency = ''; |
| 137 |
acym_trigger('getCurrency', [&$element->currency]); |
| 138 |
} |
| 139 |
|
| 140 |
public function delete($elements) |
| 141 |
{ |
| 142 |
if (!is_array($elements)) $elements = [$elements]; |
| 143 |
acym_arrayToInteger($elements); |
| 144 |
|
| 145 |
if (empty($elements)) return 0; |
| 146 |
|
| 147 |
$emailIds = $this->getEmailsByIds($elements); |
| 148 |
if (!empty($emailIds)) { |
| 149 |
$mailClass = new MailClass(); |
| 150 |
$mailClass->delete($emailIds); |
| 151 |
} |
| 152 |
|
| 153 |
$listIds = $this->getListsByIds($elements); |
| 154 |
|
| 155 |
$result = parent::delete($elements); |
| 156 |
|
| 157 |
$listClass = new ListClass(); |
| 158 |
$listClass->delete($listIds); |
| 159 |
|
| 160 |
return $result; |
| 161 |
} |
| 162 |
|
| 163 |
public function getEmailsByIds($followupIds) |
| 164 |
{ |
| 165 |
if (!is_array($followupIds)) $followupIds = [$followupIds]; |
| 166 |
acym_arrayToInteger($followupIds); |
| 167 |
|
| 168 |
if (empty($followupIds)) return []; |
| 169 |
|
| 170 |
return acym_loadResultArray( |
| 171 |
'SELECT `mail_id` |
| 172 |
FROM #__acym_followup_has_mail |
| 173 |
WHERE `followup_id` IN ('.implode(', ', $followupIds).') |
| 174 |
ORDER BY `delay`*`delay_unit` ASC' |
| 175 |
); |
| 176 |
} |
| 177 |
|
| 178 |
public function getListsByIds($followupIds) |
| 179 |
{ |
| 180 |
if (!is_array($followupIds)) $followupIds = [$followupIds]; |
| 181 |
acym_arrayToInteger($followupIds); |
| 182 |
|
| 183 |
if (empty($followupIds)) return []; |
| 184 |
|
| 185 |
return acym_loadResultArray( |
| 186 |
'SELECT `list_id` |
| 187 |
FROM #__acym_followup |
| 188 |
WHERE `id` IN ('.implode(', ', $followupIds).')' |
| 189 |
); |
| 190 |
} |
| 191 |
|
| 192 |
public function getOneById($id) |
| 193 |
{ |
| 194 |
$followup = parent::getOneById($id); |
| 195 |
|
| 196 |
if (empty($followup)) return false; |
| 197 |
|
| 198 |
if (!empty($followup->condition)) { |
| 199 |
$followup->condition = json_decode($followup->condition, true); |
| 200 |
} |
| 201 |
|
| 202 |
if (!empty($followup->loop_mail_skip)) { |
| 203 |
$followup->loop_mail_skip = json_decode($followup->loop_mail_skip, true); |
| 204 |
} |
| 205 |
|
| 206 |
return $followup; |
| 207 |
} |
| 208 |
|
| 209 |
public function getOneByListId($listId) |
| 210 |
{ |
| 211 |
$followup = acym_loadObject('SELECT * FROM `#__acym_followup` WHERE `list_id` = '.intval($listId)); |
| 212 |
|
| 213 |
if (empty($followup)) return null; |
| 214 |
|
| 215 |
if (!empty($followup->condition)) $followup->condition = json_decode($followup->condition, true); |
| 216 |
|
| 217 |
return $followup; |
| 218 |
} |
| 219 |
|
| 220 |
public function save($element) |
| 221 |
{ |
| 222 |
if (!empty($element->condition) && is_array($element->condition)) { |
| 223 |
$element->condition = json_encode($element->condition); |
| 224 |
} |
| 225 |
|
| 226 |
if (!empty($element->loop_mail_skip) && is_array($element->loop_mail_skip)) { |
| 227 |
$element->loop_mail_skip = json_encode($element->loop_mail_skip); |
| 228 |
} |
| 229 |
|
| 230 |
$this->updateListFollowup($element); |
| 231 |
|
| 232 |
return parent::save($element); |
| 233 |
} |
| 234 |
|
| 235 |
private function updateListFollowup(&$element) |
| 236 |
{ |
| 237 |
$listClass = new ListClass(); |
| 238 |
if (empty($element->list_id)) { |
| 239 |
$list = new \stdClass(); |
| 240 |
$list->name = $element->display_name; |
| 241 |
$list->description = ''; |
| 242 |
$randColor = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']; |
| 243 |
$list->color = '#'.$randColor[rand(0, 15)].$randColor[rand(0, 15)].$randColor[rand(0, 15)].$randColor[rand(0, 15)].$randColor[rand(0, 15)].$randColor[rand(0, 15)]; |
| 244 |
$list->access = ''; |
| 245 |
$list->type = ListClass::LIST_TYPE_FOLLOWUP; |
| 246 |
} else { |
| 247 |
$list = $listClass->getOneById($element->list_id); |
| 248 |
$list->name = $element->display_name; |
| 249 |
} |
| 250 |
|
| 251 |
$element->list_id = $listClass->save($list); |
| 252 |
} |
| 253 |
|
| 254 |
public function getTriggerNiceName($trigger) |
| 255 |
{ |
| 256 |
$triggers = []; |
| 257 |
acym_trigger('getFollowupTriggers', [&$triggers]); |
| 258 |
if (key_exists($trigger, $triggers)) { |
| 259 |
return $triggers[$trigger]; |
| 260 |
} |
| 261 |
|
| 262 |
return ''; |
| 263 |
} |
| 264 |
|
| 265 |
public function getConditionSummary($condition, $trigger) |
| 266 |
{ |
| 267 |
$statusArray = [ |
| 268 |
'is' => acym_strtolower(acym_translation('ACYM_IS')), |
| 269 |
'is_not' => acym_strtolower(acym_translation('ACYM_IS_NOT')), |
| 270 |
]; |
| 271 |
$listClass = new ListClass(); |
| 272 |
$segmentClass = new SegmentClass(); |
| 273 |
|
| 274 |
$return = []; |
| 275 |
if (empty($condition['lists_status']) || empty($condition['lists'])) { |
| 276 |
$return[] = acym_translation('ACYM_NO_CONDITION_USER_SUBSCRIPTION'); |
| 277 |
} else { |
| 278 |
$lists = $listClass->getListsByIds($condition['lists']); |
| 279 |
$listsToDisplay = []; |
| 280 |
foreach ($lists as $list) { |
| 281 |
$listsToDisplay[] = $list->name; |
| 282 |
} |
| 283 |
|
| 284 |
$translationKey = $trigger === 'user_subscribe' ? 'ACYM_X_SUBSCRIBING_X_LIST' : 'ACYM_X_SUBSCRIBED_X_LIST'; |
| 285 |
$return[] = acym_translationSprintf($translationKey, acym_strtolower($statusArray[$condition['lists_status']]), implode(',', $listsToDisplay)); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
if (empty($condition['segments_status']) || empty($condition['segments'])) { |
| 290 |
$return[] = acym_translation('ACYM_NO_CONDITION_SEGMENT'); |
| 291 |
} else { |
| 292 |
$segments = $segmentClass->getByIds($condition['segments']); |
| 293 |
$segmentsToDisplay = []; |
| 294 |
foreach ($segments as $segment) { |
| 295 |
$segmentsToDisplay[] = $segment->name; |
| 296 |
} |
| 297 |
$return[] = acym_translationSprintf('ACYM_X_PART_X_SEGMENT', acym_strtolower($statusArray[$condition['segments_status']]), implode(',', $segmentsToDisplay)); |
| 298 |
} |
| 299 |
|
| 300 |
acym_trigger('getFollowupConditionSummary', [&$return, $condition, $trigger, $statusArray]); |
| 301 |
|
| 302 |
return $return; |
| 303 |
} |
| 304 |
|
| 305 |
private function getKeyMailArray($key, $mailsKey) |
| 306 |
{ |
| 307 |
if (!empty($mailsKey) && in_array($key, $mailsKey)) { |
| 308 |
return $this->getKeyMailArray(++$key, $mailsKey); |
| 309 |
} |
| 310 |
|
| 311 |
return $key; |
| 312 |
} |
| 313 |
|
| 314 |
public function getOneByIdWithMails($id) |
| 315 |
{ |
| 316 |
$followup = $this->getOneById($id); |
| 317 |
|
| 318 |
if (empty($followup)) return false; |
| 319 |
|
| 320 |
$mailClass = new MailClass(); |
| 321 |
$mails = $mailClass->decode( |
| 322 |
acym_loadObjectList( |
| 323 |
'SELECT mail.subject, mail.id, followup_mail.delay, followup_mail.delay_unit |
| 324 |
FROM #__acym_mail AS mail |
| 325 |
JOIN #__acym_followup_has_mail AS followup_mail ON mail.id = followup_mail.mail_id AND followup_mail.followup_id = '.intval($id), |
| 326 |
'id' |
| 327 |
) |
| 328 |
); |
| 329 |
|
| 330 |
$return = []; |
| 331 |
foreach ($mails as $key => $mail) { |
| 332 |
$mail->delay_display = $this->getDelayDisplay($mail->delay, $mail->delay_unit); |
| 333 |
$mail->edit_link = acym_completeLink('mails&task=edit&step=editEmail&type=followup&id='.$key.'&followup_id='.$id.'&return='.urlencode(acym_currentURL()), false, true); |
| 334 |
$finalKey = $this->getKeyMailArray(intval($mail->delay) * intval($mail->delay_unit), array_keys($return)); |
| 335 |
$return[$finalKey] = $mail; |
| 336 |
} |
| 337 |
|
| 338 |
ksort($return); |
| 339 |
|
| 340 |
$followup->mails = empty($return) ? [] : $return; |
| 341 |
|
| 342 |
return $followup; |
| 343 |
} |
| 344 |
|
| 345 |
private function getDelayDisplay($delay, $delayUnit) |
| 346 |
{ |
| 347 |
$delayUnits = $this->getDelayUnits(); |
| 348 |
|
| 349 |
return acym_translationSprintf('ACYM_X_PLUS_X_FOLLOW_UP', $delayUnits[$delayUnit], $delay); |
| 350 |
} |
| 351 |
|
| 352 |
public function getDelaySettingToMail(&$mail, $followupId) |
| 353 |
{ |
| 354 |
$settings = acym_loadObject('SELECT delay, delay_unit FROM #__acym_followup_has_mail WHERE followup_id = '.intval($followupId).' AND mail_id = '.intval($mail->id)); |
| 355 |
|
| 356 |
if (empty($settings)) return false; |
| 357 |
|
| 358 |
$mail->delay = $settings->delay; |
| 359 |
$mail->delay_unit = $settings->delay_unit; |
| 360 |
|
| 361 |
return true; |
| 362 |
} |
| 363 |
|
| 364 |
public function saveDelaySettings($followupData, $mailId) |
| 365 |
{ |
| 366 |
if (empty($followupData['id']) || empty($followupData['delay_unit']) || empty($mailId)) return false; |
| 367 |
|
| 368 |
acym_arrayToInteger($followupData); |
| 369 |
|
| 370 |
$affectedRow = acym_query( |
| 371 |
'INSERT INTO #__acym_followup_has_mail (`mail_id`, `followup_id`, `delay`, `delay_unit`) VALUE ('.intval( |
| 372 |
$mailId |
| 373 |
).', '.$followupData['id'].', '.$followupData['delay'].', '.$followupData['delay_unit'].') ON DUPLICATE KEY UPDATE delay = '.$followupData['delay'].', delay_unit = '.$followupData['delay_unit'].'' |
| 374 |
); |
| 375 |
|
| 376 |
return $affectedRow !== false; |
| 377 |
} |
| 378 |
|
| 379 |
public function duplicateMail($mailId, $id) |
| 380 |
{ |
| 381 |
if (empty($mailId) || empty($id)) return false; |
| 382 |
$mailClass = new MailClass(); |
| 383 |
$mail = $mailClass->getOneById($mailId); |
| 384 |
|
| 385 |
if (empty($mail)) return false; |
| 386 |
|
| 387 |
$delaySettings = new \stdClass(); |
| 388 |
$delaySettings->id = $mail->id; |
| 389 |
$this->getDelaySettingToMail($delaySettings, $id); |
| 390 |
|
| 391 |
unset($mail->id); |
| 392 |
$mail->name .= '_copy'; |
| 393 |
|
| 394 |
$mail->id = $mailClass->save($mail); |
| 395 |
if (empty($mail->id)) return false; |
| 396 |
|
| 397 |
$affectedRow = acym_query( |
| 398 |
'INSERT INTO #__acym_followup_has_mail (`mail_id`, `followup_id`, `delay`, `delay_unit`) VALUE ('.intval($mail->id).', '.$id.', '.intval( |
| 399 |
$delaySettings->delay |
| 400 |
).', '.intval($delaySettings->delay_unit).')' |
| 401 |
); |
| 402 |
|
| 403 |
return !empty($affectedRow); |
| 404 |
} |
| 405 |
|
| 406 |
public function deleteMail($mailId) |
| 407 |
{ |
| 408 |
if (empty($mailId)) return false; |
| 409 |
$mailClass = new MailClass(); |
| 410 |
|
| 411 |
return $mailClass->delete($mailId); |
| 412 |
} |
| 413 |
|
| 414 |
public function getFollowupsWithMailsInfoByIds($followupIds) |
| 415 |
{ |
| 416 |
if (!is_array($followupIds)) $followupIds = [$followupIds]; |
| 417 |
acym_arrayToInteger($followupIds); |
| 418 |
|
| 419 |
$mailsInfo = acym_loadObjectList( |
| 420 |
'SELECT followup_mail.*, followup.send_once |
| 421 |
FROM #__acym_followup_has_mail AS followup_mail |
| 422 |
JOIN #__acym_followup AS followup ON followup.id = followup_mail.followup_id AND followup_mail.followup_id IN ('.implode(',', $followupIds).')' |
| 423 |
); |
| 424 |
|
| 425 |
if (empty($mailsInfo)) return []; |
| 426 |
|
| 427 |
$return = []; |
| 428 |
|
| 429 |
foreach ($mailsInfo as $mailInfo) { |
| 430 |
if (!isset($return[$mailInfo->followup_id])) $return[$mailInfo->followup_id] = []; |
| 431 |
$return[$mailInfo->followup_id][] = $mailInfo; |
| 432 |
} |
| 433 |
|
| 434 |
return $return; |
| 435 |
} |
| 436 |
|
| 437 |
public function subscribeUserToFollowupList(array $followupIds, $userId): array |
| 438 |
{ |
| 439 |
acym_arrayToInteger($followupIds); |
| 440 |
|
| 441 |
$followupLists = acym_loadObjectList( |
| 442 |
'SELECT followup.list_id, IF(user_list.status IS NULL, "", status) AS status, followup.id, followup.send_once FROM #__acym_followup AS followup |
| 443 |
LEFT JOIN #__acym_user_has_list AS user_list ON followup.list_id = user_list.list_id AND (user_list.user_id = '.intval($userId).' OR user_list.user_id IS NULL) |
| 444 |
WHERE followup.id IN ('.implode(',', $followupIds).')', |
| 445 |
'list_id' |
| 446 |
); |
| 447 |
|
| 448 |
if (empty($followupLists)) { |
| 449 |
return []; |
| 450 |
} |
| 451 |
|
| 452 |
$followupListIds = []; |
| 453 |
foreach ($followupLists as $listId => $followupList) { |
| 454 |
if (in_array($followupList->status, [0, '0'], true)) { |
| 455 |
continue; |
| 456 |
} |
| 457 |
|
| 458 |
if ($followupList->status === '' || $followupList->send_once == 0) { |
| 459 |
$followupListIds[$listId] = $followupList->id; |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
if (empty($followupListIds)) { |
| 464 |
return []; |
| 465 |
} |
| 466 |
|
| 467 |
$userClass = new UserClass(); |
| 468 |
$userClass->subscribe($userId, array_keys($followupListIds), false); |
| 469 |
|
| 470 |
return $followupListIds; |
| 471 |
} |
| 472 |
|
| 473 |
public function addFollowupEmailsQueue($followupTrigger, $userId, $params = []) |
| 474 |
{ |
| 475 |
if (empty($followupTrigger) || empty($userId)) return false; |
| 476 |
|
| 477 |
$followupToTrigger = acym_loadObjectList('SELECT * FROM #__acym_followup WHERE active = 1 AND `trigger` = '.acym_escapeDB($followupTrigger), 'id'); |
| 478 |
|
| 479 |
if (empty($followupToTrigger)) return false; |
| 480 |
|
| 481 |
foreach ($followupToTrigger as $key => $followup) { |
| 482 |
if (!empty($followup->condition)) { |
| 483 |
$followupToTrigger[$key]->condition = json_decode($followup->condition, true); |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
acym_trigger('matchFollowupsConditions', [&$followupToTrigger, $userId, $params]); |
| 488 |
|
| 489 |
if (empty($followupToTrigger)) return false; |
| 490 |
|
| 491 |
$followupIds = $this->subscribeUserToFollowupList(array_keys($followupToTrigger), $userId); |
| 492 |
|
| 493 |
if (empty($followupIds)) return false; |
| 494 |
|
| 495 |
$followups = $this->getFollowupsWithMailsInfoByIds($followupIds); |
| 496 |
|
| 497 |
if (empty($followups)) return false; |
| 498 |
|
| 499 |
$priority = $this->config->get('followup_max_priority', 0) == 1 ? 1 : 2; |
| 500 |
|
| 501 |
$valuesToInsert = []; |
| 502 |
|
| 503 |
foreach ($followups as $followupId => $mailsInfo) { |
| 504 |
foreach ($mailsInfo as $mailInfo) { |
| 505 |
$sendDate = time() + (intval($mailInfo->delay) * intval($mailInfo->delay_unit)); |
| 506 |
$sendDate = acym_date($sendDate, 'Y-m-d H:i:s', false); |
| 507 |
$valuesToInsert[] = ' ('.intval($mailInfo->mail_id).', '.intval($userId).', '.acym_escapeDB($sendDate).', '.intval($priority).', '.acym_escapeDB( |
| 508 |
json_encode($params) |
| 509 |
).') '; |
| 510 |
$this->addMailStat($mailInfo->mail_id); |
| 511 |
} |
| 512 |
$followupToTrigger[$followupId]->last_trigger = time(); |
| 513 |
$this->save($followupToTrigger[$followupId]); |
| 514 |
} |
| 515 |
|
| 516 |
$query = 'INSERT IGNORE INTO #__acym_queue (`mail_id`, `user_id`, `sending_date`, `priority`, `params`) VALUES '.implode(',', $valuesToInsert); |
| 517 |
|
| 518 |
$affectedRows = acym_query($query); |
| 519 |
|
| 520 |
return !empty($affectedRows); |
| 521 |
} |
| 522 |
|
| 523 |
private function addMailStat($mailId) |
| 524 |
{ |
| 525 |
$mailStatClass = new MailStatClass(); |
| 526 |
$mailStat = $mailStatClass->getOneRowByMailId($mailId); |
| 527 |
$newMailStat = [ |
| 528 |
'mail_id' => intval($mailId), |
| 529 |
'total_subscribers' => 1, |
| 530 |
]; |
| 531 |
if (empty($mailStat)) { |
| 532 |
$newMailStat['send_date'] = acym_date('now', 'Y-m-d H:i:s', false); |
| 533 |
} |
| 534 |
|
| 535 |
$mailStatClass->save($newMailStat); |
| 536 |
} |
| 537 |
|
| 538 |
public function getFollowupDailyBases() |
| 539 |
{ |
| 540 |
$triggers = []; |
| 541 |
acym_trigger('onAcymGetFollowupDailyBases', [&$triggers]); |
| 542 |
|
| 543 |
return acym_loadObjectList('SELECT * FROM #__acym_followup WHERE `trigger` IN ("'.implode('","', $triggers).'") AND active = 1'); |
| 544 |
} |
| 545 |
|
| 546 |
public function queueForSubscribers($emailId) |
| 547 |
{ |
| 548 |
if (empty($emailId)) return false; |
| 549 |
|
| 550 |
$mailInfo = acym_loadObject( |
| 551 |
'SELECT map.*, followup.list_id |
| 552 |
FROM #__acym_followup_has_mail AS map |
| 553 |
JOIN #__acym_followup AS followup |
| 554 |
ON followup.id = map.followup_id |
| 555 |
WHERE map.mail_id = '.intval($emailId) |
| 556 |
); |
| 557 |
if (empty($mailInfo)) return false; |
| 558 |
|
| 559 |
$this->addMailStat($mailInfo->mail_id); |
| 560 |
|
| 561 |
$delay = intval($mailInfo->delay) * intval($mailInfo->delay_unit); |
| 562 |
$query = 'INSERT IGNORE INTO #__acym_queue (`mail_id`, `user_id`, `sending_date`, `priority`) |
| 563 |
SELECT '.intval($mailInfo->mail_id).', user_id, TIMESTAMPADD(SECOND, '.$delay.', subscription_date), 2 |
| 564 |
FROM #__acym_user_has_list |
| 565 |
WHERE status = 1 |
| 566 |
AND list_id = '.intval($mailInfo->list_id); |
| 567 |
|
| 568 |
return acym_query($query); |
| 569 |
} |
| 570 |
|
| 571 |
public function getXFollowups(array $options): array |
| 572 |
{ |
| 573 |
$limit = $options['limit'] ?? 10; |
| 574 |
$offset = $options['offset'] ?? 0; |
| 575 |
$filters = $options['filters'] ?? []; |
| 576 |
|
| 577 |
$conditions = []; |
| 578 |
foreach ($filters as $column => $filter) { |
| 579 |
switch ($column) { |
| 580 |
case 'id': |
| 581 |
case 'active': |
| 582 |
case 'send_one': |
| 583 |
$conditions[] = acym_secureDBColumn($column).' = '.intval($filter); |
| 584 |
break; |
| 585 |
case 'name': |
| 586 |
case 'trigger': |
| 587 |
case 'display_name': |
| 588 |
$conditions[] = '`'.acym_secureDBColumn($column).'` LIKE '.acym_escapeDB('%'.$filter.'%'); |
| 589 |
break; |
| 590 |
default: |
| 591 |
$conditions[] = acym_secureDBColumn($column).' = '.acym_escapeDB('%'.$filter.'%'); |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
$query = 'SELECT * FROM #__acym_followup '; |
| 596 |
|
| 597 |
if (!empty($conditions)) { |
| 598 |
$query .= ' WHERE '.implode(' AND ', $conditions); |
| 599 |
} |
| 600 |
|
| 601 |
$followUps = acym_loadObjectList($query, $this->pkey, $offset, $limit); |
| 602 |
|
| 603 |
if (empty($followUps)) { |
| 604 |
return []; |
| 605 |
} |
| 606 |
|
| 607 |
foreach ($followUps as &$followUp) { |
| 608 |
if (!empty($followUp->condition)) { |
| 609 |
$followUp->condition = json_decode($followUp->condition, true); |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
return $followUps; |
| 614 |
} |
| 615 |
|
| 616 |
public function getOneByMailId(int $mailId): ?object |
| 617 |
{ |
| 618 |
return acym_loadObject( |
| 619 |
'SELECT followup.* |
| 620 |
FROM #__acym_followup AS followup |
| 621 |
JOIN #__acym_followup_has_mail AS map |
| 622 |
ON followup.id = map.followup_id |
| 623 |
WHERE mail_id = '.intval($mailId) |
| 624 |
); |
| 625 |
} |
| 626 |
|
| 627 |
public function getLastEmail(int $followupId): object |
| 628 |
{ |
| 629 |
return acym_loadObject( |
| 630 |
'SELECT mail_id as id, delay * delay_unit as totalDelay FROM #__acym_followup_has_mail WHERE followup_id = '.intval($followupId).' ORDER BY totalDelay DESC' |
| 631 |
); |
| 632 |
} |
| 633 |
|
| 634 |
public function triggerFollowUp(int $followUpId, int $userId, int $additionalDelay = 0, array $mailIdToSkip = []): void |
| 635 |
{ |
| 636 |
$followUp = $this->getOneById($followUpId); |
| 637 |
|
| 638 |
if (empty($followUp)) { |
| 639 |
return; |
| 640 |
} |
| 641 |
|
| 642 |
$values = intval($userId).', '.intval($followUp->list_id).', 1, '.acym_escapeDB(acym_date(time(), 'Y-m-d H:i:s')); |
| 643 |
|
| 644 |
acym_query('INSERT IGNORE INTO #__acym_user_has_list (`user_id`, `list_id`, `status`, `subscription_date`) VALUES ('.$values.') ON DUPLICATE KEY UPDATE status = 1'); |
| 645 |
|
| 646 |
$followups = $this->getFollowupsWithMailsInfoByIds($followUpId); |
| 647 |
$allValues = []; |
| 648 |
foreach ($followups as $mails) { |
| 649 |
foreach ($mails as $mail) { |
| 650 |
if (in_array($mail->mail_id, $mailIdToSkip)) { |
| 651 |
continue; |
| 652 |
} |
| 653 |
$sendDate = time() + (intval($mail->delay) * intval($mail->delay_unit)) + $additionalDelay; |
| 654 |
$sendDate = acym_escapeDB(acym_date($sendDate, 'Y-m-d H:i:s', false)); |
| 655 |
$allValues[] = '('.intval($mail->mail_id).', '.intval($userId).', '.$sendDate.', '.$this->config->get('priority_newsletter', 3).', 0'.')'; |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
$queryToProcess = 'INSERT IGNORE INTO #__acym_queue (`mail_id`, `user_id`, `sending_date`, `priority`, `try`) VALUES '.implode(', ', $allValues); |
| 660 |
|
| 661 |
if (empty(acym_query($queryToProcess))) { |
| 662 |
acym_logError("Error adding follow up email to queue \n Values: ".implode(', ', $allValues)." \n Error: ".acym_getDBError()); |
| 663 |
} |
| 664 |
} |
| 665 |
} |
| 666 |
|