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

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

307 lines 8.7 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()
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(&$data)
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(&$data)
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(&$node, $stepId, $mailId)
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(&$data)
98 {
99 $this->searchForSendEmailAction($data['flow']);
100 }
101
102 private function searchForSendEmailAction(&$node)
103 {
104 if (!empty($node['params']['action']) && $node['params']['action'] === 'acy_send_email') {
105 $mailId = !empty($node['params']['option']['acym_action[actions][__and__][acy_send_email][mail_id]'])
106 ? $node['params']['option']['acym_action[actions][__and__][acy_send_email][mail_id]'] : 0;
107
108 if (!empty($mailId)) {
109 $mail = $this->mailClass->getOneById($mailId);
110
111 if (!empty($mail)) {
112 $node['params']['option']['mail'] = $mail;
113 }
114 }
115 }
116
117 if (!empty($node['children'])) {
118 foreach ($node['children'] as &$childNode) {
119 $this->searchForSendEmailAction($childNode);
120 }
121 }
122 }
123
124 private function prepareScenario(&$data)
125 {
126 $scenarioId = acym_getVar('int', 'scenarioId', 0);
127
128 $scenarioClass = new ScenarioClass();
129 $scenario = new \stdClass();
130 $scenario->name = '';
131 $scenario->active = 0;
132 $scenario->trigger_once = 0;
133 if (!empty($scenarioId)) {
134 $scenario = $scenarioClass->getOneById($scenarioId);
135 }
136 $data['scenario'] = $scenario;
137 }
138
139 private function prepareFlow(&$data)
140 {
141 if (empty($data['scenario']->id)) {
142 return;
143 }
144
145 $flow = [
146 'condition' => false,
147 'params' => $data['scenario']->trigger_params,
148 'children' => [],
149 ];
150
151 $scenarioStepClass = new ScenarioStepClass();
152 $steps = $scenarioStepClass->getAllByScenarioId($data['scenario']->id);
153
154 $nodeReferences = [];
155
156 foreach ($steps as $item) {
157 $nodeReferences[$item->id] = [
158 'slug' => $item->id,
159 'params' => $item->params,
160 'condition' => $item->type == ScenarioClass::TYPE_CONDITION,
161 'children' => [],
162 'conditionValid' => $item->condition_valid,
163 ];
164 }
165
166 foreach ($steps as $item) {
167 if (empty($item->previous_id)) {
168 $flow['children'][] = &$nodeReferences[$item->id];
169 } else {
170 $key = 0;
171 if (!is_null($item->condition_valid)) {
172 $key = empty($item->condition_valid) ? 1 : 0;
173 }
174 $nodeReferences[$item->previous_id]['children'][$key] = &$nodeReferences[$item->id];
175 }
176 }
177
178 $data['flow'] = $flow;
179 }
180
181 private function prepareTriggers(&$data)
182 {
183 $defaultValues = [];
184 $triggers = ['classic' => [], 'user' => []];
185 acym_trigger('onAcymDeclareTriggersScenario', [&$triggers, &$defaultValues]);
186
187 $triggersFormatted = [];
188 foreach ($triggers['user'] as $key => $trigger) {
189 $trigger->key = $key;
190 $trigger->name = strip_tags($trigger->name);
191 $triggersFormatted[] = $trigger;
192 }
193
194 $data['triggers'] = $triggersFormatted;
195 }
196
197 private function prepareConditions(&$data)
198 {
199 $conditions = ['user' => [], 'classic' => []];
200 acym_trigger('onAcymDeclareConditionsScenario', [&$conditions]);
201
202 $conditionsFormatted = [];
203 foreach ($conditions['user'] as $key => $condition) {
204 $condition->key = $key;
205 $condition->name = strip_tags($condition->name);
206 $conditionsFormatted[] = $condition;
207 }
208
209 $data['conditions'] = $conditionsFormatted;
210 }
211
212 private function prepareActions(&$data)
213 {
214 $actions = [];
215 acym_trigger('onAcymDeclareActionsScenario', [&$actions]);
216
217 $actionsFormatted = [];
218 foreach ($actions as $key => $action) {
219 $action->key = $key;
220 $action->name = strip_tags($action->name);
221 $actionsFormatted[] = $action;
222 }
223
224 $data['actions'] = $actionsFormatted;
225 }
226
227 private function saveInner(): int
228 {
229 $id = acym_getVar('int', 'scenarioId', 0);
230 $scenario = acym_getVar('array', 'scenario', []);
231
232 if (empty($scenario)) {
233 return 0;
234 }
235
236 $scenario = (object)$scenario;
237
238 if (empty($scenario->name)) {
239 $scenario->name = acym_translation('ACYM_NEW_SCENARIO');
240 }
241
242 if (!empty($id)) {
243 $scenario->id = $id;
244 }
245
246 $scenarioClass = new ScenarioClass();
247
248 return $scenarioClass->save($scenario);
249 }
250
251 public function saveExit()
252 {
253 $this->saveInner();
254
255 $this->listing();
256 }
257
258 public function save()
259 {
260 $scenarioId = $this->saveInner();
261
262 acym_setVar('scenarioId', $scenarioId);
263
264 $this->performances();
265 }
266
267 public function createMail()
268 {
269 $options = acym_getVar('array', 'send_mail', []);
270
271 if (empty($options['step_id'])) {
272 return;
273 }
274
275 $scenarioId = $this->saveInner();
276
277 if (empty($scenarioId)) {
278 return;
279 }
280
281 $type = MailClass::TYPE_SCENARIO;
282
283 $urlParams = [
284 'task' => 'edit',
285 'step' => 'editEmail',
286 'type' => $type,
287 'return' => acym_completeLink('scenarios&task=edit&step=editScenario&mailId={mailid}&scenarioId='.$scenarioId.'&stepId='.$options['step_id']),
288 ];
289
290 if (!empty($options['mail_id'])) {
291 $urlParams['id'] = $options['mail_id'];
292 }
293
294 $redirectLink = acym_completeLink('mails&'.http_build_query($urlParams), false, true);
295
296 acym_redirect($redirectLink);
297 }
298
299 public function prepareStepIds(array &$data): void
300 {
301 $scenarioStepClass = new ScenarioStepClass();
302
303 $allScenarioStepIds = $scenarioStepClass->getAllStepIds();
304 $data['allScenarioStepIds'] = $allScenarioStepIds ?? [];
305 }
306 }
307