Classes

File util/pagination/Pagination.class.php

File util/pagination/Pagination.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 
<?php
/**
 * @package     Util
 * @subpackage  Pagination
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Kevin MASSY <reidlos@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2017 05 31
 * @since       PHPBoost 3.0 - 2009 12 22
 * @contributor Arnaud GENET <elenwii@phpboost.com>
*/

class Pagination
{
    const LINKS_NB = 3;
    const PREV_LINK = 'prev-page';
    const NEXT_LINK = 'next-page';

    const LIGHT_PAGINATION = 'light';
    const FULL_PAGINATION = 'full';

    private $tpl;
    private $nb_pages;
    private $current_page;
    private $url_pattern;
    private $url_builder_callback;
    private $before_links_nb = self::LINKS_NB;
    private $after_links_nb = self::LINKS_NB;

    public function __construct($nb_pages, $current_page, $type = self::FULL_PAGINATION)
    {
        $this->nb_pages = $nb_pages;
        $this->current_page = $current_page;
        $this->init_tpl($type);
    }

    public function set_url_sprintf_pattern($url_pattern)
    {
        $this->url_pattern = $url_pattern;
    }

    public function set_url_builder_callback($callback)
    {
        $this->url_builder_callback = $callback;
    }

    public function set_before_links_nb($value)
    {
        $this->before_links_nb = $value;
    }

    public function set_after_links_nb($value)
    {
        $this->after_links_nb = $value;
    }

    public function export()
    {
        $this->generate_first_page_pagination();
        $this->generate_near_pages_pagination();
        $this->generate_last_page_pagination();
        return $this->tpl;
    }

    public function get_number_pages()
    {
        return $this->nb_pages;
    }

    private function init_tpl($type)
    {
        $this->tpl = new FileTemplate('framework/util/pagination.tpl');
        $this->tpl->put_all(array(
            'C_LIGHT' => $type == self::LIGHT_PAGINATION,
            'C_FULL' => $type == self::FULL_PAGINATION,
            'L_PREVIOUS_PAGE' => LangLoader::get_message('pagination.previous', 'common'),
            'L_NEXT_PAGE'     => LangLoader::get_message('pagination.next', 'common')
        ));
    }

    private function generate_first_page_pagination()
    {
        if ($this->current_page > 1)
        {
            $this->add_pagination_page(self::PREV_LINK, 1);
        }
    }

    private function generate_near_pages_pagination()
    {
        $start = $this->current_page - $this->before_links_nb;
        $end = $this->current_page + $this->after_links_nb;
        for ($i = $start; $i <= $end; $i++)
        {
            if ($i >= 1 && $i <= $this->nb_pages)
            {
                $is_current_page = $i == $this->current_page;
                $this->add_pagination_page($i, $i, $is_current_page);
            }
        }
    }

    private function generate_last_page_pagination()
    {
        if ($this->current_page < $this->nb_pages)
        {
            $this->add_pagination_page(self::NEXT_LINK, $this->nb_pages);
        }
    }

    private function add_pagination_page($name, $page_number, $is_current_page = false)
    {
        $this->tpl->assign_block_vars('page', array(
            'URL'             => $this->get_url($page_number),
            'NAME'            => $name == self::PREV_LINK || $name == self::NEXT_LINK ? '' : $name,
            'C_CURRENT_PAGE'  => $is_current_page,
            'L_PAGE'          => $is_current_page ? LangLoader::get_message('pagination.current', 'common') : LangLoader::get_message('pagination.page', 'common') . " " . $page_number,
            'C_PREVIOUS'      => $name == self::PREV_LINK,
            'C_NEXT'          => $name == self::NEXT_LINK
        ));
    }

    private function get_url($page_number)
    {
        if (!empty($this->url_pattern))
        {
            return sprintf($this->url_pattern, $page_number);
        }
        else if (!empty($this->url_builder_callback))
        {
            return call_user_func($this->url_builder_callback, $page_number);
        }
        throw new Exception('No url builder mode defined for pagination links');
    }
}
?>