123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598 |
- <?php
- class PropelPager implements Countable, Iterator
- {
- private $recordCount;
- private $pages;
- private $peerClass;
- private $peerSelectMethod;
- private $peerCountMethod;
- private $criteria;
- private $countCriteria;
- private $page;
- private $rs = null;
-
-
- private $currentKey = 0;
-
- protected $start = 0;
-
- protected $max = 0;
-
- public function __construct($c = null, $peerClass = null, $peerSelectMethod = null, $page = 1, $rowsPerPage = 25)
- {
- if (!isset($c)) {
- $c = new Criteria();
- }
- $this->setCriteria($c);
- $this->setPeerClass($peerClass);
- $this->setPeerSelectMethod($peerSelectMethod);
- $this->guessPeerCountMethod();
- $this->setPage($page);
- $this->setRowsPerPage($rowsPerPage);
- }
-
- public function setCriteria(Criteria $c)
- {
- $this->criteria = $c;
- }
-
- public function getCriteria()
- {
- return $this->criteria;
- }
-
- public function setPeerClass($class)
- {
- $this->peerClass = $class;
- }
-
- public function getPeerClass()
- {
- return $this->peerClass;
- }
-
- public function setPeerMethod($method)
- {
- $this->setPeerSelectMethod($method);
- }
-
- public function getPeerMethod()
- {
- return $this->getPeerSelectMethod();
- }
-
- public function setPeerSelectMethod($method)
- {
- $this->peerSelectMethod = $method;
- }
-
- public function getPeerSelectMethod()
- {
- return $this->peerSelectMethod;
- }
-
- public function setPeerCountMethod($method)
- {
- $this->peerCountMethod = $method;
- }
-
- public function getPeerCountMethod()
- {
- return $this->peerCountMethod;
- }
-
- private function guessPeerCountMethod()
- {
- $selectMethod = $this->getPeerSelectMethod();
- if ($selectMethod == 'doSelect') {
- $countMethod = 'doCount';
- } elseif ( ($pos = stripos($selectMethod, 'doSelectJoin')) === 0) {
- $countMethod = 'doCount' . substr($selectMethod, strlen('doSelect'));
- } else {
-
-
-
- $countMethod = 'doCount';
- }
- $this->setPeerCountMethod($countMethod);
- }
-
- public function getResult()
- {
- if (!isset($this->rs)) {
- $this->doRs();
- }
- return $this->rs;
- }
-
- private function doRs()
- {
- $this->criteria->setOffset($this->start);
- $this->criteria->setLimit($this->max);
- $this->rs = call_user_func(array($this->getPeerClass(), $this->getPeerSelectMethod()), $this->criteria);
- }
-
- public function getFirstPage()
- {
- return '1';
- }
-
- public function atFirstPage()
- {
- return $this->getPage() == $this->getFirstPage();
- }
-
- public function getLastPage()
- {
- $totalPages = $this->getTotalPages();
- if ($totalPages == 0) {
- return 1;
- } else {
- return $totalPages;
- }
- }
-
- public function atLastPage()
- {
- return $this->getPage() == $this->getLastPage();
- }
-
- public function getTotalPages() {
- if (!isset($this->pages)) {
- $recordCount = $this->getTotalRecordCount();
- if ($this->max > 0) {
- $this->pages = ceil($recordCount/$this->max);
- } else {
- $this->pages = 0;
- }
- }
- return $this->pages;
- }
-
- public function getPrevLinks($range = 5)
- {
- $total = $this->getTotalPages();
- $start = $this->getPage() - 1;
- $end = $this->getPage() - $range;
- $first = $this->getFirstPage();
- $links = array();
- for ($i=$start; $i>$end; $i--) {
- if ($i < $first) {
- break;
- }
- $links[] = $i;
- }
- return array_reverse($links);
- }
-
- public function getNextLinks($range = 5)
- {
- $total = $this->getTotalPages();
- $start = $this->getPage() + 1;
- $end = $this->getPage() + $range;
- $last = $this->getLastPage();
- $links = array();
- for ($i=$start; $i<$end; $i++) {
- if ($i > $last) {
- break;
- }
- $links[] = $i;
- }
- return $links;
- }
-
- public function isLastPageComplete()
- {
- return !($this->getTotalRecordCount() % $this->max);
- }
-
- public function getPrev() {
- if ($this->getPage() != $this->getFirstPage()) {
- $prev = $this->getPage() - 1;
- } else {
- $prev = false;
- }
- return $prev;
- }
-
- public function getNext() {
- if ($this->getPage() != $this->getLastPage()) {
- $next = $this->getPage() + 1;
- } else {
- $next = false;
- }
- return $next;
- }
-
- public function setPage($page)
- {
- $this->page = $page;
-
- $this->calculateStart();
- }
-
- public function getPage()
- {
- return $this->page;
- }
-
- public function setRowsPerPage($r)
- {
- $this->max = $r;
-
- $this->calculateStart();
- }
-
- public function getRowsPerPage()
- {
- return $this->max;
- }
-
- private function calculateStart()
- {
- $this->start = ( ($this->page - 1) * $this->max );
- }
-
- public function getTotalRecordCount()
- {
- if (!isset($this->rs)) {
- $this->doRs();
- }
- if (empty($this->recordCount)) {
- $this->countCriteria = clone $this->criteria;
- $this->countCriteria->setLimit(0);
- $this->countCriteria->setOffset(0);
- $this->recordCount = call_user_func(
- array(
- $this->getPeerClass(),
- $this->getPeerCountMethod()
- ),
- $this->countCriteria
- );
- }
- return $this->recordCount;
- }
-
- public function setStart($v)
- {
- $this->start = $v;
- }
-
- public function setMax($v)
- {
- $this->max = $v;
- }
-
-
- public function count()
- {
- return count($this->getResult());
- }
-
-
- public function current()
- {
- if (!isset($this->rs)) {
- $this->doRs();
- }
- return $this->rs[$this->currentKey];
- }
-
-
- public function key()
- {
- return $this->currentKey;
- }
-
-
- public function next()
- {
- $this->currentKey++;
- }
-
-
- public function rewind()
- {
- $this->currentKey = 0;
- }
-
-
- public function valid()
- {
- if (!isset($this->rs)) {
- $this->doRs();
- }
- return in_array($this->currentKey, array_keys($this->rs));
- }
- }
|