| @@ -1,0 +1,196 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace AcyMailing\Controllers\Lists; | |
| 4 | + | |
| 5 | +use AcyMailing\Classes\ListClass; | |
| 6 | +use AcyMailing\Helpers\EntitySelectHelper; | |
| 7 | +use AcyMailing\Helpers\ImportHelper; | |
| 8 | +use AcyMailing\Helpers\PaginationHelper; | |
| 9 | + | |
| 10 | +trait Ajax | |
| 11 | +{ | |
| 12 | + public function ajaxCreateNewList(): void | |
| 13 | + { | |
| 14 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 15 | + $genericImport = acym_getVar('boolean', 'generic', false); | |
| 16 | + $selectedListsIds = json_decode(acym_getVar('string', 'selected', '[]')); | |
| 17 | + if (empty($selectedListsIds)) { | |
| 18 | + $selectedListsIds = []; | |
| 19 | + } | |
| 20 | + | |
| 21 | + $listClass = new ListClass(); | |
| 22 | + $listToAdd = new \stdClass(); | |
| 23 | + $listToAdd->name = acym_getVar('string', 'list_name', ''); | |
| 24 | + $listToAdd->color = '#'.substr(str_shuffle('ABCDEF0123456789'), 0, 6); | |
| 25 | + $listToAdd->visible = 1; | |
| 26 | + $listToAdd->active = 1; | |
| 27 | + | |
| 28 | + $selectedListsIds[] = $listClass->save($listToAdd); | |
| 29 | + | |
| 30 | + $entityHelper = new EntitySelectHelper(); | |
| 31 | + $importHelper = new ImportHelper(); | |
| 32 | + | |
| 33 | + $entityHelper->entitySelect( | |
| 34 | + [ | |
| 35 | + 'display' => true, | |
| 36 | + 'entity' => 'list', | |
| 37 | + 'entityParams' => ['join' => 'join_lists-'.implode(',', $selectedListsIds)], | |
| 38 | + 'columnsToDisplay' => $entityHelper->getColumnsForList('lists.list_id', true), | |
| 39 | + 'additionalData' => fn() => $importHelper->displayAdditionalDataUsersImport($genericImport), | |
| 40 | + ] | |
| 41 | + ); | |
| 42 | + exit; | |
| 43 | + } | |
| 44 | + | |
| 45 | + public function usersSummary(): void | |
| 46 | + { | |
| 47 | + $id = acym_getVar('int', 'list_id', 0); | |
| 48 | + $offset = acym_getVar('int', 'offset', 0); | |
| 49 | + $limit = acym_getVar('int', 'limit', 50); | |
| 50 | + $search = acym_getVar('string', 'modal_search', ''); | |
| 51 | + | |
| 52 | + if (empty($id)) { | |
| 53 | + acym_sendAjaxResponse(acym_translation('ACYM_COULD_NOT_RETRIEVE_DATA'), [], false); | |
| 54 | + } | |
| 55 | + | |
| 56 | + $listClass = new ListClass(); | |
| 57 | + if (!$listClass->hasUserAccess($id)) { | |
| 58 | + acym_sendAjaxResponse(acym_translation('ACYM_UNAUTHORIZED_ACCESS'), [], false); | |
| 59 | + } | |
| 60 | + | |
| 61 | + acym_sendAjaxResponse('', ['users' => $listClass->getUsersForSummaryModal($id, $offset, $limit, $search)]); | |
| 62 | + } | |
| 63 | + | |
| 64 | + public function setAjaxListing(): void | |
| 65 | + { | |
| 66 | + $showSelected = acym_getVar('string', 'show_selected'); | |
| 67 | + $matchingListsData = new \stdClass(); | |
| 68 | + $matchingListsData->ordering = 'name'; | |
| 69 | + $matchingListsData->searchFilter = acym_getVar('string', 'search_lists'); | |
| 70 | + $matchingListsData->listsPerPage = acym_getVar('int', 'listsPerPage', 20); | |
| 71 | + $matchingListsData->idsSelected = json_decode(acym_getVar('string', 'selectedLists')); | |
| 72 | + $matchingListsData->idsAlready = json_decode(acym_getVar('string', 'alreadyLists')); | |
| 73 | + $matchingListsData->page = acym_getVar('int', 'pagination_page_ajax'); | |
| 74 | + if (empty($matchingListsData->page)) { | |
| 75 | + $matchingListsData->page = 1; | |
| 76 | + } | |
| 77 | + | |
| 78 | + $matchingListsData->needDisplaySub = acym_getVar('int', 'needDisplaySub'); | |
| 79 | + $matchingListsData->displayNonActive = acym_getVar('int', 'nonActive'); | |
| 80 | + | |
| 81 | + $params = [ | |
| 82 | + 'ordering' => $matchingListsData->ordering, | |
| 83 | + 'search' => $matchingListsData->searchFilter, | |
| 84 | + 'listsPerPage' => $matchingListsData->listsPerPage, | |
| 85 | + 'offset' => ($matchingListsData->page - 1) * $matchingListsData->listsPerPage, | |
| 86 | + 'already' => $matchingListsData->idsAlready, | |
| 87 | + ]; | |
| 88 | + | |
| 89 | + if ($showSelected == 'true') { | |
| 90 | + $params['ids'] = $matchingListsData->idsSelected; | |
| 91 | + } | |
| 92 | + | |
| 93 | + $listClass = new ListClass(); | |
| 94 | + $lists = $listClass->getListsWithIdNameCount($params); | |
| 95 | + | |
| 96 | + $return = ''; | |
| 97 | + | |
| 98 | + if (empty($lists['lists'])) { | |
| 99 | + $return .= '<h1 class="cell acym__listing__empty__search__modal text-center">'.acym_translation('ACYM_NO_RESULTS_FOUND').'</h1>'; | |
| 100 | + } | |
| 101 | + | |
| 102 | + foreach ($lists['lists'] as $list) { | |
| 103 | + if (!empty($matchingListsData->displayNonActive) && $list->active == 0) continue; | |
| 104 | + | |
| 105 | + $return .= '<div class="grid-x modal__pagination__listing__lists__in-form__list cell">'; | |
| 106 | + | |
| 107 | + $return .= '<div class="cell shrink"><input type="checkbox" id="modal__pagination__listing__lists__list'.esc_attr($list->id).'" value="'.esc_attr( | |
| 108 | + $list->id | |
| 109 | + ).'" class="modal__pagination__listing__lists__list--checkbox" name="lists_checked[]"'.checked( | |
| 110 | + !empty($matchingListsData->idsSelected) && in_array($list->id, $matchingListsData->idsSelected), | |
| 111 | + true, | |
| 112 | + false | |
| 113 | + ); | |
| 114 | + | |
| 115 | + $return .= '></div><i class="cell shrink acymicon-circle" style="color:'.esc_attr( | |
| 116 | + $list->color | |
| 117 | + ).'"></i><label class="cell auto" for="modal__pagination__listing__lists__list'.esc_attr($list->id).'"> '; | |
| 118 | + | |
| 119 | + $return .= '<span class="modal__pagination__listing__lists__list-name">'.esc_html($list->name).'</span>'; | |
| 120 | + | |
| 121 | + if (!empty($matchingListsData->needDisplaySub)) { | |
| 122 | + $return .= '<span class="modal__pagination__listing__lists__list-subscribers">('.esc_html($list->subscribers).')</span>'; | |
| 123 | + } | |
| 124 | + | |
| 125 | + $return .= '</label></div>'; | |
| 126 | + } | |
| 127 | + | |
| 128 | + $pagination = new PaginationHelper(); | |
| 129 | + $pagination->setStatus($lists['total'], $matchingListsData->page, $matchingListsData->listsPerPage); | |
| 130 | + | |
| 131 | + $return .= $pagination->displayAjax(); | |
| 132 | + | |
| 133 | + acym_sendAjaxResponse('', ['paginationListing' => $return]); | |
| 134 | + } | |
| 135 | + | |
| 136 | + public function loadMoreSubscribers(): void | |
| 137 | + { | |
| 138 | + wp_verify_nonce(acym_getVar('cmd', '_wpnonce'), 'acymnonce') || die('Invalid Token'); | |
| 139 | + $listClass = new ListClass(); | |
| 140 | + | |
| 141 | + $listId = acym_getVar('int', 'listId'); | |
| 142 | + | |
| 143 | + if (!acym_isAdmin()) { | |
| 144 | + $manageableLists = $listClass->getManageableLists(); | |
| 145 | + if (!in_array($listId, $manageableLists)) { | |
| 146 | + die('Access denied for this list'); | |
| 147 | + } | |
| 148 | + } | |
| 149 | + | |
| 150 | + $offset = acym_getVar('int', 'offset'); | |
| 151 | + $perCalls = acym_getVar('int', 'perCalls'); | |
| 152 | + $status = acym_getVar('int', 'status'); | |
| 153 | + $orderBy = acym_getVar('string', 'orderBy', 'id'); | |
| 154 | + $orderingSortOrder = acym_getVar('string', 'orderByOrdering', 'desc'); | |
| 155 | + $subscribers = $listClass->getSubscribersForList( | |
| 156 | + [ | |
| 157 | + 'listIds' => [$listId], | |
| 158 | + 'offset' => $offset, | |
| 159 | + 'limit' => $perCalls, | |
| 160 | + 'status' => $status, | |
| 161 | + 'orderBy' => $orderBy, | |
| 162 | + 'orderBySort' => $orderingSortOrder, | |
| 163 | + ] | |
| 164 | + ); | |
| 165 | + foreach ($subscribers as &$oneSub) { | |
| 166 | + if ($oneSub->subscription_date == '0000-00-00 00:00:00') continue; | |
| 167 | + $oneSub->subscription_date = acym_date(strtotime($oneSub->subscription_date), acym_translation('ACYM_DATE_FORMAT_LC2')); | |
| 168 | + } | |
| 169 | + acym_sendAjaxResponse('', ['subscribers' => $subscribers]); | |
| 170 | + } | |
| 171 | + | |
| 172 | + public function loadMostUsedLists(): void | |
| 173 | + { | |
| 174 | + $listClass = new ListClass(); | |
| 175 | + if ($this->config->get('dashboard_last_update', 0) + 86400 < time()) { | |
| 176 | + $usersByList = $listClass->getListsSubscribersAndUnsubscribeUsersFromUserHasList(); | |
| 177 | + foreach ($usersByList as $list) { | |
| 178 | + $subscriberEvolution = current($listClass->getSubscribersEvolutionByListIds([$list->list_id])); | |
| 179 | + $listClass->updateListsSubscribersAndUnsubscribeUsers( | |
| 180 | + $list->list_id, | |
| 181 | + $list->subscribers, | |
| 182 | + $list->unsubscribed_users, | |
| 183 | + $subscriberEvolution->newSub ?? 0, | |
| 184 | + $subscriberEvolution->newUnsub ?? 0 | |
| 185 | + ); | |
| 186 | + } | |
| 187 | + $this->config->saveConfig(['dashboard_last_update' => time()]); | |
| 188 | + } | |
| 189 | + $mostUsedList = $listClass->getMostUsedLists(); | |
| 190 | + | |
| 191 | + $ids = array_map(fn($obj) => $obj->list_id, $mostUsedList); | |
| 192 | + $listAndSubscribersData = $listClass->getMostUsedListsByIdsWithSubscribersData($ids); | |
| 193 | + | |
| 194 | + acym_sendAjaxResponse('', ['listAndSubscribersData' => $listAndSubscribersData]); | |
| 195 | + } | |
| 196 | +} | |