| @@ -1,281 +1,0 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace AcyMailing\Controllers\Mails; | |
| 4 | - | |
| 5 | -use AcyMailing\Classes\MailClass; | |
| 6 | -use AcyMailing\Classes\TagClass; | |
| 7 | -use AcyMailing\Helpers\ExportHelper; | |
| 8 | -use AcyMailing\Helpers\PaginationHelper; | |
| 9 | -use AcyMailing\Helpers\ToolbarHelper; | |
| 10 | -use AcyMailing\Helpers\UpdateHelper; | |
| 11 | - | |
| 12 | -trait Listing | |
| 13 | -{ | |
| 14 | - public function listing(): void | |
| 15 | - { | |
| 16 | - acym_setVar('layout', 'listing'); | |
| 17 | - | |
| 18 | - // Get filters data | |
| 19 | - $searchFilter = $this->getVarFiltersListing('string', 'mails_search', ''); | |
| 20 | - $tagFilter = $this->getVarFiltersListing('string', 'mails_tag', ''); | |
| 21 | - $ordering = $this->getVarFiltersListing('cmd', 'mails_ordering', 'creation_date'); | |
| 22 | - $orderingSortOrder = $this->getVarFiltersListing('cmd', 'mails_ordering_sort_order', 'desc'); | |
| 23 | - | |
| 24 | - $pagination = new PaginationHelper(); | |
| 25 | - // Get pagination data | |
| 26 | - $mailsPerPage = $pagination->getListLimit(); | |
| 27 | - $page = $this->getVarFiltersListing('int', 'mails_pagination_page', 1); | |
| 28 | - $mailClass = new MailClass(); | |
| 29 | - $status = MailClass::TYPE_STANDARD; | |
| 30 | - | |
| 31 | - $requestData = [ | |
| 32 | - 'ordering' => $ordering, | |
| 33 | - 'search' => $searchFilter, | |
| 34 | - 'elementsPerPage' => $mailsPerPage, | |
| 35 | - 'offset' => ($page - 1) * $mailsPerPage, | |
| 36 | - 'tag' => $tagFilter, | |
| 37 | - 'status' => $status, | |
| 38 | - 'ordering_sort_order' => $orderingSortOrder, | |
| 39 | - 'onlyStandard' => true, | |
| 40 | - ]; | |
| 41 | - $matchingMails = $this->getMatchingElementsFromData($requestData, $status, $page); | |
| 42 | - | |
| 43 | - | |
| 44 | - $matchingMailsNb = count($matchingMails['elements']); | |
| 45 | - | |
| 46 | - if (empty($matchingMailsNb) && $page > 1) { | |
| 47 | - $this->setVarFiltersListing('mails_pagination_page', 1); | |
| 48 | - $this->listing(); | |
| 49 | - | |
| 50 | - return; | |
| 51 | - } | |
| 52 | - | |
| 53 | - // Prepare the pagination | |
| 54 | - $pagination->setStatus($matchingMails['total']->total, $page, $mailsPerPage); | |
| 55 | - | |
| 56 | - ob_start(); | |
| 57 | - require acym_getView('mails', 'listing_import', true); | |
| 58 | - $templateImportView = ob_get_clean(); | |
| 59 | - | |
| 60 | - $tagClass = new TagClass(); | |
| 61 | - $mailsData = [ | |
| 62 | - 'allMails' => $matchingMails['elements'], | |
| 63 | - 'allTags' => $tagClass->getAllTagsByType(TagClass::TYPE_MAIL), | |
| 64 | - 'pagination' => $pagination, | |
| 65 | - 'search' => $searchFilter, | |
| 66 | - 'tag' => $tagFilter, | |
| 67 | - 'ordering' => $ordering, | |
| 68 | - 'status' => $status, | |
| 69 | - 'mailNumberPerStatus' => $matchingMails['status'], | |
| 70 | - 'orderingSortOrder' => $orderingSortOrder, | |
| 71 | - 'templateImportView' => $templateImportView, | |
| 72 | - 'mailClass' => $mailClass, | |
| 73 | - 'favoriteTemplateId' => (int)$this->config->get('favorite_template', 0), | |
| 74 | - ]; | |
| 75 | - | |
| 76 | - if (!empty($mailsData['tag'])) { | |
| 77 | - $mailsData['status_toolbar'] = [ | |
| 78 | - 'mails_tag' => $mailsData['tag'], | |
| 79 | - ]; | |
| 80 | - } | |
| 81 | - | |
| 82 | - $this->prepareToolbar($mailsData); | |
| 83 | - parent::display($mailsData); | |
| 84 | - } | |
| 85 | - | |
| 86 | - public function prepareToolbar(array &$data): void | |
| 87 | - { | |
| 88 | - $toolbarHelper = new ToolbarHelper(); | |
| 89 | - $toolbarHelper->addSearchBar($data['search'], 'mails_search', 'ACYM_SEARCH'); | |
| 90 | - $toolbarHelper->addFilterByTag($data, 'mails_tag', 'acym__templates__filter__tags acym__select'); | |
| 91 | - $toolbarHelper->addButton(acym_translation('ACYM_ADD_DEFAULT_TMPL'), ['data-task' => 'installDefaultTmpl', 'id' => 'acym__mail__install-default'], 'content_copy'); | |
| 92 | - | |
| 93 | - $toolbarHelper->addModalButton( | |
| 94 | - [ | |
| 95 | - 'button' => '<i class="acymicon-download"></i>'.esc_html(acym_translation('ACYM_IMPORT')), | |
| 96 | - 'modalContent' => $data['templateImportView'], | |
| 97 | - 'id' => 'acym__template__import__reveal', | |
| 98 | - 'attributesButton' => [ | |
| 99 | - 'class' => 'button button-secondary cell medium-6 large-shrink', | |
| 100 | - 'data-reload' => 'true', | |
| 101 | - 'data-ajax' => 'false', | |
| 102 | - ], | |
| 103 | - ] | |
| 104 | - ); | |
| 105 | - | |
| 106 | - $toolbarHelper->addModalButton( | |
| 107 | - [ | |
| 108 | - 'button' => '<i class="acymicon-add"></i>'.esc_html(acym_translation('ACYM_CREATE')), | |
| 109 | - 'modalContent' => '<div class="cell grid-x grid-margin-x"> | |
| 110 | - <button type="button" data-task="edit" data-editor="html" class="acym__create__template button cell large-auto small-6 margin-top-1 button-secondary">'.esc_html( | |
| 111 | - acym_translation('ACYM_HTML_EDITOR') | |
| 112 | - ).'</button> | |
| 113 | - <button type="button" data-task="edit" data-editor="acyEditor" class="acym__create__template button cell medium-auto margin-top-1">'.esc_html( | |
| 114 | - acym_translation('ACYM_DD_EDITOR') | |
| 115 | - ).'</button> | |
| 116 | - </div>', | |
| 117 | - 'attributesButton' => [ | |
| 118 | - 'class' => 'acym_vcenter button cell medium-6 large-shrink', | |
| 119 | - ], | |
| 120 | - 'isLarge' => false, | |
| 121 | - ] | |
| 122 | - ); | |
| 123 | - | |
| 124 | - $data['toolbar'] = $toolbarHelper; | |
| 125 | - } | |
| 126 | - | |
| 127 | - public function doUploadTemplate(): void | |
| 128 | - { | |
| 129 | - wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 130 | - | |
| 131 | - if (!acym_isAdmin() || !acym_isAllowed('mails')) { | |
| 132 | - die('Access denied'); | |
| 133 | - } | |
| 134 | - | |
| 135 | - $mailClass = new MailClass(); | |
| 136 | - $mailClass->doupload(); | |
| 137 | - | |
| 138 | - $this->listing(); | |
| 139 | - } | |
| 140 | - | |
| 141 | - public function export(): void | |
| 142 | - { | |
| 143 | - wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 144 | - | |
| 145 | - // Get passed data and check if we have everything we need | |
| 146 | - $templateId = acym_getVar('int', 'templateId', 0); | |
| 147 | - | |
| 148 | - if (empty($templateId)) { | |
| 149 | - exit; | |
| 150 | - } | |
| 151 | - | |
| 152 | - $mailClass = new MailClass(); | |
| 153 | - $template = $mailClass->getOneById($templateId); | |
| 154 | - | |
| 155 | - // We have all we need for the export, prepare the headers for the download | |
| 156 | - $exportHelper = new ExportHelper(); | |
| 157 | - $exportHelper->exportTemplate($template); | |
| 158 | - | |
| 159 | - exit; | |
| 160 | - } | |
| 161 | - | |
| 162 | - public function installDefaultTmpl(): void | |
| 163 | - { | |
| 164 | - wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 165 | - | |
| 166 | - $updateHelper = new UpdateHelper(); | |
| 167 | - $updateHelper->installTemplates(); | |
| 168 | - | |
| 169 | - $this->listing(); | |
| 170 | - } | |
| 171 | - | |
| 172 | - public function favorite(): void | |
| 173 | - { | |
| 174 | - wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 175 | - | |
| 176 | - $templateId = acym_getVar('int', 'templateId', 0); | |
| 177 | - | |
| 178 | - if (empty($templateId)) { | |
| 179 | - acym_enqueueMessage(acym_translationSprintf('ACYM_NOT_FOUND', acym_translation('ACYM_TEMPLATE')), 'error'); | |
| 180 | - $this->listing(); | |
| 181 | - | |
| 182 | - return; | |
| 183 | - } | |
| 184 | - | |
| 185 | - $favoriteTemplateId = $this->config->get('favorite_template', 0); | |
| 186 | - $this->config->saveConfig(['favorite_template' => (int)$favoriteTemplateId === $templateId ? 0 : $templateId]); | |
| 187 | - | |
| 188 | - $this->listing(); | |
| 189 | - } | |
| 190 | - | |
| 191 | - public function massDuplicate(): void | |
| 192 | - { | |
| 193 | - wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 194 | - | |
| 195 | - $ids = acym_getVar('array', 'elements_checked', []); | |
| 196 | - if (!empty($ids)) { | |
| 197 | - $this->duplicate($ids); | |
| 198 | - } | |
| 199 | - $this->listing(); | |
| 200 | - } | |
| 201 | - | |
| 202 | - public function oneDuplicate(): void | |
| 203 | - { | |
| 204 | - wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 205 | - | |
| 206 | - $templateId = acym_getVar('int', 'templateId', 0); | |
| 207 | - | |
| 208 | - if (empty($templateId)) { | |
| 209 | - acym_enqueueMessage(acym_translation('ACYM_TEMPLATE_DUPLICATE_ERROR'), 'error'); | |
| 210 | - $this->listing(); | |
| 211 | - | |
| 212 | - return; | |
| 213 | - } | |
| 214 | - | |
| 215 | - $this->duplicate([$templateId]); | |
| 216 | - $this->listing(); | |
| 217 | - } | |
| 218 | - | |
| 219 | - public function duplicate(array $templates = []): void | |
| 220 | - { | |
| 221 | - $mailClass = new MailClass(); | |
| 222 | - $tmplError = []; | |
| 223 | - foreach ($templates as $templateId) { | |
| 224 | - $oldTemplate = $mailClass->getOneById($templateId); | |
| 225 | - | |
| 226 | - if (empty($oldTemplate)) { | |
| 227 | - $tmplError[] = $templateId; | |
| 228 | - continue; | |
| 229 | - } | |
| 230 | - | |
| 231 | - $newTemplate = $oldTemplate; | |
| 232 | - $newTemplate->id = 0; | |
| 233 | - $newTemplate->name = $oldTemplate->name.'_copy'; | |
| 234 | - $newTemplate->creator_id = acym_currentUserId(); | |
| 235 | - unset($newTemplate->thumbnail); | |
| 236 | - | |
| 237 | - $mailClass->save($newTemplate); | |
| 238 | - } | |
| 239 | - if (!empty($tmplError)) { | |
| 240 | - acym_enqueueMessage(acym_translationSprintf('ACYM_TEMPLATE_X_DUPLICATE_ERROR', implode(', ', $tmplError)), 'error'); | |
| 241 | - } | |
| 242 | - } | |
| 243 | - | |
| 244 | - public function delete(): void | |
| 245 | - { | |
| 246 | - parent::delete(); | |
| 247 | - | |
| 248 | - $returnListing = acym_getVar('string', 'return_listing', ''); | |
| 249 | - if (!empty($returnListing)) { | |
| 250 | - if (acym_isAdmin()) { | |
| 251 | - acym_redirect(acym_completeLink($returnListing, false, true)); | |
| 252 | - } else { | |
| 253 | - $itemId = acym_getVar('int', 'acym_itemid'); | |
| 254 | - $itemId = empty($itemId) ? '' : '&Itemid='.$itemId; | |
| 255 | - $urlRedirect = acym_completeLink($returnListing.$itemId, false, true); | |
| 256 | - acym_redirect($urlRedirect); | |
| 257 | - } | |
| 258 | - } | |
| 259 | - } | |
| 260 | - | |
| 261 | - public function getMailByIdAjax(): void | |
| 262 | - { | |
| 263 | - wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 264 | - $mailId = acym_getVar('int', 'id', 0); | |
| 265 | - if (empty($mailId)) { | |
| 266 | - acym_sendAjaxResponse(acym_translation('ACYM_COULD_NOT_FIND_MAIL'), [], false); | |
| 267 | - } | |
| 268 | - | |
| 269 | - $mailClass = new MailClass(); | |
| 270 | - $mail = $mailClass->getOneById($mailId); | |
| 271 | - if (empty($mail)) { | |
| 272 | - acym_sendAjaxResponse(acym_translation('ACYM_COULD_NOT_FIND_MAIL'), [], false); | |
| 273 | - } | |
| 274 | - | |
| 275 | - if (!$mailClass->hasUserAccess($mailId)) { | |
| 276 | - die('Access denied for this mail'); | |
| 277 | - } | |
| 278 | - | |
| 279 | - acym_sendAjaxResponse('', ['mail' => $mail]); | |
| 280 | - } | |
| 281 | -} | |