Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/html/pagination.php

Documentation is available at pagination.php

  1. <?php
  2. /**
  3.  * @version        $Id: pagination.php 6674 2007-02-19 05:52:03Z Jinx $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    HTML
  6.  * @copyright    Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
  7.  * @license        GNU/GPL, see LICENSE.php
  8.  *  Joomla! is free software. This version may have been modified pursuant
  9.  *  to the GNU General Public License, and as distributed it includes or
  10.  *  is derivative of works licensed under the GNU General Public License or
  11.  *  other free or open source software licenses.
  12.  *  See COPYRIGHT.php for copyright notices and details.
  13.  */
  14.  
  15. // Check to ensure this file is within the rest of the framework
  16. defined('JPATH_BASE'or die();
  17.  
  18. /**
  19.  * Pagination Class.  Provides a common interface for content pagination for the
  20.  * Joomla! Framework
  21.  *
  22.  * @author        Louis Landry <[email protected]>
  23.  * @package     Joomla.Framework
  24.  * @subpackage    HTML
  25.  * @since        1.5
  26.  */
  27. class JPagination extends JObject
  28. {
  29.     /**
  30.      * The record number to start dislpaying from
  31.      *
  32.      * @access public
  33.      * @var int 
  34.      */
  35.     var $limitstart = null;
  36.  
  37.     /**
  38.      * Number of rows to display per page
  39.      *
  40.      * @access public
  41.      * @var int 
  42.      */
  43.     var $limit = null;
  44.  
  45.     /**
  46.      * Total number of rows
  47.      *
  48.      * @access public
  49.      * @var int 
  50.      */
  51.     var $total = null;
  52.  
  53.     /**
  54.      * Base URL for pagination output
  55.      *
  56.      * @access protected
  57.      * @var string 
  58.      */
  59.     var $_link = null;
  60.  
  61.     /**
  62.      * View all flag
  63.      *
  64.      * @access protected
  65.      * @var boolean 
  66.      */
  67.     var $_viewall = false;
  68.  
  69.     /**
  70.      * Constructor
  71.      */
  72.     function __construct($total$limitstart$limit$link null)
  73.     {
  74.         global $mainframe;
  75.  
  76.         // Value/Type checking
  77.         $this->total            = (int) $total;
  78.         $this->limitstart    = (int) max($limitstart0);
  79.         $this->limit            = (int) max($limit1);
  80.  
  81.         if ($this->limit > $this->total{
  82.             $this->limitstart = 0;
  83.         }
  84.  
  85.         if ($this->limitstart > $this->total{
  86.             $this->limitstart -= $this->limitstart % $this->limit;
  87.         }
  88.  
  89.         // Set the total pages and current page values
  90.         $this->set'pages.total'ceil($this->total / $this->limit));
  91.         $this->set'pages.current'ceil(($this->limitstart + 1$this->limit));
  92.  
  93.         // Set the pagination iteration loop values
  94.         $displayedPages    10;
  95.         $this->set'pages.start'(floor(($this->get('pages.current'-1$displayedPages)) $displayedPages +1);
  96.         if ($this->get('pages.start'$displayedPages -$this->get('pages.total')) {
  97.             $this->set'pages.stop'$this->get('pages.start'$displayedPages -1);
  98.         else {
  99.             $this->set'pages.stop'$this->get('pages.total'));
  100.         }
  101.  
  102.         // Set the base link for the object
  103.         if ($link{
  104.             $this->_link = $link;
  105.         }
  106.         else
  107.         {
  108.             $config &JFactory::getConfig();
  109.             if ($config->getValue('config.sef'&& !$mainframe->isAdmin())
  110.             {
  111.                 $this->_link = 'index.php?';
  112.                 $get JRequest::get('get');
  113.                 $this->_link .= '&option='.$get['option'];
  114.  
  115.                 foreach ($get as $k => $v)
  116.                 {
  117.                     if ($k != 'option' && $k != 'Itemid'{
  118.                         $this->_link .= '&amp;'.$k.'='.$v;
  119.                     }
  120.                 }
  121.                 $this->_link .= '&Itemid='.$get['Itemid'];
  122.             }
  123.             else
  124.             {
  125.                 $this->_link = JRequest::getURI();
  126.                 if ((strpos($this->_link'index.php?'=== false&& (strpos($this->_link'&'=== false)) {
  127.                     $this->_link .= 'index.php?';
  128.                 }
  129.             }
  130.             // Strip out limit and limitstart variables from base link
  131.             $this->_link = preg_replace('#&?limit(start)?=\d+#'''$this->_link);
  132.         }
  133.  
  134.         // If we are viewing all records set the view all flag to true
  135.         if (JRequest::getVar('limit'0'''int'== && $this->limitstart == 0{
  136.             $this->_viewall = true;
  137.         }
  138.     }
  139.  
  140.     /**
  141.      * Return the rationalised offset for a row with a given index.
  142.      *
  143.      * @access    public
  144.      * @param    int    $index The row index
  145.      * @return    int Rationalised offset for a row with a given index
  146.      * @since    1.5
  147.      */
  148.     function getRowOffset($index{
  149.         return $index +$this->limitstart;
  150.     }
  151.  
  152.     /**
  153.      * Return the pagination data object, only creating it if it doesn't already exist
  154.      *
  155.      * @access    public
  156.      * @return    object Pagination data object
  157.      * @since    1.5
  158.      */
  159.     function getData()
  160.     {
  161.         static $data;
  162.         if (!is_object($data)) {
  163.             $data $this->_buildDataObject();
  164.         }
  165.         return $data;
  166.     }
  167.  
  168.     /**
  169.      * Create and return the pagination pages counter string, ie. Page 2 of 4
  170.      *
  171.      * @access public
  172.      * @return string Pagination pages counter string
  173.      * @since 1.5
  174.      */
  175.     function getPagesCounter()
  176.     {
  177.         // Initialize variables
  178.         $html null;
  179.         if ($this->get('pages.total'0{
  180.             $html .= JText::_('Page')." ".$this->get('pages.current')." ".JText::_('of')." ".$this->get('pages.total');
  181.         }
  182.         return $html;
  183.     }
  184.  
  185.     /**
  186.      * Create and return the pagination result set counter string, ie. Results 1-10 of 42
  187.      *
  188.      * @access public
  189.      * @return string Pagination result set counter string
  190.      * @since 1.5
  191.      */
  192.     function getResultsCounter()
  193.     {
  194.         // Initialize variables
  195.         $html null;
  196.         $fromResult $this->limitstart + 1;
  197.  
  198.         // If the limit is reached before the end of the list
  199.         if ($this->limitstart + $this->limit < $this->total{
  200.             $toResult $this->limitstart + $this->limit;
  201.         else {
  202.             $toResult $this->total;
  203.         }
  204.  
  205.         // If there are results found
  206.         if ($this->total > 0{
  207.             $msg JText::sprintf('Results of'$fromResult$toResult$this->total);
  208.             $html .= "\n".$msg;
  209.         else {
  210.             $html .= "\n".JText::_('No records found');
  211.         }
  212.  
  213.         return $html;
  214.     }
  215.  
  216.     /**
  217.      * Create and return the pagination page list string, ie. Previous, Next, 1 2 3 ... x
  218.      *
  219.      * @access public
  220.      * @return string Pagination page list string
  221.      * @since 1.0
  222.      */
  223.     function getPagesLinks($link null)
  224.     {
  225.         global $mainframe;
  226.  
  227.         $lang =JFactory::getLanguage();
  228.  
  229.         // Build the page navigation list
  230.         $data $this->_buildDataObject($link);
  231.  
  232.         $list array();
  233.  
  234.         $itemOverride false;
  235.         $listOverride false;
  236.  
  237.         $chromePath JPATH_BASE.'/templates/'.$mainframe->getTemplate().'/html/pagination.php';
  238.         if (file_exists($chromePath)) {
  239.             require_once ($chromePath);
  240.             if (function_exists('pagination_item_active'&& function_exists('pagination_item_inactive')) {
  241.                 $itemOverride true;
  242.             }
  243.             if (function_exists('pagination_list_render')) {
  244.                 $listOverride true;
  245.             }
  246.         }
  247.  
  248.         // Build the select list
  249.         if ($data->all->base !== null{
  250.             $list['all']['active'true;
  251.             $list['all']['data'($itemOverridepagination_item_active($data->all$this->_item_active($data->all);
  252.         else {
  253.             $list['all']['active'false;
  254.             $list['all']['data'($itemOverridepagination_item_inactive($data->all$this->_item_inactive($data->all);
  255.         }
  256.  
  257.         if ($data->start->base !== null{
  258.             $list['start']['active'true;
  259.             $list['start']['data'($itemOverridepagination_item_active($data->start$this->_item_active($data->start);
  260.         else {
  261.             $list['start']['active'false;
  262.             $list['start']['data'($itemOverridepagination_item_inactive($data->start$this->_item_inactive($data->start);
  263.         }
  264.         if ($data->previous->base !== null{
  265.             $list['previous']['active'true;
  266.             $list['previous']['data'($itemOverridepagination_item_active($data->previous$this->_item_active($data->previous);
  267.         else {
  268.             $list['previous']['active'false;
  269.             $list['previous']['data'($itemOverridepagination_item_inactive($data->previous$this->_item_inactive($data->previous);
  270.         }
  271.  
  272.         $list['pages'array()//make sure it exists
  273.         foreach ($data->pages as $i => $page)
  274.         {
  275.             if ($page->base !== null{
  276.                 $list['pages'][$i]['active'true;
  277.                 $list['pages'][$i]['data'($itemOverridepagination_item_active($page$this->_item_active($page);
  278.             else {
  279.                 $list['pages'][$i]['active'false;
  280.                 $list['pages'][$i]['data'($itemOverridepagination_item_inactive($page$this->_item_inactive($page);
  281.             }
  282.         }
  283.  
  284.         if ($data->next->base !== null{
  285.             $list['next']['active'true;
  286.             $list['next']['data'($itemOverridepagination_item_active($data->next$this->_item_active($data->next);
  287.         else {
  288.             $list['next']['active'false;
  289.             $list['next']['data'($itemOverridepagination_item_inactive($data->next$this->_item_inactive($data->next);
  290.         }
  291.         if ($data->end->base !== null{
  292.             $list['end']['active'true;
  293.             $list['end']['data'($itemOverridepagination_item_active($data->end$this->_item_active($data->end);
  294.         else {
  295.             $list['end']['active'false;
  296.             $list['end']['data'($itemOverridepagination_item_inactive($data->end$this->_item_inactive($data->end);
  297.         }
  298.  
  299.         return ($listOverridepagination_list_render($list$this->_list_render($list);
  300.     }
  301.  
  302.     /**
  303.      * Return the pagination footer
  304.      *
  305.      * @access public
  306.      * @return string Pagination footer
  307.      * @since 1.0
  308.      */
  309.     function getListFooter()
  310.     {
  311.         global $mainframe;
  312.  
  313.         $list array();
  314.         $list['limit']            $this->limit;
  315.         $list['limitstart']        $this->limitstart;
  316.         $list['total']            $this->total;
  317.         $list['limitfield']        $this->getLimitBox();
  318.         $list['pagescounter']    $this->getPagesCounter();
  319.         $list['pageslinks']        $this->getPagesLinks();
  320.  
  321.         $chromePath JPATH_BASE.'/templates/'.$mainframe->getTemplate().'/html/pagination.php';
  322.         if (file_exists($chromePath)) {
  323.             require_once ($chromePath);
  324.             if (function_exists('pagination_list_footer')) {
  325.                 $listOverride true;
  326.             }
  327.         }
  328.         return ($listOverridepagination_list_footer($list$this->_list_footer($list);
  329.     }
  330.  
  331.     /**
  332.      * Creates a dropdown box for selecting how many records to show per page
  333.      *
  334.      * @access public
  335.      * @return string The html for the limit # input box
  336.      * @since 1.0
  337.      */
  338.     function getLimitBox($link null)
  339.     {
  340.         global $mainframe;
  341.  
  342.         // Initialize variables
  343.         $limits array ();
  344.  
  345.         // Use the default link
  346.         if is_null($link) ) {
  347.             $link $this->_link;
  348.         }
  349.  
  350.         // Make the option list
  351.         for ($i 5$i <= 30$i += 5{
  352.             $limits[JHTMLSelect::option("$i");
  353.         }
  354.         $limits[JHTMLSelect::option('50');
  355.         $limits[JHTMLSelect::option('100');
  356.         $limits[JHTMLSelect::option('0''all');
  357.  
  358.         // Build the select list
  359.         if ($mainframe->isAdmin()) {
  360.             $html JHTMLSelect::genericList($limits'limit''class="inputbox" size="1" onchange="submitform();"''value''text'$this->limit);
  361.         else {
  362.             $html JHTMLSelect::genericList($limits'limit''class="inputbox" size="1" onchange="this.form.submit()"''value''text'$this->limit);
  363.         }
  364.         return $html;
  365.     }
  366.  
  367.     /**
  368.      * Return the icon to move an item UP
  369.      *
  370.      * @access public
  371.      * @param int $i The row index
  372.      * @param boolean $condition True to show the icon
  373.      * @param string $task The task to fire
  374.      * @param string $alt The image alternate text string
  375.      * @return string Either the icon to move an item up or a space
  376.      * @since 1.0
  377.      */
  378.     function orderUpIcon($i$condition true$task 'orderup'$alt 'Move Up'$enabled true)
  379.     {
  380.         $alt JText::_($alt);
  381.  
  382.         $html '&nbsp;';
  383.         if (($i || ($i $this->limitstart > 0)) && $condition)
  384.         {
  385.             if($enabled{
  386.                 $html    '<a href="#reorder" onclick="return listItemTask(\'cb'.$i.'\',\''.$task.'\')" title="'.$alt.'">';
  387.                 $html    .= '   <img src="images/uparrow.png" width="16" height="16" border="0" alt="'.$alt.'" />';
  388.                 $html    .= '</a>';
  389.             else {
  390.                 $html    '<img src="images/uparrow0.png" width="16" height="16" border="0" alt="'.$alt.'" />';
  391.             }
  392.         }
  393.  
  394.         return $html;
  395.     }
  396.  
  397.     /**
  398.      * Return the icon to move an item DOWN
  399.      *
  400.      * @access public
  401.      * @param int $i The row index
  402.      * @param int $n The number of items in the list
  403.      * @param boolean $condition True to show the icon
  404.      * @param string $task The task to fire
  405.      * @param string $alt The image alternate text string
  406.      * @return string Either the icon to move an item down or a space
  407.      * @since 1.0
  408.      */
  409.     function orderDownIcon($i$n$condition true$task 'orderdown'$alt 'Move Down'$enabled true)
  410.     {
  411.         $alt JText::_($alt);
  412.  
  413.         $html '&nbsp;';
  414.         if (($i $n -|| $i $this->limitstart < $this->total - 1&& $condition)
  415.         {
  416.             if($enabled{
  417.                 $html    '<a href="#reorder" onclick="return listItemTask(\'cb'.$i.'\',\''.$task.'\')" title="'.$alt.'">';
  418.                 $html    .= '  <img src="images/downarrow.png" width="16" height="16" border="0" alt="'.$alt.'" />';
  419.                 $html    .= '</a>';
  420.             else {
  421.                 $html    '<img src="images/downarrow0.png" width="16" height="16" border="0" alt="'.$alt.'" />';
  422.             }
  423.         }
  424.  
  425.         return $html;
  426.     }
  427.  
  428.     /**
  429.      * Return the icon to move an item UP
  430.      *
  431.      * @access public
  432.      * @param int $id The row index
  433.      * @param int $order The ordering value for the item
  434.      * @param boolean $condition [Does Not Appear To Be Used]
  435.      * @param string $task The task to fire
  436.      * @param string $alt The image alternate text string
  437.      * @return string Either the icon to move an item up or a space
  438.      * @since 1.0
  439.      */
  440.     function orderUpIcon2($id$order$condition true$task 'orderup'$alt '#')
  441.     {
  442.         // handling of default value
  443.         if ($alt '#'{
  444.             $alt JText::_('Move Up');
  445.         }
  446.  
  447.         if ($order == 0{
  448.             $img 'uparrow0.png';
  449.         else {
  450.             if ($order 0{
  451.                 $img 'uparrow-1.png';
  452.             else {
  453.                 $img 'uparrow.png';
  454.             }
  455.         }
  456.         $output '<a href="javascript:void listItemTask(\'cb'.$id.'\',\'orderup\')" title="'.$alt.'">';
  457.         $output .= '<img src="images/'.$img.'" width="16" height="16" border="0" alt="'.$alt.'" title="'.$alt.'" /></a>';
  458.  
  459.         return $output;
  460.     }
  461.  
  462.     /**
  463.      * Return the icon to move an item DOWN
  464.      *
  465.      * @access public
  466.      * @param int $id The row index
  467.      * @param int $order The ordering value for the item
  468.      * @param boolean $condition [Does Not Appear To Be Used]
  469.      * @param string $task The task to fire
  470.      * @param string $alt The image alternate text string
  471.      * @return string Either the icon to move an item down or a space
  472.      * @since 1.0
  473.      */
  474.     function orderDownIcon2($id$order$condition true$task 'orderdown'$alt '#')
  475.     {
  476.         // handling of default value
  477.         if ($alt '#'{
  478.             $alt JText::_('Move Down');
  479.         }
  480.  
  481.         if ($order == 0{
  482.             $img 'downarrow0.png';
  483.         else {
  484.             if ($order 0{
  485.                 $img 'downarrow-1.png';
  486.             else {
  487.                 $img 'downarrow.png';
  488.             }
  489.         }
  490.         $output '<a href="javascript:void listItemTask(\'cb'.$id.'\',\'orderdown\')" title="'.$alt.'">';
  491.         $output .= '<img src="images/'.$img.'" width="16" height="16" border="0" alt="'.$alt.'" title="'.$alt.'" /></a>';
  492.  
  493.         return $output;
  494.     }
  495.  
  496.     function _list_footer($list)
  497.     {
  498.         // Initialize variables
  499.         $lang =JFactory::getLanguage();
  500.         $html "<div class=\"list-footer\">\n";
  501.  
  502.         if ($lang->isRTL()) {
  503.             $html .= "\n<div class=\"counter\">".$list['pagescounter']."</div>";
  504.             $html .= $list['pageslinks'];
  505.             $html .= "\n<div class=\"limit\">".JText::_('Display Num').$list['limitfield']."</div>";
  506.         else {
  507.             $html .= "\n<div class=\"limit\">".JText::_('Display Num').$list['limitfield']."</div>";
  508.             $html .= $list['pageslinks'];
  509.             $html .= "\n<div class=\"counter\">".$list['pagescounter']."</div>";
  510.         }
  511.  
  512.         $html .= "\n<input type=\"hidden\" name=\"limitstart\" value=\"".$list['limitstart']."\" />";
  513.         $html .= "\n</div>";
  514.  
  515.         return $html;
  516.     }
  517.  
  518.     function _list_render($list)
  519.     {
  520.         global $mainframe;
  521.  
  522.         // Initialize variables
  523.         $lang =JFactory::getLanguage();
  524.         $html null;
  525.  
  526.         // Reverse output rendering for right-to-left display
  527.         if($lang->isRTL())
  528.         {
  529.             $html .=  $list['previous']['data'];
  530.             $html .= $list['start']['data'];
  531.             $list['pages'array_reverse$list['pages');
  532.             foreach$list['pages'as $page {
  533.                 $html .= $page['data'];
  534.             }
  535.             $html .= $list['end']['data'];
  536.             $html .= $list['next']['data'];
  537.         }
  538.         else
  539.         {
  540.             $html .= '&lt;&lt; ';
  541.             $html .= $list['start']['data'];
  542.             $html .= ' &lt; ';
  543.             $html .= $list['previous']['data'];
  544.             foreach$list['pages'as $page {
  545.                 $html .= ' '.$page['data'];
  546.             }
  547.             $html .= ' '$list['next']['data'];
  548.             $html .= ' &gt;';
  549.             $html .= ' '$list['end']['data'];
  550.             $html .= ' &gt;&gt;';
  551.         }
  552.         return $html;
  553.     }
  554.  
  555.     function _item_active(&$item)
  556.     {
  557.         global $mainframe;
  558.         if ($mainframe->isAdmin()) {
  559.             return "<a title=\"".$item->text."\" onclick=\"javascript: document.adminForm.limitstart.value=".$item->base."; submitform();return false;\">".$item->text."</a>";
  560.         else {
  561.             return "<a title=\"".$item->text."\" href=\"".$item->link."\" class=\"pagenav\">".$item->text."</a>";
  562.         }
  563.     }
  564.  
  565.     function _item_inactive(&$item)
  566.     {
  567.         global $mainframe;
  568.         if ($mainframe->isAdmin()) {
  569.             return "<span>".$item->text."</span>";
  570.         else {
  571.             return "<span class=\"pagenav\">".$item->text."</span>";
  572.         }
  573.     }
  574.  
  575.     /**
  576.      * Create and return the pagination data object
  577.      *
  578.      * @access    public
  579.      * @return    object    Pagination data object
  580.      * @since    1.5
  581.      */
  582.     function _buildDataObject($base=null)
  583.     {
  584.         // Initialize variables
  585.         $data new stdClass();
  586.         if ($base{
  587.             $link $base;
  588.         else {
  589.             $base $this->_link;
  590.             $link $this->_link;
  591.         }
  592.  
  593.         $data->all    new JPaginationObject(JText::_('View All'));
  594.         if (!$this->_viewall{
  595.             $data->all->base    '0';
  596.             $data->all->link    JRoute::_($base."&limitstart=0");
  597.         }
  598.  
  599.         // Set the start and previous data objects
  600.         $data->start    new JPaginationObject(JText::_('Start'));
  601.         $data->previous    new JPaginationObject(JText::_('Prev'));
  602.  
  603.         if ($this->get('pages.current'1)
  604.         {
  605.             $page ($this->get('pages.current'-2$this->limit;
  606.             $data->start->base    '0';
  607.             $data->start->link    JRoute::_($link."&limitstart=0");
  608.             $data->previous->base    $page;
  609.             $data->previous->link    JRoute::_($link."&limitstart=".$page);
  610.         }
  611.  
  612.         // Set the next and end data objects
  613.         $data->next    new JPaginationObject(JText::_('Next'));
  614.         $data->end    new JPaginationObject(JText::_('End'));
  615.  
  616.         if ($this->get('pages.current'$this->get('pages.total'))
  617.         {
  618.             $page $this->get('pages.current'$this->limit;
  619.             $endPage ($this->get('pages.total'-1$this->limit;
  620.             $data->next->base    $page;
  621.             $data->next->link    JRoute::_($link."&limitstart=".$page);
  622.             $data->end->base    $endPage;
  623.             $data->end->link    JRoute::_($link."&limitstart=".$endPage);
  624.         }
  625.  
  626.         $data->pages array();
  627.         $stop $this->get('pages.stop');
  628.         for ($i $this->get('pages.start')$i <= $stop$i ++)
  629.         {
  630.             $offset ($i -1$this->limit;
  631.             $data->pages[$inew JPaginationObject($i);
  632.             if ($i != $this->get('pages.current'|| $this->_viewall)
  633.             {
  634.                 $data->pages[$i]->base    $offset;
  635.                 $data->pages[$i]->link    JRoute::_($link."&limitstart=".$offset);
  636.             }
  637.         }
  638.         return $data;
  639.     }
  640. }
  641.  
  642. /**
  643.  * Pagination object representing a particular item in the pagination lists
  644.  *
  645.  * @author        Louis Landry <[email protected]>
  646.  * @package     Joomla.Framework
  647.  * @subpackage    HTML
  648.  * @since        1.5
  649.  */
  650. class JPaginationObject extends JObject
  651. {
  652.     var $text;
  653.     var $base;
  654.     var $link;
  655.  
  656.     function __construct($text$base=null$link=null)
  657.     {
  658.         $this->text = $text;
  659.         $this->base = $base;
  660.         $this->link = $link;
  661.     }
  662. }
  663. ?>

Documentation generated on Mon, 05 Mar 2007 21:12:54 +0000 by phpDocumentor 1.3.1