| 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(): void |
| 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(string $task): void |
| 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(array $data = []): void |
| 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(): void |
| 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 |
$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 |
$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 |
$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)) { |
| 298 |
$this->listing(); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
public function setActive() |
| 303 |
{ |
| 304 |
acym_checkToken(); |
| 305 |
$ids = acym_getVar('array', 'elements_checked', []); |
| 306 |
|
| 307 |
if (!empty($ids) && !empty($this->currentClass)) { |
| 308 |
$this->currentClass->setActive($ids); |
| 309 |
} |
| 310 |
|
| 311 |
$this->listing(); |
| 312 |
} |
| 313 |
|
| 314 |
public function setInactive() |
| 315 |
{ |
| 316 |
acym_checkToken(); |
| 317 |
$ids = acym_getVar('array', 'elements_checked', []); |
| 318 |
|
| 319 |
if (!empty($ids) && !empty($this->currentClass)) { |
| 320 |
$this->currentClass->setInactive($ids); |
| 321 |
} |
| 322 |
|
| 323 |
$this->listing(); |
| 324 |
} |
| 325 |
|
| 326 |
public function getMatchingElementsFromData($requestData, &$status, &$page, $class = '') |
| 327 |
{ |
| 328 |
$className = 'AcyMailing\\Classes\\'.ucfirst(strtolower($class)).'Class'; |
| 329 |
$classElement = empty($class) ? $this->currentClass : new $className(); |
| 330 |
$matchingElement = $classElement->getMatchingElements($requestData); |
| 331 |
|
| 332 |
if (empty($matchingElement['elements'])) { |
| 333 |
if (!empty($status) && empty($requestData['search']) && empty($requestData['tag'])) { |
| 334 |
$status = ''; |
| 335 |
$requestData['status'] = $status; |
| 336 |
} elseif (!empty($requestData['offset'])) { |
| 337 |
$page = 1; |
| 338 |
$requestData['offset'] = 0; |
| 339 |
} else { |
| 340 |
return $matchingElement; |
| 341 |
} |
| 342 |
|
| 343 |
$matchingElement = $classElement->getMatchingElements($requestData); |
| 344 |
} |
| 345 |
|
| 346 |
$matchingElement['total']->total = intval($matchingElement['total']->total); |
| 347 |
|
| 348 |
return $matchingElement; |
| 349 |
} |
| 350 |
|
| 351 |
public function checkTaskFront(string $task = ''): void |
| 352 |
{ |
| 353 |
if ($this->getName() === 'cron') { |
| 354 |
$task = 'cron'; |
| 355 |
} |
| 356 |
|
| 357 |
if (empty($task) && !empty($this->defaulttask)) { |
| 358 |
$task = $this->defaulttask; |
| 359 |
} |
| 360 |
|
| 361 |
if (in_array($task, $this->publicFrontTasks)) { |
| 362 |
$this->$task(); |
| 363 |
|
| 364 |
return; |
| 365 |
} |
| 366 |
|
| 367 |
if (empty(acym_currentUserId()) || ACYM_CMS !== 'joomla' || !acym_level(ACYM_ENTERPRISE)) { |
| 368 |
acym_redirect(acym_rootURI(), 'Front-end management not available'); |
| 369 |
} |
| 370 |
|
| 371 |
$currentMenu = acym_getMenu(); |
| 372 |
if (empty($currentMenu)) { |
| 373 |
acym_redirect(acym_rootURI(), 'Direct access denied'); |
| 374 |
} |
| 375 |
|
| 376 |
if ($this->isTaskAllowed($currentMenu->link, $task)) { |
| 377 |
$this->$task(); |
| 378 |
|
| 379 |
return; |
| 380 |
} |
| 381 |
|
| 382 |
acym_redirect(acym_rootURI(), 'Unknown route, access denied'); |
| 383 |
} |
| 384 |
|
| 385 |
private function isTaskAllowed(string $menuUrl, string $task): bool |
| 386 |
{ |
| 387 |
if (!empty($this->allowedTasks[$menuUrl]) && in_array($task, $this->allowedTasks[$menuUrl])) { |
| 388 |
return true; |
| 389 |
} |
| 390 |
|
| 391 |
if (!empty($this->menuAlias[$menuUrl])) { |
| 392 |
$menuUrl = $this->menuAlias[$menuUrl]; |
| 393 |
|
| 394 |
return $this->isTaskAllowed($menuUrl, $task); |
| 395 |
} |
| 396 |
|
| 397 |
return false; |
| 398 |
} |
| 399 |
|
| 400 |
protected function prepareMultilingualOption(&$data) |
| 401 |
{ |
| 402 |
if (!acym_isMultilingual()) return; |
| 403 |
|
| 404 |
$data['translation_languages'] = acym_getMultilingualLanguages(); |
| 405 |
} |
| 406 |
} |
| 407 |
|