← All changes
|
back/dynamics/subscription/SubscriptionAutomationActions.php
+169
-0
10.2.0
→
11.1.0
View file →
| @@ -1,0 +1,169 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +use AcyMailing\Classes\FollowupClass; | |
| 4 | +use AcyMailing\Classes\ListClass; | |
| 5 | + | |
| 6 | +trait SubscriptionAutomationActions | |
| 7 | +{ | |
| 8 | + protected function initAutomationActions(): void | |
| 9 | + { | |
| 10 | + $this->automationActions = [ | |
| 11 | + 'acy_list' => acym_translation('ACYM_ACYMAILING_LIST'), | |
| 12 | + 'subscribe_followup' => acym_translation('ACYM_SUBSCRIBE_FOLLOW_UP'), | |
| 13 | + 'unsubscribe_followup' => acym_translation('ACYM_UNSUBSCRIBE_FOLLOW_UP'), | |
| 14 | + ]; | |
| 15 | + } | |
| 16 | + | |
| 17 | + public function displayActionOptions_acy_list(): void | |
| 18 | + { | |
| 19 | + $listClass = new ListClass(); | |
| 20 | + | |
| 21 | + $listActions = [ | |
| 22 | + 'sub' => acym_translation('ACYM_SUBSCRIBE_USERS_TO'), | |
| 23 | + 'remove' => acym_translation('ACYM_REMOVE_USERS_FROM'), | |
| 24 | + 'unsub' => acym_translation('ACYM_UNSUBSCRIBE_USERS_TO'), | |
| 25 | + ]; | |
| 26 | + $lists = $listClass->getAllForSelect(); | |
| 27 | + | |
| 28 | + include acym_getPartial('actions', 'acy_list'); | |
| 29 | + } | |
| 30 | + | |
| 31 | + public function displayActionOptions_subscribe_followup(): void | |
| 32 | + { | |
| 33 | + $followupClass = new FollowupClass(); | |
| 34 | + $allListFollowups = $followupClass->getAll(); | |
| 35 | + | |
| 36 | + include acym_getPartial('actions', 'subscribe_followup'); | |
| 37 | + } | |
| 38 | + | |
| 39 | + public function displayActionOptions_unsubscribe_followup(): void | |
| 40 | + { | |
| 41 | + $followupClass = new FollowupClass(); | |
| 42 | + $allListFollowups = $followupClass->getAll(); | |
| 43 | + | |
| 44 | + include acym_getPartial('actions', 'unsubscribe_followup'); | |
| 45 | + } | |
| 46 | + | |
| 47 | + public function onAcymProcessAction_acy_list(&$query, $action) | |
| 48 | + { | |
| 49 | + if ($action['list_actions'] === 'sub') { | |
| 50 | + $queryToProcess = 'INSERT IGNORE #__acym_user_has_list (`user_id`, `list_id`, `status`, `subscription_date`) ('.$query->getQuery( | |
| 51 | + [ | |
| 52 | + 'user.id', | |
| 53 | + intval($action['list_id']), | |
| 54 | + '1', | |
| 55 | + acym_escapeDB(acym_date(time(), 'Y-m-d H:i:s')), | |
| 56 | + ] | |
| 57 | + ).') ON DUPLICATE KEY UPDATE status = 1'; | |
| 58 | + } elseif ($action['list_actions'] === 'remove') { | |
| 59 | + $queryToProcess = 'DELETE FROM #__acym_user_has_list WHERE list_id = '.intval($action['list_id']).' AND user_id IN ('.$query->getQuery(['user.id']).')'; | |
| 60 | + } elseif ($action['list_actions'] === 'unsub') { | |
| 61 | + $queryToProcess = 'UPDATE #__acym_user_has_list SET status = 0, `unsubscribe_date` = '.acym_escapeDB(gmdate('Y-m-d H:i:s', time())).' WHERE list_id = '.intval($action['list_id']).' AND user_id IN ('.$query->getQuery(['user.id']).')'; | |
| 62 | + } | |
| 63 | + | |
| 64 | + $nbAffected = acym_query($queryToProcess); | |
| 65 | + | |
| 66 | + return acym_translationSprintf('ACYM_ACTION_LIST_'.strtoupper($action['list_actions']), $nbAffected); | |
| 67 | + } | |
| 68 | + | |
| 69 | + public function onAcymProcessAction_subscribe_followup(&$query, &$action) | |
| 70 | + { | |
| 71 | + $followupClass = new FollowupClass(); | |
| 72 | + $followup = $followupClass->getOneById($action['followup_id']); | |
| 73 | + if (empty($followup->active)) { | |
| 74 | + return ''; | |
| 75 | + } | |
| 76 | + | |
| 77 | + $queryToProcess = 'INSERT IGNORE #__acym_user_has_list (`user_id`, `list_id`, `status`, `subscription_date`) ('.$query->getQuery( | |
| 78 | + [ | |
| 79 | + 'user.id', | |
| 80 | + $followup->list_id, | |
| 81 | + '1', | |
| 82 | + acym_escapeDB(acym_date(time(), 'Y-m-d H:i:s')), | |
| 83 | + ] | |
| 84 | + ).') ON DUPLICATE KEY UPDATE status = 1'; | |
| 85 | + | |
| 86 | + $nbAffected = acym_query($queryToProcess); | |
| 87 | + | |
| 88 | + $followups = $followupClass->getFollowupsWithMailsInfoByIds([$action['followup_id']]); | |
| 89 | + foreach ($followups as $mails) { | |
| 90 | + foreach ($mails as $mail) { | |
| 91 | + $sendDate = time() + (intval($mail->delay) * intval($mail->delay_unit)); | |
| 92 | + $sendDate = acym_escapeDB(acym_date($sendDate, 'Y-m-d H:i:s', false)); | |
| 93 | + $queryToProcess = 'INSERT IGNORE #__acym_queue (`mail_id`, `user_id`, `sending_date`, `priority`, `try`) ('.$query->getQuery( | |
| 94 | + [ | |
| 95 | + $mail->mail_id, | |
| 96 | + 'user.id', | |
| 97 | + $sendDate, | |
| 98 | + $this->config->get('priority_newsletter', 3), | |
| 99 | + 0, | |
| 100 | + ] | |
| 101 | + ).')'; | |
| 102 | + | |
| 103 | + acym_query($queryToProcess); | |
| 104 | + } | |
| 105 | + } | |
| 106 | + | |
| 107 | + return acym_translationSprintf('ACYM_ACTION_LIST_SUB', $nbAffected); | |
| 108 | + } | |
| 109 | + | |
| 110 | + public function onAcymProcessAction_unsubscribe_followup(&$query, &$action) | |
| 111 | + { | |
| 112 | + $followupClass = new FollowupClass(); | |
| 113 | + $followup = $followupClass->getOneById($action['followup_id']); | |
| 114 | + if (empty($followup)) { | |
| 115 | + return ''; | |
| 116 | + } | |
| 117 | + | |
| 118 | + $mailIds = $followupClass->getEmailsByIds([$action['followup_id']]); | |
| 119 | + if (!empty($mailIds)) { | |
| 120 | + acym_query( | |
| 121 | + 'DELETE FROM #__acym_queue | |
| 122 | + WHERE user_id IN ('.$query->getQuery(['user.id']).') | |
| 123 | + AND mail_id IN ('.implode(',', $mailIds).')' | |
| 124 | + ); | |
| 125 | + } | |
| 126 | + | |
| 127 | + $unsubscribeDate = gmdate('Y-m-d H:i:s', time()); | |
| 128 | + $nbAffected = acym_query( | |
| 129 | + 'UPDATE #__acym_user_has_list | |
| 130 | + SET `status` = 0, `unsubscribe_date` = '.acym_escapeDB($unsubscribeDate).' | |
| 131 | + WHERE list_id = '.intval($followup->list_id).' | |
| 132 | + AND user_id IN ('.$query->getQuery(['user.id']).')' | |
| 133 | + ); | |
| 134 | + | |
| 135 | + return acym_translationSprintf('ACYM_ACTION_LIST_UNSUB', $nbAffected); | |
| 136 | + } | |
| 137 | + | |
| 138 | + public function onAcymDeclareSummary_actions(&$automationAction) | |
| 139 | + { | |
| 140 | + if (!empty($automationAction['acy_list'])) { | |
| 141 | + $listClass = new ListClass(); | |
| 142 | + $list = $listClass->getOneById($automationAction['acy_list']['list_id'] ? : 0); | |
| 143 | + if ($automationAction['acy_list']['list_actions'] == 'sub') $automationAction['acy_list']['list_actions'] = 'ACYM_SUBSCRIBED_TO'; | |
| 144 | + if ($automationAction['acy_list']['list_actions'] == 'unsub') $automationAction['acy_list']['list_actions'] = 'ACYM_UNSUBSCRIBE_FROM'; | |
| 145 | + if ($automationAction['acy_list']['list_actions'] == 'remove') $automationAction['acy_list']['list_actions'] = 'ACYM_REMOVE_FROM'; | |
| 146 | + if (empty($list)) { | |
| 147 | + $automationAction = '<span class="acym__color__red">'.acym_translation('ACYM_SELECT_A_LIST').'</span>'; | |
| 148 | + } else { | |
| 149 | + $automationAction = acym_translationSprintf('ACYM_ACTION_LIST_SUMMARY', acym_translation($automationAction['acy_list']['list_actions']), $list->name); | |
| 150 | + } | |
| 151 | + } | |
| 152 | + | |
| 153 | + if (!empty($automationAction['subscribe_followup'])) { | |
| 154 | + $followupClass = new FollowupClass(); | |
| 155 | + $followup = $followupClass->getOneById($automationAction['subscribe_followup']['followup_id']); | |
| 156 | + $automationAction = !empty($followup) | |
| 157 | + ? acym_translationSprintf('ACYM_ACTION_SUBSCRIBE_FOLLOWUP_SUMMARY', $followup->name) | |
| 158 | + : acym_translation('ACYM_FOLLOWUP_NOT_FOUND'); | |
| 159 | + } | |
| 160 | + | |
| 161 | + if (!empty($automationAction['unsubscribe_followup'])) { | |
| 162 | + $followupClass = new FollowupClass(); | |
| 163 | + $followup = $followupClass->getOneById($automationAction['unsubscribe_followup']['followup_id']); | |
| 164 | + $automationAction = !empty($followup) | |
| 165 | + ? acym_translationSprintf('ACYM_ACTION_UNSUBSCRIBE_FOLLOWUP_SUMMARY', $followup->name) | |
| 166 | + : acym_translation('ACYM_FOLLOWUP_NOT_FOUND'); | |
| 167 | + } | |
| 168 | + } | |
| 169 | +} | |