PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.0.1
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.0.1
11.1.1 11.1.0 11.0.5 11.0.4 11.0.3 11.0.2 11.0.1 11.0.0 10.11.1 10.11.0 10.10.2 10.10.1 10.10.0 10.9.1 trunk 10.0.0 10.0.1 10.1.0 10.1.1 10.1.2 10.1.3 10.1.4 10.2.0 10.2.1 10.2.2 All 62 releases
acymailing / back / Classes / AutomationClass.php

AutomationClass.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 10.0.1, at back/Classes/AutomationClass.php

291 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AcyMailing\Classes;
4
5 use AcyMailing\Helpers\AutomationHelper;
6 use AcyMailing\Helpers\PaginationHelper;
7 use AcyMailing\Core\AcymClass;
8
9 class AutomationClass extends AcymClass
10 {
11 const ACTIONS_TO_SKIP = ['acy_send_email'];
12 var $didAnAction = false;
13 var $report = [];
14
15 public function __construct()
16 {
17 parent::__construct();
18
19 $this->table = 'automation';
20 $this->pkey = 'id';
21 }
22
23 public function getMatchingElements(array $settings = []): array
24 {
25 $query = 'SELECT * FROM #__acym_automation';
26 $queryCount = 'SELECT COUNT(id) AS total, SUM(active) AS totalActive FROM #__acym_automation';
27 $filters = [];
28
29 if (!empty($settings['search'])) {
30 $filters[] = 'name LIKE '.acym_escapeDB('%'.$settings['search'].'%');
31 }
32
33 if (!empty($filters)) {
34 $query .= ' WHERE ('.implode(') AND (', $filters).')';
35 $queryCount .= ' WHERE ('.implode(') AND (', $filters).')';
36 }
37
38 if (!empty($settings['status'])) {
39 $query .= empty($filters) ? ' WHERE ' : ' AND ';
40 $query .= 'active = '.($settings['status'] == 'active' ? '1' : '0');
41 }
42
43 if (!empty($settings['ordering']) && !empty($settings['ordering_sort_order'])) {
44 $query .= ' ORDER BY '.acym_secureDBColumn($settings['ordering']).' '.acym_secureDBColumn(strtoupper($settings['ordering_sort_order']));
45 } else {
46 $query .= ' ORDER BY id asc';
47 }
48
49 if (empty($settings['offset']) || $settings['offset'] < 0) {
50 $settings['offset'] = 0;
51 }
52
53 if (empty($settings['elementsPerPage']) || $settings['elementsPerPage'] < 1) {
54 $pagination = new PaginationHelper();
55 $settings['elementsPerPage'] = $pagination->getListLimit();
56 }
57
58 $results['elements'] = acym_loadObjectList($query, '', $settings['offset'], $settings['elementsPerPage']);
59
60 $results['total'] = acym_loadObject($queryCount);
61
62 return $results;
63 }
64
65 public function save($automation)
66 {
67 foreach ($automation as $oneAttribute => $value) {
68 if (empty($value)) {
69 continue;
70 }
71
72 if (is_array($value)) {
73 $value = json_encode($value);
74 }
75
76 $automation->$oneAttribute = strip_tags($value);
77 }
78
79 return parent::save($automation);
80 }
81
82 public function delete($elements)
83 {
84 if (empty($elements)) return 0;
85
86 if (!is_array($elements)) $elements = [$elements];
87 acym_arrayToInteger($elements);
88
89 $steps = acym_loadResultArray('SELECT id FROM #__acym_step WHERE automation_id IN ('.implode(',', $elements).')');
90 $stepClass = new StepClass();
91 $stepsDeleted = $stepClass->delete($steps);
92
93 return parent::delete($elements);
94 }
95
96 public function trigger($trigger, $data = [])
97 {
98 if (!acym_level(ACYM_ENTERPRISE) || empty($trigger)) {
99 return;
100 }
101
102 $stepClass = new StepClass();
103 $actionClass = new ActionClass();
104 $conditionClass = new ConditionClass();
105 $steps = $stepClass->getActiveStepByTrigger($trigger);
106
107 $data['time'] = time();
108 foreach ($steps as $step) {
109 $newData = $data;
110 $execute = false;
111
112 if (!empty($step->next_execution) && $step->next_execution <= $newData['time']) {
113 $execute = true;
114 }
115
116 acym_trigger('onAcymExecuteTrigger', [&$step, &$execute, &$newData]);
117
118 $newData['automation'] = $this->getOneById($step->automation_id);
119
120 if ($execute) {
121 $step->last_execution = $newData['time'];
122 $conditions = $conditionClass->getConditionsByStepId($step->id);
123 if (!empty($conditions)) {
124 foreach ($conditions as $condition) {
125 if (!$this->_verifyCondition($condition->conditions, $newData)) continue;
126
127 $actions = $actionClass->getActionsByStepId($step->id);
128 if (empty($actions)) continue;
129
130 foreach ($actions as $action) {
131 $this->execute($action, $newData);
132 }
133 }
134 }
135 }
136
137 $stepClass->save($step);
138 }
139 }
140
141 public function execute($action, $data = [])
142 {
143 $usersTriggeringAction = empty($data['userIds']) ? [] : $data['userIds'];
144 $userTriggeringAction = empty($data['userId']) ? 0 : $data['userId'];
145 $action->actions = json_decode($action->actions, true);
146 if (empty($action->actions)) return false;
147
148 $isMassAction = false;
149 static $massAction = 0;
150 if (empty($action->id)) {
151 $action->id = $massAction--;
152 $isMassAction = true;
153 }
154
155 $action->filters = json_decode($action->filters, true);
156 if (empty($action->filters)) return false;
157
158
159 $initialWhere = ['1 = 1'];
160 $query = new AutomationHelper();
161 $query->removeFlag($action->id);
162
163 if (!empty($action->filters['type_filter']) && $action->filters['type_filter'] == 'user') {
164 if (empty($usersTriggeringAction)) {
165 if (empty($userTriggeringAction)) return false;
166
167 $initialWhere = ['user.id = '.intval($userTriggeringAction)];
168 } else {
169 acym_arrayToInteger($usersTriggeringAction);
170 $initialWhere = ['user.id IN ('.implode(', ', $usersTriggeringAction).')'];
171 }
172 }
173
174 $typeFilter = $action->filters['type_filter'];
175
176 unset($action->filters['type_filter']);
177 if (empty($action->filters)) {
178 $query->where = $initialWhere;
179 }
180
181 foreach ($action->filters as $or => $orValue) {
182 if (empty($orValue)) {
183 continue;
184 }
185 $num = 0;
186 $query->where = $initialWhere;
187 foreach ($orValue as $and => $andValue) {
188 $num++;
189 foreach ($andValue as $filterName => $filterOptions) {
190 acym_trigger('onAcymProcessFilter_'.$filterName, [&$query, &$filterOptions, &$num]);
191 }
192 }
193
194 $query->addFlag($action->id);
195 }
196
197 $this->didAnAction = $this->didAnAction || $query->count() > 0;
198 foreach ($action->actions as $and => $andValue) {
199 foreach ($andValue as $actionName => $actionOptions) {
200 $this->report = array_merge(
201 $this->report,
202 acym_trigger(
203 'onAcymProcessAction_'.$actionName,
204 [&$query, &$actionOptions, ['automationAdmin' => !empty($data['automation']->admin), 'user_id' => $userTriggeringAction]]
205 )
206 );
207 $action->actions[$and][$actionName] = $actionOptions;
208 }
209 }
210
211 if (!$isMassAction) {
212 $action->filters['type_filter'] = $typeFilter;
213 $action->filters = json_encode($action->filters);
214 $action->actions = json_encode($action->actions);
215 $actionClass = new ActionClass();
216 $actionClass->save($action);
217 }
218
219 $query->removeFlag($action->id);
220
221 return $this->didAnAction;
222 }
223
224 private function _verifyCondition($conditions, $data = [])
225 {
226 if (empty($conditions)) return true;
227 $userTriggeringAction = empty($data['userId']) ? 0 : $data['userId'];
228 $usersTriggeringAction = empty($data['userIds']) ? [] : $data['userIds'];
229
230 $conditions = json_decode($conditions, true);
231 $query = new AutomationHelper();
232 $initialWhere = ['1 = 1'];
233 if (!empty($conditions['type_condition']) && $conditions['type_condition'] == 'user') {
234 if (empty($usersTriggeringAction)) {
235 if (empty($userTriggeringAction)) return false;
236
237 $initialWhere = ['user.id = '.intval($userTriggeringAction)];
238 } else {
239 acym_arrayToInteger($usersTriggeringAction);
240 $initialWhere = ['user.id IN ('.implode(', ', $usersTriggeringAction).')'];
241 }
242 }
243 unset($conditions['type_condition']);
244
245 if (empty($conditions)) return true;
246
247 foreach ($conditions as $or => $orValue) {
248 if (empty($orValue)) continue;
249
250 $conditionNotValid = 0;
251 $num = 0;
252 foreach ($orValue as $and => $andValue) {
253 $num++;
254 $query->where = $initialWhere;
255 foreach ($andValue as $filterName => $filterOptions) {
256 acym_trigger('onAcymProcessCondition_'.$filterName, [&$query, &$filterOptions, &$num, &$conditionNotValid]);
257 }
258 }
259
260 if ($conditionNotValid == 0) return true;
261 }
262
263 return false;
264 }
265
266 public function getAutomationsAdmin($ids = [])
267 {
268 acym_arrayToInteger($ids);
269
270 $query = 'SELECT * FROM #__acym_automation WHERE `admin` = 1';
271 if (!empty($ids)) {
272 $query .= ' AND `id` IN ('.implode(', ', $ids).')';
273 }
274
275 return acym_loadObjectList($query, 'name');
276 }
277
278 public function getActionsByAutomationId($id)
279 {
280 return acym_loadObjectList(
281 'SELECT action.*
282 FROM `#__acym_action` AS `action`
283 JOIN `#__acym_condition` AS `condition`
284 ON `action`.`condition_id` = `condition`.`id`
285 JOIN `#__acym_step` AS `step`
286 ON `condition`.`step_id` = `step`.`id`
287 WHERE `step`.`automation_id` = '.intval($id)
288 );
289 }
290 }
291