00001 <?php
00003
00004
00005
00006
00007
00008
00010
00029 class FLEA_Helper_Pager
00030 {
00039 var $source;
00040
00047 var $dbo;
00048
00054 var $_conditions;
00055
00061 var $_sortby;
00062
00068 var $_basePageIndex = 0;
00069
00075 var $pageSize = -1;
00076
00082 var $totalCount = -1;
00083
00089 var $count = -1;
00090
00096 var $pageCount = -1;
00097
00103 var $firstPage = -1;
00104
00110 var $firstPageNumber = -1;
00111
00117 var $lastPage = -1;
00118
00124 var $lastPageNumber = -1;
00125
00131 var $prevPage = -1;
00132
00138 var $prevPageNumber = -1;
00139
00145 var $nextPage = -1;
00146
00152 var $nextPageNumber = -1;
00153
00159 var $currentPage = -1;
00160
00166 var $_currentPage = -1;
00167
00173 var $currentPageNumber = -1;
00174
00198 function FLEA_Helper_Pager(& $source, $currentPage, $pageSize = 20, $conditions = null, $sortby = null, $basePageIndex = 0)
00199 {
00200 $this->_basePageIndex = $basePageIndex;
00201 $this->_currentPage = $this->currentPage = $currentPage;
00202 $this->pageSize = $pageSize;
00203
00204 if (is_object($source)) {
00205 $this->source =& $source;
00206 $this->_conditions = $conditions;
00207 $this->_sortby = $sortby;
00208 $this->totalCount = $this->count = (int)$this->source->findCount($conditions);
00209 $this->computingPage();
00210 } elseif (!empty($source)) {
00211 $this->source = $source;
00212 $sql = "SELECT COUNT(*) FROM ( $source ) as _count_table";
00213 $this->dbo =& FLEA::getDBO();
00214 $this->totalCount = $this->count = (int)$this->dbo->getOne($sql);
00215 $this->computingPage();
00216 }
00217 }
00218
00224 function setBasePageIndex($index)
00225 {
00226 $this->_basePageIndex = $index;
00227 $this->currentPage = $this->_currentPage;
00228 $this->computingPage();
00229 }
00230
00236 function setPage($page)
00237 {
00238 $this->_currentPage = $page;
00239 $this->currentPage = $page;
00240 $this->computingPage();
00241 }
00242
00248 function setCount($count)
00249 {
00250 $this->count = $count;
00251 $this->computingPage();
00252 }
00253
00259 function setDBO(& $dbo)
00260 {
00261 $this->dbo =& $dbo;
00262 }
00263
00272 function & findAll($fields = '*', $queryLinks = true)
00273 {
00274 if ($this->count == -1) {
00275 $this->count = 20;
00276 }
00277
00278 $offset = ($this->currentPage - $this->_basePageIndex) * $this->pageSize;
00279 if (is_object($this->source)) {
00280 $limit = array($this->pageSize, $offset);
00281 $rowset = $this->source->findAll($this->_conditions, $this->_sortby, $limit, $fields, $queryLinks);
00282 } else {
00283 if (is_null($this->dbo)) {
00284 $this->dbo =& FLEA::getDBO(false);
00285 }
00286 $rs = $this->dbo->selectLimit($this->source, $this->pageSize, $offset);
00287 $rowset = $this->dbo->getAll($rs);
00288 }
00289 return $rowset;
00290 }
00291
00299 function getPagerData($returnPageNumbers = true)
00300 {
00301 $data = array(
00302 'pageSize' => $this->pageSize,
00303 'totalCount' => $this->totalCount,
00304 'count' => $this->count,
00305 'pageCount' => $this->pageCount,
00306 'firstPage' => $this->firstPage,
00307 'firstPageNumber' => $this->firstPageNumber,
00308 'lastPage' => $this->lastPage,
00309 'lastPageNumber' => $this->lastPageNumber,
00310 'prevPage' => $this->prevPage,
00311 'prevPageNumber' => $this->prevPageNumber,
00312 'nextPage' => $this->nextPage,
00313 'nextPageNumber' => $this->nextPageNumber,
00314 'currentPage' => $this->currentPage,
00315 'currentPageNumber' => $this->currentPageNumber,
00316 );
00317
00318 if ($returnPageNumbers) {
00319 $data['pagesNumber'] = array();
00320 for ($i = 0; $i < $this->pageCount; $i++) {
00321 $data['pagesNumber'][$i] = $i + 1;
00322 }
00323 }
00324
00325 return $data;
00326 }
00327
00336 function getNavbarIndexs($currentPage = 0, $navbarLen = 8)
00337 {
00338 $mid = intval($navbarLen / 2);
00339 if ($currentPage < $this->firstPage) {
00340 $currentPage = $this->firstPage;
00341 }
00342 if ($currentPage > $this->lastPage) {
00343 $currentPage = $this->lastPage;
00344 }
00345
00346 $begin = $currentPage - $mid;
00347 if ($begin < $this->firstPage) { $begin = $this->firstPage; }
00348 $end = $begin + $navbarLen - 1;
00349 if ($end >= $this->lastPage) {
00350 $end = $this->lastPage;
00351 $begin = $end - $navbarLen + 1;
00352 if ($begin < $this->firstPage) { $begin = $this->firstPage; }
00353 }
00354
00355 $data = array();
00356 for ($i = $begin; $i <= $end; $i++) {
00357 $data[] = array('index' => $i, 'number' => ($i + 1 - $this->_basePageIndex));
00358 }
00359 return $data;
00360 }
00361
00368 function renderPageJumper($caption = '%u', $jsfunc = 'fnOnPageChanged')
00369 {
00370 $out = "<select name=\"PageJumper\" onchange=\"{$jsfunc}(this.value);\">\n";
00371 for ($i = $this->firstPage; $i <= $this->lastPage; $i++) {
00372 $out .= "<option value=\"{$i}\"";
00373 if ($i == $this->currentPage) {
00374 $out .= " selected";
00375 }
00376 $out .=">";
00377 $out .= sprintf($caption, $i + 1 - $this->_basePageIndex);
00378 $out .= "</option>\n";
00379 }
00380 $out .= "</select>\n";
00381 echo $out;
00382 }
00383
00387 function computingPage()
00388 {
00389 $this->pageCount = ceil($this->count / $this->pageSize);
00390 $this->firstPage = $this->_basePageIndex;
00391 $this->lastPage = $this->pageCount + $this->_basePageIndex - 1;
00392 if ($this->lastPage < $this->firstPage) { $this->lastPage = $this->firstPage; }
00393
00394 if ($this->lastPage < $this->_basePageIndex) {
00395 $this->lastPage = $this->_basePageIndex;
00396 }
00397
00398 if ($this->currentPage >= $this->pageCount + $this->_basePageIndex) {
00399 $this->currentPage = $this->lastPage;
00400 }
00401
00402 if ($this->currentPage < $this->_basePageIndex) {
00403 $this->currentPage = $this->firstPage;
00404 }
00405
00406 if ($this->currentPage < $this->lastPage - 1) {
00407 $this->nextPage = $this->currentPage + 1;
00408 } else {
00409 $this->nextPage = $this->lastPage;
00410 }
00411
00412 if ($this->currentPage > $this->_basePageIndex) {
00413 $this->prevPage = $this->currentPage - 1;
00414 } else {
00415 $this->prevPage = $this->_basePageIndex;
00416 }
00417
00418 $this->firstPageNumber = $this->firstPage + 1 - $this->_basePageIndex;
00419 $this->lastPageNumber = $this->lastPage + 1 - $this->_basePageIndex;
00420 $this->nextPageNumber = $this->nextPage + 1 - $this->_basePageIndex;
00421 $this->prevPageNumber = $this->prevPage + 1 - $this->_basePageIndex;
00422 $this->currentPageNumber = $this->currentPage + 1 - $this->_basePageIndex;
00423 }
00424 }