Skip to content

Commit 6b6d7e6

Browse files
committed
Merge branch 'master' of github.com:tigron/skeleton-pager
2 parents 8604eea + cbf8cd8 commit 6b6d7e6

File tree

8 files changed

+168
-8
lines changed

8 files changed

+168
-8
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ Installation via composer:
4242
*/
4343
\Skeleton\Pager\Config::$items_per_page = 20;
4444

45+
/**
46+
* Per page list
47+
*/
48+
\Skeleton\Pager\Config::$per_page_list = [20, 50, 100];
49+
4550
/**
4651
* Sticky pager
4752
*

lib/Skeleton/Pager/Config.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ class Config {
1919
*/
2020
public static $items_per_page = 20;
2121

22+
/**
23+
* Per page list
24+
*
25+
* @access public
26+
* @var array $per_page_list
27+
*/
28+
public static $per_page_list = [20, 50, 100];
29+
2230
/**
2331
* Sticky pager
2432
*
@@ -49,6 +57,16 @@ class Config {
4957
*/
5058
public static $links_template = '@skeleton-pager\bootstrap3\links.twig';
5159

60+
/**
61+
* Per page switch template
62+
*
63+
* Set the template to render the per page switch
64+
*
65+
* @access public
66+
* @var string $per_page_template
67+
*/
68+
public static $per_page_template = '@skeleton-pager\bootstrap3\per_page.twig';
69+
5270
/**
5371
* header template
5472
*

lib/Skeleton/Pager/Page.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ trait Page {
2626
* @param array $extra_conditions
2727
* @param array $extra_joins
2828
*/
29-
public static function get_paged($sort = null, $direction = 'asc', $page = 1, $extra_conditions = [], $all = false, $extra_joins = []) {
29+
public static function get_paged($sort = null, $direction = 'asc', $page = 1, $extra_conditions = [], $all = false, $extra_joins = [], $per_page = null) {
3030
$db = self::trait_get_database();
3131
$table = self::trait_get_database_table();
3232
$field_id = self::trait_get_table_field_id();
@@ -73,7 +73,11 @@ public static function get_paged($sort = null, $direction = 'asc', $page = 1, $e
7373
}
7474

7575
if (!$all) {
76-
$limit = Config::$items_per_page;
76+
if ($per_page === null) {
77+
$limit = Config::$items_per_page;
78+
} else {
79+
$limit = $per_page;
80+
}
7781
} else {
7882
$limit = 1000;
7983
}

lib/Skeleton/Pager/Pager.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public function __construct($classname = null) {
7777

7878
$this->classname = $classname;
7979
$this->set_jump_to(Config::$jump_to);
80+
$this->set_per_page(Config::$items_per_page);
8081
}
8182

8283
/**
@@ -307,6 +308,26 @@ public function get_jump_to() {
307308
return $this->options['jump_to'];
308309
}
309310

311+
/**
312+
* Set 'per page items'
313+
*
314+
* @access public
315+
* @param int $per_page
316+
*/
317+
public function set_per_page($per_page) {
318+
$this->options['per_page'] = $per_page;
319+
}
320+
321+
/**
322+
* Get per page
323+
*
324+
* @access public
325+
* @return bool $jump_to
326+
*/
327+
public function get_per_page() {
328+
return $this->options['per_page'];
329+
}
330+
310331
/**
311332
* Set a search
312333
*
@@ -406,6 +427,7 @@ protected static function get_options_from_hash($hash) {
406427
'joins' => [],
407428
'sort_permissions' => [],
408429
'classname' => null,
430+
'per_page' => Config::$items_per_page,
409431
];
410432
}
411433

@@ -445,7 +467,7 @@ protected static function get_options_from_hash($hash) {
445467
* @param int $sort
446468
* @param string $direction
447469
*/
448-
public function create_options_hash($conditions = null, $page = null, $sort = null, $direction = null) {
470+
public function create_options_hash($conditions = null, $page = null, $sort = null, $direction = null, $joins = null, $per_page = null) {
449471
if ($conditions === null) {
450472
$conditions = $this->options['conditions'];
451473
}
@@ -462,6 +484,10 @@ public function create_options_hash($conditions = null, $page = null, $sort = nu
462484
$direction = $this->options['direction'];
463485
}
464486

487+
if ($per_page === null) {
488+
$per_page = $this->options['per_page'];
489+
}
490+
465491
if (is_array($conditions)) {
466492
$flat_conditions = [];
467493

@@ -499,6 +525,7 @@ public function create_options_hash($conditions = null, $page = null, $sort = nu
499525
'sort' => $sort,
500526
'direction' => $direction,
501527
'joins' => $joins,
528+
'per_page' => $per_page,
502529
];
503530

504531
$data = json_encode($options);
@@ -550,7 +577,8 @@ public function page($all = false) {
550577
$this->options['page'],
551578
$this->options['conditions'],
552579
$this->options['all'],
553-
$this->options['joins']
580+
$this->options['joins'],
581+
$this->options['per_page']
554582
];
555583

556584
$this->items = call_user_func_array([$this->classname, 'get_paged'], $params);

lib/Skeleton/Pager/Web/Pager.php

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ class Pager extends \Skeleton\Pager\Pager {
2323
*/
2424
public $links;
2525

26+
/**
27+
* Per page link: a html string with per page links
28+
*
29+
* @access public
30+
* @var string $per_page_links
31+
*/
32+
public $per_page_links;
33+
2634
/**
2735
* Create the header cells of the paged table
2836
*
@@ -91,6 +99,7 @@ public function page($all = false) {
9199

92100
parent::page($all);
93101
$this->generate_links();
102+
$this->generate_per_page_links();
94103
$hash = $this->create_options_hash($this->options['conditions'], $this->options['page'], $this->options['sort'], $this->options['direction'], $this->options['joins']);
95104

96105
if (Config::$sticky_pager) {
@@ -104,7 +113,7 @@ public function page($all = false) {
104113
* @access private
105114
*/
106115
private function generate_links() {
107-
$items_per_page = Config::$items_per_page;
116+
$items_per_page = $this->options['per_page'];
108117
if ($items_per_page == 0) {
109118
$pages = 0;
110119
} else {
@@ -117,7 +126,6 @@ private function generate_links() {
117126
return;
118127
}
119128

120-
$str_links = '';
121129
$links = [];
122130

123131
$links[] = [
@@ -164,8 +172,6 @@ private function generate_links() {
164172
$links[$key] = $link;
165173
}
166174

167-
168-
169175
$qry_str = '';
170176
if (isset($_SERVER['QUERY_STRING'])) {
171177
$qry_str = $_SERVER['QUERY_STRING'];
@@ -208,6 +214,64 @@ private function generate_links() {
208214
$this->links = $output;
209215
}
210216

217+
/**
218+
* Generate the necessary per page links to change the amount of items per page
219+
*
220+
* @access private
221+
*/
222+
private function generate_per_page_links() {
223+
$items_per_page = $this->options['per_page'];
224+
if ($items_per_page == 0) {
225+
$pages = 0;
226+
} else {
227+
$pages = ceil($this->item_count / $items_per_page);
228+
}
229+
230+
// Don't make links if there is only one page and the items per page is 20
231+
if ($pages == 1 && $items_per_page === 20) {
232+
$this->per_page_links = '';
233+
return;
234+
}
235+
236+
$qry_str = '';
237+
if (isset($_SERVER['QUERY_STRING'])) {
238+
$qry_str = $_SERVER['QUERY_STRING'];
239+
}
240+
parse_str($qry_str, $qry_str_parts);
241+
242+
// list of items per page
243+
$per_page_links = [];
244+
foreach (Config::$per_page_list as $per_page) {
245+
$hash = $this->create_options_hash(
246+
$this->options['conditions'],
247+
1,
248+
$this->options['sort'],
249+
$this->options['direction'],
250+
$this->options['joins'],
251+
$per_page
252+
);
253+
254+
$qry_str_parts['q'] = $hash;
255+
if (isset($qry_str_parts['per_page']) === true) {
256+
unset($qry_str_parts['per_page']);
257+
}
258+
259+
$url = self::find_page_uri() . '?' . http_build_query($qry_str_parts);
260+
$per_page_links[] = [
261+
'per_page' => $per_page,
262+
'url' => $url,
263+
'hash' => $hash
264+
];
265+
}
266+
267+
$template = \Skeleton\Application\Web\Template::get();
268+
$template->assign('per_page_links', $per_page_links);
269+
$template->assign('classname', $this->classname);
270+
$template->assign('options', $this->options);
271+
$output = $template->render(\Skeleton\Pager\Config::$per_page_template, false);
272+
$this->per_page_links = $output;
273+
}
274+
211275
/**
212276
* Clear conditions
213277
*

template/bootstrap3/per_page.twig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<div class="dropdown per-page">
2+
<button class="btn dropdown-toggle" type="button" id="per-page-{{ classname|replace({'_': '-'}) }}" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
3+
{{ options.per_page }}
4+
<span class="caret"></span>
5+
</button>
6+
<ul class="dropdown-menu" aria-labelledby="per-page-{{ classname|replace({'_': '-'}) }}">
7+
{% for per_page_link in per_page_links %}
8+
<li{% if per_page_link.per_page == options.per_page %} class="disabled"{% endif %}>
9+
<a href="{{ per_page_link.url }}">
10+
{{ per_page_link.per_page }}
11+
</a>
12+
</li>
13+
{% endfor %}
14+
</ul>
15+
</div>

template/bootstrap4/per_page.twig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<div class="dropdown per-page">
2+
<button class="btn dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false">
3+
{{ options.per_page }}
4+
</button>
5+
<div class="dropdown-menu">
6+
{% for per_page_link in per_page_links %}
7+
<a class="dropdown-item" {% if per_page_link.per_page == options.per_page %}disabled{% endif %} href="{{ per_page_link.url }}">
8+
{{ per_page_link.per_page }}
9+
</a>
10+
{% endfor %}
11+
</div>
12+
</div>

template/bootstrap5/per_page.twig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<div class="dropdown per-page">
2+
<button class="btn dropdown-toggle" type="button" id="per-page-{{ classname|replace({'_': '-'}) }}" data-bs-toggle="dropdown" aria-expanded="false">
3+
{{ options.per_page }}
4+
</button>
5+
<ul class="dropdown-menu" aria-labelledby="per-page-{{ classname|replace({'_': '-'}) }}">
6+
{% for per_page_link in per_page_links %}
7+
<li>
8+
<a class="dropdown-item{% if per_page_link.per_page == options.per_page %} disabled{% endif %}" href="{{ per_page_link.url }}">
9+
{{ per_page_link.per_page }}
10+
</a>
11+
</li>
12+
{% endfor %}
13+
</ul>
14+
</div>

0 commit comments

Comments
 (0)