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 / Controllers / Scenarios / Edition.php

Edition.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 10.1.4, at back/Controllers/Scenarios/Edition.php

306 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AcyMailing\Controllers\Scenarios;
4
5 use AcyMailing\Classes\MailClass;
6 use AcyMailing\Classes\ScenarioClass;
7 use AcyMailing\Classes\ScenarioStepClass;
8 use AcyMailing\Helpers\WorkflowHelper;
9
10 trait Edition
11 {
12 public function editScenario(): void
13 {
14 acym_setVar('layout', 'edit_scenario');
15 acym_setVar('step', 'editScenario');
16
17 $data = [
18 'workflowHelper' => new WorkflowHelper(),
19 ];
20
21 $this->prepareScenario($data);
22 $this->prepareFlow($data);
23 $this->prepareTriggers($data);
24 $this->prepareConditions($data);
25 $this->prepareActions($data);
26 $this->prepareReturnFromMailCreation($data);
27 $this->prepareSendEmailAction($data);
28 $this->prepareStepIds($data);
29 $this->prepareTemplateModal($data);
30
31 if (!empty($data['scenario']->id)) {
32 $this->breadcrumb[$data['scenario']->name] = acym_completeLink('scenarios&task=edit&step=editScenario&scenarioId='.$data['scenario']->id);
33 } else {
34 $this->breadcrumb[acym_translation('ACYM_NEW_SCENARIO')] = acym_completeLink('scenarios&task=edit&step=editScenario');
35 }
36
37 parent::display($data);
38 }
39
40 private function prepareTemplateModal(array &$data): void
41 {
42 $data['defaultTemplates'] = [
43 [
44 'name' => acym_translation('ACYM_ONBOARDING'),
45 'image' => 'onboarding.png',
46 ],
47 [
48 'name' => acym_translation('ACYM_ONBOARDING'),
49 'image' => 'onboarding.png',
50 ],
51 [
52 'name' => acym_translation('ACYM_ONBOARDING'),
53 'image' => 'onboarding.png',
54 ],
55 [
56 'name' => acym_translation('ACYM_ONBOARDING'),
57 'image' => 'onboarding.png',
58 ],
59 ];
60
61 ob_start();
62 include acym_getPartial('scenarios', 'modal_template');
63 $data['modalTemplate'] = ob_get_clean();
64 }
65
66 private function prepareReturnFromMailCreation(array &$data): void
67 {
68 $mailId = acym_getVar('int', 'mailId', 0);
69 $stepId = acym_getVar('string', 'stepId', '');
70
71 if (empty($mailId) || empty($stepId)) {
72 return;
73 }
74
75 $data['returnFromMailCreationStepId'] = $stepId;
76
77 $this->changeMailIdInFlowByStepId($data['flow'], $stepId, $mailId);
78 }
79
80 private function changeMailIdInFlowByStepId(array &$node, string $stepId, int $mailId): void
81 {
82 if (!empty($node['slug']) && $node['slug'] === $stepId) {
83 $node['params']['option'] = [
84 'acym_action[actions][__and__][acy_send_email][mail_id]' => $mailId,
85 ];
86
87 return;
88 }
89
90 if (!empty($node['children'])) {
91 foreach ($node['children'] as &$childNode) {
92 $this->changeMailIdInFlowByStepId($childNode, $stepId, $mailId);
93 }
94 }
95 }
96
97 private function prepareSendEmailAction(array &$data): void
98 {
99 if (!empty($data['flow'])) {
100 $this->searchForSendEmailAction($data['flow']);
101 }
102 }
103
104 private function searchForSendEmailAction(array &$node): void
105 {
106 if (!empty($node['params']['action']) && $node['params']['action'] === 'acy_send_email') {
107 $mailId = !empty($node['params']['option']['acym_action[actions][__and__][acy_send_email][mail_id]'])
108 ? $node['params']['option']['acym_action[actions][__and__][acy_send_email][mail_id]'] : 0;
109
110 if (!empty($mailId)) {
111 $mail = $this->mailClass->getOneById($mailId);
112
113 if (!empty($mail)) {
114 $node['params']['option']['mail'] = $mail;
115 }
116 }
117 }
118
119 if (!empty($node['children'])) {
120 foreach ($node['children'] as &$childNode) {
121 $this->searchForSendEmailAction($childNode);
122 }
123 }
124 }
125
126 private function prepareScenario(array &$data): void
127 {
128 $scenarioId = acym_getVar('int', 'scenarioId', 0);
129
130 $scenarioClass = new ScenarioClass();
131 $scenario = new \stdClass();
132 $scenario->name = '';
133 $scenario->active = 0;
134 $scenario->trigger_once = 0;
135 if (!empty($scenarioId)) {
136 $scenario = $scenarioClass->getOneById($scenarioId);
137 }
138 $data['scenario'] = $scenario;
139 }
140
141 private function prepareFlow(array &$data): void
142 {
143 if (empty($data['scenario']->id)) {
144 return;
145 }
146
147 $flow = [
148 'condition' => false,
149 'params' => $data['scenario']->trigger_params,
150 'children' => [],
151 ];
152
153 $scenarioStepClass = new ScenarioStepClass();
154 $steps = $scenarioStepClass->getAllByScenarioId($data['scenario']->id);
155
156 $nodeReferences = [];
157
158 foreach ($steps as $item) {
159 $nodeReferences[$item->id] = [
160 'slug' => $item->id,
161 'params' => $item->params,
162 'condition' => $item->type == ScenarioClass::TYPE_CONDITION,
163 'children' => [],
164 'conditionValid' => $item->condition_valid,
165 ];
166 }
167
168 foreach ($steps as $item) {
169 if (empty($item->previous_id)) {
170 $flow['children'][] = &$nodeReferences[$item->id];
171 } else {
172 $key = 0;
173 if (!is_null($item->condition_valid)) {
174 $key = empty($item->condition_valid) ? 1 : 0;
175 }
176 $nodeReferences[$item->previous_id]['children'][$key] = &$nodeReferences[$item->id];
177 }
178 }
179
180 $data['flow'] = $flow;
181 }
182
183 private function prepareTriggers(array &$data): void
184 {
185 $defaultValues = [];
186 $triggers = ['classic' => [], 'user' => []];
187 acym_trigger('onAcymDeclareTriggersScenario', [&$triggers, &$defaultValues]);
188
189 $triggersFormatted = [];
190 foreach ($triggers['user'] as $key => $trigger) {
191 $trigger->key = $key;
192 $trigger->name = strip_tags($trigger->name);
193 $triggersFormatted[] = $trigger;
194 }
195
196 $data['triggers'] = $triggersFormatted;
197 }
198
199 private function prepareConditions(array &$data): void
200 {
201 $conditions = ['user' => [], 'classic' => []];
202 acym_trigger('onAcymDeclareConditionsScenario', [&$conditions]);
203
204 $conditionsFormatted = [];
205 foreach ($conditions['user'] as $key => $condition) {
206 $condition->key = $key;
207 $condition->name = strip_tags($condition->name);
208 $conditionsFormatted[] = $condition;
209 }
210
211 $data['conditions'] = $conditionsFormatted;
212 }
213
214 private function prepareActions(array &$data): void
215 {
216 $actions = [];
217 acym_trigger('onAcymDeclareActionsScenario', [&$actions]);
218
219 $actionsFormatted = [];
220 foreach ($actions as $key => $action) {
221 $action->key = $key;
222 $action->name = strip_tags($action->name);
223 $actionsFormatted[] = $action;
224 }
225
226 $data['actions'] = $actionsFormatted;
227 }
228
229 private function saveInner(): int
230 {
231 $id = acym_getVar('int', 'scenarioId', 0);
232 $scenario = acym_getVar('array', 'scenario', []);
233
234 if (empty($scenario)) {
235 return 0;
236 }
237
238 $scenario = (object)$scenario;
239
240 if (empty($scenario->name)) {
241 $scenario->name = acym_translation('ACYM_NEW_SCENARIO');
242 }
243
244 if (!empty($id)) {
245 $scenario->id = $id;
246 }
247
248 $scenarioClass = new ScenarioClass();
249
250 return $scenarioClass->save($scenario);
251 }
252
253 public function saveExit(): void
254 {
255 $this->saveInner();
256 $this->listing();
257 }
258
259 public function save(): void
260 {
261 $scenarioId = $this->saveInner();
262
263 acym_setVar('scenarioId', $scenarioId);
264
265 $this->performances();
266 }
267
268 public function createMail(): void
269 {
270 $options = acym_getVar('array', 'send_mail', []);
271
272 if (empty($options['step_id'])) {
273 return;
274 }
275
276 $scenarioId = $this->saveInner();
277
278 if (empty($scenarioId)) {
279 return;
280 }
281
282 $type = MailClass::TYPE_SCENARIO;
283
284 $urlParams = [
285 'task' => 'edit',
286 'step' => 'editEmail',
287 'type' => $type,
288 'return' => acym_completeLink('scenarios&task=edit&step=editScenario&mailId={mailid}&scenarioId='.$scenarioId.'&stepId='.$options['step_id']),
289 ];
290
291 if (!empty($options['mail_id'])) {
292 $urlParams['id'] = $options['mail_id'];
293 }
294
295 $redirectLink = acym_completeLink('mails&'.http_build_query($urlParams), false, true);
296
297 acym_redirect($redirectLink);
298 }
299
300 public function prepareStepIds(array &$data): void
301 {
302 $scenarioStepClass = new ScenarioStepClass();
303 $data['allScenarioStepIds'] = $scenarioStepClass->getAllStepIds();
304 }
305 }
306