X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=classes%2Fplset.php;h=106c18a006f2051324b08732bed86c7e1f153822;hb=4d1c62e006bc896e9ddb59d15dc7a7e30f9eb48e;hp=8ef6ba84fc94bf31d3d1f28143cec94e34a912ee;hpb=7efca70c0648539d1fd38d77d4e60b33356c01dd;p=platal.git diff --git a/classes/plset.php b/classes/plset.php index 8ef6ba8..106c18a 100644 --- a/classes/plset.php +++ b/classes/plset.php @@ -1,6 +1,6 @@ array($parameters) private $modParams = array(); + // The current view name private $mod = null; + // The default view name private $default = null; - public function __construct(PlFilterCondition &$cond, $orders) + public function __construct(PlFilterCondition &$cond, $orders = null) { if ($cond instanceof PFC_And) { $this->conds = $cond; @@ -42,15 +49,22 @@ abstract class PlSet $this->conds = new PFC_And($cond); } - if ($orders instanceof PlFilterOrder) { - $this->orders = array($order); - } else { + if (!is_null($orders) && $orders instanceof PlFilterOrder) { + $this->addSort($orders); + } else if (is_array($orders)){ foreach ($orders as $order) { - $this->orders[] = $order; + $this->addSort($order); } } } + /** Adds a new view (minifiche, trombi, map) + * @param $name The name of the view (cf buildView) + * @param $description A user-friendly name for the view + * @param $default Whether this is the default view + * @param $params Parameters used to tune the view (display promo, order by + * score...) + */ public function addMod($name, $description, $default = false, array $params = array()) { $name = strtolower($name); @@ -67,11 +81,15 @@ abstract class PlSet unset($this->mods[$name]); } + /** Adds a new sort (on the PlFilter) + */ public function addSort(PlFilterOrder &$order) { $this->orders[] = $order; } + /** Adds a new condition to the PlFilter + */ public function addCond(PlFilterCondition &$cond) { $this->conds->addChild($cond); @@ -83,15 +101,46 @@ abstract class PlSet */ abstract protected function buildFilter(PlFilterCondition &$cond, $orders); - public function &get(PlFilterLimit $limit = null) + /** This function returns the results of the given filter + * wihtin $limit; can be use to replace the default $pf->get call. + * @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, and sets $count with the + * total number of results. + * @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()) { - $pf = $this->buildFilter($this->conds, $this->orders); + if (!is_array($orders)) { + $orders = array($orders); + } + + $orders = array_merge($orders, $this->orders); - $it = $pf->get($limit); + $pf = $this->buildFilter($this->conds, $orders); + + if (is_null($limit)) { + $limit = new PlLimit(self::DEFAULT_MAX_RES, 0); + } + $it = $this->getFilterResults($pf, $limit); $this->count = $pf->getTotalCount(); return $it; } + /** Return an array containing all pertinent parameters for this page + * Generated from $_GET, after some cleanup (remove 'n' (plat/al field + * for the handler path) + */ public function args() { $get = $_GET; @@ -99,6 +148,10 @@ abstract class PlSet return $get; } + /** Convert an array into an URL query (?foo=bar) + * @param $args An associative array to convert to a query string + * @param $encode Whether to url-encode the string + */ protected function encodeArgs(array $args, $encode = false) { $qs = '?'; @@ -118,7 +171,12 @@ abstract class PlSet return $this->count; } - private function &buildView($view, $data) + /** Builds the view class from the given parameters + * @param $view A string ('profile' for 'ProfileView'); if null, + * the default view is used. + * @return A new PlView instance. + */ + private function &buildView($view) { $view = strtolower($view); if (!$view || !class_exists($view . 'View') || !isset($this->mods[$view])) { @@ -130,7 +188,7 @@ abstract class PlSet if (!class_exists($class)) { $view = null; } else { - $view = new $class($this, $data, $this->modParams[$this->mod]); + $view = new $class($this, $this->modParams[$this->mod]); if (!$view instanceof PlView) { $view = null; } @@ -138,22 +196,24 @@ abstract class PlSet return $view; } - public function apply($baseurl, PlPage &$page, $view = null, $data = null) + /** Creates the view: sets the page template, assigns Smarty vars. + * @param $baseurl The base URL for this (for instance, "search/") + * @param $page The page in which the view should be loaded + * @param $view The name of the view; if null, the default one will be used. + */ + public function apply($baseurl, PlPage &$page, $view = null) { - $view =& $this->buildView($view, $data); + $view =& $this->buildView($view); if (is_null($view)) { return false; } $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)); + $page->assign('plset_args', $this->encodeArgs($args)); + $page->assign('plset_args_enc', $this->encodeArgs($args, true)); foreach ($this->modParams[$this->mod] as $param=>$value) { $page->assign($this->mod . '_' . $param, $value); } @@ -165,8 +225,23 @@ abstract class PlSet interface PlView { - public function __construct(PlSet &$set, $data, array $params); + /** Constructs a new PlView + * @param $set The set + * @param $params Parameters to tune the view (sort by score, include promo...) + */ + public function __construct(PlSet &$set, array $params); + + /** Applies the view to a page + * The content of the set is fetched here. + * @param $page Page to which the view will be applied + * @return The name of the global view template (for displaying the view, + * not the items of the set) + */ public function apply(PlPage &$page); + + /** As PlSet->args(), returns the ?foo=bar part of the URL for generating + * this PlSet, after adding the necessary components and removing useless ones. + */ public function args(); } @@ -177,24 +252,24 @@ interface PlView */ class PlViewOrder { - public $pfo = null; + 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 $pfo PlFilterOrder for the order + * @param $pfos Array of PlFilterOrder for the order */ - public function __construct($name, PlFilterOrder &$pfo, $displaytext = null) + public function __construct($name, $pfos, $displaytext = null) { - $this->name = $name; + $this->name = $name; if (is_null($displaytext)) { $this->displaytext = ucfirst($name); } else { - $this->displaytext = $displaytext; + $this->displaytext = $displaytext; } - $this->pfo = $pfo; + $this->pfos = $pfos; } } @@ -216,10 +291,9 @@ abstract class MultipageView implements PlView /** 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) + public function __construct(PlSet &$set, array $params) { $this->set =& $set; $this->page = Env::i('page', 1); @@ -237,7 +311,7 @@ abstract class MultipageView implements PlView } } - /** Returns a list of PFO objects, the "default" one first + /** Returns a list of PFO objects in accordance with the user's choice */ public function order() { @@ -246,22 +320,14 @@ abstract class MultipageView implements PlView if ($invert) { $order = substr($order, 1); } - $list = array(); - if (count($this->sortkeys)) { - $list[0] = null; - } - foreach ($this->sortkeys as $name => $sort) { - $desc = $sort->pfo->isDescending();; - if ($invert) { - $sort->pfo->toggleDesc(); - } - if ($name == $order) { - $list[0] = $sort->pfo; - } else { - $list[] = $sort->pfo; + + $ordering = $this->sortkeys[$order]; + if ($invert) { + foreach ($ordering->pfos as $pfo) { + $pfo->toggleDesc(); } } - return $list; + return $ordering->pfos; } /** Returns information on the order of bounds @@ -276,38 +342,41 @@ abstract class MultipageView implements PlView public function limit() { - return null; + return new PlLimit($this->entriesPerPage, $this->offset); } /** Name of the template to use for displaying items of the view + * e.g plview.minifiche.tpl, plview.trombi.pl, ... */ abstract public function templateName(); /** 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 + * (e.g a Profile, ...) + * @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) { - $this->set->addSort($order->pfo); + if (!is_null($order)) { + $this->set->addSort($order); + } } $res = $this->set->get($this->limit()); $show_bounds = $this->bounds(); - $end = end($res); if ($show_bounds) { + $start = current($res); + $end = end($res); if ($show_bounds == 1) { - $first = $this->getBoundValue($res[0]); + $first = $this->getBoundValue($start); $last = $this->getBoundValue($end); } elseif ($show_bounds == -1) { $first = $this->getBoundValue($end); - $last = $this->getBoundValue($res[0]); + $last = $this->getBoundValue($start); } $page->assign('first', $first); $page->assign('last', $last); @@ -317,12 +386,18 @@ abstract class MultipageView implements PlView $page->assign('order', Env::v('order', $this->defaultkey)); $page->assign('orders', $this->sortkeys); $page->assign_by_ref('plview', $this); + 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'); } + /** Arguments are those needed by the set, minus 'page' and 'order' which + * will be set to new values in the html links. + */ public function args() { $list = $this->set->args();