]> git.mxchange.org Git - friendica.git/blob - src/Content/Pager.php
Remove stripping search term in Pager
[friendica.git] / src / Content / Pager.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Content;
23
24 use Friendica\Core\Renderer;
25 use Friendica\DI;
26 use Friendica\Util\Strings;
27
28 /**
29  * The Pager has two very different output, Minimal and Full, see renderMinimal() and renderFull() for more details.
30  */
31 class Pager
32 {
33         /**
34          * @var integer
35          */
36         private $page = 1;
37         /**
38          * @var integer
39          */
40         private $itemsPerPage = 50;
41
42         /**
43          * @var string
44          */
45         private $baseQueryString = '';
46
47         /**
48          * Instantiates a new Pager with the base parameters.
49          *
50          * Guesses the page number from the GET parameter 'page'.
51          *
52          * @param string  $queryString  The query string of the current page
53          * @param integer $itemsPerPage An optional number of items per page to override the default value
54          */
55         public function __construct($queryString, $itemsPerPage = 50)
56         {
57                 $this->setQueryString($queryString);
58                 $this->setItemsPerPage($itemsPerPage);
59                 $this->setPage(($_GET['page'] ?? 0) ?: 1);
60         }
61
62         /**
63          * Returns the start offset for a LIMIT clause. Starts at 0.
64          *
65          * @return integer
66          */
67         public function getStart()
68         {
69                 return max(0, ($this->page * $this->itemsPerPage) - $this->itemsPerPage);
70         }
71
72         /**
73          * Returns the number of items per page
74          *
75          * @return integer
76          */
77         public function getItemsPerPage()
78         {
79                 return $this->itemsPerPage;
80         }
81
82         /**
83          * Returns the current page number
84          *
85          * @return int
86          */
87         public function getPage()
88         {
89                 return $this->page;
90         }
91
92         /**
93          * Returns the base query string.
94          *
95          * Warning: this isn't the same value as passed to the constructor.
96          * See setQueryString() for the inventory of transformations
97          *
98          * @see setBaseQuery()
99          * @return string
100          */
101         public function getBaseQueryString()
102         {
103                 return Strings::ensureQueryParameter($this->baseQueryString);
104         }
105
106         /**
107          * Sets the number of items per page, 1 minimum.
108          *
109          * @param integer $itemsPerPage
110          */
111         public function setItemsPerPage($itemsPerPage)
112         {
113                 $this->itemsPerPage = max(1, intval($itemsPerPage));
114         }
115
116         /**
117          * Sets the current page number. Starts at 1.
118          *
119          * @param integer $page
120          */
121         public function setPage($page)
122         {
123                 $this->page = max(1, intval($page));
124         }
125
126         /**
127          * Sets the base query string from a full query string.
128          *
129          * Strips the 'page' parameter, and remove the 'q=' string for some reason.
130          *
131          * @param string $queryString
132          */
133         public function setQueryString($queryString)
134         {
135                 $stripped = preg_replace('/([&?]page=[0-9]*)/', '', $queryString);
136
137                 $stripped = trim($stripped, '/');
138
139                 $this->baseQueryString = $stripped;
140         }
141
142         /**
143          * Minimal pager (newer/older)
144          *
145          * This mode is intended for reverse chronological pages and presents only two links, newer (previous) and older (next).
146          * The itemCount is the number of displayed items. If no items are displayed, the older button is disabled.
147          *
148          * Example usage:
149          *
150          * $pager = new Pager($a->query_string);
151          *
152          * $params = ['order' => ['sort_field' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
153          * $items = DBA::toArray(DBA::select($table, $fields, $condition, $params));
154          *
155          * $html = $pager->renderMinimal(count($items));
156          *
157          * @param integer $itemCount The number of displayed items on the page
158          * @return string HTML string of the pager
159          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
160          */
161         public function renderMinimal($itemCount)
162         {
163                 $displayedItemCount = max(0, intval($itemCount));
164
165                 $data = [
166                         'class' => 'pager',
167                         'prev'  => [
168                                 'url'   => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)),
169                                 'text'  => DI::l10n()->t('newer'),
170                                 'class' => 'previous' . ($this->getPage() == 1 ? ' disabled' : '')
171                         ],
172                         'next'  => [
173                                 'url'   => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)),
174                                 'text'  => DI::l10n()->t('older'),
175                                 'class' =>  'next' . ($displayedItemCount < $this->getItemsPerPage() ? ' disabled' : '')
176                         ]
177                 ];
178
179                 $tpl = Renderer::getMarkupTemplate('paginate.tpl');
180                 return Renderer::replaceMacros($tpl, ['pager' => $data]);
181         }
182
183         /**
184          * Full pager (first / prev / 1 / 2 / ... / 14 / 15 / next / last)
185          *
186          * This mode presents page numbers as well as first, previous, next and last links.
187          * The itemCount is the total number of items including those not displayed.
188          *
189          * Example usage:
190          *
191          * $total = DBA::count($table, $condition);
192          *
193          * $pager = new Pager($a->query_string, $total);
194          *
195          * $params = ['limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
196          * $items = DBA::toArray(DBA::select($table, $fields, $condition, $params));
197          *
198          * $html = $pager->renderFull();
199          *
200          * @param integer $itemCount The total number of items including those note displayed on the page
201          * @return string HTML string of the pager
202          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
203          */
204         public function renderFull($itemCount)
205         {
206                 $totalItemCount = max(0, intval($itemCount));
207
208                 $data = [];
209
210                 $data['class'] = 'pagination';
211                 if ($totalItemCount > $this->getItemsPerPage()) {
212                         $data['first'] = [
213                                 'url'   => Strings::ensureQueryParameter($this->baseQueryString . '&page=1'),
214                                 'text'  => DI::l10n()->t('first'),
215                                 'class' => $this->getPage() == 1 ? 'disabled' : ''
216                         ];
217                         $data['prev'] = [
218                                 'url'   => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)),
219                                 'text'  => DI::l10n()->t('prev'),
220                                 'class' => $this->getPage() == 1 ? 'disabled' : ''
221                         ];
222
223                         $numpages = $totalItemCount / $this->getItemsPerPage();
224
225                         $numstart = 1;
226                         $numstop = $numpages;
227
228                         // Limit the number of displayed page number buttons.
229                         if ($numpages > 8) {
230                                 $numstart = (($this->getPage() > 4) ? ($this->getPage() - 4) : 1);
231                                 $numstop = (($this->getPage() > ($numpages - 7)) ? $numpages : ($numstart + 8));
232                         }
233
234                         $pages = [];
235
236                         for ($i = $numstart; $i <= $numstop; $i++) {
237                                 if ($i == $this->getPage()) {
238                                         $pages[$i] = [
239                                                 'url'   => '#',
240                                                 'text'  => $i,
241                                                 'class' => 'current active'
242                                         ];
243                                 } else {
244                                         $pages[$i] = [
245                                                 'url'   => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $i),
246                                                 'text'  => $i,
247                                                 'class' => 'n'
248                                         ];
249                                 }
250                         }
251
252                         if (($totalItemCount % $this->getItemsPerPage()) != 0) {
253                                 if ($i == $this->getPage()) {
254                                         $pages[$i] = [
255                                                 'url'   => '#',
256                                                 'text'  => $i,
257                                                 'class' => 'current active'
258                                         ];
259                                 } else {
260                                         $pages[$i] = [
261                                                 'url'   => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $i),
262                                                 'text'  => $i,
263                                                 'class' => 'n'
264                                         ];
265                                 }
266                         }
267
268                         $data['pages'] = $pages;
269
270                         $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
271
272                         $data['next'] = [
273                                 'url'   => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)),
274                                 'text'  => DI::l10n()->t('next'),
275                                 'class' => $this->getPage() == $lastpage ? 'disabled' : ''
276                         ];
277                         $data['last'] = [
278                                 'url'   => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $lastpage),
279                                 'text'  => DI::l10n()->t('last'),
280                                 'class' => $this->getPage() == $lastpage ? 'disabled' : ''
281                         ];
282                 }
283
284                 $tpl = Renderer::getMarkupTemplate('paginate.tpl');
285                 return Renderer::replaceMacros($tpl, ['pager' => $data]);
286         }
287 }