Commit | Line | Data |
---|---|---|
8c4a0c30 | 1 | <?php |
2 | /*************************************************************************** | |
e92ecb8c | 3 | * Copyright (C) 2003-2011 Polytechnique.org * |
8c4a0c30 | 4 | * http://opensource.polytechnique.org/ * |
5 | * * | |
6 | * This program is free software; you can redistribute it and/or modify * | |
7 | * it under the terms of the GNU General Public License as published by * | |
8 | * the Free Software Foundation; either version 2 of the License, or * | |
9 | * (at your option) any later version. * | |
10 | * * | |
11 | * This program is distributed in the hope that it will be useful, * | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
14 | * GNU General Public License for more details. * | |
15 | * * | |
16 | * You should have received a copy of the GNU General Public License * | |
17 | * along with this program; if not, write to the Free Software * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
9a122520 | 22 | |
8c4a0c30 | 23 | /** UserSet is a light-weight Model/View tool for displaying a set of items |
24 | */ | |
7efca70c | 25 | abstract class PlSet |
8c4a0c30 | 26 | { |
4aa4899b RB |
27 | const DEFAULT_MAX_RES = 20; |
28 | ||
54d6549c RB |
29 | protected $conds = null; |
30 | protected $orders = array(); | |
31 | protected $limit = null; | |
8c4a0c30 | 32 | |
b39f2e65 | 33 | protected $count = null; |
da2b7816 | 34 | protected $total_count = null; |
852e6400 RB |
35 | protected $groups = false; |
36 | ||
37 | // Handle the "restrict to values of the current order" | |
38 | protected $restrict_to = null; | |
8c4a0c30 | 39 | |
8ce35b4a | 40 | // A list of available views |
8c4a0c30 | 41 | private $mods = array(); |
8ce35b4a | 42 | // An array of $view_name => array($parameters) |
8c4a0c30 | 43 | private $modParams = array(); |
8ce35b4a | 44 | // The current view name |
8c4a0c30 | 45 | private $mod = null; |
8ce35b4a | 46 | // The default view name |
8c4a0c30 | 47 | private $default = null; |
48 | ||
ed4f7de0 | 49 | public function __construct(PlFilterCondition $cond, $orders = null) |
8c4a0c30 | 50 | { |
bd9b36fe RB |
51 | if ($cond instanceof PFC_And) { |
52 | $this->conds = $cond; | |
53 | } else { | |
54 | $this->conds = new PFC_And($cond); | |
55 | } | |
56 | ||
24bb3936 RB |
57 | if (!is_null($orders) && $orders instanceof PlFilterOrder) { |
58 | $this->addSort($orders); | |
59 | } else if (is_array($orders)){ | |
bd9b36fe | 60 | foreach ($orders as $order) { |
24bb3936 | 61 | $this->addSort($order); |
bd9b36fe RB |
62 | } |
63 | } | |
8c4a0c30 | 64 | } |
65 | ||
8ce35b4a RB |
66 | /** Adds a new view (minifiche, trombi, map) |
67 | * @param $name The name of the view (cf buildView) | |
68 | * @param $description A user-friendly name for the view | |
69 | * @param $default Whether this is the default view | |
70 | * @param $params Parameters used to tune the view (display promo, order by | |
71 | * score...) | |
72 | */ | |
8c4a0c30 | 73 | public function addMod($name, $description, $default = false, array $params = array()) |
74 | { | |
75 | $name = strtolower($name); | |
76 | $this->mods[$name] = $description; | |
77 | $this->modParams[$name] = $params; | |
78 | if ($default) { | |
79 | $this->default = $name; | |
80 | } | |
81 | } | |
82 | ||
83 | public function rmMod($name) | |
84 | { | |
85 | $name = strtolower($name); | |
86 | unset($this->mods[$name]); | |
87 | } | |
88 | ||
8ce35b4a RB |
89 | /** Adds a new sort (on the PlFilter) |
90 | */ | |
ed4f7de0 | 91 | public function addSort(PlFilterOrder $order) |
8c4a0c30 | 92 | { |
bd9b36fe RB |
93 | $this->orders[] = $order; |
94 | } | |
95 | ||
8ce35b4a RB |
96 | /** Adds a new condition to the PlFilter |
97 | */ | |
ed4f7de0 | 98 | public function addCond(PlFilterCondition $cond) |
7efca70c RB |
99 | { |
100 | $this->conds->addChild($cond); | |
101 | } | |
102 | ||
852e6400 RB |
103 | /** Restricts a PlFilter to values of a given PlFilterOrder |
104 | */ | |
105 | public function restrictTo($value) | |
106 | { | |
107 | $this->restrict_to = $value; | |
108 | } | |
109 | ||
bd9b36fe RB |
110 | /** This function builds the right kind of PlFilter from given data |
111 | * @param $cond The PlFilterCondition for the filter | |
112 | * @param $orders An array of PlFilterOrder for the filter | |
852e6400 | 113 | * @return a PlFilter |
bd9b36fe | 114 | */ |
ed4f7de0 | 115 | abstract protected function buildFilter(PlFilterCondition $cond, $orders); |
bd9b36fe | 116 | |
501a6db2 | 117 | /** This function returns the results of the given filter |
852e6400 | 118 | * within $limit; can be use to replace the default $pf->get call. |
ed4f7de0 | 119 | * @param $pf The filter |
501a6db2 RB |
120 | * @param $limit The PlLimit |
121 | * @return The results of the filter | |
122 | */ | |
ed4f7de0 | 123 | protected function &getFilterResults(PlFilter $pf, PlLimit $limit) |
501a6db2 | 124 | { |
b0a8daa8 FB |
125 | $res = $pf->get($limit); |
126 | return $res; | |
501a6db2 RB |
127 | } |
128 | ||
4f4f166c SJ |
129 | /** Helper function, calls buildFilter with the adequate condition/orders. |
130 | * @param $orders Additional orders to use before the default ones. | |
131 | * @return A newly created PlFilter. | |
24bb3936 | 132 | */ |
852e6400 | 133 | private function buildFilterHelper($orders = array(), $extra_cond=null) |
bd9b36fe | 134 | { |
24bb3936 RB |
135 | if (!is_array($orders)) { |
136 | $orders = array($orders); | |
137 | } | |
24bb3936 RB |
138 | $orders = array_merge($orders, $this->orders); |
139 | ||
852e6400 RB |
140 | if ($extra_cond != null) { |
141 | $conds = clone $this->conds; | |
142 | $conds->addChild($extra_cond); | |
143 | } else { | |
144 | $conds = $this->conds; | |
145 | } | |
146 | return $this->buildFilter($conds, $orders); | |
4f4f166c | 147 | } |
bd9b36fe | 148 | |
4f4f166c SJ |
149 | /** This function returns the values of the set, and sets $count with the |
150 | * total number of results. | |
151 | * @param $limit A PlLimit for selecting users | |
152 | * @param $orders An optional array of PFO to use before the "default" ones | |
153 | * @return The result of $pf->get(); | |
154 | */ | |
155 | public function &get(PlLimit $limit = null, $orders = array()) | |
156 | { | |
9a122520 | 157 | if (is_null($limit)) { |
4aa4899b | 158 | $limit = new PlLimit(self::DEFAULT_MAX_RES, 0); |
9a122520 | 159 | } |
852e6400 RB |
160 | $pf_res = $this->buildFilterHelper($orders); |
161 | $pf_groups = $pf_res; | |
da2b7816 | 162 | $this->total_count = $pf_res->getTotalCount(); |
852e6400 RB |
163 | if ($this->restrict_to != null |
164 | && count($this->orders) | |
165 | && $this->orders[0] instanceof PlFilterGroupableOrder) | |
166 | { | |
167 | $main_order = $this->orders[0]; | |
168 | $pf_res = $this->buildFilterHelper($orders, $main_order->getCondition($this->restrict_to)); | |
169 | } | |
170 | ||
171 | $it = $this->getFilterResults($pf_res, $limit); | |
172 | $this->count = $pf_res->getTotalCount(); | |
173 | if ($pf_groups->hasGroups()) { | |
174 | $this->groups = $pf_groups->getGroups(); | |
175 | } else { | |
176 | $this->groups = null; | |
177 | } | |
8c4a0c30 | 178 | return $it; |
179 | } | |
180 | ||
4f4f166c SJ |
181 | /** This function returns the ids of the set, and sets $count with the |
182 | * total number of results. | |
183 | * @param $limit A PlLimit for selecting profiles | |
184 | * @param $orders An optional array of PFO to use before the "default" ones | |
185 | * @return The result of $pf->getId(); | |
186 | */ | |
187 | public function &getIds(PlLimit $limit = null, $orders = array()) | |
188 | { | |
189 | if (is_null($limit)) { | |
190 | $limit = new PlLimit(self::DEFAULT_MAX_RES, 0); | |
191 | } | |
192 | $pf = $this->buildFilterHelper($orders); | |
193 | $result = $pf->getIds($limit); | |
194 | $this->count = count($result); | |
195 | return $result; | |
196 | } | |
197 | ||
e8e034fd RB |
198 | /** Return an array containing all pertinent parameters for this page |
199 | * Generated from $_GET, after some cleanup (remove 'n' (plat/al field | |
200 | * for the handler path) | |
8ce35b4a | 201 | */ |
35fa92e8 | 202 | public function args() |
203 | { | |
204 | $get = $_GET; | |
205 | unset($get['n']); | |
206 | return $get; | |
207 | } | |
208 | ||
e8e034fd RB |
209 | /** Convert an array into an URL query (?foo=bar) |
210 | * @param $args An associative array to convert to a query string | |
211 | * @param $encode Whether to url-encode the string | |
8ce35b4a | 212 | */ |
35fa92e8 | 213 | protected function encodeArgs(array $args, $encode = false) |
214 | { | |
215 | $qs = '?'; | |
a2aa8436 | 216 | $sep = '&'; |
35fa92e8 | 217 | foreach ($args as $k=>$v) { |
218 | if (!$encode) { | |
219 | $k = urlencode($k); | |
220 | $v = urlencode($v); | |
221 | } | |
222 | $qs .= "$k=$v$sep"; | |
223 | } | |
224 | return $encode ? urlencode($qs) : $qs; | |
225 | } | |
226 | ||
8c4a0c30 | 227 | public function count() |
228 | { | |
229 | return $this->count; | |
230 | } | |
231 | ||
8ce35b4a RB |
232 | /** Builds the view class from the given parameters |
233 | * @param $view A string ('profile' for 'ProfileView'); if null, | |
234 | * the default view is used. | |
235 | * @return A new PlView instance. | |
236 | */ | |
237 | private function &buildView($view) | |
8c4a0c30 | 238 | { |
239 | $view = strtolower($view); | |
240 | if (!$view || !class_exists($view . 'View') || !isset($this->mods[$view])) { | |
ff3eb9b7 | 241 | reset($this->mods); |
242 | $view = $this->default ? $this->default : key($this->mods); | |
8c4a0c30 | 243 | } |
244 | $this->mod = $view; | |
245 | $class = $view . 'View'; | |
246 | if (!class_exists($class)) { | |
247 | $view = null; | |
248 | } else { | |
8ce35b4a | 249 | $view = new $class($this, $this->modParams[$this->mod]); |
8c4a0c30 | 250 | if (!$view instanceof PlView) { |
251 | $view = null; | |
252 | } | |
253 | } | |
254 | return $view; | |
255 | } | |
256 | ||
8ce35b4a RB |
257 | /** Creates the view: sets the page template, assigns Smarty vars. |
258 | * @param $baseurl The base URL for this (for instance, "search/") | |
259 | * @param $page The page in which the view should be loaded | |
260 | * @param $view The name of the view; if null, the default one will be used. | |
261 | */ | |
ed4f7de0 | 262 | public function apply($baseurl, PlPage $page, $view = null) |
8c4a0c30 | 263 | { |
8ce35b4a | 264 | $view =& $this->buildView($view); |
8c4a0c30 | 265 | if (is_null($view)) { |
266 | return false; | |
267 | } | |
35fa92e8 | 268 | $args = $view->args(); |
7cb40d85 | 269 | $page->coreTpl('plset.tpl'); |
8c4a0c30 | 270 | $page->assign('plset_base', $baseurl); |
271 | $page->assign('plset_mods', $this->mods); | |
272 | $page->assign('plset_mod', $this->mod); | |
e8e034fd RB |
273 | $page->assign('plset_args', $this->encodeArgs($args)); |
274 | $page->assign('plset_args_enc', $this->encodeArgs($args, true)); | |
8c4a0c30 | 275 | foreach ($this->modParams[$this->mod] as $param=>$value) { |
276 | $page->assign($this->mod . '_' . $param, $value); | |
277 | } | |
278 | $page->assign('plset_content', $view->apply($page)); | |
279 | $page->assign('plset_count', $this->count); | |
da2b7816 | 280 | $page->assign('plset_total_count', $this->total_count); |
852e6400 RB |
281 | $page->assign('plset_has_groups', $this->groups != null); |
282 | $page->assign('plset_groups', $this->groups); | |
8c4a0c30 | 283 | return true; |
284 | } | |
285 | } | |
286 | ||
287 | interface PlView | |
288 | { | |
8ce35b4a RB |
289 | /** Constructs a new PlView |
290 | * @param $set The set | |
291 | * @param $params Parameters to tune the view (sort by score, include promo...) | |
292 | */ | |
ed4f7de0 | 293 | public function __construct(PlSet $set, array $params); |
8ce35b4a RB |
294 | |
295 | /** Applies the view to a page | |
296 | * The content of the set is fetched here. | |
297 | * @param $page Page to which the view will be applied | |
298 | * @return The name of the global view template (for displaying the view, | |
299 | * not the items of the set) | |
300 | */ | |
ed4f7de0 | 301 | public function apply(PlPage $page); |
8ce35b4a | 302 | |
e8e034fd RB |
303 | /** As PlSet->args(), returns the ?foo=bar part of the URL for generating |
304 | * this PlSet, after adding the necessary components and removing useless ones. | |
8ce35b4a | 305 | */ |
35fa92e8 | 306 | public function args(); |
8c4a0c30 | 307 | } |
308 | ||
bd9b36fe RB |
309 | /** This class describes an Order as used in a PlView : |
310 | * - It is based on a PlFilterOrder | |
311 | * - It has a short identifier | |
312 | * - It has a full name, to display on the page | |
313 | */ | |
314 | class PlViewOrder | |
315 | { | |
4c27b6a2 | 316 | public $pfos = null; |
bd9b36fe RB |
317 | public $name = null; |
318 | public $displaytext = null; | |
319 | ||
320 | /** Build a PlViewOrder | |
321 | * @param $name Name of the order (key) | |
322 | * @param $displaytext Text to display | |
4c27b6a2 | 323 | * @param $pfos Array of PlFilterOrder for the order |
bd9b36fe | 324 | */ |
4c27b6a2 | 325 | public function __construct($name, $pfos, $displaytext = null) |
bd9b36fe | 326 | { |
4aa4899b | 327 | $this->name = $name; |
bd9b36fe RB |
328 | if (is_null($displaytext)) { |
329 | $this->displaytext = ucfirst($name); | |
330 | } else { | |
4aa4899b | 331 | $this->displaytext = $displaytext; |
bd9b36fe | 332 | } |
4c27b6a2 | 333 | $this->pfos = $pfos; |
bd9b36fe RB |
334 | } |
335 | } | |
336 | ||
8c4a0c30 | 337 | abstract class MultipageView implements PlView |
338 | { | |
339 | protected $set; | |
340 | ||
341 | public $pages = 1; | |
342 | public $page = 1; | |
852e6400 | 343 | public $restrict = null; |
8c4a0c30 | 344 | public $offset = 0; |
345 | ||
8c4a0c30 | 346 | protected $entriesPerPage = 20; |
347 | protected $params = array(); | |
348 | ||
35fa92e8 | 349 | protected $sortkeys = array(); |
350 | protected $defaultkey = null; | |
351 | ||
5e92949d FB |
352 | protected $bound_field = null; |
353 | ||
bd9b36fe RB |
354 | /** Builds a MultipageView |
355 | * @param $set The associated PlSet | |
bd9b36fe RB |
356 | * @param $params Parameters of the view |
357 | */ | |
ed4f7de0 | 358 | public function __construct(PlSet $set, array $params) |
8c4a0c30 | 359 | { |
360 | $this->set =& $set; | |
361 | $this->page = Env::i('page', 1); | |
852e6400 | 362 | $this->restrict = Env::s('restrict', null); |
8c4a0c30 | 363 | $this->offset = $this->entriesPerPage * ($this->page - 1); |
364 | $this->params = $params; | |
365 | } | |
366 | ||
bd9b36fe RB |
367 | /** Add an order to the view |
368 | */ | |
ed4f7de0 | 369 | protected function addSort(PlViewOrder $pvo, $default = false) |
5e92949d | 370 | { |
bd9b36fe | 371 | $this->sortkeys[$pvo->name] = $pvo; |
35fa92e8 | 372 | if (!$this->defaultkey || $default) { |
bd9b36fe | 373 | $this->defaultkey = $pvo->name; |
35fa92e8 | 374 | } |
375 | } | |
376 | ||
4c27b6a2 | 377 | /** Returns a list of PFO objects in accordance with the user's choice |
bd9b36fe | 378 | */ |
8c4a0c30 | 379 | public function order() |
380 | { | |
35fa92e8 | 381 | $order = Env::v('order', $this->defaultkey); |
382 | $invert = ($order{0} == '-'); | |
383 | if ($invert) { | |
384 | $order = substr($order, 1); | |
385 | } | |
4c27b6a2 RB |
386 | |
387 | $ordering = $this->sortkeys[$order]; | |
388 | if ($invert) { | |
389 | foreach ($ordering->pfos as $pfo) { | |
390 | $pfo->toggleDesc(); | |
35fa92e8 | 391 | } |
8c4a0c30 | 392 | } |
4c27b6a2 | 393 | return $ordering->pfos; |
bd9b36fe RB |
394 | } |
395 | ||
396 | /** Returns information on the order of bounds | |
397 | * @return * 1 if normal bounds | |
398 | * * -1 if inversed bounds | |
399 | * * 0 if bounds shouldn't be displayed | |
400 | */ | |
401 | public function bounds() | |
402 | { | |
403 | return null; | |
8c4a0c30 | 404 | } |
405 | ||
7efca70c RB |
406 | public function limit() |
407 | { | |
43343e2a | 408 | return new PlLimit($this->entriesPerPage, $this->offset); |
7efca70c RB |
409 | } |
410 | ||
bd9b36fe | 411 | /** Name of the template to use for displaying items of the view |
8ce35b4a | 412 | * e.g plview.minifiche.tpl, plview.trombi.pl, ... |
bd9b36fe | 413 | */ |
8c4a0c30 | 414 | abstract public function templateName(); |
415 | ||
bd9b36fe RB |
416 | /** Returns the value of a boundary of the current view (in order |
417 | * to show "from C to F") | |
418 | * @param $obj The boundary result whose value must be shown to the user | |
8ce35b4a | 419 | * (e.g a Profile, ...) |
4c27b6a2 | 420 | * @return The bound |
bd9b36fe | 421 | */ |
7efca70c | 422 | abstract protected function getBoundValue($obj); |
bd9b36fe | 423 | |
ed4f7de0 | 424 | public function apply(PlPage $page) |
8c4a0c30 | 425 | { |
bd9b36fe | 426 | foreach ($this->order() as $order) { |
4c27b6a2 | 427 | if (!is_null($order)) { |
24bb3936 | 428 | $this->set->addSort($order); |
4c27b6a2 | 429 | } |
bd9b36fe | 430 | } |
852e6400 RB |
431 | if ($this->restrict != null) { |
432 | $this->set->restrictTo($this->restrict); | |
433 | } | |
bd9b36fe RB |
434 | $res = $this->set->get($this->limit()); |
435 | ||
5e92949d | 436 | $show_bounds = $this->bounds(); |
5e92949d | 437 | if ($show_bounds) { |
b9bd8dd3 SJ |
438 | $start = current($res); |
439 | $end = end($res); | |
5e92949d | 440 | if ($show_bounds == 1) { |
701c0b6f | 441 | $first = $this->getBoundValue($start); |
bd9b36fe | 442 | $last = $this->getBoundValue($end); |
5e92949d | 443 | } elseif ($show_bounds == -1) { |
bd9b36fe | 444 | $first = $this->getBoundValue($end); |
701c0b6f | 445 | $last = $this->getBoundValue($start); |
5e92949d FB |
446 | } |
447 | $page->assign('first', $first); | |
448 | $page->assign('last', $last); | |
449 | } | |
450 | ||
451 | $page->assign('show_bounds', $show_bounds); | |
35fa92e8 | 452 | $page->assign('order', Env::v('order', $this->defaultkey)); |
453 | $page->assign('orders', $this->sortkeys); | |
852e6400 | 454 | $page->assign('restrict', $this->restrict); |
8c4a0c30 | 455 | $page->assign_by_ref('plview', $this); |
b0a8daa8 FB |
456 | if (is_array($res)) { |
457 | $page->assign('set_keys', array_keys($res)); | |
458 | } | |
5e92949d | 459 | $page->assign_by_ref('set', $res); |
8c4a0c30 | 460 | $count = $this->set->count(); |
461 | $this->pages = intval(ceil($count / $this->entriesPerPage)); | |
7cb40d85 | 462 | return PlPage::getCoreTpl('plview.multipage.tpl'); |
8c4a0c30 | 463 | } |
35fa92e8 | 464 | |
e8e034fd RB |
465 | /** Arguments are those needed by the set, minus 'page' and 'order' which |
466 | * will be set to new values in the html links. | |
467 | */ | |
35fa92e8 | 468 | public function args() |
469 | { | |
470 | $list = $this->set->args(); | |
471 | unset($list['page']); | |
472 | unset($list['order']); | |
852e6400 | 473 | unset($list['restrict']); |
35fa92e8 | 474 | return $list; |
eaf30d86 | 475 | } |
8c4a0c30 | 476 | } |
477 | ||
478 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
479 | ?> |