home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Pager / HtmlWidgets.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  9.8 KB  |  247 lines

  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.alberton@quipo.it>
  33.  * @copyright 2003-2007 Lorenzo Alberton
  34.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  35.  * @version   CVS: $Id: HtmlWidgets.php,v 1.6 2008/03/26 21:36:28 quipo Exp $
  36.  * @link      http://pear.php.net/package/Pager
  37.  */
  38.  
  39. /**
  40.  * Pager_HtmlWidgets
  41.  *
  42.  * @category  HTML
  43.  * @package   Pager
  44.  * @author    Lorenzo Alberton <l.alberton@quipo.it>
  45.  * @copyright 2003-2007 Lorenzo Alberton
  46.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  47.  * @link      http://pear.php.net/package/Pager
  48.  */
  49. class Pager_HtmlWidgets
  50. {
  51.     var $pager = null;
  52.     
  53.     // {{{ constructor
  54.     
  55.     /**
  56.      * Constructor
  57.      *
  58.      * @param object &$pager Pager instance
  59.      */
  60.     function Pager_HtmlWidgets(&$pager)
  61.     {
  62.         $this->pager =& $pager;
  63.     }
  64.     
  65.     // }}}
  66.     // {{{ getPerPageSelectBox()
  67.  
  68.     /**
  69.      * Returns a string with a XHTML SELECT menu,
  70.      * useful for letting the user choose how many items per page should be
  71.      * displayed. If parameter useSessions is TRUE, this value is stored in
  72.      * a session var. The string isn't echoed right now so you can use it
  73.      * with template engines.
  74.      *
  75.      * @param integer $start       starting value for the select menu
  76.      * @param integer $end         ending value for the select menu
  77.      * @param integer $step        step between values in the select menu
  78.      * @param boolean $showAllData If true, perPage is set equal to totalItems.
  79.      * @param array   $extraParams (or string $optionText for BC reasons)
  80.      *                - 'optionText': text to show in each option.
  81.      *                  Use '%d' where you want to see the number of pages selected.
  82.      *                - 'attributes': (html attributes) Tag attributes or
  83.      *                  HTML attributes (id="foo" pairs), will be inserted in the
  84.      *                  <select> tag
  85.      *                - 'checkMaxLimit': if true, Pager checks if $end is bigger
  86.      *                  than $totalItems, and doesn't show the extra select options
  87.      *
  88.      * @return string xhtml select box
  89.      * @access public
  90.      */
  91.     function getPerPageSelectBox($start=5, $end=30, $step=5, $showAllData=false, $extraParams=array())
  92.     {
  93.         // FIXME: needs POST support
  94.         $optionText = '%d';
  95.         $attributes = '';
  96.         $checkMaxLimit = false;
  97.         if (is_string($extraParams)) {
  98.             //old behavior, BC maintained
  99.             $optionText = $extraParams;
  100.         } else {
  101.             if (array_key_exists('optionText', $extraParams)) {
  102.                 $optionText = $extraParams['optionText'];
  103.             }
  104.             if (array_key_exists('attributes', $extraParams)) {
  105.                 $attributes = $extraParams['attributes'];
  106.             }
  107.             if (array_key_exists('checkMaxLimit', $extraParams)) {
  108.                 $checkMaxLimit = $extraParams['checkMaxLimit'];
  109.             }
  110.         }
  111.  
  112.         if (!strstr($optionText, '%d')) {
  113.             return $this->pager->raiseError(
  114.                 $this->pager->errorMessage(ERROR_PAGER_INVALID_PLACEHOLDER),
  115.                 ERROR_PAGER_INVALID_PLACEHOLDER
  116.             );
  117.         }
  118.         $start = (int)$start;
  119.         $end   = (int)$end;
  120.         $step  = (int)$step;
  121.         if (!empty($_SESSION[$this->pager->_sessionVar])) {
  122.             $selected = (int)$_SESSION[$this->pager->_sessionVar];
  123.         } else {
  124.             $selected = $this->pager->_perPage;
  125.         }
  126.  
  127.         if ($checkMaxLimit && $this->pager->_totalItems >= 0 && $this->pager->_totalItems < $end) {
  128.             $end = $this->pager->_totalItems;
  129.         }
  130.  
  131.         $tmp = '<select name="'.$this->pager->_sessionVar.'"';
  132.         if (!empty($attributes)) {
  133.             $tmp .= ' '.$attributes;
  134.         }
  135.         $tmp .= '>';
  136.         $last = $start;
  137.         for ($i=$start; $i<=$end; $i+=$step) {
  138.             $last = $i;
  139.             $tmp .= '<option value="'.$i.'"';
  140.             if ($i == $selected) {
  141.                 $tmp .= ' selected="selected"';
  142.             }
  143.             $tmp .= '>'.sprintf($optionText, $i).'</option>';
  144.         }
  145.         if ($showAllData && $last != $this->pager->_totalItems) {
  146.             $tmp .= '<option value="'.$this->pager->_totalItems.'"';
  147.             if ($this->pager->_totalItems == $selected) {
  148.                 $tmp .= ' selected="selected"';
  149.             }
  150.             $tmp .= '>';
  151.             if (empty($this->pager->_showAllText)) {
  152.                 $tmp .= str_replace('%d', $this->pager->_totalItems, $optionText);
  153.             } else {
  154.                 $tmp .= $this->pager->_showAllText;
  155.             }
  156.             $tmp .= '</option>';
  157.         }
  158.         if (substr($tmp, -9, 9) !== '</option>') {
  159.             //empty select
  160.             $tmp .= '<option />';
  161.         }
  162.         $tmp .= '</select>';
  163.         return $tmp;
  164.     }
  165.  
  166.     // }}}
  167.     // {{{ getPageSelectBox()
  168.  
  169.     /**
  170.      * Returns a string with a XHTML SELECT menu with the page numbers,
  171.      * useful as an alternative to the links
  172.      *
  173.      * @param array  $params          - 'optionText': text to show in each option.
  174.      *                                  Use '%d' where you want to see the number
  175.      *                                  of pages selected.
  176.      *                                - 'autoSubmit': if TRUE, add some js code
  177.      *                                  to submit the form on the onChange event
  178.      * @param string $extraAttributes (html attributes) Tag attributes or
  179.      *                                HTML attributes (id="foo" pairs), will be
  180.      *                                inserted in the <select> tag
  181.      *
  182.      * @return string xhtml select box
  183.      * @access public
  184.      */
  185.     function getPageSelectBox($params = array(), $extraAttributes = '')
  186.     {
  187.         $optionText = '%d';
  188.         if (array_key_exists('optionText', $params)) {
  189.             $optionText = $params['optionText'];
  190.         }
  191.  
  192.         if (!strstr($optionText, '%d')) {
  193.             return $this->pager->raiseError(
  194.                 $this->pager->errorMessage(ERROR_PAGER_INVALID_PLACEHOLDER),
  195.                 ERROR_PAGER_INVALID_PLACEHOLDER
  196.             );
  197.         }
  198.         
  199.         $tmp = '<select name="'.$this->pager->_urlVar.'"';
  200.         if (!empty($extraAttributes)) {
  201.             $tmp .= ' '.$extraAttributes;
  202.         }
  203.         if (!empty($params['autoSubmit'])) {
  204.             if ($this->pager->_httpMethod == 'GET') {
  205.                 $selector = '\' + '.'this.options[this.selectedIndex].value + \'';
  206.                 if ($this->pager->_append) {
  207.                     $href = '?' . $this->pager->_http_build_query_wrapper($this->pager->_linkData);
  208.                     $href = htmlentities($this->pager->_url). preg_replace(
  209.                         '/(&|&|\?)('.$this->pager->_urlVar.'=)(\d+)/',
  210.                         '\\1\\2'.$selector,
  211.                         htmlentities($href)
  212.                     );
  213.                 } else {
  214.                     $href = htmlentities($this->pager->_url . str_replace('%d', $selector, $this->pager->_fileName));
  215.                 }
  216.                 $tmp .= ' onchange="document.location.href=\''
  217.                      . $href .'\''
  218.                      . '"';
  219.             } elseif ($this->pager->_httpMethod == 'POST') {
  220.                 $tmp .= " onchange='"
  221.                      . $this->pager->_generateFormOnClick($this->pager->_url, $this->pager->_linkData)
  222.                      . "'";
  223.                 $tmp = preg_replace(
  224.                     '/(input\.name = \"'.$this->pager->_urlVar.'\"; input\.value =) \"(\d+)\";/',
  225.                     '\\1 this.options[this.selectedIndex].value;',
  226.                     $tmp
  227.                 );
  228.             }
  229.         }
  230.         $tmp .= '>';
  231.         $start = 1;
  232.         $end   = $this->pager->numPages();
  233.         $selected = $this->pager->getCurrentPageID();
  234.         for ($i=$start; $i<=$end; $i++) {
  235.             $tmp .= '<option value="'.$i.'"';
  236.             if ($i == $selected) {
  237.                 $tmp .= ' selected="selected"';
  238.             }
  239.             $tmp .= '>'.sprintf($optionText, $i).'</option>';
  240.         }
  241.         $tmp .= '</select>';
  242.         return $tmp;
  243.     }
  244.     
  245.     // }}}
  246. }
  247. ?>