* made the paginator work
[vsc.git] / _res / _libs / tspaginator.class.php
blobac8eb4f5c7331333410b0c7c6870af8f1838fb3b
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 public $start, $count, $page, $order;
12 /**
13 * @var tdoAbstract
15 protected $tdo;
17 public function __construct () {
18 $this->getPaginationVariables ();
20 public function __destruct () {}
22 public function getTotal () {
23 // getting the number of elements
24 if (empty ($this->total)) {
25 $this->total = $this->tdo->getCount ();
27 return $this->total;
30 public function addObject (&$object) {
31 if (!($object instanceof tdoAbstract))
32 return false;
33 $this->tdo = &$object;
35 if (!empty ($this->order))
36 $this->addOrder ();
37 if (!empty ($this->start))
38 $this->addLimit ();
41 protected function getStart () {
42 if ((int)$this->page <= 0) {
43 $this->page = 1;
45 // calculate the limit based on $total count of items and the pagination
46 return $this->count * ($this->page-1) /*/+ 1/**/;
49 public function getPaginationVariables () {
50 // get the page variables: URL?p=?
51 $this->count = PAG_COUNT;
52 $this->page = tsController::getRequest (PAG_VAR);
54 $this->start = $this->getStart ();
56 // get the ORDER BY variables: URL?o=column_name:(bool)ASC
57 $o = tsController::getRequest (PAG_ORDER);
58 if (!empty ($o))
59 $this->order = explode (':', $o);
62 public function outputOrderHeaders () {
63 return;
66 public function outputPagination () {
67 $retVal = $left = $right = '';
68 // this should happen after we made all our stuff with the tdo (joins, wheres, orders..bla)
69 // how many stuff we got
70 $t = $this->getTotal();
71 $cnt = ceil ($this->getTotal () / $this->count);
72 $here = tsController::getRequest(NAV_VAR);
74 for ($i=1; $i <= $cnt; $i++) {
75 $r = tsController::setRequest($here, array(PAG_VAR => $i));
76 if ($i != $this->page)
77 $retVal .= '<a href="' . tsController::setRequest($here, array(PAG_VAR => $i)) . '">'.$i.'</a> ';
78 else
79 $retVal .= $i.' ';
82 if ($this->page > 1)
83 $left = '<a href="'.tsController::setRequest($here, array (PAG_VAR => $this->page-1)).'">&laquo;</a> ';
85 if ($this->page < $cnt)
86 $right = '<a href="'.tsController::setRequest($here, array (PAG_VAR => $this->page+1)).'">&raquo;</a>';;
88 return $left . $retVal . $right;
91 public function addOrder () {
92 if ($this->tdo instanceof tdoAbstract && !empty ($this->order)) {
93 $fieldName = $this->order[0];
94 $order = (bool)$this->order[1]; // if we have no value we treat it as DESC
96 $this->tdo->addOrder ($fieldName, $order);
100 public function addLimit () {
101 if ($this->tdo instanceof tdoAbstract) {
102 // $this->tdo->addLimit ($this->start, $this->count);