3 namespace Friendica\Content;
5 use Friendica\Core\L10n;
8 * The Pager has two very different output, Minimal and Full, see renderMinimal() and renderFull() for more details.
10 * @author Hypolite Petovan <mrpetovan@gmail.com>
21 private $itemsPerPage = 50;
26 private $baseQueryString = '';
29 * Instantiates a new Pager with the base parameters.
31 * Guesses the page number from the GET parameter 'page'.
33 * @param string $queryString The query string of the current page
34 * @param integer $itemsPerPage An optional number of items per page to override the default value
36 public function __construct($queryString, $itemsPerPage = 50)
38 $this->setQueryString($queryString);
39 $this->setItemsPerPage($itemsPerPage);
40 $this->setPage(defaults($_GET, 'page', 1));
44 * Returns the start offset for a LIMIT clause. Starts at 0.
48 public function getStart()
50 return max(0, ($this->page * $this->itemsPerPage) - $this->itemsPerPage);
54 * Returns the number of items per page
58 public function getItemsPerPage()
60 return $this->itemsPerPage;
64 * Returns the current page number
68 public function getPage()
74 * Returns the base query string.
76 * Warning: this isn't the same value as passed to the constructor.
77 * See setQueryString() for the inventory of transformations
82 public function getBaseQueryString()
84 return $this->baseQueryString;
88 * Sets the number of items per page, 1 minimum.
90 * @param integer $itemsPerPage
92 public function setItemsPerPage($itemsPerPage)
94 $this->itemsPerPage = max(1, intval($itemsPerPage));
98 * Sets the current page number. Starts at 1.
100 * @param integer $page
102 public function setPage($page)
104 $this->page = max(1, intval($page));
108 * Sets the base query string from a full query string.
110 * Strips the 'page' parameter, and remove the 'q=' string for some reason.
112 * @param string $queryString
114 public function setQueryString($queryString)
116 $stripped = preg_replace('/([&?]page=[0-9]*)/', '', $queryString);
118 $stripped = str_replace('q=', '', $stripped);
119 $stripped = trim($stripped, '/');
121 $this->baseQueryString = $stripped;
125 * Ensures the provided URI has its query string punctuation in order.
130 private function ensureQueryParameter($uri)
132 if (strpos($uri, '?') === false && ($pos = strpos($uri, '&')) !== false) {
133 $uri = substr($uri, 0, $pos) . '?' . substr($uri, $pos + 1);
140 * @brief Minimal pager (newer/older)
142 * This mode is intended for reverse chronological pages and presents only two links, newer (previous) and older (next).
143 * The itemCount is the number of displayed items. If no items are displayed, the older button is disabled.
147 * $pager = new Pager($a->query_string);
149 * $params = ['order' => ['sort_field' => true], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
150 * $items = DBA::toArray(DBA::select($table, $fields, $condition, $params));
152 * $html = $pager->renderMinimal(count($items));
154 * @param integer $itemCount The number of displayed items on the page
155 * @return string HTML string of the pager
157 public function renderMinimal($itemCount)
159 $displayedItemCount = max(0, intval($itemCount));
164 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)),
165 'text' => L10n::t('newer'),
166 'class' => 'previous' . ($this->getPage() == 1 ? ' disabled' : '')
169 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)),
170 'text' => L10n::t('older'),
171 'class' => 'next' . ($displayedItemCount <= 0 ? ' disabled' : '')
175 $tpl = get_markup_template('paginate.tpl');
176 return replace_macros($tpl, ['pager' => $data]);
180 * @brief Full pager (first / prev / 1 / 2 / ... / 14 / 15 / next / last)
182 * This mode presents page numbers as well as first, previous, next and last links.
183 * The itemCount is the total number of items including those not displayed.
187 * $total = DBA::count($table, $condition);
189 * $pager = new Pager($a->query_string, $total);
191 * $params = ['limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
192 * $items = DBA::toArray(DBA::select($table, $fields, $condition, $params));
194 * $html = $pager->renderFull();
196 * @param integer $itemCount The total number of items including those note displayed on the page
197 * @return string HTML string of the pager
199 public function renderFull($itemCount)
201 $totalItemCount = max(0, intval($itemCount));
205 $data['class'] = 'pagination';
206 if ($totalItemCount > $this->getItemsPerPage()) {
208 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=1'),
209 'text' => L10n::t('first'),
210 'class' => $this->getPage() == 1 ? 'disabled' : ''
213 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() - 1)),
214 'text' => L10n::t('prev'),
215 'class' => $this->getPage() == 1 ? 'disabled' : ''
218 $numpages = $totalItemCount / $this->getItemsPerPage();
221 $numstop = $numpages;
223 // Limit the number of displayed page number buttons.
225 $numstart = (($this->getPage() > 4) ? ($this->getPage() - 4) : 1);
226 $numstop = (($this->getPage() > ($numpages - 7)) ? $numpages : ($numstart + 8));
231 for ($i = $numstart; $i <= $numstop; $i++) {
232 if ($i == $this->getPage()) {
236 'class' => 'current active'
240 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . $i),
247 if (($totalItemCount % $this->getItemsPerPage()) != 0) {
248 if ($i == $this->getPage()) {
252 'class' => 'current active'
256 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . $i),
263 $data['pages'] = $pages;
265 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
268 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)),
269 'text' => L10n::t('next'),
270 'class' => $this->getPage() == $lastpage ? 'disabled' : ''
273 'url' => $this->ensureQueryParameter($this->baseQueryString . '&page=' . $lastpage),
274 'text' => L10n::t('last'),
275 'class' => $this->getPage() == $lastpage ? 'disabled' : ''
279 $tpl = get_markup_template('paginate.tpl');
280 return replace_macros($tpl, ['pager' => $data]);