X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=classes%2Fplset.php;h=baac0728522934dae287cd9a9b77887f516892c9;hb=6f58e0613a47d9eeb5004c992e6c59b7cf9ec0ea;hp=576e864988a66c46c529b449b33c3926ee8647e3;hpb=a2aa843639f292ed0197ffb06edf7e19174b27e8;p=platal.git diff --git a/classes/plset.php b/classes/plset.php index 576e864..baac072 100644 --- a/classes/plset.php +++ b/classes/plset.php @@ -1,6 +1,6 @@ from = $from; - $this->joins = $joins; - $this->where = $where; - $this->groupby = $groupby; + if ($cond instanceof PFC_And) { + $this->conds = $cond; + } else { + $this->conds = new PFC_And($cond); + } + + if (!is_null($orders) && $orders instanceof PlFilterOrder) { + $this->addSort($orders); + } else if (is_array($orders)){ + foreach ($orders as $order) { + $this->addSort($order); + } + } } public function addMod($name, $description, $default = false, array $params = array()) @@ -59,30 +70,42 @@ class PlSet unset($this->mods[$name]); } - private function &query($fields, $from, $joins, $where, $groupby, $order, $limit) + public function addSort(PlFilterOrder &$order) { - if (trim($order)) { - $order = "ORDER BY $order"; - } - if (trim($where)) { - $where = "WHERE $where"; + $this->orders[] = $order; + } + + public function addCond(PlFilterCondition &$cond) + { + $this->conds->addChild($cond); + } + + /** This function builds the right kind of PlFilter from given data + * @param $cond The PlFilterCondition for the filter + * @param $orders An array of PlFilterOrder for the filter + */ + abstract protected function buildFilter(PlFilterCondition &$cond, $orders); + + /** This function returns the values of the set + * @param $limit A PlLimit for selecting users + * @param $orders An optional array of PFO to use before the "default" ones + * @return The result of $pf->get(); + */ + public function &get(PlLimit $limit = null, $orders = array()) + { + if (!is_array($orders)) { + $orders = array($orders); } - if (trim($groupby)) { - $groupby = "GROUP BY $groupby"; + + $orders = array_merge($orders, $this->orders); + + $pf = $this->buildFilter($this->conds, $orders); + + if (is_null($limit)) { + $limit = new PlLimit(self::DEFAULT_MAX_RES, 0); } - $query = "SELECT SQL_CALC_FOUND_ROWS - $fields - FROM $from - $joins - $where - $groupby - $order - $limit"; -// echo $query; -// print_r($this); - $it = XDB::iterator($query); - $count = XDB::query('SELECT FOUND_ROWS()'); - $this->count = intval($count->fetchOneCell()); + $it = $pf->get($limit); + $this->count = $pf->getTotalCount(); return $it; } @@ -107,28 +130,6 @@ class PlSet return $encode ? urlencode($qs) : $qs; } - public function &get($fields, $joins, $where, $groupby, $order, $limitcount = null, $limitfrom = null) - { - if (!is_null($limitcount)) { - if (!is_null($limitfrom)) { - $limitcount = "$limitfrom,$limitcount"; - } - $limitcount = "LIMIT $limitcount"; - } - $joins = $this->joins . ' ' . $joins; - $where = $where; - if (trim($this->where)) { - if (trim($where)) { - $where .= ' AND '; - } - $where .= $this->where; - } - if (!$groupby) { - $groupby = $this->groupby; - } - return $this->query($fields, $this->from, $joins, $where, $groupby, $order, $limitcount); - } - public function count() { return $this->count; @@ -154,7 +155,7 @@ class PlSet return $view; } - public function apply($baseurl, PlatalPage &$page, $view = null, $data = null) + public function apply($baseurl, PlPage &$page, $view = null, $data = null) { $view =& $this->buildView($view, $data); if (is_null($view)) { @@ -164,7 +165,7 @@ class PlSet if (!isset($args['rechercher'])) { $args['rechercher'] = 'Chercher'; } - $page->changeTpl('core/plset.tpl'); + $page->coreTpl('plset.tpl'); $page->assign('plset_base', $baseurl); $page->assign('plset_mods', $this->mods); $page->assign('plset_mod', $this->mod); @@ -182,10 +183,38 @@ class PlSet interface PlView { public function __construct(PlSet &$set, $data, array $params); - public function apply(PlatalPage &$page); + public function apply(PlPage &$page); public function args(); } +/** This class describes an Order as used in a PlView : + * - It is based on a PlFilterOrder + * - It has a short identifier + * - It has a full name, to display on the page + */ +class PlViewOrder +{ + public $pfos = null; + public $name = null; + public $displaytext = null; + + /** Build a PlViewOrder + * @param $name Name of the order (key) + * @param $displaytext Text to display + * @param $pfos Array of PlFilterOrder for the order + */ + public function __construct($name, $pfos, $displaytext = null) + { + $this->name = $name; + if (is_null($displaytext)) { + $this->displaytext = ucfirst($name); + } else { + $this->displaytext = $displaytext; + } + $this->pfos = $pfos; + } +} + abstract class MultipageView implements PlView { protected $set; @@ -200,6 +229,13 @@ abstract class MultipageView implements PlView protected $sortkeys = array(); protected $defaultkey = null; + protected $bound_field = null; + + /** Builds a MultipageView + * @param $set The associated PlSet + * @param $data Data for the PlSet + * @param $params Parameters of the view + */ public function __construct(PlSet &$set, $data, array $params) { $this->set =& $set; @@ -208,29 +244,18 @@ abstract class MultipageView implements PlView $this->params = $params; } - public function joins() + /** Add an order to the view + */ + protected function addSort(PlViewOrder &$pvo, $default = false) { - return null; - } - - public function where() - { - return null; - } - - public function groupBy() - { - return null; - } - - protected function addSortKey($name, array $keys, $desc, $default = false) - { - $this->sortkeys[$name] = array('keys' => $keys, 'desc' => $desc); + $this->sortkeys[$pvo->name] = $pvo; if (!$this->defaultkey || $default) { - $this->defaultkey = $name; + $this->defaultkey = $pvo->name; } } + /** Returns a list of PFO objects in accordance with the user's choice + */ public function order() { $order = Env::v('order', $this->defaultkey); @@ -238,38 +263,76 @@ abstract class MultipageView implements PlView if ($invert) { $order = substr($order, 1); } - $list = array(); - foreach ($this->sortkeys[$order]['keys'] as $item) { - $desc = ($item{0} == '-'); - if ($desc) { - $item = substr($item, 1); - } - if ($desc xor $invert) { - $item .= ' DESC'; + + $ordering = $this->sortkeys[$order]; + if ($invert) { + foreach ($ordering->pfos as $pfo) { + $pfo->toggleDesc(); } - $list[] = $item; } - return implode(', ', $list); + return $ordering->pfos; } + /** Returns information on the order of bounds + * @return * 1 if normal bounds + * * -1 if inversed bounds + * * 0 if bounds shouldn't be displayed + */ + public function bounds() + { + return null; + } + + public function limit() + { + return null; + } + + /** Name of the template to use for displaying items of the view + */ abstract public function templateName(); - public function apply(PlatalPage &$page) + /** Returns the value of a boundary of the current view (in order + * to show "from C to F") + * @param $obj The boundary result whose value must be shown to the user + * @return The bound + */ + abstract protected function getBoundValue($obj); + + /** Applies the view to a page + * @param $page Page to which the view will be applied + */ + public function apply(PlPage &$page) { + foreach ($this->order() as $order) { + if (!is_null($order)) { + $this->set->addSort($order); + } + } + $res = $this->set->get($this->limit()); + + $show_bounds = $this->bounds(); + $end = end($res); + if ($show_bounds) { + if ($show_bounds == 1) { + $first = $this->getBoundValue($res[0]); + $last = $this->getBoundValue($end); + } elseif ($show_bounds == -1) { + $first = $this->getBoundValue($end); + $last = $this->getBoundValue($res[0]); + } + $page->assign('first', $first); + $page->assign('last', $last); + } + + $page->assign('show_bounds', $show_bounds); $page->assign('order', Env::v('order', $this->defaultkey)); $page->assign('orders', $this->sortkeys); $page->assign_by_ref('plview', $this); - $page->assign_by_ref('set', - $this->set->get($this->fields(), - $this->joins(), - $this->where(), - $this->groupBy(), - $this->order(), - $this->entriesPerPage, - $this->offset)); + $page->assign_by_ref('set', $res); $count = $this->set->count(); $this->pages = intval(ceil($count / $this->entriesPerPage)); - return 'include/plview.multipage.tpl'; + return PlPage::getCoreTpl('plview.multipage.tpl'); } public function args() @@ -278,7 +341,7 @@ abstract class MultipageView implements PlView unset($list['page']); unset($list['order']); return $list; - } + } } // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: