* modifications related to enabling memcache and adding some expire headers
[vsc.git] / _res / _libs / tspaginator.class.php
blobfb979a0838379a7982f3ac77e4562c79e07431f1
1 <?php
2 // the url variable
3 define ('PAG_VAR', 'pag');
4 define ('PAG_COUNT', 10);//
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 // this will be replaced by the helper functionality of using templates :D
48 if ($this->page > 1)
49 $left = '<span class="paginator_left"><a href="'.tsController::setRequest($here, array (PAG_VAR => $this->page-1)).'">&laquo;</a></span> ';
50 else
51 $left = '<span class="paginator_left">&laquo;</span> ';
52 if ($this->page < $cnt)
53 $right = '<span class="paginator_left"><a href="'.tsController::setRequest($here, array (PAG_VAR => $this->page+1)).'">&raquo;</a></span>';
54 else
55 $right = '<span class="paginator_right">&raquo;</span>';
57 return $left . $retVal . $right;
60 public function addObject (&$object) {
61 if (!($object instanceof tdoAbstract))
62 return false;
63 $this->tdo = &$object;
65 if (!empty ($this->order))
66 $this->addOrder ();
67 // if (!empty ($this->start))
68 $this->addLimit ();
71 protected function getTotal () {
72 // getting the number of elements
73 if (empty ($this->total) && ($this->tdo instanceof tdoAbstract)) {
74 $this->total = (int)$this->tdo->getCount ();
76 return (int)$this->total;
79 protected function getStart () {
80 if ((int)$this->page <= 0 /*/||
81 (int)$this->start > $this->getTotal()/**/
82 ) {
83 $this->page = 1;
85 // $this->start =
86 // calculate the limit based on $total count of items and the pagination
87 return $this->count * ($this->page-1) /*/+ 1/**/;//$this->start;
90 protected function getPaginationVariables () {
91 // get the page variables: URL?p=?
92 if (empty($this->count))
93 $this->count = PAG_COUNT;
95 $this->page = (int)tsController::getRequest (PAG_VAR);
97 $this->start = $this->getStart ();
99 // get the ORDER BY variables: URL?o=column_name:(bool)ASC
100 $o = tsController::getRequest (PAG_ORDER);
102 if (!empty ($o))
103 $this->order = explode (':', $o);
107 protected function addOrder () {
108 // this is dumb as it requires a column name to comre from user input
109 if ($this->tdo instanceof tdoAbstract && !empty ($this->order)) {
110 $fieldName = $this->order[0];
111 $order = (bool)$this->order[1]; // if we have no value we treat it as DESC
113 // $this->tdo->addOrder ($fieldName, $order);
117 protected function addLimit () {
118 // var_dump($this->start, $this->count);
119 if ($this->tdo instanceof tdoAbstract) {
120 $this->tdo->addLimit ($this->start, $this->count);