| 7 | jpm | 1 | <?php
 | 
        
           |  |  | 2 | /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
 | 
        
           |  |  | 3 |   | 
        
           |  |  | 4 | /**
 | 
        
           |  |  | 5 |  * Contains the Pager_Jumping 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 |  * @author     Richard Heyes <richard@phpguru.org>,
 | 
        
           |  |  | 34 |  * @copyright  2003-2006 Lorenzo Alberton, Richard Heyes
 | 
        
           |  |  | 35 |  * @license    http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
 | 
        
           | 10 | jpm | 36 |  * @version    CVS: $Id: Jumping.php 10 2010-03-05 14:15:42Z jpm $
 | 
        
           | 7 | jpm | 37 |  * @link       http://pear.php.net/package/Pager
 | 
        
           |  |  | 38 |  */
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 | /**
 | 
        
           |  |  | 41 |  * require PEAR::Pager_Common base class
 | 
        
           |  |  | 42 |  */
 | 
        
           |  |  | 43 | require_once 'Pager/Common.php';
 | 
        
           |  |  | 44 |   | 
        
           |  |  | 45 | /**
 | 
        
           |  |  | 46 |  * Pager_Jumping - Generic data paging class  ("jumping window" style)
 | 
        
           |  |  | 47 |  * Handles paging a set of data. For usage see the example.php provided.
 | 
        
           |  |  | 48 |  *
 | 
        
           |  |  | 49 |  * @category   HTML
 | 
        
           |  |  | 50 |  * @package    Pager
 | 
        
           |  |  | 51 |  * @author     Lorenzo Alberton <l dot alberton at quipo dot it>
 | 
        
           |  |  | 52 |  * @author     Richard Heyes <richard@phpguru.org>,
 | 
        
           |  |  | 53 |  * @copyright  2003-2005 Lorenzo Alberton, Richard Heyes
 | 
        
           |  |  | 54 |  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
 | 
        
           |  |  | 55 |  * @link       http://pear.php.net/package/Pager
 | 
        
           |  |  | 56 |  */
 | 
        
           |  |  | 57 | class Pager_Jumping extends Pager_Common
 | 
        
           |  |  | 58 | {
 | 
        
           |  |  | 59 |     // {{{ Pager_Jumping()
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 |     /**
 | 
        
           |  |  | 62 |      * Constructor
 | 
        
           |  |  | 63 |      *
 | 
        
           |  |  | 64 |      * @param array $options    An associative array of option names
 | 
        
           |  |  | 65 |      *                          and their values
 | 
        
           |  |  | 66 |      * @access public
 | 
        
           |  |  | 67 |      */
 | 
        
           |  |  | 68 |     function Pager_Jumping($options = array())
 | 
        
           |  |  | 69 |     {
 | 
        
           |  |  | 70 |         $err = $this->setOptions($options);
 | 
        
           |  |  | 71 |         if ($err !== PAGER_OK) {
 | 
        
           |  |  | 72 |             return $this->raiseError($this->errorMessage($err), $err);
 | 
        
           |  |  | 73 |         }
 | 
        
           |  |  | 74 |         $this->build();
 | 
        
           |  |  | 75 |     }
 | 
        
           |  |  | 76 |   | 
        
           |  |  | 77 |     // }}}
 | 
        
           |  |  | 78 |     // {{{ getPageIdByOffset()
 | 
        
           |  |  | 79 |   | 
        
           |  |  | 80 |     /**
 | 
        
           |  |  | 81 |      * Returns pageID for given offset
 | 
        
           |  |  | 82 |      *
 | 
        
           |  |  | 83 |      * @param $index Offset to get pageID for
 | 
        
           |  |  | 84 |      * @return int PageID for given offset
 | 
        
           |  |  | 85 |      */
 | 
        
           |  |  | 86 |     function getPageIdByOffset($index)
 | 
        
           |  |  | 87 |     {
 | 
        
           |  |  | 88 |         if (!isset($this->_pageData)) {
 | 
        
           |  |  | 89 |             $this->_generatePageData();
 | 
        
           |  |  | 90 |         }
 | 
        
           |  |  | 91 |   | 
        
           |  |  | 92 |         if (($index % $this->_perPage) > 0) {
 | 
        
           |  |  | 93 |             $pageID = ceil((float)$index / (float)$this->_perPage);
 | 
        
           |  |  | 94 |         } else {
 | 
        
           |  |  | 95 |             $pageID = $index / $this->_perPage;
 | 
        
           |  |  | 96 |         }
 | 
        
           |  |  | 97 |         return $pageID;
 | 
        
           |  |  | 98 |     }
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 |     // }}}
 | 
        
           |  |  | 101 |     // {{{ getPageRangeByPageId()
 | 
        
           |  |  | 102 |   | 
        
           |  |  | 103 |     /**
 | 
        
           |  |  | 104 |      * Given a PageId, it returns the limits of the range of pages displayed.
 | 
        
           |  |  | 105 |      * While getOffsetByPageId() returns the offset of the data within the
 | 
        
           |  |  | 106 |      * current page, this method returns the offsets of the page numbers interval.
 | 
        
           |  |  | 107 |      * E.g., if you have pageId=3 and delta=10, it will return (1, 10).
 | 
        
           |  |  | 108 |      * PageID of 8 would give you (1, 10) as well, because 1 <= 8 <= 10.
 | 
        
           |  |  | 109 |      * PageID of 11 would give you (11, 20).
 | 
        
           |  |  | 110 |      * If the method is called without parameter, pageID is set to currentPage#.
 | 
        
           |  |  | 111 |      *
 | 
        
           |  |  | 112 |      * @param integer PageID to get offsets for
 | 
        
           |  |  | 113 |      * @return array  First and last offsets
 | 
        
           |  |  | 114 |      * @access public
 | 
        
           |  |  | 115 |      */
 | 
        
           |  |  | 116 |     function getPageRangeByPageId($pageid = null)
 | 
        
           |  |  | 117 |     {
 | 
        
           |  |  | 118 |         $pageid = isset($pageid) ? (int)$pageid : $this->_currentPage;
 | 
        
           |  |  | 119 |         if (isset($this->_pageData[$pageid]) || is_null($this->_itemData)) {
 | 
        
           |  |  | 120 |             // I'm sure I'm missing something here, but this formula works
 | 
        
           |  |  | 121 |             // so I'm using it until I find something simpler.
 | 
        
           |  |  | 122 |             $start = ((($pageid + (($this->_delta - ($pageid % $this->_delta))) % $this->_delta) / $this->_delta) - 1) * $this->_delta +1;
 | 
        
           |  |  | 123 |             return array(
 | 
        
           |  |  | 124 |                 max($start, 1),
 | 
        
           |  |  | 125 |                 min($start+$this->_delta-1, $this->_totalPages)
 | 
        
           |  |  | 126 |             );
 | 
        
           |  |  | 127 |         } else {
 | 
        
           |  |  | 128 |             return array(0, 0);
 | 
        
           |  |  | 129 |         }
 | 
        
           |  |  | 130 |     }
 | 
        
           |  |  | 131 |   | 
        
           |  |  | 132 |     // }}}
 | 
        
           |  |  | 133 |     // {{{ getLinks()
 | 
        
           |  |  | 134 |   | 
        
           |  |  | 135 |     /**
 | 
        
           |  |  | 136 |      * Returns back/next/first/last and page links,
 | 
        
           |  |  | 137 |      * both as ordered and associative array.
 | 
        
           |  |  | 138 |      *
 | 
        
           |  |  | 139 |      * NB: in original PEAR::Pager this method accepted two parameters,
 | 
        
           |  |  | 140 |      * $back_html and $next_html. Now the only parameter accepted is
 | 
        
           |  |  | 141 |      * an integer ($pageID), since the html text for prev/next links can
 | 
        
           |  |  | 142 |      * be set in the constructor. If a second parameter is provided, then
 | 
        
           |  |  | 143 |      * the method act as it previously did. This hack's only purpose is to
 | 
        
           |  |  | 144 |      * mantain backward compatibility.
 | 
        
           |  |  | 145 |      *
 | 
        
           |  |  | 146 |      * @param integer $pageID Optional pageID. If specified, links
 | 
        
           |  |  | 147 |      *                for that page are provided instead of current one.
 | 
        
           |  |  | 148 |      *                [ADDED IN NEW PAGER VERSION]
 | 
        
           |  |  | 149 |      * @param  string $next_html HTML to put inside the next link
 | 
        
           |  |  | 150 |      *                [deprecated: use the constructor instead]
 | 
        
           |  |  | 151 |      * @return array Back/pages/next links
 | 
        
           |  |  | 152 |      */
 | 
        
           |  |  | 153 |     function getLinks($pageID=null, $next_html='')
 | 
        
           |  |  | 154 |     {
 | 
        
           |  |  | 155 |         //BC hack
 | 
        
           |  |  | 156 |         if (!empty($next_html)) {
 | 
        
           |  |  | 157 |             $back_html = $pageID;
 | 
        
           |  |  | 158 |             $pageID    = null;
 | 
        
           |  |  | 159 |         } else {
 | 
        
           |  |  | 160 |             $back_html = '';
 | 
        
           |  |  | 161 |         }
 | 
        
           |  |  | 162 |   | 
        
           |  |  | 163 |         if (!is_null($pageID)) {
 | 
        
           |  |  | 164 |             $this->links = '';
 | 
        
           |  |  | 165 |             if ($this->_totalPages > $this->_delta) {
 | 
        
           |  |  | 166 |                 $this->links .= $this->_printFirstPage();
 | 
        
           |  |  | 167 |             }
 | 
        
           |  |  | 168 |   | 
        
           |  |  | 169 |             $_sav = $this->_currentPage;
 | 
        
           |  |  | 170 |             $this->_currentPage = $pageID;
 | 
        
           |  |  | 171 |   | 
        
           |  |  | 172 |             $this->links .= $this->_getBackLink('', $back_html);
 | 
        
           |  |  | 173 |             $this->links .= $this->_getPageLinks();
 | 
        
           |  |  | 174 |             $this->links .= $this->_getNextLink('', $next_html);
 | 
        
           |  |  | 175 |             if ($this->_totalPages > $this->_delta) {
 | 
        
           |  |  | 176 |                 $this->links .= $this->_printLastPage();
 | 
        
           |  |  | 177 |             }
 | 
        
           |  |  | 178 |         }
 | 
        
           |  |  | 179 |   | 
        
           |  |  | 180 |         $back  = str_replace(' ', '', $this->_getBackLink());
 | 
        
           |  |  | 181 |         $next  = str_replace(' ', '', $this->_getNextLink());
 | 
        
           |  |  | 182 |         $pages = $this->_getPageLinks();
 | 
        
           |  |  | 183 |         $first = $this->_printFirstPage();
 | 
        
           |  |  | 184 |         $last  = $this->_printLastPage();
 | 
        
           |  |  | 185 |         $all   = $this->links;
 | 
        
           |  |  | 186 |         $linkTags = $this->linkTags;
 | 
        
           |  |  | 187 |   | 
        
           |  |  | 188 |         if (!is_null($pageID)) {
 | 
        
           |  |  | 189 |             $this->_currentPage = $_sav;
 | 
        
           |  |  | 190 |         }
 | 
        
           |  |  | 191 |   | 
        
           |  |  | 192 |         return array(
 | 
        
           |  |  | 193 |             $back,
 | 
        
           |  |  | 194 |             $pages,
 | 
        
           |  |  | 195 |             trim($next),
 | 
        
           |  |  | 196 |             $first,
 | 
        
           |  |  | 197 |             $last,
 | 
        
           |  |  | 198 |             $all,
 | 
        
           |  |  | 199 |             $linkTags,
 | 
        
           |  |  | 200 |             'back'  => $back,
 | 
        
           |  |  | 201 |             'pages' => $pages,
 | 
        
           |  |  | 202 |             'next'  => $next,
 | 
        
           |  |  | 203 |             'first' => $first,
 | 
        
           |  |  | 204 |             'last'  => $last,
 | 
        
           |  |  | 205 |             'all'   => $all,
 | 
        
           |  |  | 206 |             'linktags' => $linkTags
 | 
        
           |  |  | 207 |         );
 | 
        
           |  |  | 208 |     }
 | 
        
           |  |  | 209 |   | 
        
           |  |  | 210 |     // }}}
 | 
        
           |  |  | 211 |     // {{{ _getPageLinks()
 | 
        
           |  |  | 212 |   | 
        
           |  |  | 213 |     /**
 | 
        
           |  |  | 214 |      * Returns pages link
 | 
        
           |  |  | 215 |      *
 | 
        
           |  |  | 216 |      * @param $url  URL to use in the link
 | 
        
           |  |  | 217 |      *              [deprecated: use the constructor instead]
 | 
        
           |  |  | 218 |      * @return string Links
 | 
        
           |  |  | 219 |      * @access private
 | 
        
           |  |  | 220 |      */
 | 
        
           |  |  | 221 |     function _getPageLinks($url = '')
 | 
        
           |  |  | 222 |     {
 | 
        
           |  |  | 223 |         //legacy setting... the preferred way to set an option now
 | 
        
           |  |  | 224 |         //is adding it to the constuctor
 | 
        
           |  |  | 225 |         if (!empty($url)) {
 | 
        
           |  |  | 226 |             $this->_path = $url;
 | 
        
           |  |  | 227 |         }
 | 
        
           |  |  | 228 |   | 
        
           |  |  | 229 |         //If there's only one page, don't display links
 | 
        
           |  |  | 230 |         if ($this->_clearIfVoid && ($this->_totalPages < 2)) {
 | 
        
           |  |  | 231 |             return '';
 | 
        
           |  |  | 232 |         }
 | 
        
           |  |  | 233 |   | 
        
           |  |  | 234 |         $links = '';
 | 
        
           |  |  | 235 |         $limits = $this->getPageRangeByPageId($this->_currentPage);
 | 
        
           |  |  | 236 |   | 
        
           |  |  | 237 |         for ($i=$limits[0]; $i<=min($limits[1], $this->_totalPages); $i++) {
 | 
        
           |  |  | 238 |             if ($i != $this->_currentPage) {
 | 
        
           |  |  | 239 |                 $this->range[$i] = false;
 | 
        
           |  |  | 240 |                 $this->_linkData[$this->_urlVar] = $i;
 | 
        
           |  |  | 241 |                 $links .= $this->_renderLink($this->_altPage.' '.$i, $i);
 | 
        
           |  |  | 242 |             } else {
 | 
        
           |  |  | 243 |                 $this->range[$i] = true;
 | 
        
           |  |  | 244 |                 $links .= $this->_curPageSpanPre . $i . $this->_curPageSpanPost;
 | 
        
           |  |  | 245 |             }
 | 
        
           |  |  | 246 |             $links .= $this->_spacesBefore
 | 
        
           |  |  | 247 |                    . (($i != $this->_totalPages) ? $this->_separator.$this->_spacesAfter : '');
 | 
        
           |  |  | 248 |         }
 | 
        
           |  |  | 249 |         return $links;
 | 
        
           |  |  | 250 |     }
 | 
        
           |  |  | 251 |   | 
        
           |  |  | 252 |     // }}}
 | 
        
           |  |  | 253 | }
 | 
        
           |  |  | 254 | ?>
 |