3 namespace Friendica\Content;
5 use Friendica\Core\L10n;
6 use Friendica\Core\Renderer;
7 use Friendica\Util\Strings;
10 * The Pager has two very different output, Minimal and Full, see renderMinimal() and renderFull() for more details.
12 * @author Hypolite Petovan <mrpetovan@gmail.com>
23 private $itemsPerPage = 50;
28 private $baseQueryString = '';
31 * Instantiates a new Pager with the base parameters.
33 * Guesses the page number from the GET parameter 'page'.
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
38 public function __construct($queryString, $itemsPerPage = 50)
40 $this->setQueryString($queryString);
41 $this->setItemsPerPage($itemsPerPage);
42 $this->setPage(($_GET['page'] ?? 0) ?: 1);
46 * Returns the start offset for a LIMIT clause. Starts at 0.
50 public function getStart()
52 return max(0, ($this->page * $this->itemsPerPage) - $this->itemsPerPage);
56 * Returns the number of items per page
60 public function getItemsPerPage()
62 return $this->itemsPerPage;
66 * Returns the current page number
70 public function getPage()
76 * Returns the base query string.
78 * Warning: this isn't the same value as passed to the constructor.
79 * See setQueryString() for the inventory of transformations
84 public function getBaseQueryString()
86 return Strings::ensureQueryParameter($this->baseQueryString);
90 * Sets the number of items per page, 1 minimum.
92 * @param integer $itemsPerPage
94 public function setItemsPerPage($itemsPerPage)
96 $this->itemsPerPage = max(1, intval($itemsPerPage));
100 * Sets the current page number. Starts at 1.
102 * @param integer $page
104 public function setPage($page)
106 $this->page = max(1, intval($page));
110 * Sets the base query string from a full query string.
112 * Strips the 'page' parameter, and remove the 'q=' string for some reason.
114 * @param string $queryString
116 public function setQueryString($queryString)
118 $stripped = preg_replace('/([&?]page=[0-9]*)/', '', $queryString);
120 $stripped = str_replace('q=', '', $stripped);
121 $stripped = trim($stripped, '/');
123 $this->baseQueryString = $stripped;
127 * @brief Minimal pager (newer/older)
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.
134 * $pager = new Pager($a->query_string);
136 * $params = ['order' => ['sort_field' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
137 * $items = DBA::toArray(DBA::select($table, $fields, $condition, $params));
139 * $html = $pager->renderMinimal(count($items));
141 * @param integer $itemCount The number of displayed items on the page
142 * @return string HTML string of the pager
143 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
145 public function renderMinimal($itemCount)
147 $displayedItemCount = max(0, intval($itemCount));
152 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)),
153 'text' => L10n::t('newer'),
154 'class' => 'previous' . ($this->getPage() == 1 ? ' disabled' : '')
157 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)),
158 'text' => L10n::t('older'),
159 'class' => 'next' . ($displayedItemCount < $this->getItemsPerPage() ? ' disabled' : '')
163 $tpl = Renderer::getMarkupTemplate('paginate.tpl');
164 return Renderer::replaceMacros($tpl, ['pager' => $data]);
168 * @brief Full pager (first / prev / 1 / 2 / ... / 14 / 15 / next / last)
170 * This mode presents page numbers as well as first, previous, next and last links.
171 * The itemCount is the total number of items including those not displayed.
175 * $total = DBA::count($table, $condition);
177 * $pager = new Pager($a->query_string, $total);
179 * $params = ['limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
180 * $items = DBA::toArray(DBA::select($table, $fields, $condition, $params));
182 * $html = $pager->renderFull();
184 * @param integer $itemCount The total number of items including those note displayed on the page
185 * @return string HTML string of the pager
186 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
188 public function renderFull($itemCount)
190 $totalItemCount = max(0, intval($itemCount));
194 $data['class'] = 'pagination';
195 if ($totalItemCount > $this->getItemsPerPage()) {
197 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=1'),
198 'text' => L10n::t('first'),
199 'class' => $this->getPage() == 1 ? 'disabled' : ''
202 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)),
203 'text' => L10n::t('prev'),
204 'class' => $this->getPage() == 1 ? 'disabled' : ''
207 $numpages = $totalItemCount / $this->getItemsPerPage();
210 $numstop = $numpages;
212 // Limit the number of displayed page number buttons.
214 $numstart = (($this->getPage() > 4) ? ($this->getPage() - 4) : 1);
215 $numstop = (($this->getPage() > ($numpages - 7)) ? $numpages : ($numstart + 8));
220 for ($i = $numstart; $i <= $numstop; $i++) {
221 if ($i == $this->getPage()) {
225 'class' => 'current active'
229 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $i),
236 if (($totalItemCount % $this->getItemsPerPage()) != 0) {
237 if ($i == $this->getPage()) {
241 'class' => 'current active'
245 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $i),
252 $data['pages'] = $pages;
254 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
257 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)),
258 'text' => L10n::t('next'),
259 'class' => $this->getPage() == $lastpage ? 'disabled' : ''
262 'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $lastpage),
263 'text' => L10n::t('last'),
264 'class' => $this->getPage() == $lastpage ? 'disabled' : ''
268 $tpl = Renderer::getMarkupTemplate('paginate.tpl');
269 return Renderer::replaceMacros($tpl, ['pager' => $data]);