[ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 define("DEFAULT_PAGER_REGS_FOR_PAGE", 25); 4 define("DEFAULT_PAGER_MAX_PAGES", 1); 5 6 /** 7 * \defgroup Pager 8 * 9 * generic implementation of a pager. It doesn't take of generating SQL queries for paging or 10 * anythign like that, it only takes care of generating the number of pages based on the number 11 * of registers, keeping track of the current page, etc. 12 * 13 * There also needs to be some display logic in order to get it to work fine. 14 * 15 * At the PHP level, some code like this is necessary: 16 * 17 * <pre> 18 * $pager = new Pager( "?op=editPosts&showStatus={$this->_showStatus}&page=", 19 * $this->_page, 20 * $numPosts, 21 * $this->_itemsPerPage ); 22 * $view->setValue( "pager", $pager ); 23 * </pre> 24 * 25 * The first parameter passed to the constructor is the string that the pager class will use 26 * to generate the page links. It will only append a page number, nothing else. 27 * 28 * At the Smarty/template level, some code like this is necessary in order to display the links 29 * properly, etc: 30 * 31 * <pre> 32 * {if !$pager->isFirstPage() && !$pager->isEmpty()} 33 * <a class="pagerLink" href="{$pager->getPrevPageLink()}">«Prev</a> 34 * {/if} 35 * {foreach from=$pager->getPageLinks() item=pageLink key=pageId} 36 * {if $pageId == $pager->getCurrentPage()} 37 * <span class="pagerCurrent"> {$pageId} >/span< 38 * {else} 39 * <a class="pagerLink" href="{$pageLink}"> {$pageId} </a> 40 * {/if} 41 * {/foreach} 42 * {if !$pager->isLastPage() && !$pager->isEmpty()} 43 * <a class="pagerLink" href="{$pager->getNextPageLink()}">Next»</a> 44 * {/if} 45 * </pre> 46 * 47 * The display logic might look a bit complex but it is unavoidable... 48 */ 49 class Pager 50 { 51 var $_baseUrl; 52 var $_totalRegs; 53 var $_regsForPage; 54 var $_maxPages; 55 var $_totalPages; 56 var $_curPage; 57 var $_startPage; 58 var $_endPage; 59 var $_pageLinks; 60 61 /** 62 * Constructor of the pager 63 * 64 * @param baseUrl The base url that will be used to generate the different URLs to the pages 65 * @param curPage The current page 66 * @param totalRegs The total number of registers 67 * @param regsForPage The maximum number of registers per page, defaults to 68 * DEFAULT_PAGER_REGS_FOR_PAGE 69 */ 70 function Pager($baseUrl, $curPage, $totalRegs, $regsForPage = DEFAULT_PAGER_REGS_FOR_PAGE ) 71 { 72 $this->_baseUrl = $baseUrl; 73 $this->_curPage = $curPage; 74 $this->_totalRegs = $totalRegs; 75 $this->_regsForPage = ($regsForPage < 1) ? DEFAULT_PAGER_REGS_FOR_PAGE : $regsForPage; 76 $this->_maxPages = DEFAULT_PAGER_MAX_PAGES; 77 78 $this->_init(); 79 80 } 81 82 /** 83 * Sets the base url 84 * 85 * @param url The url 86 */ 87 function setBaseUrl($url) 88 { 89 $this->_baseUrl = $url; 90 } 91 92 /** 93 * @return returns the base url 94 */ 95 function getBaseUrl() 96 { 97 return $this->_baseUrl; 98 } 99 100 /** 101 * @return Returns the current total amount of registers 102 */ 103 function getTotalRegs() 104 { 105 return $this->_totalRegs; 106 } 107 108 /** 109 * @return Returns the current number of records per page 110 */ 111 function getRegsForPage() 112 { 113 return $this->_regsForPage; 114 } 115 116 /** 117 * @return Returns the maximum number of pages 118 */ 119 function getMaxPages() 120 { 121 return $this->_maxPages; 122 } 123 124 /** 125 * @return The total number of pages 126 */ 127 function getTotalPages() 128 { 129 return $this->_totalPages; 130 } 131 132 /** 133 * @return The current page 134 */ 135 function getCurrentPage() 136 { 137 return $this->_curPage; 138 } 139 140 /** 141 * returns the number of the next page 142 * 143 * @return number of the next page 144 */ 145 function getNextPage() 146 { 147 $page = $this->getCurrentPage(); 148 if( !$this->isLastPage()) 149 $page++; 150 151 return $page; 152 } 153 154 /** 155 * returns the number of the previous page 156 * 157 * @return number of the previous page 158 */ 159 function getPrevPage() 160 { 161 $page = $this->getCurrentPage(); 162 if( !$this->isFirstPage()) 163 $page--; 164 165 return $page; 166 } 167 168 /** 169 * @return Returns the current start page, if any 170 */ 171 function getStartPage() 172 { 173 return $this->_startPage; 174 } 175 176 /** 177 * @return Returns the last page 178 */ 179 function getEndPage() 180 { 181 return $this->_endPage; 182 } 183 184 /** 185 * @return True if the current page is the first page or false otherwise 186 */ 187 function isFirstPage() 188 { 189 return ($this->_curPage == 1); 190 } 191 192 /** 193 * @return Returns true if the current page is the last one or false otherwise 194 */ 195 function isLastPage() 196 { 197 return ($this->_curPage == $this->_totalPages); 198 } 199 200 /** 201 * @return Returns an array containing all the links to the different pages 202 */ 203 function getPageLinks() 204 { 205 return $this->_pageLinks; 206 } 207 208 /** 209 * @return Returns a link to the next page 210 */ 211 function getNextPageLink() 212 { 213 return $this->_pageLinks[$this->getNextPage()]; 214 } 215 216 /** 217 * @return Returns a link to the previous page 218 */ 219 function getPrevPageLink() 220 { 221 return $this->_pageLinks[$this->getPrevPage()]; 222 } 223 224 /** 225 * @return returns the link to the first page 226 */ 227 function getFirstPageLink() 228 { 229 return $this->_pageLinks[1]; 230 } 231 232 /** 233 * @return returns the link to the last page 234 */ 235 function getLastPageLink() 236 { 237 return $this->_pageLinks[$this->getEndPage()]; 238 } 239 240 /** 241 * generates the links to the different pages 242 * 243 * @return An associative array 244 */ 245 function generateLinks() 246 { 247 $i = 1; 248 $pages = Array(); 249 250 // check wether we need to perform a replacement or not... 251 // if not, we'll just append the page number at the end of the string 252 $replace = strpos( $this->_baseUrl, "{page}" ); 253 254 while( $i <= $this->_totalPages ) { 255 if( $replace ) { 256 $pages[$i] = str_replace( "{page}", $i, $this->_baseUrl ); 257 } 258 else { 259 $pages[$i] = $this->_baseUrl.$i; 260 } 261 262 $i++; 263 } 264 265 return $pages; 266 } 267 268 /** 269 * returns true if the pager is empty (has no pages or links) or false otherwise 270 * 271 * @return true if the pager is empty or false if not 272 */ 273 function isEmpty() 274 { 275 return( $this->_totalPages == 0 ); 276 } 277 278 /** 279 * @private 280 */ 281 function _init() 282 { 283 $this->_totalPages = ceil($this->_totalRegs / $this->_regsForPage); 284 $this->_startPage = 1; 285 $this->_endPage = $this->_totalPages; 286 $this->_pageLinks = $this->generateLinks(); 287 } 288 289 } 290 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |