X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=classes%2Fplset.php;h=1c29af20bd087ad1f55a89e918e3ac26ec3cd500;hb=92d96cb7270caff156df7785b800a7a3084e8a16;hp=d2267d02ad88b1cf192569184987efa49b1d06f7;hpb=8c4a0c30c699f190cb7906746e22f67014df3368;p=platal.git diff --git a/classes/plset.php b/classes/plset.php index d2267d0..1c29af2 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,53 +70,76 @@ 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 results of the given filter + * wihtin $limit ; available when the PlFilter getter isn't the usual get + * @param &$pf The filter + * @param $limit The PlLimit + * @return The results of the filter + */ + protected function &getFilterResults(PlFilter &$pf, PlLimit $limit) + { + $res = $pf->get($limit); + return $res; + } + + /** 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 = $this->getFilterResults($pf, $limit); + $this->count = $pf->getTotalCount(); return $it; } - public function &get($fields, $joins, $where, $groupby, $order, $limitcount = null, $limitfrom = null) + public function args() { - 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 '; + $get = $_GET; + unset($get['n']); + return $get; + } + + protected function encodeArgs(array $args, $encode = false) + { + $qs = '?'; + $sep = '&'; + foreach ($args as $k=>$v) { + if (!$encode) { + $k = urlencode($k); + $v = urlencode($v); } - $where .= $this->where; - } - if (!$groupby) { - $groupby = $this->groupby; + $qs .= "$k=$v$sep"; } - return $this->query($fields, $this->from, $joins, $where, $groupby, $order, $limitcount); + return $encode ? urlencode($qs) : $qs; } public function count() @@ -117,7 +151,8 @@ class PlSet { $view = strtolower($view); if (!$view || !class_exists($view . 'View') || !isset($this->mods[$view])) { - $view = $this->default ? $this->default : $this->mods[0]; + reset($this->mods); + $view = $this->default ? $this->default : key($this->mods); } $this->mod = $view; $class = $view . 'View'; @@ -132,16 +167,22 @@ 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)) { return false; } - $page->changeTpl('core/plset.tpl'); + $args = $view->args(); + if (!isset($args['rechercher'])) { + $args['rechercher'] = 'Chercher'; + } + $page->coreTpl('plset.tpl'); $page->assign('plset_base', $baseurl); $page->assign('plset_mods', $this->mods); $page->assign('plset_mod', $this->mod); + $page->assign('plset_search', $this->encodeArgs($args)); + $page->assign('plset_search_enc', $this->encodeArgs($args, true)); foreach ($this->modParams[$this->mod] as $param=>$value) { $page->assign($this->mod . '_' . $param, $value); } @@ -154,7 +195,36 @@ 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 @@ -165,10 +235,19 @@ abstract class MultipageView implements PlView public $page = 1; public $offset = 0; - protected $order = array(); protected $entriesPerPage = 20; protected $params = array(); + 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; @@ -177,48 +256,107 @@ 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; + $this->sortkeys[$pvo->name] = $pvo; + if (!$this->defaultkey || $default) { + $this->defaultkey = $pvo->name; + } } - public function where() + /** Returns a list of PFO objects in accordance with the user's choice + */ + public function order() { - return null; + $order = Env::v('order', $this->defaultkey); + $invert = ($order{0} == '-'); + if ($invert) { + $order = substr($order, 1); + } + + $ordering = $this->sortkeys[$order]; + if ($invert) { + foreach ($ordering->pfos as $pfo) { + $pfo->toggleDesc(); + } + } + return $ordering->pfos; } - public function groupBy() + /** 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 order() + public function limit() { - foreach ($this->order as &$item) { - if ($item{0} == '-') { - $item = substr($item, 1) . ' DESC'; - } - } - return implode(', ', $this->order); + return new PlLimit($this->entriesPerPage, $this->offset); } + /** 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(); + if ($show_bounds) { + $start = current($res); + $end = end($res); + if ($show_bounds == 1) { + $first = $this->getBoundValue($start); + $last = $this->getBoundValue($end); + } elseif ($show_bounds == -1) { + $first = $this->getBoundValue($end); + $last = $this->getBoundValue($start); + } + $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)); + if (is_array($res)) { + $page->assign('set_keys', array_keys($res)); + } + $page->assign_by_ref('set', $res); $count = $this->set->count(); $this->pages = intval(ceil($count / $this->entriesPerPage)); + return PlPage::getCoreTpl('plview.multipage.tpl'); + } - return 'include/plview.multipage.tpl'; + public function args() + { + $list = $this->set->args(); + unset($list['page']); + unset($list['order']); + return $list; } }