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 / Core / AcymController.php

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

403 lines 13.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AcyMailing\Core;
4
5 use AcyMailing\Helpers\HeaderHelper;
6
7 abstract class AcymController extends AcymObject
8 {
9 private $currentClass = null;
10 private string $sessionName;
11
12 protected array $breadcrumb = [];
13 protected array $loadScripts = [];
14 protected array $publicFrontTasks = [];
15 protected array $allowedTasks = [];
16 protected array $menuAlias = [];
17 protected string $taskCalled = '';
18 protected string $menuClass = '';
19
20 public string $defaulttask = 'listing';
21 public string $name = '';
22
23 public function __construct()
24 {
25 parent::__construct();
26
27 $classname = get_class($this);
28 $classname = substr($classname, strrpos($classname, '\\') + 1);
29 $ctrlpos = strpos($classname, 'Controller');
30 $this->name = strtolower(substr($classname, 0, $ctrlpos));
31
32 $currentClassName = 'AcyMailing\\Classes\\'.rtrim(ucfirst(str_replace(['Front', 'front'], '', $this->name)), 's').'Class';
33 if (class_exists($currentClassName)) {
34 $this->currentClass = new $currentClassName;
35 }
36 $this->sessionName = 'acym_filters_'.$this->name;
37 $this->taskCalled = acym_getVar('string', 'task', '');
38
39 $this->breadcrumb['AcyMailing'] = acym_completeLink('dashboard');
40 }
41
42 private function initSession()
43 {
44 acym_session();
45 if (empty($_SESSION[$this->sessionName])) {
46 $_SESSION[$this->sessionName] = [];
47 }
48 }
49
50 public function getVarFiltersListing($type, $varName, $default, $overrideIfNull = false)
51 {
52 if ($this->taskCalled == 'clearFilters') return $default;
53
54 $this->initSession();
55 $returnValue = acym_getVar($type, $varName);
56
57 if (is_null($returnValue) && $overrideIfNull) $returnValue = $default;
58
59 if (!is_null($returnValue)) {
60 $_SESSION[$this->sessionName][$varName] = $returnValue;
61
62 return $returnValue;
63 }
64
65 if (!empty($_SESSION[$this->sessionName][$varName])) {
66 return $_SESSION[$this->sessionName][$varName];
67 }
68
69 return $default;
70 }
71
72 public function setVarFiltersListing($varName, $value)
73 {
74 acym_setVar($varName, $value);
75 $this->initSession();
76 $_SESSION[$this->sessionName][$varName] = $value;
77 }
78
79 public function clearFilters()
80 {
81 $this->initSession();
82 $_SESSION[$this->sessionName] = [];
83
84 $taskToCall = acym_getVar('string', 'cleartask', $this->defaulttask);
85 $this->call($taskToCall);
86 }
87
88 public function call($task)
89 {
90 if (!in_array($task, ['countResultsTotal', 'countGlobalBySegmentId', 'countResults']) && strpos($task, 'Ajax') === false && !acym_isAllowed($this->name, $task)) {
91 acym_enqueueMessage(acym_translation('ACYM_ACCESS_DENIED'), 'warning');
92 acym_redirect(acym_completeLink('dashboard'));
93
94 return;
95 }
96
97 if (!method_exists($this, $task)) {
98 acym_enqueueMessage(acym_translation('ACYM_NON_EXISTING_PAGE'), 'warning');
99 $task = $this->defaulttask;
100 acym_setVar('task', $task);
101 }
102
103 $this->$task();
104 }
105
106 public function loadScripts($task)
107 {
108 if (empty($this->loadScripts)) return;
109
110 $scripts = [];
111 if (!empty($this->loadScripts['all'])) {
112 $scripts = $this->loadScripts['all'];
113 }
114
115 if (!empty($task) && !empty($this->loadScripts[$task])) {
116 $scripts = array_merge($scripts, $this->loadScripts[$task]);
117 }
118
119 if (empty($scripts)) return;
120
121 if (in_array('editor-wysid', $scripts)) {
122 acym_addStyle(false, ACYM_CSS.'editorWYSID.min.css?v='.filemtime(ACYM_MEDIA.'css'.DS.'editorWYSID.min.css'));
123 acym_addScript(false, ACYM_JS.'editor_wysid_utils.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'editor_wysid_utils.min.js'));
124
125 $scripts = array_merge($scripts, ['colorpicker', 'datepicker', 'thumbnail', 'foundation-email', 'parse-css', 'vue-prism-editor', 'masonry']);
126
127 if (empty($scripts['vue-applications'])) {
128 $scripts['vue-applications'] = ['code_editor'];
129 } else {
130 $scripts['vue-applications'][] = 'code_editor';
131 }
132 }
133
134 if (in_array('sankey', $scripts)) {
135 acym_addScript(false, ACYM_JS.'libraries/sankey.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'sankey.min.js'));
136 }
137
138 if (in_array('colorpicker', $scripts)) {
139 acym_addScript(false, ACYM_JS.'libraries/spectrum.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'spectrum.min.js'));
140 acym_addStyle(false, ACYM_CSS.'libraries/spectrum.min.css?v='.filemtime(ACYM_MEDIA.'css'.DS.'libraries'.DS.'spectrum.min.css'));
141 }
142
143 if (in_array('datepicker', $scripts)) {
144 acym_addScript(false, ACYM_JS.'libraries/moment.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'moment.min.js'));
145 acym_addScript(false, ACYM_JS.'libraries/rome.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'rome.min.js'));
146 acym_addScript(false, ACYM_JS.'libraries/material-datetime-picker.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'material-datetime-picker.min.js'));
147 acym_addStyle(false, ACYM_CSS.'libraries/material-datetime-picker.min.css?v='.filemtime(ACYM_MEDIA.'css'.DS.'libraries'.DS.'material-datetime-picker.min.css'));
148 }
149
150 if (in_array('dtextPicker', $scripts)) {
151 acym_addScript(false, ACYM_JS.'dtext_picker.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'dtext_picker.min.js'));
152 acym_addStyle(false, ACYM_CSS.'dtext_picker.min.css?v='.filemtime(ACYM_MEDIA.'css'.DS.'dtext_picker.min.css'));
153 }
154
155 if (in_array('thumbnail', $scripts)) {
156 acym_addScript(false, ACYM_JS.'libraries/html2canvas.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'html2canvas.min.js'));
157 }
158
159 if (in_array('foundation-email', $scripts)) {
160 acym_addStyle(false, ACYM_CSS.'libraries/foundation_email.min.css?v='.filemtime(ACYM_MEDIA.'css'.DS.'libraries'.DS.'foundation_email.min.css'));
161 acym_addStyle(true, acym_getEmailCssFixes());
162 }
163
164 if (in_array('parse-css', $scripts)) {
165 acym_addScript(false, ACYM_JS.'libraries/parse-css.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'parse-css.min.js'));
166 }
167
168 if (in_array('masonry', $scripts)) {
169 acym_addScript(false, ACYM_JS.'libraries/masonry.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'masonry.min.js'));
170 }
171
172 if (!empty($scripts['vue-applications'])) {
173 acym_addScript(false, ACYM_JS.'libraries/vuejs.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'vuejs.min.js'));
174 acym_addScript(false, ACYM_JS.'libraries/vue-infinite-scroll.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'vue-infinite-scroll.min.js'));
175 acym_addScript(false, ACYM_JS.'vue/vue_components.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'vue'.DS.'vue_components.min.js'));
176 foreach ($scripts['vue-applications'] as $script) {
177 acym_addScript(false, ACYM_JS.'vue/'.$script.'.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'vue'.DS.$script.'.min.js'));
178 }
179 }
180
181 if (in_array('vue-prism-editor', $scripts)) {
182 acym_addScript(false, ACYM_JS.'libraries/prism-editor.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'prism-editor.min.js'));
183 acym_addScript(false, ACYM_JS.'libraries/prism.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'prism.min.js'));
184 acym_addStyle(false, ACYM_CSS.'libraries/prism.min.css?v='.filemtime(ACYM_MEDIA.'css'.DS.'libraries'.DS.'prism.min.css'));
185 acym_addScript(false, ACYM_JS.'libraries/beautify-html.min.js?v='.filemtime(ACYM_MEDIA.'js'.DS.'libraries'.DS.'beautify-html.min.js'));
186 }
187 }
188
189 public function setDefaultTask(string $task): void
190 {
191 $this->defaulttask = $task;
192 }
193
194 public function getName(): string
195 {
196 return $this->name;
197 }
198
199 public function display($data = [])
200 {
201 if (acym_isAdmin()) {
202 if (!acym_isNoTemplate()) {
203 $header = new HeaderHelper();
204 $data['header'] = $header->display($this->breadcrumb);
205 }
206 $viewNamespace = 'AcyMailing\\Views\\';
207 } else {
208 $viewNamespace = 'AcyMailing\\FrontViews\\';
209 }
210
211 $viewName = ucfirst($this->getName());
212 $viewNamespace .= $viewName.'\\'.$viewName.'View';
213 $view = new $viewNamespace;
214 $view->display($this, $data);
215 }
216
217 public function cancel()
218 {
219 acym_setVar('layout', 'listing');
220 $this->display();
221 }
222
223 public function listing()
224 {
225 acym_setVar('layout', 'listing');
226
227 $this->display();
228 }
229
230 public function edit()
231 {
232 $nextstep = acym_getVar('string', 'nextstep', '');
233 $step = acym_getVar('string', 'step', '');
234 if (empty($nextstep)) {
235 $nextstep = $step;
236 }
237
238 if (empty($nextstep)) {
239 acym_setVar('layout', 'edit');
240
241 $this->display();
242 } else {
243 acym_setVar('step', $nextstep);
244
245 return $this->$nextstep();
246 }
247 }
248
249 public function apply()
250 {
251 $this->store();
252
253 return $this->edit();
254 }
255
256 public function add()
257 {
258 acym_setVar('cid', []);
259 acym_setVar('layout', 'form');
260
261 return $this->display();
262 }
263
264 public function save()
265 {
266 $step = acym_getVar('string', 'step', '');
267
268 if (!empty($step)) {
269 $saveMethod = 'save'.ucfirst($step);
270 if (!method_exists($this, $saveMethod)) {
271 die('Save method '.acym_escape($saveMethod).' not found');
272 }
273
274 return $this->$saveMethod();
275 }
276
277 if (method_exists($this, 'store')) $this->store();
278
279 return $this->listing();
280 }
281
282 public function delete()
283 {
284 acym_checkToken();
285 $ids = acym_getVar('array', 'elements_checked', []);
286 $allChecked = acym_getVar('string', 'checkbox_all');
287 $currentPage = explode('_', acym_getVar('string', 'page', ''));
288 $pageNumber = $this->getVarFiltersListing('int', end($currentPage).'_pagination_page', 1);
289
290 if (!empty($ids) && !empty($this->currentClass)) {
291 $this->currentClass->delete($ids);
292 if ($allChecked == 'on') {
293 $this->setVarFiltersListing(end($currentPage).'_pagination_page', $pageNumber - 1);
294 }
295 }
296
297 if (!acym_getVar('bool', 'no_listing', false)) $this->listing();
298 }
299
300 public function setActive()
301 {
302 acym_checkToken();
303 $ids = acym_getVar('array', 'elements_checked', []);
304
305 if (!empty($ids) && !empty($this->currentClass)) {
306 $this->currentClass->setActive($ids);
307 }
308
309 $this->listing();
310 }
311
312 public function setInactive()
313 {
314 acym_checkToken();
315 $ids = acym_getVar('array', 'elements_checked', []);
316
317 if (!empty($ids) && !empty($this->currentClass)) {
318 $this->currentClass->setInactive($ids);
319 }
320
321 $this->listing();
322 }
323
324 public function getMatchingElementsFromData($requestData, &$status, &$page, $class = '')
325 {
326 $className = 'AcyMailing\\Classes\\'.ucfirst(strtolower($class)).'Class';
327 $classElement = empty($class) ? $this->currentClass : new $className();
328 $matchingElement = $classElement->getMatchingElements($requestData);
329
330 if (empty($matchingElement['elements'])) {
331 if (!empty($status) && empty($requestData['search']) && empty($requestData['tag'])) {
332 $status = '';
333 $requestData['status'] = $status;
334 } elseif (!empty($requestData['offset'])) {
335 $page = 1;
336 $requestData['offset'] = 0;
337 } else {
338 return $matchingElement;
339 }
340
341 $matchingElement = $classElement->getMatchingElements($requestData);
342 }
343
344 return $matchingElement;
345 }
346
347 public function checkTaskFront(string $task = ''): void
348 {
349 if ($this->getName() === 'cron') {
350 $task = 'cron';
351 }
352
353 if (empty($task) && !empty($this->defaulttask)) {
354 $task = $this->defaulttask;
355 }
356
357 if (in_array($task, $this->publicFrontTasks)) {
358 $this->$task();
359
360 return;
361 }
362
363 if (empty(acym_currentUserId()) || ACYM_CMS !== 'joomla' || !acym_level(ACYM_ENTERPRISE)) {
364 acym_redirect(acym_rootURI(), 'Front-end management not available');
365 }
366
367 $currentMenu = acym_getMenu();
368 if (empty($currentMenu)) {
369 acym_redirect(acym_rootURI(), 'Direct access denied');
370 }
371
372 if ($this->isTaskAllowed($currentMenu->link, $task)) {
373 $this->$task();
374
375 return;
376 }
377
378 acym_redirect(acym_rootURI(), 'Unknown route, access denied');
379 }
380
381 private function isTaskAllowed(string $menuUrl, string $task): bool
382 {
383 if (!empty($this->allowedTasks[$menuUrl]) && in_array($task, $this->allowedTasks[$menuUrl])) {
384 return true;
385 }
386
387 if (!empty($this->menuAlias[$menuUrl])) {
388 $menuUrl = $this->menuAlias[$menuUrl];
389
390 return $this->isTaskAllowed($menuUrl, $task);
391 }
392
393 return false;
394 }
395
396 protected function prepareMultilingualOption(&$data)
397 {
398 if (!acym_isMultilingual()) return;
399
400 $data['translation_languages'] = acym_getMultilingualLanguages();
401 }
402 }
403