* some minor fixes + more debugging on queries
[vsc.git] / _res / _libs / tspaginator.class.php
blob9621ee6e0f8377f3acd60bab48eda6fd5e0ef963
1 <?php
2 // the url variable
3 define ('PAG_VAR', 'pag');
4 define ('PAG_COUNT', 3);//
5 define ('PAG_ORDER', 'o');
6 /**
7 * @desc Decorator class for tdoAbstract
8 */
10 class tsPaginator {
11 private $start, $count, $total, $page, $order;
12 /**
13 * @var tdoAbstract
15 protected $tdo;
17 public function __construct () {
18 $this->getPaginationVariables ();
20 public function __destruct () {}
22 public function setCount ($int) {
23 $this->count = $int;
24 $this->start = $this->getStart();
27 public function outputOrderHeaders () {
28 return;
31 public function outputPagination () {
32 $retVal = $left = $right = '';
33 // this should happen after we made all our stuff with the tdo (joins, wheres, orders..bla)
34 // how many stuff we got
35 $t = $this->getTotal();
36 $cnt = ceil ($this->getTotal () / $this->count);
37 $here = tsController::getRequest(NAV_VAR);
39 for ($i=1; $i <= $cnt; $i++) {
40 $r = tsController::setRequest($here, array(PAG_VAR => $i));
41 if ($i != $this->page)
42 $retVal .= '<a href="' . tsController::setRequest($here, array(PAG_VAR => $i)) . '">'.$i.'</a> ';
43 else
44 $retVal .= $i.' ';
47 if ($this->page > 1)
48 $left = '<a href="'.tsController::setRequest($here, array (PAG_VAR => $this->page-1)).'">&laquo;</a> ';
50 if ($this->page < $cnt)
51 $right = '<a href="'.tsController::setRequest($here, array (PAG_VAR => $this->page+1)).'">&raquo;</a>';;
53 return $left . $retVal . $right;
56 public function addObject (&$object) {
57 if (!($object instanceof tdoAbstract))
58 return false;
59 $this->tdo = &$object;
61 if (!empty ($this->order))
62 $this->addOrder ();
63 // if (!empty ($this->start))
64 $this->addLimit ();
67 protected function getTotal () {
68 // getting the number of elements
69 if (empty ($this->total) && ($this->tdo instanceof tdoAbstract)) {
70 $this->total = (int)$this->tdo->getCount ();
72 return (int)$this->total;
75 protected function getStart () {
76 if ((int)$this->page <= 0 /*/||
77 (int)$this->start > $this->getTotal()/**/
78 ) {
79 $this->page = 1;
81 // $this->start =
82 // calculate the limit based on $total count of items and the pagination
83 return $this->count * ($this->page-1) /*/+ 1/**/;//$this->start;
86 protected function getPaginationVariables () {
87 // get the page variables: URL?p=?
88 if (empty($this->count))
89 $this->count = PAG_COUNT;
91 $this->page = (int)tsController::getRequest (PAG_VAR);
93 $this->start = $this->getStart ();
95 // get the ORDER BY variables: URL?o=column_name:(bool)ASC
96 $o = tsController::getRequest (PAG_ORDER);
98 if (!empty ($o))
99 $this->order = explode (':', $o);
103 protected function addOrder () {
104 // this is dumb as it requires a column name to comre from user input
105 if ($this->tdo instanceof tdoAbstract && !empty ($this->order)) {
106 $fieldName = $this->order[0];
107 $order = (bool)$this->order[1]; // if we have no value we treat it as DESC
109 // $this->tdo->addOrder ($fieldName, $order);
113 protected function addLimit () {
114 // var_dump($this->start, $this->count);
115 if ($this->tdo instanceof tdoAbstract) {
116 $this->tdo->addLimit ($this->start, $this->count);