| @@ -1,147 +1,0 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace AcyMailing\Helpers; | |
| 4 | - | |
| 5 | -use AcyMailing\Core\AcymObject; | |
| 6 | - | |
| 7 | -class PaginationHelper extends AcymObject | |
| 8 | -{ | |
| 9 | - private int $totalNbOfElements; | |
| 10 | - private int $currentPage; | |
| 11 | - private int $nbPerPage; | |
| 12 | - | |
| 13 | - public function setStatus(int $total, int $page, int $nbPerPage) | |
| 14 | - { | |
| 15 | - $this->totalNbOfElements = $total; | |
| 16 | - $this->currentPage = empty($page) ? 1 : $page; | |
| 17 | - $this->nbPerPage = $nbPerPage; | |
| 18 | - } | |
| 19 | - | |
| 20 | - public function display(string $page = '', string $suffix = '', bool $dynamics = false): string | |
| 21 | - { | |
| 22 | - $name = empty($page) ? 'pagination_page_ajax' : $page.'_pagination_page'; | |
| 23 | - | |
| 24 | - if (empty($this->nbPerPage)) { | |
| 25 | - return ''; | |
| 26 | - } | |
| 27 | - | |
| 28 | - $nbPages = ceil($this->totalNbOfElements / $this->nbPerPage); | |
| 29 | - | |
| 30 | - $class = $dynamics ? ' margin-bottom-1' : ''; | |
| 31 | - | |
| 32 | - if (acym_isAdmin() || ACYM_CMS === 'joomla') { | |
| 33 | - $classNavigationContainer = 'shrink grid-margin-x'; | |
| 34 | - $classNavigation = 'small-auto medium-shrink'; | |
| 35 | - $classDisplayNumber = 'shrink'; | |
| 36 | - $classPadding = 'pagination_padding '; | |
| 37 | - } else { | |
| 38 | - $classNavigationContainer = ''; | |
| 39 | - $classNavigation = ''; | |
| 40 | - $classDisplayNumber = 'align-center'; | |
| 41 | - $classPadding = ''; | |
| 42 | - } | |
| 43 | - | |
| 44 | - $pagination = '<div class="pagination text-center cell grid-x'.$class.'" role="navigation" aria-label="Pagination"> | |
| 45 | - <div class="cell '.$classNavigationContainer.' margin-auto grid-x align-center"> | |
| 46 | - <div class="'.$classNavigation.' pagination_container cell grid-x acym_vcenter align-center">'; | |
| 47 | - | |
| 48 | - if (!$dynamics) { | |
| 49 | - $pagination .= '<div class="cell shrink pagination-turbo-left pagination_one_pagination '.$classPadding; | |
| 50 | - $pagination .= $this->currentPage > 1 ? 'acym__pagination__page'.$suffix.'" page="1' : 'pagination_disabled'; | |
| 51 | - $pagination .= '"><i class="acymicon-play-arrow rotate180deg pagination__i"></i><i class="acymicon-play-arrow rotate180deg pagination__i"></i></div>'; | |
| 52 | - $pagination .= '<div class="cell shrink pagination_border_left"></div>'; | |
| 53 | - } | |
| 54 | - | |
| 55 | - $pagination .= '<div class="cell shrink pagination-previous pagination_one_pagination '.$classPadding; | |
| 56 | - $pagination .= $this->currentPage > 1 ? 'acym__pagination__page'.$suffix.'" page="'.($this->currentPage - 1) : 'pagination_disabled'; | |
| 57 | - $pagination .= '"><i class="acymicon-play-arrow rotate180deg pagination__i"></i></div>'; | |
| 58 | - | |
| 59 | - $pagination .= '<div class="cell shrink pagination_border_left"></div>'; | |
| 60 | - $pagination .= '<input type="number" name="'.$name.'" min="1" max="'.(empty($nbPages) ? 1 | |
| 61 | - : $nbPages).'" value="'.$this->currentPage.'" class="cell shrink pagination_input" id="acym_pagination'.$suffix.'">'; | |
| 62 | - $pagination .= '<p class="cell shrink pagination_text">'.acym_translation('ACYM_OUT_OF').' '.$nbPages.'</p>'; | |
| 63 | - $pagination .= '<div class="cell shrink pagination_border_right"></div>'; | |
| 64 | - | |
| 65 | - if ($this->currentPage < $nbPages) { | |
| 66 | - $paramsNext = 'acym__pagination__page'.$suffix.'" page="'.($this->currentPage + 1); | |
| 67 | - $paramsTurboNext = 'acym__pagination__page'.$suffix.'" page="'.$nbPages; | |
| 68 | - } else { | |
| 69 | - $paramsNext = 'pagination_disabled'; | |
| 70 | - $paramsTurboNext = 'pagination_disabled'; | |
| 71 | - } | |
| 72 | - | |
| 73 | - $pagination .= '<div class="cell shrink pagination-next pagination_one_pagination '.$classPadding.$paramsNext.'"><i class="acymicon-play-arrow pagination__i"></i></div>'; | |
| 74 | - | |
| 75 | - if (!$dynamics) { | |
| 76 | - $pagination .= '<div class="cell shrink pagination_border_right"></div>'; | |
| 77 | - $pagination .= '<div class="cell shrink pagination-turbo-right pagination_one_pagination '.$classPadding.$paramsTurboNext.'"> | |
| 78 | - <i class="acymicon-play-arrow pagination__i"></i> | |
| 79 | - <i class="acymicon-play-arrow pagination__i"></i> | |
| 80 | - </div>'; | |
| 81 | - } | |
| 82 | - | |
| 83 | - $pagination .= '</div>'; | |
| 84 | - | |
| 85 | - if (!$dynamics) { | |
| 86 | - $nbPagesOptions = [ | |
| 87 | - '5' => 5, | |
| 88 | - '10' => 10, | |
| 89 | - '15' => 15, | |
| 90 | - '20' => 20, | |
| 91 | - '30' => 30, | |
| 92 | - '50' => 50, | |
| 93 | - '100' => 100, | |
| 94 | - '200' => 200, | |
| 95 | - ]; | |
| 96 | - $pagination .= '<div class="cell '.$classDisplayNumber.' grid-x acym_vcenter acym__pagination__pagenb">'; | |
| 97 | - | |
| 98 | - $selectValue = $page === 'archive' ? $this->getListLimit($this->nbPerPage) : $this->getListLimit(); | |
| 99 | - $paginationNumberEntries = '<div class="acym__select__pagination">'.acym_select( | |
| 100 | - $nbPagesOptions, | |
| 101 | - 'acym_pagination_element_per_page', | |
| 102 | - $this->getClosest($selectValue, $nbPagesOptions), | |
| 103 | - ['class' => 'acym__select__pagination__dropdown'] | |
| 104 | - ).'</div>'; | |
| 105 | - | |
| 106 | - $pagination .= '<p class="cell shrink">'.acym_translationSprintf('ACYM_DISPLAY_NUMBER_ENTRIES', $paginationNumberEntries).'</p>'; | |
| 107 | - $pagination .= '</div>'; | |
| 108 | - } | |
| 109 | - | |
| 110 | - $pagination .= '</div>'; | |
| 111 | - $pagination .= '</div>'; | |
| 112 | - | |
| 113 | - return $pagination; | |
| 114 | - } | |
| 115 | - | |
| 116 | - public function displayAjax(bool $dynamics = false): string | |
| 117 | - { | |
| 118 | - return $this->display('', '__ajax', $dynamics); | |
| 119 | - } | |
| 120 | - | |
| 121 | - public function getListLimit(int $default = 20): int | |
| 122 | - { | |
| 123 | - $currentUserId = acym_currentUserId(); | |
| 124 | - if (empty($currentUserId)) $currentUserId = 0; | |
| 125 | - $currentConfig = (int)$this->config->get('list_limit_'.$currentUserId, $default); | |
| 126 | - $listLimitSelect = acym_getVar('int', 'acym_pagination_element_per_page', 0); | |
| 127 | - | |
| 128 | - if (!empty($listLimitSelect) && $listLimitSelect !== $currentConfig) { | |
| 129 | - $this->config->saveConfig(['list_limit_'.$currentUserId => $listLimitSelect]); | |
| 130 | - $currentConfig = $listLimitSelect; | |
| 131 | - } | |
| 132 | - | |
| 133 | - return $currentConfig ?: $default; | |
| 134 | - } | |
| 135 | - | |
| 136 | - private function getClosest(int $search, array $arr): string | |
| 137 | - { | |
| 138 | - $closest = null; | |
| 139 | - foreach ($arr as $item) { | |
| 140 | - if ($closest === null || abs($search - $closest) > abs($item - $search)) { | |
| 141 | - $closest = $item; | |
| 142 | - } | |
| 143 | - } | |
| 144 | - | |
| 145 | - return (string)$closest; | |
| 146 | - } | |
| 147 | -} | |