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