]> git.mxchange.org Git - friendica.git/blob - src/Content/BoundariesPager.php
Add new BoundariesPager
[friendica.git] / src / Content / BoundariesPager.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\Network;
27 use Friendica\Util\Strings;
28
29 /**
30  * This pager should be used by lists using the since_id/max_id parameters
31  *
32  * In this context, "id" refers to the value of the column that the list is ordered by.
33  * This pager automatically identifies if the sorting is done increasingly or decreasingly if the first item id
34  * and last item id are different. Otherwise it defaults to decreasingly like reverse chronological lists.
35  */
36 class BoundariesPager extends Pager
37 {
38         protected $first_item_id;
39         protected $last_item_id;
40         protected $first_page = true;
41
42         /**
43          * Instantiates a new Pager with the base parameters.
44          *
45          * @param string  $queryString   The query string of the current page
46          * @param string  $first_item_id The i
47          * @param string  $last_item_id
48          * @param integer $itemsPerPage An optional number of items per page to override the default value
49          */
50         public function __construct($queryString, $first_item_id = null, $last_item_id = null, $itemsPerPage = 50)
51         {
52                 parent::__construct($queryString, $itemsPerPage);
53
54                 $this->first_item_id = $first_item_id;
55                 $this->last_item_id = $last_item_id;
56
57                 $parsed = parse_url($this->getBaseQueryString());
58                 if ($parsed) {
59                         parse_str($parsed['query'], $queryParameters);
60
61                         $this->first_page = !($queryParameters['since_id'] ?? null) && !($queryParameters['max_id'] ?? null);
62
63                         unset($queryParameters['since_id']);
64                         unset($queryParameters['max_id']);
65
66                         $parsed['query'] = http_build_query($queryParameters);
67
68                         $url = Network::unparseURL($parsed);
69
70                         $this->setQueryString($url);
71                 }
72         }
73
74         public function getStart()
75         {
76                 throw new \BadMethodCallException();
77         }
78
79         public function getPage()
80         {
81                 throw new \BadMethodCallException();
82         }
83
84         /**
85          * Minimal pager (newer/older)
86          *
87          * This mode is intended for reverse chronological pages and presents only two links, newer (previous) and older (next).
88          * The itemCount is the number of displayed items. If no items are displayed, the older button is disabled.
89          *
90          * Example usage:
91          *
92          * $params = ['order' => ['sort_field' => true], 'limit' => $itemsPerPage];
93          * $items = DBA::toArray(DBA::select($table, $fields, $condition, $params));
94          *
95          * $pager = new BoundariesPager($a->query_string, $items[0]['sort_field'], $items[coutn($items) - 1]['sort_field'], $itemsPerPage);
96          *
97          * $html = $pager->renderMinimal(count($items));
98          *
99          * @param int $itemCount The number of displayed items on the page
100          * @return string HTML string of the pager
101          * @throws \Exception
102          */
103         public function renderMinimal(int $itemCount)
104         {
105                 $displayedItemCount = max(0, intval($itemCount));
106
107                 $data = [
108                         'class' => 'pager',
109                         'prev'  => [
110                                 'url'   => Strings::ensureQueryParameter($this->baseQueryString .
111                                         ($this->first_item_id >= $this->last_item_id ?
112                                                 '&since_id=' . $this->first_item_id : '&max_id=' . $this->first_item_id)
113                                 ),
114                                 'text'  => DI::l10n()->t('newer'),
115                                 'class' => 'previous' . ($this->first_page ? ' disabled' : '')
116                         ],
117                         'next'  => [
118                                 'url'   => Strings::ensureQueryParameter($this->baseQueryString .
119                                         ($this->first_item_id >= $this->last_item_id ?
120                                         '&max_id=' . $this->last_item_id : '&since_id=' . $this->last_item_id)
121                                 ),
122                                 'text'  => DI::l10n()->t('older'),
123                                 'class' =>  'next' . ($displayedItemCount < $this->getItemsPerPage() ? ' disabled' : '')
124                         ]
125                 ];
126
127                 $tpl = Renderer::getMarkupTemplate('paginate.tpl');
128                 return Renderer::replaceMacros($tpl, ['pager' => $data]);
129         }
130
131         public function renderFull($itemCount)
132         {
133                 throw new \BadMethodCallException();
134         }
135 }