Pager
[ class tree: Pager ] [ index: Pager ] [ all elements ]

Source for file HtmlWidgets.php

Documentation is available at HtmlWidgets.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Contains the Pager_HtmlWidgets class
  6.  *
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. The name of the author may not be used to endorse or promote products
  17.  *    derived from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  20.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22.  * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
  23.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * @category   HTML
  31.  * @package    Pager
  32.  * @author     Lorenzo Alberton <l dot alberton at quipo dot it>
  33.  * @copyright  2003-2006 Lorenzo Alberton
  34.  * @license    http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  35.  * @version    CVS: $Id: HtmlWidgets.php,v 1.1 2006/03/08 15:22:42 quipo Exp $
  36.  * @link       http://pear.php.net/package/Pager
  37.  */
  38.  
  39. /**
  40.  * Two constants used to guess the path- and file-name of the page
  41.  * when the user doesn't set any other value
  42.  */
  43. {
  44.     var $pager = null;
  45.     
  46.     // {{{ constructor
  47.     
  48.     function Pager_HtmlWidgets(&$pager)
  49.     {
  50.         $this->pager =$pager;
  51.     }
  52.     
  53.     // }}}
  54.     // {{{ getPerPageSelectBox()
  55.  
  56.     /**
  57.      * Returns a string with a XHTML SELECT menu,
  58.      * useful for letting the user choose how many items per page should be
  59.      * displayed. If parameter useSessions is TRUE, this value is stored in
  60.      * a session var. The string isn't echoed right now so you can use it
  61.      * with template engines.
  62.      *
  63.      * @param integer $start 
  64.      * @param integer $end 
  65.      * @param integer $step 
  66.      * @param boolean $showAllData If true, perPage is set equal to totalItems.
  67.      * @param array   (or string $optionText for BC reasons)
  68.      *                 - 'optionText': text to show in each option.
  69.      *                   Use '%d' where you want to see the number of pages selected.
  70.      *                 - 'attributes': (html attributes) Tag attributes or
  71.      *                   HTML attributes (id="foo" pairs), will be inserted in the
  72.      *                   <select> tag
  73.      * @return string xhtml select box
  74.      * @access public
  75.      */
  76.     function getPerPageSelectBox($start=5$end=30$step=5$showAllData=false$extraParams=array())
  77.     {
  78.         // FIXME: needs POST support
  79.         $optionText '%d';
  80.         $attributes '';
  81.         if (is_string($extraParams)) {
  82.             //old behavior, BC maintained
  83.             $optionText $extraParams;
  84.         else {
  85.             if (array_key_exists('optionText'$extraParams)) {
  86.                 $optionText $extraParams['optionText'];
  87.             }
  88.             if (array_key_exists('attributes'$extraParams)) {
  89.                 $attributes $extraParams['attributes'];
  90.             }
  91.         }
  92.  
  93.         if (!strstr($optionText'%d')) {
  94.             return $this->pager->raiseError(
  95.                 $this->pager->errorMessage(ERROR_PAGER_INVALID_PLACEHOLDER),
  96.                 ERROR_PAGER_INVALID_PLACEHOLDER
  97.             );
  98.         }
  99.         $start = (int)$start;
  100.         $end   = (int)$end;
  101.         $step  = (int)$step;
  102.         if (!empty($_SESSION[$this->pager->_sessionVar])) {
  103.             $selected = (int)$_SESSION[$this->pager->_sessionVar];
  104.         else {
  105.             $selected $this->pager->_perPage;
  106.         }
  107.  
  108.         $tmp '<select name="'.$this->pager->_sessionVar.'"';
  109.         if (!empty($attributes)) {
  110.             $tmp .= ' '.$attributes;
  111.         }
  112.         $tmp .= '>';
  113.         for ($i=$start$i<=$end$i+=$step{
  114.             $tmp .= '<option value="'.$i.'"';
  115.             if ($i == $selected{
  116.                 $tmp .= ' selected="selected"';
  117.             }
  118.             $tmp .= '>'.sprintf($optionText$i).'</option>';
  119.         }
  120.         if ($showAllData && $end $this->pager->_totalItems{
  121.             $tmp .= '<option value="'.$this->pager->_totalItems.'"';
  122.             if ($this->pager->_totalItems == $selected{
  123.                 $tmp .= ' selected="selected"';
  124.             }
  125.             $tmp .= '>';
  126.             if (empty($this->pager->_showAllText)) {
  127.                 $tmp .= str_replace('%d'$this->pager->_totalItems$optionText);
  128.             else {
  129.                 $tmp .= $this->pager->_showAllText;
  130.             }
  131.             $tmp .= '</option>';
  132.         }
  133.         $tmp .= '</select>';
  134.         return $tmp;
  135.     }
  136.  
  137.     // }}}
  138.     // {{{ getPageSelectBox()
  139.  
  140.     /**
  141.      * Returns a string with a XHTML SELECT menu with the page numbers,
  142.      * useful as an alternative to the links
  143.      *
  144.      * @param array   - 'optionText': text to show in each option.
  145.      *                   Use '%d' where you want to see the number of pages selected.
  146.      *                 - 'autoSubmit': if TRUE, add some js code to submit the
  147.      *                   form on the onChange event
  148.      * @param string    $extraAttributes (html attributes) Tag attributes or
  149.      *                   HTML attributes (id="foo" pairs), will be inserted in the
  150.      *                   <select> tag
  151.      * @return string xhtml select box
  152.      * @access public
  153.      */
  154.     function getPageSelectBox($params array()$extraAttributes '')
  155.     {
  156.         $optionText '%d';
  157.         if (array_key_exists('optionText'$params)) {
  158.             $optionText $params['optionText'];
  159.         }
  160.  
  161.         if (!strstr($optionText'%d')) {
  162.             return $this->pager->raiseError(
  163.                 $this->pager->errorMessage(ERROR_PAGER_INVALID_PLACEHOLDER),
  164.                 ERROR_PAGER_INVALID_PLACEHOLDER
  165.             );
  166.         }
  167.         
  168.         $tmp '<select name="'.$this->pager->_urlVar.'"';
  169.         if (!empty($extraAttributes)) {
  170.             $tmp .= ' '.$extraAttributes;
  171.         }
  172.         if (!empty($params['autoSubmit'])) {
  173.             if ($this->pager->_httpMethod == 'GET'{
  174.                 $selector '\' + '.'this.options[this.selectedIndex].value + \'';
  175.                 if ($this->pager->_append{
  176.                     $href '?' $this->pager->_http_build_query_wrapper($this->pager->_linkData);
  177.                     $href htmlentities($this->pager->_url)preg_replace(
  178.                         '/(&|&amp;|\?)('.$this->pager->_urlVar.'=)(\d+)/',
  179.                         '\\1\\2'.$selector,
  180.                         htmlentities($href)
  181.                     );
  182.                 else {
  183.                     $href htmlentities($this->pager->_url str_replace('%d'$selector$this->pager->_fileName));
  184.                 }
  185.                 $tmp .= ' onchange="document.location.href=\''
  186.                      . $href .'\''
  187.                      . '"';
  188.             elseif ($this->pager->_httpMethod == 'POST'{
  189.                 $tmp .= " onchange='"
  190.                      . $this->pager->_generateFormOnClick($this->pager->_url$this->pager->_linkData)
  191.                      . "'";
  192.                 $tmp preg_replace(
  193.                     '/(input\.name = \"'.$this->pager->_urlVar.'\"; input\.value =) \"(\d+)\";/',
  194.                     '\\1 this.options[this.selectedIndex].value;',
  195.                     $tmp
  196.                 );
  197.             }
  198.         }
  199.         $tmp .= '>';
  200.         $start 1;
  201.         $end $this->pager->numPages();
  202.         $selected $this->pager->getCurrentPageID();
  203.         for ($i=$start$i<=$end$i++{
  204.             $tmp .= '<option value="'.$i.'"';
  205.             if ($i == $selected{
  206.                 $tmp .= ' selected="selected"';
  207.             }
  208.             $tmp .= '>'.sprintf($optionText$i).'</option>';
  209.         }
  210.         $tmp .= '</select>';
  211.         return $tmp;
  212.     }
  213.     
  214.     // }}}
  215. }
  216. ?>

Documentation generated on Thu, 12 Jun 2008 13:39:55 -0500 by phpDocumentor 1.4.1