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