+ add paginator goodness, atm not working
[vsc.git] / _res / _libs / tspaginator.class.php
blobb88ea7b9284f205339f260e45e5eb2996582df48
1 <?php
2 // the url variable
3 define ('PAG_VAR', 'pag');
4 define ('PAG_COUNT', 4);//
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->tdo->limit = null;
26 // $this->total = $this->tdo->getCount ();
27 // $this->tdo->reset ();
29 $this->total = 33;
30 return $this->total;
33 public function addObject (&$object) {
34 if (!($object instanceof tdoAbstract))
35 return false;
36 // add the pagination shit
37 $this->tdo = &$object;
40 protected function getStart ($pageNo) {
41 if ((int)$pageNo <= 0)
42 $pageNo = 1;
43 // calculate the limit based on $total count of items and the pagination
44 return $this->count * ($pageNo-1) + 1;
47 public function getPaginationVariables () {
48 // get the page variables: URL?p=?
49 $this->count = PAG_COUNT;
50 $this->page = tsController::getRequest (PAG_VAR);
51 $this->start = $this->getStart ($this->page);
53 // get the ORDER BY variables: URL?o=column_name:(bool)ASC
54 $this->order = explode (':', tsController::getRequest (PAG_ORDER));
56 public function outputOrderHeaders () {
57 return;
60 public function outputPagination () {
61 $retVal = $left = $right = '';
62 // how many stuff we got
63 $t = $this->getTotal();
64 $cnt = floor ($this->getTotal() / $this->count);
65 $here = tsController::getRequest(NAV_VAR);
67 if (!empty ($this->order))
68 $this->addOrder ();
69 if (!empty ($this->limit))
70 $this->addLimit ();
72 for ($i=1; $i <= $cnt; $i++) {
73 $r = tsController::setRequest($here, array(PAG_VAR => $i));
74 if ($i != $this->page)
75 $retVal .= '<a href="'.tsController::setRequest($here, array(PAG_VAR => $i)).'">'.$i.'</a> ';
76 else
77 $retVal .= $i.' ';
80 if ($this->page > 1)
81 $left = '<a href="'.tsController::setRequest($here, array(PAG_VAR=>$this->page-1)).'">&laquo;</a> ';
83 if ($this->page < $cnt)
84 $right = '<a href="'.tsController::setRequest($here, array(PAG_VAR=>$this->page+1)).'">&raquo;</a>';;
86 return $left . $retVal . $right;
89 public function addOrder () {
90 $fieldName = $this->order[0];
91 $order = (bool)@$this->order[1]; // if we have no value we treat it as DESC
93 if ($this->tdo instanceof tdoAbstract && !empty ($this->order)) {
94 // var_dump ($fieldName, $order, $this->tdo);die;
95 $this->tdo->addOrder ($fieldName, $order);
99 public function addLimit () {
100 if ($this->tdo instanceof tdoAbstract && !empty ($this->start)) {
101 $this->tdo->addLimit ($this->start, $this->count);