| @@ -1,0 +1,108 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +use AcyMailing\Classes\FollowupClass; | |
| 4 | +use AcyMailing\Classes\SegmentClass; | |
| 5 | +use AcyMailing\Helpers\AutomationHelper; | |
| 6 | +use AcyMailing\Classes\AutomationClass; | |
| 7 | +use AcyMailing\Helpers\ScenarioHelper; | |
| 8 | + | |
| 9 | +trait SubscriberFollowup | |
| 10 | +{ | |
| 11 | + public function onAcymAfterUserCreate(&$user) | |
| 12 | + { | |
| 13 | + $automationClass = new AutomationClass(); | |
| 14 | + $automationClass->trigger('user_creation', ['userId' => $user->id]); | |
| 15 | + | |
| 16 | + $followupClass = new FollowupClass(); | |
| 17 | + $followupClass->addFollowupEmailsQueue(self::FOLLOWUP_USER_CREATION, $user->id); | |
| 18 | + | |
| 19 | + $scenarioHelper = new ScenarioHelper(); | |
| 20 | + $scenarioHelper->trigger('user_creation', ['userId' => $user->id]); | |
| 21 | + } | |
| 22 | + | |
| 23 | + public function matchFollowupsConditions(&$followups, $userId, $params) | |
| 24 | + { | |
| 25 | + $segmentClass = new SegmentClass(); | |
| 26 | + foreach ($followups as $key => $followup) { | |
| 27 | + if (empty($followup->condition['segments_status']) || empty($followup->condition['segments'])) continue; | |
| 28 | + | |
| 29 | + $segments = $segmentClass->getByIds($followup->condition['segments']); | |
| 30 | + if (empty($segments)) continue; | |
| 31 | + | |
| 32 | + $mustMatch = $followup->condition['segments_status'] === 'is'; | |
| 33 | + | |
| 34 | + if ($mustMatch) { | |
| 35 | + foreach ($segments as $segment) { | |
| 36 | + $segmentMatched = false; | |
| 37 | + foreach ($segment->filters as $orBlock) { | |
| 38 | + if ($this->isUserMatchingOr($userId, $orBlock)) { | |
| 39 | + $segmentMatched = true; | |
| 40 | + // No need to test the other OR blocks since one matched | |
| 41 | + break; | |
| 42 | + } | |
| 43 | + } | |
| 44 | + | |
| 45 | + // Must match all segments and the user didn't match a segment | |
| 46 | + if (!$segmentMatched) { | |
| 47 | + unset($followups[$key]); | |
| 48 | + | |
| 49 | + break; | |
| 50 | + } | |
| 51 | + } | |
| 52 | + } else { | |
| 53 | + foreach ($segments as $segment) { | |
| 54 | + foreach ($segment->filters as $orBlock) { | |
| 55 | + // Must not match the segments and the user matched at least one OR of a segment | |
| 56 | + if ($this->isUserMatchingOr($userId, $orBlock)) { | |
| 57 | + unset($followups[$key]); | |
| 58 | + | |
| 59 | + break 2; | |
| 60 | + } | |
| 61 | + } | |
| 62 | + } | |
| 63 | + } | |
| 64 | + } | |
| 65 | + } | |
| 66 | + | |
| 67 | + private function isUserMatchingOr($userId, $orBlock): bool | |
| 68 | + { | |
| 69 | + $automationHelper = new AutomationHelper(); | |
| 70 | + $automationHelper->where[] = 'user.id = '.intval($userId); | |
| 71 | + foreach ($orBlock as $and => $andValues) { | |
| 72 | + $and = intval($and); | |
| 73 | + foreach ($andValues as $filterName => $options) { | |
| 74 | + acym_trigger('onAcymProcessFilter_'.$filterName, [&$automationHelper, &$options, &$and]); | |
| 75 | + } | |
| 76 | + } | |
| 77 | + $userMatchingOr = acym_loadResult($automationHelper->getQuery(['user.id'])); | |
| 78 | + | |
| 79 | + return !empty($userMatchingOr); | |
| 80 | + } | |
| 81 | + | |
| 82 | + public function getFollowupTriggers(&$triggers) | |
| 83 | + { | |
| 84 | + $triggers[self::FOLLOWUP_USER_CREATION] = acym_translation('ACYM_SUBSCRIBER_CREATION'); | |
| 85 | + $triggers[self::FOLLOWUP_USER_CONFIRMATION] = acym_translation('ACYM_SUBSCRIPTION_CONFIRMATION'); | |
| 86 | + } | |
| 87 | + | |
| 88 | + public function getFollowupTriggerBlock(&$blocks) | |
| 89 | + { | |
| 90 | + $blocks[] = [ | |
| 91 | + 'name' => acym_translation('ACYM_SUBSCRIBER_CREATION'), | |
| 92 | + 'description' => acym_translation('ACYM_SUBSCRIBER_CREATION_DESC'), | |
| 93 | + 'icon' => 'acymicon-user-plus', | |
| 94 | + 'link' => acym_completeLink('campaigns&task=edit&step=followupCondition&trigger='.self::FOLLOWUP_USER_CREATION), | |
| 95 | + 'level' => 2, | |
| 96 | + 'alias' => self::FOLLOWUP_USER_CREATION, | |
| 97 | + ]; | |
| 98 | + | |
| 99 | + $blocks[] = [ | |
| 100 | + 'name' => acym_translation('ACYM_SUBSCRIPTION_CONFIRMATION'), | |
| 101 | + 'description' => acym_translation('ACYM_SUBSCRIPTION_CONFIRMATION_DESC'), | |
| 102 | + 'icon' => 'acymicon-user-check', | |
| 103 | + 'link' => acym_completeLink('campaigns&task=edit&step=followupCondition&trigger='.self::FOLLOWUP_USER_CONFIRMATION), | |
| 104 | + 'level' => 2, | |
| 105 | + 'alias' => self::FOLLOWUP_USER_CONFIRMATION, | |
| 106 | + ]; | |
| 107 | + } | |
| 108 | +} | |