| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace AcyMailing\Helpers; |
| 4 | 4 | |
| 5 | +use AcyMailing\Classes\HistoryClass; | |
| 5 | 6 | use AcyMailing\Classes\ScenarioClass; |
| 6 | 7 | use AcyMailing\Classes\ScenarioHistoryLineClass; |
| 7 | 8 | use AcyMailing\Classes\ScenarioProcessClass; |
| 8 | 9 | use AcyMailing\Classes\ScenarioStepClass; |
| @@ -27,13 +28,17 @@ | ||
| 27 | 28 | $this->scenarioHistoryLineClass = new ScenarioHistoryLineClass(); |
| 28 | 29 | $this->scenarioProcessClass = new ScenarioProcessClass(); |
| 29 | 30 | } |
| 30 | 31 | |
| 31 | - public function trigger(string $trigger, array $options) | |
| 32 | + public function trigger(string $trigger, array $options): void | |
| 32 | 33 | { |
| 33 | - if (!acym_level(ACYM_ENTERPRISE) || empty($trigger)) { | |
| 34 | - acym_logError('ScenarioHelper::trigger - Missing trigger or not allowed, trigger: '.$trigger); | |
| 34 | + if (!acym_level(ACYM_ENTERPRISE)) { | |
| 35 | + return; | |
| 36 | + } | |
| 35 | 37 | |
| 38 | + if (empty($trigger)) { | |
| 39 | + acym_logError('ScenarioHelper::trigger - Missing trigger for options: '.json_encode($options)); | |
| 40 | + | |
| 36 | 41 | return; |
| 37 | 42 | } |
| 38 | 43 | |
| 39 | 44 | if (empty($options['userId'])) { |
| @@ -54,13 +59,14 @@ | ||
| 54 | 59 | $scenarios = $this->scenarioClass->getAllActiveByTriggers([$trigger]); |
| 55 | 60 | |
| 56 | 61 | foreach ($scenarios as $scenario) { |
| 57 | 62 | if ($scenario->trigger_once) { |
| 58 | - if (!empty($this->scenarioProcessClass->getOneByScenarioIdUserId($scenario->id, $userId))) { | |
| 63 | + if ($this->scenarioProcessClass->hasUserProcess($scenario->id, $userId)) { | |
| 59 | 64 | continue; |
| 60 | 65 | } |
| 61 | 66 | } |
| 62 | 67 | |
| 68 | + // For the trigger TimeAutomationTriggers, this way we know if we are triggering a scenario or not | |
| 63 | 69 | $scenario->is_scenario = true; |
| 64 | 70 | $scenario->triggers = [$scenario->trigger => 1]; |
| 65 | 71 | $execute = false; |
| 66 | 72 | $options = $this->formatOptions($scenario->trigger_params, 'trigger'); |
| @@ -90,9 +96,9 @@ | ||
| 90 | 96 | $this->handleStep($firstStep, $userId, $scenarioProcess->id); |
| 91 | 97 | } |
| 92 | 98 | } |
| 93 | 99 | |
| 94 | - private function handleStep(object $step, int $userId, int $scenarioProcessId) | |
| 100 | + private function handleStep(object $step, int $userId, int $scenarioProcessId): void | |
| 95 | 101 | { |
| 96 | 102 | switch ($step->type) { |
| 97 | 103 | case ScenarioClass::TYPE_DELAY: |
| 98 | 104 | $this->handleDelay($step, $userId, $scenarioProcessId); |
| @@ -105,14 +111,14 @@ | ||
| 105 | 111 | break; |
| 106 | 112 | } |
| 107 | 113 | } |
| 108 | 114 | |
| 109 | - private function handleDelay(object $step, int $userId, int $scenarioProcessId) | |
| 115 | + private function handleDelay(object $step, int $userId, int $scenarioProcessId): void | |
| 110 | 116 | { |
| 111 | 117 | $nextStep = $this->scenarioStepClass->getStepByPreviousStepId($step->id); |
| 112 | 118 | |
| 113 | 119 | if (empty($nextStep)) { |
| 114 | - $this->ending($step->scenario_id, $userId, $scenarioProcessId); | |
| 120 | + $this->ending($scenarioProcessId); | |
| 115 | 121 | |
| 116 | 122 | return; |
| 117 | 123 | } |
| 118 | 124 | |
| @@ -127,9 +133,9 @@ | ||
| 127 | 133 | |
| 128 | 134 | $this->createHistory($scenarioProcessId, ScenarioClass::TYPE_DELAY, 'success', $step->id, $step->params); |
| 129 | 135 | } |
| 130 | 136 | |
| 131 | - private function calculateDelaySecondsToAdd(array $stepDelayParams) | |
| 137 | + private function calculateDelaySecondsToAdd(array $stepDelayParams): int | |
| 132 | 138 | { |
| 133 | 139 | if (empty($stepDelayParams['delay']) || empty($stepDelayParams['unit'])) { |
| 134 | 140 | return 0; |
| 135 | 141 | } |
| @@ -136,9 +142,9 @@ | ||
| 136 | 142 | |
| 137 | 143 | return $stepDelayParams['delay'] * $stepDelayParams['unit']; |
| 138 | 144 | } |
| 139 | 145 | |
| 140 | - private function handleCondition(object $step, int $userId, int $scenarioProcessId) | |
| 146 | + private function handleCondition(object $step, int $userId, int $scenarioProcessId): void | |
| 141 | 147 | { |
| 142 | 148 | $conditionNotValidCount = 0; |
| 143 | 149 | |
| 144 | 150 | $query = new AutomationHelper(); |
| @@ -144,8 +150,9 @@ | ||
| 144 | 150 | $query = new AutomationHelper(); |
| 145 | 151 | $query->where = ['user.id = '.$userId]; |
| 146 | 152 | |
| 147 | 153 | $conditionOptions = $this->formatOptions($step->params['option'], $step->type); |
| 154 | + // Unused variable | |
| 148 | 155 | $number = 0; |
| 149 | 156 | acym_trigger('onAcymProcessCondition_'.$step->params['condition'], [&$query, &$conditionOptions, &$number, &$conditionNotValidCount]); |
| 150 | 157 | $this->scenarioQueueClass->deleteByStepIds([$step->id]); |
| 151 | 158 | |
| @@ -156,9 +163,9 @@ | ||
| 156 | 163 | |
| 157 | 164 | $nextStep = $this->scenarioStepClass->getStepByPreviousConditionId($step->id, $conditionValid); |
| 158 | 165 | |
| 159 | 166 | if (empty($nextStep)) { |
| 160 | - $this->ending($step->scenario_id, $userId, $scenarioProcessId); | |
| 167 | + $this->ending($scenarioProcessId); | |
| 161 | 168 | |
| 162 | 169 | return; |
| 163 | 170 | } |
| 164 | 171 | |
| @@ -197,10 +204,19 @@ | ||
| 197 | 204 | |
| 198 | 205 | return $formattedOptions; |
| 199 | 206 | } |
| 200 | 207 | |
| 201 | - private function handleAction(object $step, int $userId, int $scenarioProcessId) | |
| 208 | + private function handleAction(object $step, int $userId, int $scenarioProcessId): void | |
| 202 | 209 | { |
| 210 | + $scenarioClass = new ScenarioClass(); | |
| 211 | + $markUnsubscribed = $scenarioClass->checkUserMarkedUnsubscribed($userId, $scenarioProcessId); | |
| 212 | + if ($markUnsubscribed && $step->params['action'] === 'acy_send_email') { | |
| 213 | + $historyClass = new HistoryClass(); | |
| 214 | + $historyClass->insert($userId, 'unsubscribed', [acym_translation('ACYM_SCENARIO_USER_UNSUBSCRIBED')]); | |
| 215 | + | |
| 216 | + return; | |
| 217 | + } | |
| 218 | + | |
| 203 | 219 | $query = new AutomationHelper(); |
| 204 | 220 | $query->where = ['user.id = '.$userId]; |
| 205 | 221 | |
| 206 | 222 | $actionOptions = $this->formatOptions($step->params['option'], $step->type); |
| @@ -220,9 +236,9 @@ | ||
| 220 | 236 | |
| 221 | 237 | $nextStep = $this->scenarioStepClass->getStepByPreviousStepId($step->id); |
| 222 | 238 | |
| 223 | 239 | if (empty($nextStep)) { |
| 224 | - $this->ending($step->scenario_id, $userId, $scenarioProcessId); | |
| 240 | + $this->ending($scenarioProcessId); | |
| 225 | 241 | |
| 226 | 242 | return; |
| 227 | 243 | } |
| 228 | 244 | |
| @@ -228,9 +244,9 @@ | ||
| 228 | 244 | |
| 229 | 245 | $this->handleStep($nextStep, $userId, $scenarioProcessId); |
| 230 | 246 | } |
| 231 | 247 | |
| 232 | - private function ending(int $scenarioId, int $userId, int $scenarioProcessId) | |
| 248 | + private function ending(int $scenarioProcessId): void | |
| 233 | 249 | { |
| 234 | 250 | $this->createHistory($scenarioProcessId, 'ending', acym_translation('ACYM_SCENARIO_ENDED')); |
| 235 | 251 | |
| 236 | 252 | $this->scenarioProcessClass->endProcess($scenarioProcessId); |
| @@ -235,9 +251,9 @@ | ||
| 235 | 251 | |
| 236 | 252 | $this->scenarioProcessClass->endProcess($scenarioProcessId); |
| 237 | 253 | } |
| 238 | 254 | |
| 239 | - public function executeAvailableSteps() | |
| 255 | + public function executeAvailableSteps(): void | |
| 240 | 256 | { |
| 241 | 257 | $dateNowUtc = acym_date('now', 'Y-m-d H:i:s', false); |
| 242 | 258 | |
| 243 | 259 | $steps = $this->scenarioStepClass->getAvailableStepsByDate($dateNowUtc); |
| @@ -287,8 +303,9 @@ | ||
| 287 | 303 | |
| 288 | 304 | $scenario->is_scenario = true; |
| 289 | 305 | $scenario->triggers = [$scenario->trigger => $options]; |
| 290 | 306 | |
| 307 | + // Get the users matching the trigger | |
| 291 | 308 | acym_trigger('onAcymExecuteTrigger', [&$scenario, &$execute, &$options]); |
| 292 | 309 | |
| 293 | 310 | if ($nextExecution !== $scenario->next_execution) { |
| 294 | 311 | $scenario->trigger_params['next_execution'] = $scenario->next_execution; |
| @@ -299,9 +316,9 @@ | ||
| 299 | 316 | continue; |
| 300 | 317 | } |
| 301 | 318 | |
| 302 | 319 | foreach ($options['userIds'] as $userId) { |
| 303 | - if ($scenario->trigger_once && !empty($this->scenarioProcessClass->getOneByScenarioIdUserId($scenario->id, $userId))) { | |
| 320 | + if ($scenario->trigger_once && $this->scenarioProcessClass->hasUserProcess($scenario->id, $userId)) { | |
| 304 | 321 | continue; |
| 305 | 322 | } |
| 306 | 323 | |
| 307 | 324 | $scenarioProcess = new \stdClass(); |
| @@ -316,14 +333,15 @@ | ||
| 316 | 333 | } |
| 317 | 334 | } |
| 318 | 335 | } |
| 319 | 336 | |
| 320 | - private function createHistory(int $scenarioProcessId, string $type, string $result, string $scenarioStepId = '', array $params = [], string $log = '') | |
| 337 | + private function createHistory(int $scenarioProcessId, string $type, string $result, string $scenarioStepId = '', array $params = [], string $log = ''): void | |
| 321 | 338 | { |
| 322 | 339 | $scenarioHistoryLine = new \stdClass(); |
| 323 | 340 | $scenarioHistoryLine->scenario_process_id = $scenarioProcessId; |
| 324 | 341 | $scenarioHistoryLine->date = acym_date('now', 'Y-m-d H:i:s', false); |
| 325 | 342 | $scenarioHistoryLine->type = $type; |
| 343 | + //TODO maybe add something more specific | |
| 326 | 344 | $scenarioHistoryLine->result = $result; |
| 327 | 345 | |
| 328 | 346 | if (!empty($scenarioStepId)) { |
| 329 | 347 | $scenarioHistoryLine->scenario_step_id = $scenarioStepId; |
| @@ -339,14 +357,14 @@ | ||
| 339 | 357 | |
| 340 | 358 | $this->scenarioHistoryLineClass->save($scenarioHistoryLine); |
| 341 | 359 | } |
| 342 | 360 | |
| 343 | - public function getConditionValue(\stdClass $step): string | |
| 361 | + public function getConditionValue(object $step): string | |
| 344 | 362 | { |
| 345 | 363 | return $step->params['condition']; |
| 346 | 364 | } |
| 347 | 365 | |
| 348 | - public function getActionValue(\stdClass $step): string | |
| 366 | + public function getActionValue(object $step): string | |
| 349 | 367 | { |
| 350 | 368 | return $step->params['action']; |
| 351 | 369 | } |
| 352 | 370 | } |