| 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 |
acym_checkToken(); |
| 15 |
$genericImport = acym_getVar('boolean', 'generic', false); |
| 16 |
$selectedListsIds = json_decode(acym_getVar('string', 'selected', '[]')); |
| 17 |
|
| 18 |
$listClass = new ListClass(); |
| 19 |
$listToAdd = new \stdClass(); |
| 20 |
$listToAdd->name = acym_getVar('string', 'list_name', ''); |
| 21 |
$listToAdd->color = '#'.substr(str_shuffle('ABCDEF0123456789'), 0, 6); |
| 22 |
$listToAdd->visible = 1; |
| 23 |
$listToAdd->active = 1; |
| 24 |
|
| 25 |
$selectedListsIds[] = $listClass->save($listToAdd); |
| 26 |
|
| 27 |
$entityHelper = new EntitySelectHelper(); |
| 28 |
$importHelper = new ImportHelper(); |
| 29 |
|
| 30 |
|
| 31 |
$return = $entityHelper->entitySelect( |
| 32 |
'list', |
| 33 |
['join' => 'join_lists-'.implode(',', $selectedListsIds)], |
| 34 |
$entityHelper->getColumnsForList('lists.list_id', true), |
| 35 |
[], |
| 36 |
true, |
| 37 |
$importHelper->additionalDataUsersImport($genericImport) |
| 38 |
); |
| 39 |
|
| 40 |
echo $return; |
| 41 |
exit; |
| 42 |
} |
| 43 |
|
| 44 |
public function usersSummary(): void |
| 45 |
{ |
| 46 |
$id = acym_getVar('int', 'list_id', 0); |
| 47 |
$offset = acym_getVar('int', 'offset', 0); |
| 48 |
$limit = acym_getVar('int', 'limit', 50); |
| 49 |
$search = acym_getVar('string', 'modal_search', ''); |
| 50 |
|
| 51 |
if (empty($id)) { |
| 52 |
acym_sendAjaxResponse(acym_translation('ACYM_COULD_NOT_RETRIEVE_DATA'), [], false); |
| 53 |
} |
| 54 |
|
| 55 |
$listClass = new ListClass(); |
| 56 |
if (!$listClass->hasUserAccess($id)) { |
| 57 |
acym_sendAjaxResponse(acym_translation('ACYM_UNAUTHORIZED_ACCESS'), [], false); |
| 58 |
} |
| 59 |
|
| 60 |
acym_sendAjaxResponse('', ['users' => $listClass->getUsersForSummaryModal($id, $offset, $limit, $search)]); |
| 61 |
} |
| 62 |
|
| 63 |
public function setAjaxListing(): void |
| 64 |
{ |
| 65 |
$showSelected = acym_getVar('string', 'show_selected'); |
| 66 |
$matchingListsData = new \stdClass(); |
| 67 |
$matchingListsData->ordering = 'name'; |
| 68 |
$matchingListsData->searchFilter = acym_getVar('string', 'search_lists'); |
| 69 |
$matchingListsData->listsPerPage = acym_getVar('int', 'listsPerPage', 20); |
| 70 |
$matchingListsData->idsSelected = json_decode(acym_getVar('string', 'selectedLists')); |
| 71 |
$matchingListsData->idsAlready = json_decode(acym_getVar('string', 'alreadyLists')); |
| 72 |
$matchingListsData->page = acym_getVar('int', 'pagination_page_ajax'); |
| 73 |
if (empty($matchingListsData->page)) { |
| 74 |
$matchingListsData->page = 1; |
| 75 |
} |
| 76 |
|
| 77 |
$matchingListsData->needDisplaySub = acym_getVar('int', 'needDisplaySub'); |
| 78 |
$matchingListsData->displayNonActive = acym_getVar('int', 'nonActive'); |
| 79 |
|
| 80 |
$params = [ |
| 81 |
'ordering' => $matchingListsData->ordering, |
| 82 |
'search' => $matchingListsData->searchFilter, |
| 83 |
'listsPerPage' => $matchingListsData->listsPerPage, |
| 84 |
'offset' => ($matchingListsData->page - 1) * $matchingListsData->listsPerPage, |
| 85 |
'already' => $matchingListsData->idsAlready, |
| 86 |
]; |
| 87 |
|
| 88 |
if ($showSelected == 'true') { |
| 89 |
$params['ids'] = $matchingListsData->idsSelected; |
| 90 |
} |
| 91 |
|
| 92 |
$listClass = new ListClass(); |
| 93 |
$lists = $listClass->getListsWithIdNameCount($params); |
| 94 |
|
| 95 |
$return = ''; |
| 96 |
|
| 97 |
if (empty($lists['lists'])) { |
| 98 |
$return .= '<h1 class="cell acym__listing__empty__search__modal text-center">'.acym_translation('ACYM_NO_RESULTS_FOUND').'</h1>'; |
| 99 |
} |
| 100 |
|
| 101 |
foreach ($lists['lists'] as $list) { |
| 102 |
if (!empty($matchingListsData->displayNonActive) && $list->active == 0) continue; |
| 103 |
|
| 104 |
$return .= '<div class="grid-x modal__pagination__listing__lists__in-form__list cell">'; |
| 105 |
|
| 106 |
$return .= '<div class="cell shrink"><input type="checkbox" id="modal__pagination__listing__lists__list'.acym_escape($list->id).'" value="'.acym_escape( |
| 107 |
$list->id |
| 108 |
).'" class="modal__pagination__listing__lists__list--checkbox" name="lists_checked[]"'; |
| 109 |
|
| 110 |
if (!empty($matchingListsData->idsSelected) && in_array($list->id, $matchingListsData->idsSelected)) { |
| 111 |
$return .= 'checked'; |
| 112 |
} |
| 113 |
|
| 114 |
$return .= '></div><i class="cell shrink acymicon-circle" style="color:'.acym_escape( |
| 115 |
$list->color |
| 116 |
).'"></i><label class="cell auto" for="modal__pagination__listing__lists__list'.acym_escape($list->id).'"> '; |
| 117 |
|
| 118 |
$return .= '<span class="modal__pagination__listing__lists__list-name">'.acym_escape($list->name).'</span>'; |
| 119 |
|
| 120 |
if (!empty($matchingListsData->needDisplaySub)) { |
| 121 |
$return .= '<span class="modal__pagination__listing__lists__list-subscribers">('.acym_escape($list->subscribers).')</span>'; |
| 122 |
} |
| 123 |
|
| 124 |
$return .= '</label></div>'; |
| 125 |
} |
| 126 |
|
| 127 |
$pagination = new PaginationHelper(); |
| 128 |
$pagination->setStatus($lists['total'], $matchingListsData->page, $matchingListsData->listsPerPage); |
| 129 |
|
| 130 |
$return .= $pagination->displayAjax(); |
| 131 |
|
| 132 |
echo $return; |
| 133 |
exit; |
| 134 |
} |
| 135 |
|
| 136 |
public function loadMoreSubscribers(): void |
| 137 |
{ |
| 138 |
acym_checkToken(); |
| 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->getSubscribersEvolutionByList($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->save(['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 |
} |
| 197 |
|