| @@ -1,0 +1,222 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\Controllers\Campaigns; | |
| 4 | + | |
| 5 | +use AcyMailing\Classes\CampaignClass; | |
| 6 | +use AcyMailing\Classes\MailClass; | |
| 7 | +use AcyMailing\Classes\TagClass; | |
| 8 | +use AcyMailing\Helpers\PaginationHelper; | |
| 9 | +use AcyMailing\Helpers\ToolbarHelper; | |
| 10 | +use AcyMailing\Helpers\WorkflowHelper; | |
| 11 | + | |
| 12 | + | |
| 13 | +trait Listing | |
| 14 | +{ | |
| 15 | + public function listing(): void | |
| 16 | + { | |
| 17 | + $this->storeRedirectListing(true); | |
| 18 | + } | |
| 19 | + | |
| 20 | + public function storeRedirectListing(bool $fromListing = false): void | |
| 21 | + { | |
| 22 | + $isFrontJoomla = !acym_isAdmin() && ACYM_CMS === 'joomla'; | |
| 23 | + $variableName = $isFrontJoomla ? 'ctrl_stored_front' : 'ctrl_stored'; | |
| 24 | + | |
| 25 | + $currentTask = acym_getVar('string', 'task', ''); | |
| 26 | + if (!in_array($currentTask, self::TASKS_TO_REMEMBER_FOR_LISTING) && !$fromListing) { | |
| 27 | + return; | |
| 28 | + } | |
| 29 | + | |
| 30 | + $controller = acym_getVar('RAW', $variableName, null, 'SESSION'); | |
| 31 | + if ((empty($currentTask) || !in_array($currentTask, self::TASKS_TO_REMEMBER_FOR_LISTING)) && !empty($controller)) { | |
| 32 | + $taskToGo = is_array($controller) | |
| 33 | + ? $controller['task'].'&type='.$controller['type'] | |
| 34 | + : $controller; | |
| 35 | + $link = acym_completeLink(($isFrontJoomla ? 'front' : '').'campaigns&task='.$taskToGo, false, true); | |
| 36 | + acym_redirect($link); | |
| 37 | + } else { | |
| 38 | + if (empty($currentTask) || !in_array($currentTask, self::TASKS_TO_REMEMBER_FOR_LISTING)) { | |
| 39 | + $currentTask = self::TASK_TYPE_CAMPAIGN; | |
| 40 | + } | |
| 41 | + | |
| 42 | + if ($currentTask === self::TASK_TYPE_SPECIFIC_LISTING) { | |
| 43 | + $type = acym_getVar('string', 'type', ''); | |
| 44 | + $currentTask = empty($type) ? self::TASK_TYPE_CAMPAIGN : ['task' => $currentTask, 'type' => $type]; | |
| 45 | + } | |
| 46 | + | |
| 47 | + acym_setSession($variableName, $currentTask); | |
| 48 | + } | |
| 49 | + | |
| 50 | + $taskToCall = is_array($currentTask) ? $currentTask['task'] : $currentTask; | |
| 51 | + | |
| 52 | + if ($fromListing && method_exists($this, $taskToCall)) { | |
| 53 | + $this->$taskToCall(); | |
| 54 | + } | |
| 55 | + } | |
| 56 | + | |
| 57 | + public function setTaskListing(string $type): bool | |
| 58 | + { | |
| 59 | + $task = self::CAMPAIGN_TYPE_TO_TASK[$type] ?? self::TASK_TYPE_CAMPAIGN; | |
| 60 | + | |
| 61 | + if ($task === self::TASK_TYPE_SPECIFIC_LISTING) { | |
| 62 | + $task = [ | |
| 63 | + 'task' => self::TASK_TYPE_SPECIFIC_LISTING, | |
| 64 | + 'type' => $type, | |
| 65 | + ]; | |
| 66 | + } | |
| 67 | + | |
| 68 | + $isFrontJoomla = !acym_isAdmin() && ACYM_CMS === 'joomla'; | |
| 69 | + $variableName = $isFrontJoomla ? 'ctrl_stored_front' : 'ctrl_stored'; | |
| 70 | + acym_setSession($variableName, $task); | |
| 71 | + | |
| 72 | + return true; | |
| 73 | + } | |
| 74 | + | |
| 75 | + private function prepareListingClasses(array &$data): void | |
| 76 | + { | |
| 77 | + $data['workflowHelper'] = new WorkflowHelper(); | |
| 78 | + } | |
| 79 | + | |
| 80 | + public function specificListing(): void | |
| 81 | + { | |
| 82 | + acym_setVar('layout', 'specific_listing'); | |
| 83 | + | |
| 84 | + $type = acym_getVar('string', 'type'); | |
| 85 | + | |
| 86 | + $data = [ | |
| 87 | + 'type' => $type, | |
| 88 | + //We set campaign here to generate the statuses in the campaign class in the function getCountStatusFilter | |
| 89 | + 'campaign_type' => 'campaigns', | |
| 90 | + ]; | |
| 91 | + $this->getAllParamsRequest($data); | |
| 92 | + $this->prepareListingClasses($data); | |
| 93 | + $this->prepareToolbar($data); | |
| 94 | + | |
| 95 | + acym_trigger('onAcymCampaignDataSpecificListing', [&$data, $type]); | |
| 96 | + | |
| 97 | + parent::display($data); | |
| 98 | + } | |
| 99 | + | |
| 100 | + public function campaigns(): void | |
| 101 | + { | |
| 102 | + acym_setVar('layout', self::TASK_TYPE_CAMPAIGN); | |
| 103 | + | |
| 104 | + $data = [ | |
| 105 | + 'campaign_type' => 'campaigns', | |
| 106 | + 'element_to_display' => lcfirst(acym_translation('ACYM_CAMPAIGNS')), | |
| 107 | + ]; | |
| 108 | + $this->prepareAllCampaignsListing($data); | |
| 109 | + $this->prepareToolbar($data); | |
| 110 | + $this->prepareListingClasses($data); | |
| 111 | + | |
| 112 | + $data['menuClass'] = $this->menuClass; | |
| 113 | + | |
| 114 | + parent::display($data); | |
| 115 | + } | |
| 116 | + | |
| 117 | + public function prepareToolbar(array &$data): void | |
| 118 | + { | |
| 119 | + $toolbarHelper = new ToolbarHelper(); | |
| 120 | + $toolbarHelper->addSearchBar($data['search'], 'campaigns_search'); | |
| 121 | + $toolbarHelper->addButton(acym_translation('ACYM_CREATE'), ['data-task' => 'newEmail'], 'add', true); | |
| 122 | + if (acym_isAdmin() && (empty($data['campaign_type']) || $data['campaign_type'] !== MailClass::TYPE_FOLLOWUP)) { | |
| 123 | + $toolbarHelper->addFilterByTag($data, 'campaigns_tag', 'acym__campaigns__filter__tags acym__select'); | |
| 124 | + } | |
| 125 | + | |
| 126 | + $data['toolbar'] = $toolbarHelper; | |
| 127 | + } | |
| 128 | + | |
| 129 | + private function getAllParamsRequest(array &$data): void | |
| 130 | + { | |
| 131 | + $tagClass = new TagClass(); | |
| 132 | + $data['search'] = $this->getVarFiltersListing('string', 'campaigns_search', ''); | |
| 133 | + $data['tag'] = $this->getVarFiltersListing('string', 'campaigns_tag', ''); | |
| 134 | + $data['allTags'] = $tagClass->getAllTagsByType(TagClass::TYPE_MAIL); | |
| 135 | + $data['pagination'] = new PaginationHelper(); | |
| 136 | + $data['status'] = ''; | |
| 137 | + if (isset($data['campaign_type'])) { | |
| 138 | + $data['status'] = $this->getVarFiltersListing('string', $data['campaign_type'].'_status', ''); | |
| 139 | + $data['ordering'] = $this->getVarFiltersListing('cmd', $data['campaign_type'].'_ordering', 'id'); | |
| 140 | + $data['orderingSortOrder'] = $this->getVarFiltersListing('cmd', $data['campaign_type'].'_ordering_sort_order', 'desc'); | |
| 141 | + } elseif (isset($data['email_type'])) { | |
| 142 | + $data['ordering'] = $this->getVarFiltersListing('cmd', $data['email_type'].'_ordering', 'id'); | |
| 143 | + $data['orderingSortOrder'] = $this->getVarFiltersListing('cmd', $data['email_type'].'_ordering_sort_order', 'desc'); | |
| 144 | + } | |
| 145 | + | |
| 146 | + if (!empty($data['tag'])) { | |
| 147 | + $data['status_toolbar'] = [ | |
| 148 | + 'campaigns_tag' => $data['tag'], | |
| 149 | + ]; | |
| 150 | + } | |
| 151 | + } | |
| 152 | + | |
| 153 | + private function prepareAllCampaignsListing(array &$data): void | |
| 154 | + { | |
| 155 | + $this->getAllParamsRequest($data); | |
| 156 | + $this->prepareEmailsListing($data, $data['campaign_type']); | |
| 157 | + if ($data['campaign_type'] === self::TASK_TYPE_CAMPAIGN_AUTO) { | |
| 158 | + $this->getAutoCampaignsFrequency($data); | |
| 159 | + $this->getIsPendingGenerated($data); | |
| 160 | + } | |
| 161 | + } | |
| 162 | + | |
| 163 | + public function prepareEmailsListing(array &$data, string $campaignType = '', string $class = ''): void | |
| 164 | + { | |
| 165 | + // Prepare the pagination | |
| 166 | + $campaignsPerPage = $data['pagination']->getListLimit(); | |
| 167 | + $page = $this->getVarFiltersListing('int', 'campaigns_pagination_page', 1); | |
| 168 | + $status = $data['status']; | |
| 169 | + | |
| 170 | + // Get the matching campaigns | |
| 171 | + $matchingCampaigns = $this->getMatchingElementsFromData( | |
| 172 | + [ | |
| 173 | + 'element_tab' => $campaignType, | |
| 174 | + 'ordering' => $data['ordering'], | |
| 175 | + 'search' => $data['search'], | |
| 176 | + 'elementsPerPage' => $campaignsPerPage, | |
| 177 | + 'offset' => ($page - 1) * $campaignsPerPage, | |
| 178 | + 'tag' => $data['tag'], | |
| 179 | + 'ordering_sort_order' => $data['orderingSortOrder'], | |
| 180 | + 'status' => $data['status'], | |
| 181 | + 'creator_id' => acym_isAdmin() ? 0 : acym_currentUserId(), | |
| 182 | + 'campaign_type' => $data['campaign_type'] ?? '', | |
| 183 | + 'advanced_total' => empty($class), | |
| 184 | + ], | |
| 185 | + $status, | |
| 186 | + $page, | |
| 187 | + empty($class) ? '' : $class | |
| 188 | + ); | |
| 189 | + | |
| 190 | + if (empty($class)) { | |
| 191 | + foreach ($matchingCampaigns['elements'] as $key => $campaign) { | |
| 192 | + $matchingCampaigns['elements'][$key]->scheduled = CampaignClass::SENDING_TYPE_SCHEDULED === $campaign->sending_type; | |
| 193 | + } | |
| 194 | + | |
| 195 | + $data['allStatusFilter'] = $matchingCampaigns['total']; | |
| 196 | + $totalElement = empty($status) ? $data['allStatusFilter']->all : $data['allStatusFilter']->$status; | |
| 197 | + $data['statusAuto'] = CampaignClass::SENDING_TYPE_AUTO; | |
| 198 | + } else { | |
| 199 | + $totalElement = $matchingCampaigns['total']->total; | |
| 200 | + } | |
| 201 | + | |
| 202 | + $data['pagination']->setStatus((int)$totalElement, $page, $campaignsPerPage); | |
| 203 | + $data['allCampaigns'] = $matchingCampaigns['elements']; | |
| 204 | + } | |
| 205 | + | |
| 206 | + public function mailbox_action(): void | |
| 207 | + { | |
| 208 | + acym_setVar('layout', self::TASK_TYPE_MAILBOX_ACTION); | |
| 209 | + | |
| 210 | + $data = [ | |
| 211 | + 'campaign_type' => 'campaigns', | |
| 212 | + 'element_to_display' => lcfirst(acym_translation('ACYM_MAILBOX_ACTION_CAMPAIGN')), | |
| 213 | + ]; | |
| 214 | + $this->prepareAllCampaignsListing($data); | |
| 215 | + $this->prepareToolbar($data); | |
| 216 | + $this->prepareListingClasses($data); | |
| 217 | + | |
| 218 | + $data['menuClass'] = $this->menuClass; | |
| 219 | + | |
| 220 | + parent::display($data); | |
| 221 | + } | |
| 222 | +} | |