3 namespace Friendica\Content;
5 use Friendica\Core\L10n;
6 use Friendica\Core\Renderer;
9 * The Pager has two very different output, Minimal and Full, see renderMinimal() and renderFull() for more details.
11 * @author Hypolite Petovan <mrpetovan@gmail.com>
22 private $itemsPerPage = 50;
27 private $baseQueryString = '';
30 * Instantiates a new Pager with the base parameters.
32 * Guesses the page number from the GET parameter 'page'.
34 * @param string $queryString The query string of the current page
35 * @param integer $itemsPerPage An optional number of items per page to override the default value
37 public function __construct($queryString, $itemsPerPage = 50)
39 $this->setQueryString($queryString);
40 $this->setItemsPerPage($itemsPerPage);
41 $this->setPage(defaults($_GET, 'page', 1));
45 * Returns the start offset for a LIMIT clause. Starts at 0.
49 public function getStart()
51 return max(0, ($this->page * $this->itemsPerPage) - $this->itemsPerPage);
55 * Returns the number of items per page
59 public function getItemsPerPage()
61 return $this->itemsPerPage;
65 * Returns the current page number
69 public function getPage()
75 * Returns the base query string.
77 * Warning: this isn't the same value as passed to the constructor.
78 * See setQueryString() for the inventory of transformations
83 public function getBaseQueryString()
85 return $this->baseQueryString;
89 * Sets the number of items per page, 1 minimum.
91 * @param integer $itemsPerPage
93 public function setItemsPerPage($itemsPerPage)
95 $this->itemsPerPage = max(1, intval($itemsPerPage));
99 * Sets the current page number. Starts at 1.
101 * @param integer $page
103 public function setPage($page)
105 $this->page = max(1, intval($page));
109 * Sets the base query string from a full query string.
111 * Strips the 'page' parameter, and remove the 'q=' string for some reason.
113 * @param string $queryString
115 public function setQueryString($queryString)
117 $stripped = preg_replace('/([&?]page=[0-9]*)/', '', $queryString);
119 $stripped = str_replace('q=', '', $stripped);
120 $stripped = trim($stripped, '/');
122 $this->baseQueryString = $stripped;
126 * Ensures the provided URI has its query string punctuation in order.
131 private function ensureQueryParameter($uri)
133 if (strpos($uri, '?') === false && ($pos = strpos($uri, '&')) !== false) {
134 $uri = substr($uri, 0, $pos) . '?' . substr($uri, $pos + 1);
141 * @brief Minimal pager (newer/older)
143 * This mode is intended for reverse chronological pages and presents only two links, newer (previous) and older (next).
144 * The itemCount is the number of displayed items. If no items are displayed, the older button is disabled.
148 * $pager = new Pager($a->query_string);
150 * $params = ['order' => ['sort_field' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
151 * $items = DBA::toArray(DBA::select($table, $fields, $condition, $params));
153 * $html = $pager->renderMinimal(count($items));
155 * @param integer $itemCount The number of displayed items on the page
156 * @return string HTML string of the pager
158 public function renderMinimal($itemCount)
160 $displayedItemCount = max(0, intval($itemCount));
165 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)),
166 'text' => L10n::t('newer'),
167 'class' => 'previous' . ($this->getPage() == 1 ? ' disabled' : '')
170 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)),
171 'text' => L10n::t('older'),
172 'class' => 'next' . ($displayedItemCount <= 0 ? ' disabled' : '')
176 $tpl = Renderer::getMarkupTemplate('paginate.tpl');
177 return Renderer::replaceMacros($tpl, ['pager' => $data]);
181 * @brief Full pager (first / prev / 1 / 2 / ... / 14 / 15 / next / last)
183 * This mode presents page numbers as well as first, previous, next and last links.
184 * The itemCount is the total number of items including those not displayed.
188 * $total = DBA::count($table, $condition);
190 * $pager = new Pager($a->query_string, $total);
192 * $params = ['limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
193 * $items = DBA::toArray(DBA::select($table, $fields, $condition, $params));
195 * $html = $pager->renderFull();
197 * @param integer $itemCount The total number of items including those note displayed on the page
198 * @return string HTML string of the pager
200 public function renderFull($itemCount)
202 $totalItemCount = max(0, intval($itemCount));
206 $data['class'] = 'pagination';
207 if ($totalItemCount > $this->getItemsPerPage()) {
209 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=1'),
210 'text' => L10n::t('first'),
211 'class' => $this->getPage() == 1 ? 'disabled' : ''
214 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)),
215 'text' => L10n::t('prev'),
216 'class' => $this->getPage() == 1 ? 'disabled' : ''
219 $numpages = $totalItemCount / $this->getItemsPerPage();
222 $numstop = $numpages;
224 // Limit the number of displayed page number buttons.
226 $numstart = (($this->getPage() > 4) ? ($this->getPage() - 4) : 1);
227 $numstop = (($this->getPage() > ($numpages - 7)) ? $numpages : ($numstart + 8));
232 for ($i = $numstart; $i <= $numstop; $i++) {
233 if ($i == $this->getPage()) {
237 'class' => 'current active'
241 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . $i),
248 if (($totalItemCount % $this->getItemsPerPage()) != 0) {
249 if ($i == $this->getPage()) {
253 'class' => 'current active'
257 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . $i),
264 $data['pages'] = $pages;
266 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
269 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)),
270 'text' => L10n::t('next'),
271 'class' => $this->getPage() == $lastpage ? 'disabled' : ''
274 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . $lastpage),
275 'text' => L10n::t('last'),
276 'class' => $this->getPage() == $lastpage ? 'disabled' : ''
280 $tpl = Renderer::getMarkupTemplate('paginate.tpl');
281 return Renderer::replaceMacros($tpl, ['pager' => $data]);