From ec2f84590efbd949e15e220180fcf307fa67795e Mon Sep 17 00:00:00 2001 From: habarnam Date: Thu, 3 Jul 2008 23:42:23 +0300 Subject: [PATCH] + add paginator goodness, atm not working --- _res/_libs/tdoabstract.class.php | 72 +++++++++++++++++++++------ _res/_libs/tspaginator.class.php | 104 +++++++++++++++++++++++++++++++++++++++ _res/_libs/tsurl.class.php | 7 +++ 3 files changed, 167 insertions(+), 16 deletions(-) create mode 100644 _res/_libs/tspaginator.class.php diff --git a/_res/_libs/tdoabstract.class.php b/_res/_libs/tdoabstract.class.php index a9fa5e8..a819d9f 100644 --- a/_res/_libs/tdoabstract.class.php +++ b/_res/_libs/tdoabstract.class.php @@ -10,7 +10,8 @@ class tdoAbstract { public $db, $wheres = array(), $groups, - $orders, +// $orders, + $limit, $refers = array(); protected $id, // public to allow debugging outside the class @@ -91,7 +92,7 @@ class tdoAbstract { $this->instantiateMembers(); } - + public function __destruct() {} /** @@ -138,7 +139,7 @@ class tdoAbstract { $this->wheres = array(); $this->groups = ''; - $this->orders = ''; +// $this->orders = ''; $this->refers = array(); } /** @@ -238,7 +239,7 @@ class tdoAbstract { $sql = $this->db->_SELECT ($this->id->name) .$this->db->_FROM ($this->name); - $this->addOrder($this->id, false); + $this->addOrder ($this->id, false); $sql .= $this->db->_ORDER ($this->outputOrders ()); @@ -382,7 +383,7 @@ class tdoAbstract { $this->wheres = array(); $this->groups = array(); - $this->orders = array(); +// $this->orders = array(); $this->refers = array(); return true; } @@ -428,7 +429,8 @@ class tdoAbstract { } } else { - throw new Exception ($fieldName . ' is not a valid member of ' . get_class($this)); + trigger_error ($fieldName . ' is not a valid member of ' . get_class($this)); +// throw new Exception ($fieldName . ' is not a valid member of ' . get_class($this)); return false; } } @@ -484,7 +486,8 @@ class tdoAbstract { if (!is_null($field->value)) $this->addWhere ($field, '=', $this->escape($field->value)); } else { - throw new Exception ($fieldName . ' is not a valid member of ' . get_class($this)); + trigger_error ($fieldName . ' is not a valid member of ' . get_class($this)); +// throw new Exception ($fieldName . ' is not a valid member of ' . get_class($this)); } } } @@ -518,7 +521,7 @@ class tdoAbstract { $orderField = &$this->fields[$orderField]; } if ($this->isValidMember ($orderField)) - $orderField->set_order ($asc); + $orderField->set_order ($asc); } public function addGroup (&$groupField) { @@ -531,28 +534,39 @@ class tdoAbstract { } /** + * @param int $start + * @param int $count + */ + + public function addLimit ($start, $count=0) { + if (empty($this->limit)) + $this->limit = $this->db->_LIMIT ($start, $count); + } + + /** * building a normal SELECT query * * @param int $start * @param int $end * @return string */ - protected function buildSql ($start = 0, $end = 0) { + protected function buildSql ($start = 0, $count = 0) { $sql = $this->db->_SELECT($this->outputFieldList()). ' FROM '.$this->name.' AS '.$this->alias.' '; $this->buildInherentWheres(); // will it work $sql .= $this->outputRefers(); - $sql .= $this->db->_WHERE($this->outputWheres());// add LIMITS - - $sql .= $this->db->_GROUP($this->outputGroups()); + $sql .= $this->db->_WHERE ($this->outputWheres());// add LIMITS - $sql .= $this->db->_ORDER($this->outputOrders()); + $sql .= $this->db->_GROUP ($this->outputGroups()); - if (!empty($start)) - $sql .= $this->db->_LIMIT($start, $end); + $sql .= $this->db->_ORDER ($this->outputOrders()); + if (!empty($start) && empty ($this->limit)) { + $this->addLimit ($start, $count); + } + $sql .= $this->limit; return $sql; } @@ -582,8 +596,34 @@ class tdoAbstract { } public function getCount() { + $this->reset(); + $this->set_fieldModifier('COUNT(%s)', $this->id); - $this->db->query($this->buildSql()); + + $sql = $this->db->_SELECT ( + sprintf ( + $this->id->modifier, + (!is_null ($this->alias) ? $this->alias. '.' : '') . + $this->id->name + ) . + $this->db->_AS($this->id->name) + ); + + $this->set_fieldModifier(null, $this->id); + $sql .= $this->db->_FROM( $this->name. $this->db->_AS($this->alias) ); + + $this->buildInherentWheres(); + + $sql .= $this->outputRefers(); + + $sql .= $this->db->_WHERE ($this->outputWheres());// add LIMITS + + $sql .= $this->db->_GROUP ($this->outputGroups()); + + $sql .= $this->db->_ORDER ($this->outputOrders()); + + $this->db->query($sql); + return $this->db->getScalar(); } diff --git a/_res/_libs/tspaginator.class.php b/_res/_libs/tspaginator.class.php new file mode 100644 index 0000000..b88ea7b --- /dev/null +++ b/_res/_libs/tspaginator.class.php @@ -0,0 +1,104 @@ +getPaginationVariables (); + } + public function __destruct () {} + + public function getTotal () { + // getting the number of elements + if (empty ($this->total)) { +// $this->tdo->limit = null; +// $this->total = $this->tdo->getCount (); +// $this->tdo->reset (); + } + $this->total = 33; + return $this->total; + } + + public function addObject (&$object) { + if (!($object instanceof tdoAbstract)) + return false; + // add the pagination shit + $this->tdo = &$object; + } + + protected function getStart ($pageNo) { + if ((int)$pageNo <= 0) + $pageNo = 1; + // calculate the limit based on $total count of items and the pagination + return $this->count * ($pageNo-1) + 1; + } + + public function getPaginationVariables () { + // get the page variables: URL?p=? + $this->count = PAG_COUNT; + $this->page = tsController::getRequest (PAG_VAR); + $this->start = $this->getStart ($this->page); + + // get the ORDER BY variables: URL?o=column_name:(bool)ASC + $this->order = explode (':', tsController::getRequest (PAG_ORDER)); + } + public function outputOrderHeaders () { + return; + } + + public function outputPagination () { + $retVal = $left = $right = ''; + // how many stuff we got + $t = $this->getTotal(); + $cnt = floor ($this->getTotal() / $this->count); + $here = tsController::getRequest(NAV_VAR); + + if (!empty ($this->order)) + $this->addOrder (); + if (!empty ($this->limit)) + $this->addLimit (); + + for ($i=1; $i <= $cnt; $i++) { + $r = tsController::setRequest($here, array(PAG_VAR => $i)); + if ($i != $this->page) + $retVal .= ''.$i.' '; + else + $retVal .= $i.' '; + } + + if ($this->page > 1) + $left = '« '; + + if ($this->page < $cnt) + $right = '»';; + + return $left . $retVal . $right; + } + + public function addOrder () { + $fieldName = $this->order[0]; + $order = (bool)@$this->order[1]; // if we have no value we treat it as DESC + + if ($this->tdo instanceof tdoAbstract && !empty ($this->order)) { +// var_dump ($fieldName, $order, $this->tdo);die; + $this->tdo->addOrder ($fieldName, $order); + } + } + + public function addLimit () { + if ($this->tdo instanceof tdoAbstract && !empty ($this->start)) { + $this->tdo->addLimit ($this->start, $this->count); + } + } +} \ No newline at end of file diff --git a/_res/_libs/tsurl.class.php b/_res/_libs/tsurl.class.php index 8106e49..bdaeb2d 100644 --- a/_res/_libs/tsurl.class.php +++ b/_res/_libs/tsurl.class.php @@ -84,6 +84,10 @@ class tsUrl { return null; } + static public function getController () { + return tsUrl::getRequest (NAV_VAR); + } + static private function getFriendlyParams () { $retArr = array (); $parArr = explode ('/', $_SERVER['REQUEST_URI']); // fixme: $separator needed @@ -174,6 +178,9 @@ class tsUrl { static public function setRequest ($whereTo = null, $varVal = null) { // var_dump($varVal); // generating friendly URLs +// if (is_null ($whereTo)) +// $to = tsUrl::getController (); + if (is_null($varVal) && !is_array ($varVal)) { $varVal[] = (int)$varVal; } -- 2.11.4.GIT