Browse code

Implement pagination

Benjamin Roth authored on03/02/2023 10:49:12
Showing4 changed files
... ...
@@ -12,4 +12,4 @@ declare(strict_types=1);
12 12
 
13 13
 use vossmedien\AloxBundle\Controller\Frontend\Module\JoblistModuleController;
14 14
 
15
-$GLOBALS['TL_DCA']['tl_module']['palettes'][JoblistModuleController::TYPE] = '{title_legend},name,type;{template_legend:hide},customTpl;{protected_legend:hide},protected;{expert_legend:hide},guests,cssID';
15
+$GLOBALS['TL_DCA']['tl_module']['palettes'][JoblistModuleController::TYPE] = '{title_legend},name,type;{alox_legend},perPage;{template_legend:hide},customTpl;{protected_legend:hide},protected;{expert_legend:hide},guests,cssID';
16 16
new file mode 100644
... ...
@@ -0,0 +1,14 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+/*
6
+ * This file is part of alox bundle for Contao.
7
+ *
8
+ * (c) Benjamin Roth
9
+ *
10
+ * @license commercial
11
+ */
12
+
13
+
14
+$GLOBALS['TL_LANG']['tl_module']['alox_legend'] = 'Listen-Konfiguration';
... ...
@@ -91,6 +91,7 @@
91 91
                 </li>
92 92
             {% endfor %}
93 93
             </ul>
94
+            {{ pagination|raw }}
94 95
         </div>
95 96
     {% endif %}
96 97
 {% endblock %}
... ...
@@ -12,6 +12,7 @@ declare(strict_types=1);
12 12
 
13 13
 namespace vossmedien\AloxBundle\Controller\Frontend\Module;
14 14
 
15
+use Contao\Config;
15 16
 use Contao\CoreBundle\Controller\FrontendModule\AbstractFrontendModuleController;
16 17
 use Contao\CoreBundle\ServiceAnnotation\FrontendModule;
17 18
 use Contao\Input;
... ...
@@ -41,6 +42,9 @@ class JoblistModuleController extends AbstractFrontendModuleController
41 42
 
42 43
     protected function getResponse(Template $template, ModuleModel $model, Request $request): ?Response
43 44
     {
45
+        $limit = null;
46
+        $offset = 0;
47
+
44 48
         $strFormId = 'joblist-form-' . $model->id;
45 49
         $arrSearchParams = [];
46 50
         $arrSetFilters = [
... ...
@@ -99,6 +103,49 @@ class JoblistModuleController extends AbstractFrontendModuleController
99 103
         // Get (filtered) jobs
100 104
         $jobs = $this->api->getStellenFiltered($arrSearchParams);
101 105
 
106
+        // Get the total number of items
107
+        $intTotal = $jobs->TotalItems;
108
+        $total = $intTotal;
109
+        $pagination = '';
110
+
111
+        if ($model->perPage > 0)
112
+        {
113
+            // Adjust the overall limit
114
+            if (isset($limit))
115
+            {
116
+                $total = min($limit, $total);
117
+            }
118
+
119
+            // Get the current page
120
+            $id = 'list_page' . $model->id;
121
+            $page = Input::get($id) ?? 1;
122
+
123
+            // Do not index or cache the page if the page number is outside the range
124
+            if ($page < 1 || $page > max(ceil($total / $model->perPage), 1))
125
+            {
126
+                throw new \PageNotFoundException('Page not found: ' . \Environment::get('uri'));
127
+            }
128
+
129
+            // Set limit and offset
130
+            $limit = $model->perPage;
131
+            $offset += (max($page, 1) - 1) * $model->perPage;
132
+
133
+            // Overall limit
134
+            if ($offset + $limit > $total)
135
+            {
136
+                $limit = $total - $offset;
137
+            }
138
+
139
+            // Add the pagination menu
140
+            $objPagination = new \Pagination($total, $model->perPage, Config::get('maxPaginationLinks'), $id);
141
+            $pagination = $objPagination->generate("\n  ");
142
+
143
+            $arrSearchParams['pageNo'] = $page;
144
+            $arrSearchParams['pageSize'] = $limit;
145
+            $jobs = $this->api->getStellenFiltered($arrSearchParams);
146
+
147
+        }
148
+
102 149
 
103 150
         // Get filter options
104 151
         $filterVertragsarten = $this->api->getKatalogByRelationName('StelleVertragsart','ChildEntity');
... ...
@@ -123,6 +170,7 @@ class JoblistModuleController extends AbstractFrontendModuleController
123 170
 
124 171
         $template->formSubmit = $strFormId;
125 172
         $template->formSetFilters = $arrSetFilters;
173
+        $template->pagination = $pagination;
126 174
 
127 175
 
128 176
         // Add scripts