| 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): void |
| 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 |
echo '<div class="pagination text-center cell grid-x'.esc_attr($class).'" role="navigation" aria-label="Pagination"> |
| 45 |
<div class="cell '.esc_attr($classNavigationContainer).' margin-auto grid-x align-center"> |
| 46 |
<div class="'.esc_attr($classNavigation).' pagination_container cell grid-x acym_vcenter align-center">'; |
| 47 |
|
| 48 |
// Turbo first button |
| 49 |
if (!$dynamics) { |
| 50 |
echo '<div class="cell shrink pagination-turbo-left pagination_one_pagination '.esc_attr($classPadding); |
| 51 |
echo $this->currentPage > 1 ? 'acym__pagination__page'.esc_attr($suffix).'" page="1' : 'pagination_disabled'; |
| 52 |
echo '"><i class="acymicon-play-arrow rotate180deg pagination__i"></i><i class="acymicon-play-arrow rotate180deg pagination__i"></i></div>'; |
| 53 |
echo '<div class="cell shrink pagination_border_left"></div>'; |
| 54 |
} |
| 55 |
|
| 56 |
// Previous button |
| 57 |
echo '<div class="cell shrink pagination-previous pagination_one_pagination '.esc_attr($classPadding); |
| 58 |
echo $this->currentPage > 1 ? 'acym__pagination__page'.esc_attr($suffix).'" page="'.intval($this->currentPage - 1) : 'pagination_disabled'; |
| 59 |
echo '"><i class="acymicon-play-arrow rotate180deg pagination__i"></i></div>'; |
| 60 |
|
| 61 |
echo '<div class="cell shrink pagination_border_left"></div>'; |
| 62 |
echo '<input type="number" |
| 63 |
name="'.esc_attr($name).'" |
| 64 |
min="1" |
| 65 |
max="'.intval(empty($nbPages) ? 1 : $nbPages).'" |
| 66 |
value="'.esc_attr($this->currentPage).'" |
| 67 |
class="cell shrink pagination_input" |
| 68 |
id="acym_pagination'.esc_attr($suffix).'">'; |
| 69 |
echo '<p class="cell shrink pagination_text">'.esc_html(acym_translation('ACYM_OUT_OF').' '.$nbPages).'</p>'; |
| 70 |
echo '<div class="cell shrink pagination_border_right"></div>'; |
| 71 |
|
| 72 |
// Next button |
| 73 |
echo '<div class="cell shrink pagination-next pagination_one_pagination '.esc_attr($classPadding); |
| 74 |
echo $this->currentPage < $nbPages ? 'acym__pagination__page'.esc_attr($suffix).'" page="'.intval($this->currentPage + 1) : 'pagination_disabled'; |
| 75 |
echo '"><i class="acymicon-play-arrow pagination__i"></i></div>'; |
| 76 |
|
| 77 |
// Turbo last button |
| 78 |
if (!$dynamics) { |
| 79 |
echo '<div class="cell shrink pagination_border_right"></div>'; |
| 80 |
echo '<div class="cell shrink pagination-turbo-right pagination_one_pagination '.esc_attr($classPadding); |
| 81 |
echo $this->currentPage < $nbPages ? 'acym__pagination__page'.esc_attr($suffix).'" page="'.intval($nbPages) : 'pagination_disabled'; |
| 82 |
echo '"> |
| 83 |
<i class="acymicon-play-arrow pagination__i"></i> |
| 84 |
<i class="acymicon-play-arrow pagination__i"></i> |
| 85 |
</div>'; |
| 86 |
} |
| 87 |
|
| 88 |
echo '</div>'; |
| 89 |
|
| 90 |
if (!$dynamics) { |
| 91 |
$nbPagesOptions = [ |
| 92 |
'5' => 5, |
| 93 |
'10' => 10, |
| 94 |
'15' => 15, |
| 95 |
'20' => 20, |
| 96 |
'30' => 30, |
| 97 |
'50' => 50, |
| 98 |
'100' => 100, |
| 99 |
'200' => 200, |
| 100 |
]; |
| 101 |
echo '<div class="cell '.esc_attr($classDisplayNumber).' grid-x acym_vcenter acym__pagination__pagenb">'; |
| 102 |
|
| 103 |
$selectValue = $page === 'archive' ? $this->getListLimit($this->nbPerPage) : $this->getListLimit(); |
| 104 |
$paginationNumberEntries = '<div class="acym__select__pagination">'.acym_select( |
| 105 |
$nbPagesOptions, |
| 106 |
'acym_pagination_element_per_page', |
| 107 |
$this->getClosest($selectValue, $nbPagesOptions), |
| 108 |
['class' => 'acym__select__pagination__dropdown'] |
| 109 |
).'</div>'; |
| 110 |
|
| 111 |
echo '<p class="cell shrink">'.wp_kses( |
| 112 |
acym_translationSprintf('ACYM_DISPLAY_NUMBER_ENTRIES', $paginationNumberEntries), |
| 113 |
[ |
| 114 |
'div' => [ |
| 115 |
'class' => true, |
| 116 |
], |
| 117 |
'select' => [ |
| 118 |
'class' => true, |
| 119 |
'name' => true, |
| 120 |
'id' => true, |
| 121 |
], |
| 122 |
'option' => [ |
| 123 |
'value' => true, |
| 124 |
'selected' => true, |
| 125 |
], |
| 126 |
] |
| 127 |
).'</p>'; |
| 128 |
echo '</div>'; |
| 129 |
} |
| 130 |
|
| 131 |
echo '</div>'; |
| 132 |
echo '</div>'; |
| 133 |
} |
| 134 |
|
| 135 |
public function displayAjax(bool $dynamics = false): string |
| 136 |
{ |
| 137 |
ob_start(); |
| 138 |
$this->display('', '__ajax', $dynamics); |
| 139 |
|
| 140 |
return ob_get_clean(); |
| 141 |
} |
| 142 |
|
| 143 |
public function getListLimit(int $default = 20): int |
| 144 |
{ |
| 145 |
$currentUserId = acym_currentUserId(); |
| 146 |
if (empty($currentUserId)) $currentUserId = 0; |
| 147 |
$currentConfig = (int)$this->config->get('list_limit_'.$currentUserId, $default); |
| 148 |
$listLimitSelect = acym_getVar('int', 'acym_pagination_element_per_page', 0); |
| 149 |
|
| 150 |
if (!empty($listLimitSelect) && $listLimitSelect !== $currentConfig) { |
| 151 |
$this->config->saveConfig(['list_limit_'.$currentUserId => $listLimitSelect]); |
| 152 |
$currentConfig = $listLimitSelect; |
| 153 |
} |
| 154 |
|
| 155 |
return $currentConfig ? : $default; |
| 156 |
} |
| 157 |
|
| 158 |
private function getClosest(int $search, array $arr): string |
| 159 |
{ |
| 160 |
$closest = null; |
| 161 |
foreach ($arr as $item) { |
| 162 |
if ($closest === null || abs($search - $closest) > abs($item - $search)) { |
| 163 |
$closest = $item; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
return (string)$closest; |
| 168 |
} |
| 169 |
} |
| 170 |
|