|
13 | 13 |
|
14 | 14 | namespace App\Base\Abstracts\Controllers; |
15 | 15 |
|
| 16 | +use App\Base\Abstracts\Models\BaseCollection; |
16 | 17 | use App\Base\Abstracts\Models\BaseModel; |
17 | 18 | use App\Base\Exceptions\PermissionDeniedException; |
18 | 19 | use App\Site\Routing\RouteInfo; |
|
26 | 27 | use Symfony\Component\HttpFoundation\Request; |
27 | 28 | use App\Base\Abstracts\Models\FrontendModel; |
28 | 29 | use Degami\Basics\Html\TagElement; |
| 30 | +use App\Site\Models\User; |
29 | 31 |
|
30 | 32 | /** |
31 | 33 | * Base for admin page that manages a Model |
@@ -62,6 +64,7 @@ public function __construct( |
62 | 64 | ) { |
63 | 65 | parent::__construct($container, $request, $route_info); |
64 | 66 | if ($this->template_data['action'] == 'list') { |
| 67 | + $this->addPaginationSizeSelector(); |
65 | 68 | $this->addNewButton(); |
66 | 69 |
|
67 | 70 | $paginate_params = [ |
@@ -90,19 +93,43 @@ public function __construct( |
90 | 93 |
|
91 | 94 | $paginate_params['condition'] = array_filter($conditions); |
92 | 95 | } |
| 96 | + |
| 97 | + $itemsPerPage = $this->getItemsPerPage(); |
93 | 98 | /** @var \App\Base\Abstracts\Models\BaseCollection $collection */ |
94 | 99 | $collection = $this->containerCall([$this->getObjectClass(), 'getCollection']); |
95 | 100 | $collection->addCondition($paginate_params['condition'])->addOrder($paginate_params['order']); |
96 | | - $data = $this->containerCall([$collection, 'paginate']); |
| 101 | + $data = $this->containerCall([$collection, 'paginate'], ['page_size' => $itemsPerPage]); |
97 | 102 | $this->template_data += [ |
98 | 103 | 'table' => $this->getHtmlRenderer()->renderAdminTable($this->getTableElements($data['items']), $this->getTableHeader(), $this), |
99 | 104 | 'total' => $data['total'], |
100 | 105 | 'current_page' => $data['page'], |
101 | | - 'paginator' => $this->getHtmlRenderer()->renderPaginator($data['page'], $data['total'], $this), |
| 106 | + 'paginator' => $this->getHtmlRenderer()->renderPaginator($data['page'], $data['total'], $this, $itemsPerPage, 5), |
102 | 107 | ]; |
103 | 108 | } |
104 | 109 | } |
105 | 110 |
|
| 111 | + /** |
| 112 | + * get items per page on listing |
| 113 | + * |
| 114 | + * @return int |
| 115 | + */ |
| 116 | + protected function getItemsPerPage() : int |
| 117 | + { |
| 118 | + /** @var User $user */ |
| 119 | + $user = $this->getCurrentUser(); |
| 120 | + |
| 121 | + $uiSettings = $user->getUserSession()->getSessionKey('uiSettings'); |
| 122 | + $currentRoute = $this->getRouteInfo()->getRouteName(); |
| 123 | + |
| 124 | + if (is_array($uiSettings) && isset($uiSettings[$currentRoute])) { |
| 125 | + if (isset($uiSettings[$currentRoute]['itemsPerPage'])) { |
| 126 | + return intval($uiSettings[$currentRoute]['itemsPerPage']); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return BaseCollection::ITEMS_PER_PAGE; |
| 131 | + } |
| 132 | + |
106 | 133 | /** |
107 | 134 | * gets search parameters |
108 | 135 | * |
@@ -221,6 +248,36 @@ public function addNewButton() |
221 | 248 | $this->addActionLink('new-btn', 'new-btn', $this->getHtmlRenderer()->getIcon('plus') . ' ' . $this->getUtils()->translate('New', $this->getCurrentLocale()), $this->getControllerUrl() . '?action=new', 'btn btn-sm btn-success'); |
222 | 249 | } |
223 | 250 |
|
| 251 | + public function addPaginationSizeSelector() |
| 252 | + { |
| 253 | + // calculate options values, including value used for pagination |
| 254 | + $options = array_unique(array_merge([10, 25, 50, 200, 500], [$this->getItemsPerPage()])); |
| 255 | + sort($options); |
| 256 | + |
| 257 | + $select = $this->containerMake(TagElement::class, ['options' => [ |
| 258 | + 'tag' => 'select', |
| 259 | + 'id' => 'pagination-size-selector', |
| 260 | + 'attributes' => [ |
| 261 | + 'class' => 'paginator-items-choice', |
| 262 | + 'style' => 'width: 50px', |
| 263 | + ], |
| 264 | + 'children' => array_map(function($val) { |
| 265 | + $selected = []; |
| 266 | + if ($val == $this->getItemsPerPage()) { |
| 267 | + $selected = ['selected' => 'selected']; |
| 268 | + } |
| 269 | + return $this->containerMake(TagElement::class, ['options' => [ |
| 270 | + 'tag' => 'option', |
| 271 | + 'value' => $val, |
| 272 | + 'attributes' => [ |
| 273 | + 'class' => '', |
| 274 | + ] + $selected, |
| 275 | + 'text' => $val, |
| 276 | + ]]); |
| 277 | + }, $options), |
| 278 | + ]]); |
| 279 | + $this->action_buttons[] = __('Items per page'). ':' . $select; |
| 280 | + } |
224 | 281 |
|
225 | 282 | /** |
226 | 283 | * gets action button html |
|
0 commit comments