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