PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 11.0.5
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v11.0.5
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 / ScenarioStepClass.php

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

157 lines 5.0 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\Core\AcymClass;
6
7 class ScenarioStepClass extends AcymClass
8 {
9 public function __construct()
10 {
11 parent::__construct();
12
13 $this->table = 'scenario_step';
14 $this->pkey = 'id';
15 }
16
17 public function save(object $element): ?int
18 {
19 if (!empty($element->params) && !is_string($element->params)) {
20 $element->params = json_encode($element->params);
21 }
22
23 // The id is a string
24 $scenarioStep = acym_loadObject('SELECT * FROM #__acym_scenario_step WHERE `id` = '.acym_escapeDB($element->id));
25
26 $previousForce = $this->forceInsert;
27 $this->forceInsert = empty($scenarioStep);
28 $id = parent::save($element);
29 $this->forceInsert = $previousForce;
30
31 return (int)$id;
32 }
33
34 public function getAllByScenarioId(int $scenarioId): array
35 {
36 $steps = array_map([$this, 'formatStep'], acym_loadObjectList('SELECT * FROM #__acym_scenario_step WHERE scenario_id = '.$scenarioId, 'id'));
37
38 usort($steps, function ($a, $b) {
39 if ($a->previous_id === $b->id) {
40 return 1;
41 }
42
43 if ($b->previous_id === $a->id) {
44 return -1;
45 }
46
47 return 0;
48 });
49
50
51 return $steps;
52 }
53
54 public function getFirstStepByScenarioId(int $scenarioId): ?object
55 {
56 $step = acym_loadObject('SELECT * FROM #__acym_scenario_step WHERE previous_id IS NULL AND scenario_id = '.$scenarioId);
57 if (empty($step)) {
58 return null;
59 }
60
61 return $this->formatStep($step);
62 }
63
64 public function getStepByPreviousStepId(string $parentId): ?object
65 {
66 $step = acym_loadObject('SELECT * FROM #__acym_scenario_step WHERE previous_id = '.acym_escapeDB($parentId));
67 if (empty($step)) {
68 return null;
69 }
70
71 return $this->formatStep($step);
72 }
73
74 public function getAllStepsToDelete(int $scenarioId, array $stepIdsToKeep): array
75 {
76 $stepIdsToKeepEscaped = [];
77 foreach ($stepIdsToKeep as $stepIdToKeep) {
78 $stepIdsToKeepEscaped[] = acym_escapeDB($stepIdToKeep);
79 }
80
81 $query = 'SELECT id FROM #__acym_scenario_step WHERE scenario_id = '.$scenarioId;
82
83 if (!empty($stepIdsToKeepEscaped)) {
84 $query .= ' AND id NOT IN ('.implode(',', $stepIdsToKeepEscaped).')';
85 }
86
87 return acym_loadResultArray($query);
88 }
89
90 public function getStepByPreviousConditionId(string $parentId, bool $conditionValid): ?object
91 {
92 $conditionValidValue = $conditionValid ? 1 : 0;
93
94 $step = acym_loadObject('SELECT * FROM #__acym_scenario_step WHERE condition_valid = '.$conditionValidValue.' AND previous_id = '.acym_escapeDB($parentId));
95 if (empty($step)) {
96 return null;
97 }
98
99 return $this->formatStep($step);
100 }
101
102 public function getAvailableStepsByDate(string $date): array
103 {
104 $query = 'SELECT scenario_step.*, scenario_process.user_id, scenario_process.id AS scenario_process_id
105 FROM #__acym_scenario_step AS scenario_step
106 JOIN #__acym_scenario_queue AS scenario_queue ON scenario_step.id = scenario_queue.step_id AND scenario_queue.execution_date <= '.acym_escapeDB($date).'
107 JOIN #__acym_scenario_process AS scenario_process ON scenario_queue.scenario_process_id = scenario_process.id';
108
109 return array_map([$this, 'formatStep'], acym_loadObjectList($query));
110 }
111
112 public function getNumberOfProcessByStepByScenarioId(int $scenarioId): array
113 {
114 $query = 'SELECT scenario_history.scenario_step_id , COUNT(scenario_history.id) AS count
115 FROM #__acym_scenario_step AS scenario_step
116 JOIN #__acym_scenario_history_line AS scenario_history ON scenario_step.id = scenario_history.scenario_step_id AND scenario_history.scenario_step_id IS NOT NULL
117 WHERE scenario_step.scenario_id = '.$scenarioId.'
118 GROUP BY scenario_step_id';
119
120 return acym_loadObjectList($query, 'scenario_step_id');
121 }
122
123 public function getAllStepIds(): array
124 {
125 return acym_loadResultArray('SELECT id FROM #__acym_scenario_step');
126 }
127
128 public function generateNewStepId(array $allStepIds = []): string
129 {
130 if (empty($allStepIds)) {
131 $allStepIds = $this->getAllStepIds();
132 }
133
134 $letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
135 $randomString = '';
136 $length = 24;
137
138 for ($i = 0; $i < $length; $i++) {
139 $randomIndex = acym_rand(0, strlen($letters) - 1);
140 $randomString .= $letters[$randomIndex];
141 }
142
143 if (in_array($randomString, $allStepIds)) {
144 return $this->generateNewStepId($allStepIds);
145 }
146
147 return $randomString;
148 }
149
150 private function formatStep(object $step): object
151 {
152 $step->params = json_decode($step->params, true);
153
154 return $step;
155 }
156 }
157