| @@ -1,111 +1,0 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace AcyMailing\Controllers\Scenarios; | |
| 4 | - | |
| 5 | -use AcyMailing\Classes\ScenarioClass; | |
| 6 | -use AcyMailing\Helpers\PaginationHelper; | |
| 7 | -use AcyMailing\Helpers\ToolbarHelper; | |
| 8 | - | |
| 9 | -trait Listing | |
| 10 | -{ | |
| 11 | - public function listing(): void | |
| 12 | - { | |
| 13 | - if (!acym_level(ACYM_ENTERPRISE)) { | |
| 14 | - acym_redirect(acym_completeLink('dashboard&task=upgrade&version=enterprise', false, true)); | |
| 15 | - } | |
| 16 | - $ordering = $this->getVarFiltersListing('cmd', 'scenario_ordering', 'ordering'); | |
| 17 | - $orderingSortOrder = $this->getVarFiltersListing('cmd', 'scenario_ordering_sort_order', 'asc'); | |
| 18 | - | |
| 19 | - acym_setVar('layout', 'listing'); | |
| 20 | - | |
| 21 | - $data = [ | |
| 22 | - 'ordering' => $ordering, | |
| 23 | - 'ordering_sort_order' => $orderingSortOrder, | |
| 24 | - 'pagination' => new PaginationHelper(), | |
| 25 | - ]; | |
| 26 | - | |
| 27 | - $this->prepareListingFilters($data); | |
| 28 | - $this->prepareListingElements($data); | |
| 29 | - $this->prepareToolbar($data); | |
| 30 | - | |
| 31 | - parent::display($data); | |
| 32 | - } | |
| 33 | - | |
| 34 | - private function prepareListingFilters(array &$data): void | |
| 35 | - { | |
| 36 | - $data['search'] = $this->getVarFiltersListing('string', 'scenario_search', ''); | |
| 37 | - $data['status'] = $this->getVarFiltersListing('string', 'scenarios_status', ''); | |
| 38 | - $data['ordering'] = $this->getVarFiltersListing('cmd', 'scenarios_ordering', 'id'); | |
| 39 | - $data['ordering_sort_order'] = $this->getVarFiltersListing('cmd', 'scenarios_ordering_sort_order', 'asc'); | |
| 40 | - $data['page'] = $this->getVarFiltersListing('int', 'scenarios_pagination_page', 1); | |
| 41 | - } | |
| 42 | - | |
| 43 | - private function prepareListingElements(array &$data): void | |
| 44 | - { | |
| 45 | - // Prepare the pagination | |
| 46 | - $scenarioPerPage = $data['pagination']->getListLimit(); | |
| 47 | - | |
| 48 | - $matchingScenarios = $this->getMatchingElementsFromData( | |
| 49 | - [ | |
| 50 | - 'search' => $data['search'], | |
| 51 | - 'elementsPerPage' => $scenarioPerPage, | |
| 52 | - 'offset' => ($data['page'] - 1) * $scenarioPerPage, | |
| 53 | - 'status' => $data['status'], | |
| 54 | - 'ordering' => $data['ordering'], | |
| 55 | - 'ordering_sort_order' => $data['ordering_sort_order'], | |
| 56 | - ], | |
| 57 | - $data['status'], | |
| 58 | - $data['page'] | |
| 59 | - ); | |
| 60 | - | |
| 61 | - $data['pagination']->setStatus($matchingScenarios['total']->total, $data['page'], $scenarioPerPage); | |
| 62 | - | |
| 63 | - $data['scenarios'] = $matchingScenarios['elements']; | |
| 64 | - $data['totalOverall'] = $matchingScenarios['totalOverall']; | |
| 65 | - $data['scenariosNumberStatus'] = $matchingScenarios['scenariosNumberStatus']; | |
| 66 | - } | |
| 67 | - | |
| 68 | - protected function prepareToolbar(array &$data): void | |
| 69 | - { | |
| 70 | - $data['toolbar'] = new ToolbarHelper(); | |
| 71 | - $data['toolbar']->addSearchBar($data['search'], 'scenario_search'); | |
| 72 | - | |
| 73 | - $data['toolbar']->addButton(acym_translation('ACYM_CREATE'), ['data-task' => 'edit', 'data-step' => 'editScenario'], 'add', true); | |
| 74 | - } | |
| 75 | - | |
| 76 | - public function duplicate(): void | |
| 77 | - { | |
| 78 | - wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 79 | - | |
| 80 | - $scenarioIds = acym_getVar('array', 'elements_checked', []); | |
| 81 | - | |
| 82 | - acym_arrayToInteger($scenarioIds); | |
| 83 | - | |
| 84 | - if (empty($scenarioIds)) { | |
| 85 | - $this->listing(); | |
| 86 | - } | |
| 87 | - | |
| 88 | - $scenarioClass = new ScenarioClass(); | |
| 89 | - | |
| 90 | - foreach ($scenarioIds as $scenarioId) { | |
| 91 | - $scenarioClass->duplicate($scenarioId); | |
| 92 | - } | |
| 93 | - | |
| 94 | - $this->listing(); | |
| 95 | - } | |
| 96 | - | |
| 97 | - public function ajaxSetOrdering(): void | |
| 98 | - { | |
| 99 | - wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 100 | - | |
| 101 | - $order = json_decode(acym_getVar('string', 'order', '[]'), true); | |
| 102 | - | |
| 103 | - $error = false; | |
| 104 | - foreach ($order as $index => $scenarioId) { | |
| 105 | - $query = 'UPDATE #__acym_scenario SET `ordering` = '.intval($index + 1).' WHERE `id` = '.intval($scenarioId); | |
| 106 | - $error = acym_query($query) < 0 || $error; | |
| 107 | - } | |
| 108 | - | |
| 109 | - acym_sendAjaxResponse('', [], !$error); | |
| 110 | - } | |
| 111 | -} | |