Advertisement
Guest User

Bootstrap Friendly FuelPHP Pagination

a guest
Dec 15th, 2011
1,585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.73 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Part of the Fuel framework.
  4.  *
  5.  * @package     Fuel
  6.  * @version     1.0
  7.  * @author      Dan Horrigan <http://dhorrigan.com>
  8.  * @license     MIT License
  9.  * @copyright   2010 - 2011 Fuel Development Team
  10.  */
  11.  
  12. namespace Fuel\Core;
  13.  
  14.  
  15.  
  16. class Pagination
  17. {
  18.  
  19.     /**
  20.      * @var integer The current page
  21.      */
  22.     public static $current_page = null;
  23.  
  24.     /**
  25.      * @var integer The offset that the current page starts at
  26.      */
  27.     public static $offset = 0;
  28.  
  29.     /**
  30.      * @var integer The number of items per page
  31.      */
  32.     public static $per_page = 10;
  33.  
  34.     /**
  35.      * @var integer The number of total pages
  36.      */
  37.     public static $total_pages = 0;
  38.  
  39.     /**
  40.      * @var array The HTML for the display
  41.      */
  42.     public static $template = array(
  43.         'wrapper_start'  => '<div class="pagination"><ul> ',
  44.         'wrapper_end'    => ' </ul></div>',
  45.         'page_start'     => '<span class="page-links"> ',
  46.         'page_end'       => ' </span>',
  47.         'previous_start' => '<li class="prev"> ',
  48.         'prevdisabled_start' => '<li class="prev disabled"> ',
  49.         'previous_end'   => ' </li>',
  50.         'previous_mark'  => '&laquo; ',
  51.         'pagelinks_start' => '<li>',
  52.         'pagelinks_end' => '</li>',
  53.         'next_start'     => '<li class="next"> ',
  54.         'nextdisabled_start' => '<li class="next disabled"> ',
  55.         'next_end'       => ' </li>',
  56.         'next_mark'      => ' &raquo;',
  57.         'active_start'   => '<li class="active"> ',
  58.         'active_end'     => ' </li>',
  59.     );
  60.  
  61.     /**
  62.      * @var integer The total number of items
  63.      */
  64.     protected static $total_items = 0;
  65.  
  66.     /**
  67.      * @var integer The total number of links to show
  68.      */
  69.     protected static $num_links = 5;
  70.  
  71.     /**
  72.      * @var integer The URI segment containg page number
  73.      */
  74.     protected static $uri_segment = 3;
  75.  
  76.     /**
  77.      * @var mixed   The pagination URL
  78.      */
  79.     protected static $pagination_url;
  80.  
  81.     /**
  82.      * Init
  83.      *
  84.      * Loads in the config and sets the variables
  85.      *
  86.      * @access  public
  87.      * @return  void
  88.      */
  89.     public static function _init()
  90.     {
  91.         $config = \Config::get('pagination', array());
  92.  
  93.         static::set_config($config);
  94.     }
  95.  
  96.     // --------------------------------------------------------------------
  97.  
  98.     /**
  99.      * Set Config
  100.      *
  101.      * Sets the configuration for pagination
  102.      *
  103.      * @access public
  104.      * @param array   $config The configuration array
  105.      * @return void
  106.      */
  107.     public static function set_config(array $config)
  108.     {
  109.  
  110.         foreach ($config as $key => $value)
  111.         {
  112.             if ($key == 'template')
  113.             {
  114.                 static::$template = array_merge(static::$template, $config['template']);
  115.                 continue;
  116.             }
  117.  
  118.             static::${$key} = $value;
  119.         }
  120.  
  121.         static::initialize();
  122.     }
  123.  
  124.     // --------------------------------------------------------------------
  125.  
  126.     /**
  127.      * Prepares vars for creating links
  128.      *
  129.      * @access public
  130.      * @return array    The pagination variables
  131.      */
  132.     protected static function initialize()
  133.     {
  134.         static::$total_pages = ceil(static::$total_items / static::$per_page) ?: 1;
  135.  
  136.         static::$current_page = (int) \URI::segment(static::$uri_segment);
  137.  
  138.         if (static::$current_page > static::$total_pages)
  139.         {
  140.             static::$current_page = static::$total_pages;
  141.         }
  142.         elseif (static::$current_page < 1)
  143.         {
  144.             static::$current_page = 1;
  145.         }
  146.  
  147.         // The current page must be zero based so that the offset for page 1 is 0.
  148.         static::$offset = (static::$current_page - 1) * static::$per_page;
  149.     }
  150.  
  151.     // --------------------------------------------------------------------
  152.  
  153.     /**
  154.      * Creates the pagination links
  155.      *
  156.      * @access public
  157.      * @return mixed    The pagination links
  158.      */
  159.     public static function create_links()
  160.     {
  161.         if (static::$total_pages == 1)
  162.         {
  163.             return '';
  164.         }
  165.  
  166.         \Lang::load('pagination', true);
  167.  
  168.         $pagination  = static::$template['wrapper_start'];
  169.         $pagination .= static::prev_link(\Lang::get('pagination.previous'));
  170.         $pagination .= static::page_links();
  171.         $pagination .= static::next_link(\Lang::get('pagination.next'));
  172.         $pagination .= static::$template['wrapper_end'];
  173.  
  174.         return $pagination;
  175.     }
  176.  
  177.     // --------------------------------------------------------------------
  178.  
  179.     /**
  180.      * Pagination Page Number links
  181.      *
  182.      * @access public
  183.      * @return mixed    Markup for page number links
  184.      */
  185.     public static function page_links()
  186.     {
  187.         if (static::$total_pages == 1)
  188.         {
  189.             return '';
  190.         }
  191.  
  192.         $pagination = '';
  193.  
  194.         // Let's get the starting page number, this is determined using num_links
  195.         $start = ((static::$current_page - static::$num_links) > 0) ? static::$current_page - (static::$num_links - 1) : 1;
  196.  
  197.         // Let's get the ending page number
  198.         $end   = ((static::$current_page + static::$num_links) < static::$total_pages) ? static::$current_page + static::$num_links : static::$total_pages;
  199.  
  200.         for($i = $start; $i <= $end; $i++)
  201.         {
  202.             if (static::$current_page == $i)
  203.             {
  204.                 $url = ($i == 1) ? '' : '/'.$i;
  205.                 $pagination .= static::$template['active_start'].\Html::anchor(rtrim(static::$pagination_url, '/').$url, $i).static::$template['active_end'];
  206.             }
  207.             else
  208.             {
  209.                 $url = ($i == 1) ? '' : '/'.$i;
  210.                 $pagination .= static::$template['pagelinks_start'].\Html::anchor(rtrim(static::$pagination_url, '/').$url, $i).static::$template['pagelinks_end'];
  211.             }
  212.         }
  213.  
  214.         return $pagination;
  215.     }
  216.  
  217.     // --------------------------------------------------------------------
  218.  
  219.     /**
  220.      * Pagination "Next" link
  221.      *
  222.      * @access public
  223.      * @param string $value The text displayed in link
  224.      * @return mixed    The next link
  225.      */
  226.     public static function next_link($value)
  227.     {
  228.         if (static::$total_pages == 1)
  229.         {
  230.             return '';
  231.         }
  232.  
  233.         if (static::$current_page == static::$total_pages)
  234.         {
  235.             return static::$template['nextdisabled_start'].\Html::anchor(rtrim('#'),$value.static::$template['next_mark']).static::$template['next_end'];
  236.         }
  237.         else
  238.         {
  239.             $next_page = static::$current_page + 1;
  240.             return static::$template['next_start'].\Html::anchor(rtrim(static::$pagination_url, '/').'/'.$next_page, $value.static::$template['next_mark']).static::$template['next_end'];
  241.         }
  242.     }
  243.  
  244.     // --------------------------------------------------------------------
  245.  
  246.     /**
  247.      * Pagination "Previous" link
  248.      *
  249.      * @access public
  250.      * @param string $value The text displayed in link
  251.      * @return mixed    The previous link
  252.      */
  253.     public static function prev_link($value)
  254.     {
  255.         if (static::$total_pages == 1)
  256.         {
  257.             return '';
  258.         }
  259.  
  260.         if (static::$current_page == 1)
  261.         {
  262.             return static::$template['prevdisabled_start'].\Html::anchor(rtrim('#'),$value.static::$template['previous_mark']).static::$template['previous_end'];
  263.         }
  264.         else
  265.         {
  266.             $previous_page = static::$current_page - 1;
  267.             $previous_page = ($previous_page == 1) ? '' : '/'.$previous_page;
  268.             return static::$template['previous_start'].\Html::anchor(rtrim(static::$pagination_url, '/').$previous_page, static::$template['previous_mark'].$value).static::$template['previous_end'];
  269.         }
  270.     }
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement