PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.1.4
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.1.4
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.1.4, at back/Classes/AutomationClass.php

290 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 return [
59 'elements' => acym_loadObjectList($query, '', $settings['offset'], $settings['elementsPerPage']),
60 'total' => acym_loadObject($queryCount),
61 ];
62 }
63
64 public function save($automation)
65 {
66 foreach ($automation as $oneAttribute => $value) {
67 if (empty($value)) {
68 continue;
69 }
70
71 if (is_array($value)) {
72 $value = json_encode($value);
73 }
74
75 $automation->$oneAttribute = strip_tags($value);
76 }
77
78 return parent::save($automation);
79 }
80
81 public function delete($elements)
82 {
83 if (empty($elements)) return 0;
84
85 if (!is_array($elements)) $elements = [$elements];
86 acym_arrayToInteger($elements);
87
88 $steps = acym_loadResultArray('SELECT id FROM #__acym_step WHERE automation_id IN ('.implode(',', $elements).')');
89 $stepClass = new StepClass();
90 $stepsDeleted = $stepClass->delete($steps);
91
92 return parent::delete($elements);
93 }
94
95 public function trigger($trigger, $data = [])
96 {
97 if (!acym_level(ACYM_ENTERPRISE) || empty($trigger)) {
98 return;
99 }
100
101 $stepClass = new StepClass();
102 $actionClass = new ActionClass();
103 $conditionClass = new ConditionClass();
104 $steps = $stepClass->getActiveStepByTrigger($trigger);
105
106 $data['time'] = time();
107 foreach ($steps as $step) {
108 $newData = $data;
109 $execute = false;
110
111 if (!empty($step->next_execution) && $step->next_execution <= $newData['time']) {
112 $execute = true;
113 }
114
115 acym_trigger('onAcymExecuteTrigger', [&$step, &$execute, &$newData]);
116
117 $newData['automation'] = $this->getOneById($step->automation_id);
118
119 if ($execute) {
120 $step->last_execution = $newData['time'];
121 $conditions = $conditionClass->getConditionsByStepId($step->id);
122 if (!empty($conditions)) {
123 foreach ($conditions as $condition) {
124 if (!$this->_verifyCondition($condition->conditions, $newData)) continue;
125
126 $actions = $actionClass->getActionsByStepId($step->id);
127 if (empty($actions)) continue;
128
129 foreach ($actions as $action) {
130 $this->execute($action, $newData);
131 }
132 }
133 }
134 }
135
136 $stepClass->save($step);
137 }
138 }
139
140 public function execute($action, $data = [])
141 {
142 $usersTriggeringAction = empty($data['userIds']) ? [] : $data['userIds'];
143 $userTriggeringAction = empty($data['userId']) ? 0 : $data['userId'];
144 $action->actions = json_decode($action->actions, true);
145 if (empty($action->actions)) return false;
146
147 $isMassAction = false;
148 static $massAction = 0;
149 if (empty($action->id)) {
150 $action->id = $massAction--;
151 $isMassAction = true;
152 }
153
154 $action->filters = json_decode($action->filters, true);
155 if (empty($action->filters)) return false;
156
157
158 $initialWhere = ['1 = 1'];
159 $query = new AutomationHelper();
160 $query->removeFlag($action->id);
161
162 if (!empty($action->filters['type_filter']) && $action->filters['type_filter'] == 'user') {
163 if (empty($usersTriggeringAction)) {
164 if (empty($userTriggeringAction)) return false;
165
166 $initialWhere = ['user.id = '.intval($userTriggeringAction)];
167 } else {
168 acym_arrayToInteger($usersTriggeringAction);
169 $initialWhere = ['user.id IN ('.implode(', ', $usersTriggeringAction).')'];
170 }
171 }
172
173 $typeFilter = $action->filters['type_filter'];
174
175 unset($action->filters['type_filter']);
176 if (empty($action->filters)) {
177 $query->where = $initialWhere;
178 }
179
180 foreach ($action->filters as $or => $orValue) {
181 if (empty($orValue)) {
182 continue;
183 }
184 $num = 0;
185 $query->where = $initialWhere;
186 foreach ($orValue as $and => $andValue) {
187 $num++;
188 foreach ($andValue as $filterName => $filterOptions) {
189 acym_trigger('onAcymProcessFilter_'.$filterName, [&$query, &$filterOptions, &$num]);
190 }
191 }
192
193 $query->addFlag($action->id);
194 }
195
196 $this->didAnAction = $this->didAnAction || $query->count() > 0;
197 foreach ($action->actions as $and => $andValue) {
198 foreach ($andValue as $actionName => $actionOptions) {
199 $this->report = array_merge(
200 $this->report,
201 acym_trigger(
202 'onAcymProcessAction_'.$actionName,
203 [&$query, &$actionOptions, ['automationAdmin' => !empty($data['automation']->admin), 'user_id' => $userTriggeringAction]]
204 )
205 );
206 $action->actions[$and][$actionName] = $actionOptions;
207 }
208 }
209
210 if (!$isMassAction) {
211 $action->filters['type_filter'] = $typeFilter;
212 $action->filters = json_encode($action->filters);
213 $action->actions = json_encode($action->actions);
214 $actionClass = new ActionClass();
215 $actionClass->save($action);
216 }
217
218 $query->removeFlag($action->id);
219
220 return $this->didAnAction;
221 }
222
223 private function _verifyCondition($conditions, $data = [])
224 {
225 if (empty($conditions)) return true;
226 $userTriggeringAction = empty($data['userId']) ? 0 : $data['userId'];
227 $usersTriggeringAction = empty($data['userIds']) ? [] : $data['userIds'];
228
229 $conditions = json_decode($conditions, true);
230 $query = new AutomationHelper();
231 $initialWhere = ['1 = 1'];
232 if (!empty($conditions['type_condition']) && $conditions['type_condition'] == 'user') {
233 if (empty($usersTriggeringAction)) {
234 if (empty($userTriggeringAction)) return false;
235
236 $initialWhere = ['user.id = '.intval($userTriggeringAction)];
237 } else {
238 acym_arrayToInteger($usersTriggeringAction);
239 $initialWhere = ['user.id IN ('.implode(', ', $usersTriggeringAction).')'];
240 }
241 }
242 unset($conditions['type_condition']);
243
244 if (empty($conditions)) return true;
245
246 foreach ($conditions as $or => $orValue) {
247 if (empty($orValue)) continue;
248
249 $conditionNotValid = 0;
250 $num = 0;
251 foreach ($orValue as $and => $andValue) {
252 $num++;
253 $query->where = $initialWhere;
254 foreach ($andValue as $filterName => $filterOptions) {
255 acym_trigger('onAcymProcessCondition_'.$filterName, [&$query, &$filterOptions, &$num, &$conditionNotValid]);
256 }
257 }
258
259 if ($conditionNotValid == 0) return true;
260 }
261
262 return false;
263 }
264
265 public function getAutomationsAdmin($ids = [])
266 {
267 acym_arrayToInteger($ids);
268
269 $query = 'SELECT * FROM #__acym_automation WHERE `admin` = 1';
270 if (!empty($ids)) {
271 $query .= ' AND `id` IN ('.implode(', ', $ids).')';
272 }
273
274 return acym_loadObjectList($query, 'name');
275 }
276
277 public function getActionsByAutomationId($id)
278 {
279 return acym_loadObjectList(
280 'SELECT action.*
281 FROM `#__acym_action` AS `action`
282 JOIN `#__acym_condition` AS `condition`
283 ON `action`.`condition_id` = `condition`.`id`
284 JOIN `#__acym_step` AS `step`
285 ON `condition`.`step_id` = `step`.`id`
286 WHERE `step`.`automation_id` = '.intval($id)
287 );
288 }
289 }
290