Skip to content

Commit

Permalink
Merge branch 'master' of github.com:tigron/skeleton-pager
Browse files Browse the repository at this point in the history
  • Loading branch information
kvassia committed May 24, 2024
2 parents 8604eea + cbf8cd8 commit 6b6d7e6
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 8 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ Installation via composer:
*/
\Skeleton\Pager\Config::$items_per_page = 20;

/**
* Per page list
*/
\Skeleton\Pager\Config::$per_page_list = [20, 50, 100];

/**
* Sticky pager
*
Expand Down
18 changes: 18 additions & 0 deletions lib/Skeleton/Pager/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ class Config {
*/
public static $items_per_page = 20;

/**
* Per page list
*
* @access public
* @var array $per_page_list
*/
public static $per_page_list = [20, 50, 100];

/**
* Sticky pager
*
Expand Down Expand Up @@ -49,6 +57,16 @@ class Config {
*/
public static $links_template = '@skeleton-pager\bootstrap3\links.twig';

/**
* Per page switch template
*
* Set the template to render the per page switch
*
* @access public
* @var string $per_page_template
*/
public static $per_page_template = '@skeleton-pager\bootstrap3\per_page.twig';

/**
* header template
*
Expand Down
8 changes: 6 additions & 2 deletions lib/Skeleton/Pager/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ trait Page {
* @param array $extra_conditions
* @param array $extra_joins
*/
public static function get_paged($sort = null, $direction = 'asc', $page = 1, $extra_conditions = [], $all = false, $extra_joins = []) {
public static function get_paged($sort = null, $direction = 'asc', $page = 1, $extra_conditions = [], $all = false, $extra_joins = [], $per_page = null) {
$db = self::trait_get_database();
$table = self::trait_get_database_table();
$field_id = self::trait_get_table_field_id();
Expand Down Expand Up @@ -73,7 +73,11 @@ public static function get_paged($sort = null, $direction = 'asc', $page = 1, $e
}

if (!$all) {
$limit = Config::$items_per_page;
if ($per_page === null) {
$limit = Config::$items_per_page;
} else {
$limit = $per_page;
}
} else {
$limit = 1000;
}
Expand Down
32 changes: 30 additions & 2 deletions lib/Skeleton/Pager/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public function __construct($classname = null) {

$this->classname = $classname;
$this->set_jump_to(Config::$jump_to);
$this->set_per_page(Config::$items_per_page);
}

/**
Expand Down Expand Up @@ -307,6 +308,26 @@ public function get_jump_to() {
return $this->options['jump_to'];
}

/**
* Set 'per page items'
*
* @access public
* @param int $per_page
*/
public function set_per_page($per_page) {
$this->options['per_page'] = $per_page;
}

/**
* Get per page
*
* @access public
* @return bool $jump_to
*/
public function get_per_page() {
return $this->options['per_page'];
}

/**
* Set a search
*
Expand Down Expand Up @@ -406,6 +427,7 @@ protected static function get_options_from_hash($hash) {
'joins' => [],
'sort_permissions' => [],
'classname' => null,
'per_page' => Config::$items_per_page,
];
}

Expand Down Expand Up @@ -445,7 +467,7 @@ protected static function get_options_from_hash($hash) {
* @param int $sort
* @param string $direction
*/
public function create_options_hash($conditions = null, $page = null, $sort = null, $direction = null) {
public function create_options_hash($conditions = null, $page = null, $sort = null, $direction = null, $joins = null, $per_page = null) {
if ($conditions === null) {
$conditions = $this->options['conditions'];
}
Expand All @@ -462,6 +484,10 @@ public function create_options_hash($conditions = null, $page = null, $sort = nu
$direction = $this->options['direction'];
}

if ($per_page === null) {
$per_page = $this->options['per_page'];
}

if (is_array($conditions)) {
$flat_conditions = [];

Expand Down Expand Up @@ -499,6 +525,7 @@ public function create_options_hash($conditions = null, $page = null, $sort = nu
'sort' => $sort,
'direction' => $direction,
'joins' => $joins,
'per_page' => $per_page,
];

$data = json_encode($options);
Expand Down Expand Up @@ -550,7 +577,8 @@ public function page($all = false) {
$this->options['page'],
$this->options['conditions'],
$this->options['all'],
$this->options['joins']
$this->options['joins'],
$this->options['per_page']
];

$this->items = call_user_func_array([$this->classname, 'get_paged'], $params);
Expand Down
72 changes: 68 additions & 4 deletions lib/Skeleton/Pager/Web/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ class Pager extends \Skeleton\Pager\Pager {
*/
public $links;

/**
* Per page link: a html string with per page links
*
* @access public
* @var string $per_page_links
*/
public $per_page_links;

/**
* Create the header cells of the paged table
*
Expand Down Expand Up @@ -91,6 +99,7 @@ public function page($all = false) {

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

if (Config::$sticky_pager) {
Expand All @@ -104,7 +113,7 @@ public function page($all = false) {
* @access private
*/
private function generate_links() {
$items_per_page = Config::$items_per_page;
$items_per_page = $this->options['per_page'];
if ($items_per_page == 0) {
$pages = 0;
} else {
Expand All @@ -117,7 +126,6 @@ private function generate_links() {
return;
}

$str_links = '';
$links = [];

$links[] = [
Expand Down Expand Up @@ -164,8 +172,6 @@ private function generate_links() {
$links[$key] = $link;
}



$qry_str = '';
if (isset($_SERVER['QUERY_STRING'])) {
$qry_str = $_SERVER['QUERY_STRING'];
Expand Down Expand Up @@ -208,6 +214,64 @@ private function generate_links() {
$this->links = $output;
}

/**
* Generate the necessary per page links to change the amount of items per page
*
* @access private
*/
private function generate_per_page_links() {
$items_per_page = $this->options['per_page'];
if ($items_per_page == 0) {
$pages = 0;
} else {
$pages = ceil($this->item_count / $items_per_page);
}

// Don't make links if there is only one page and the items per page is 20
if ($pages == 1 && $items_per_page === 20) {
$this->per_page_links = '';
return;
}

$qry_str = '';
if (isset($_SERVER['QUERY_STRING'])) {
$qry_str = $_SERVER['QUERY_STRING'];
}
parse_str($qry_str, $qry_str_parts);

// list of items per page
$per_page_links = [];
foreach (Config::$per_page_list as $per_page) {
$hash = $this->create_options_hash(
$this->options['conditions'],
1,
$this->options['sort'],
$this->options['direction'],
$this->options['joins'],
$per_page
);

$qry_str_parts['q'] = $hash;
if (isset($qry_str_parts['per_page']) === true) {
unset($qry_str_parts['per_page']);
}

$url = self::find_page_uri() . '?' . http_build_query($qry_str_parts);
$per_page_links[] = [
'per_page' => $per_page,
'url' => $url,
'hash' => $hash
];
}

$template = \Skeleton\Application\Web\Template::get();
$template->assign('per_page_links', $per_page_links);
$template->assign('classname', $this->classname);
$template->assign('options', $this->options);
$output = $template->render(\Skeleton\Pager\Config::$per_page_template, false);
$this->per_page_links = $output;
}

/**
* Clear conditions
*
Expand Down
15 changes: 15 additions & 0 deletions template/bootstrap3/per_page.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div class="dropdown per-page">
<button class="btn dropdown-toggle" type="button" id="per-page-{{ classname|replace({'_': '-'}) }}" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
{{ options.per_page }}
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="per-page-{{ classname|replace({'_': '-'}) }}">
{% for per_page_link in per_page_links %}
<li{% if per_page_link.per_page == options.per_page %} class="disabled"{% endif %}>
<a href="{{ per_page_link.url }}">
{{ per_page_link.per_page }}
</a>
</li>
{% endfor %}
</ul>
</div>
12 changes: 12 additions & 0 deletions template/bootstrap4/per_page.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="dropdown per-page">
<button class="btn dropdown-toggle" type="button" data-toggle="dropdown" aria-expanded="false">
{{ options.per_page }}
</button>
<div class="dropdown-menu">
{% for per_page_link in per_page_links %}
<a class="dropdown-item" {% if per_page_link.per_page == options.per_page %}disabled{% endif %} href="{{ per_page_link.url }}">
{{ per_page_link.per_page }}
</a>
{% endfor %}
</div>
</div>
14 changes: 14 additions & 0 deletions template/bootstrap5/per_page.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div class="dropdown per-page">
<button class="btn dropdown-toggle" type="button" id="per-page-{{ classname|replace({'_': '-'}) }}" data-bs-toggle="dropdown" aria-expanded="false">
{{ options.per_page }}
</button>
<ul class="dropdown-menu" aria-labelledby="per-page-{{ classname|replace({'_': '-'}) }}">
{% for per_page_link in per_page_links %}
<li>
<a class="dropdown-item{% if per_page_link.per_page == options.per_page %} disabled{% endif %}" href="{{ per_page_link.url }}">
{{ per_page_link.per_page }}
</a>
</li>
{% endfor %}
</ul>
</div>

0 comments on commit 6b6d7e6

Please sign in to comment.