Commit | Line | Data |
---|---|---|
8c4a0c30 | 1 | <?php |
2 | /*************************************************************************** | |
2ab75571 | 3 | * Copyright (C) 2003-2010 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; |
8c4a0c30 | 34 | |
8ce35b4a | 35 | // A list of available views |
8c4a0c30 | 36 | private $mods = array(); |
8ce35b4a | 37 | // An array of $view_name => array($parameters) |
8c4a0c30 | 38 | private $modParams = array(); |
8ce35b4a | 39 | // The current view name |
8c4a0c30 | 40 | private $mod = null; |
8ce35b4a | 41 | // The default view name |
8c4a0c30 | 42 | private $default = null; |
43 | ||
24bb3936 | 44 | public function __construct(PlFilterCondition &$cond, $orders = null) |
8c4a0c30 | 45 | { |
bd9b36fe RB |
46 | if ($cond instanceof PFC_And) { |
47 | $this->conds = $cond; | |
48 | } else { | |
49 | $this->conds = new PFC_And($cond); | |
50 | } | |
51 | ||
24bb3936 RB |
52 | if (!is_null($orders) && $orders instanceof PlFilterOrder) { |
53 | $this->addSort($orders); | |
54 | } else if (is_array($orders)){ | |
bd9b36fe | 55 | foreach ($orders as $order) { |
24bb3936 | 56 | $this->addSort($order); |
bd9b36fe RB |
57 | } |
58 | } | |
8c4a0c30 | 59 | } |
60 | ||
8ce35b4a RB |
61 | /** Adds a new view (minifiche, trombi, map) |
62 | * @param $name The name of the view (cf buildView) | |
63 | * @param $description A user-friendly name for the view | |
64 | * @param $default Whether this is the default view | |
65 | * @param $params Parameters used to tune the view (display promo, order by | |
66 | * score...) | |
67 | */ | |
8c4a0c30 | 68 | public function addMod($name, $description, $default = false, array $params = array()) |
69 | { | |
70 | $name = strtolower($name); | |
71 | $this->mods[$name] = $description; | |
72 | $this->modParams[$name] = $params; | |
73 | if ($default) { | |
74 | $this->default = $name; | |
75 | } | |
76 | } | |
77 | ||
78 | public function rmMod($name) | |
79 | { | |
80 | $name = strtolower($name); | |
81 | unset($this->mods[$name]); | |
82 | } | |
83 | ||
8ce35b4a RB |
84 | /** Adds a new sort (on the PlFilter) |
85 | */ | |
bd9b36fe | 86 | public function addSort(PlFilterOrder &$order) |
8c4a0c30 | 87 | { |
bd9b36fe RB |
88 | $this->orders[] = $order; |
89 | } | |
90 | ||
8ce35b4a RB |
91 | /** Adds a new condition to the PlFilter |
92 | */ | |
7efca70c RB |
93 | public function addCond(PlFilterCondition &$cond) |
94 | { | |
95 | $this->conds->addChild($cond); | |
96 | } | |
97 | ||
bd9b36fe RB |
98 | /** This function builds the right kind of PlFilter from given data |
99 | * @param $cond The PlFilterCondition for the filter | |
100 | * @param $orders An array of PlFilterOrder for the filter | |
101 | */ | |
7efca70c | 102 | abstract protected function buildFilter(PlFilterCondition &$cond, $orders); |
bd9b36fe | 103 | |
501a6db2 | 104 | /** This function returns the results of the given filter |
8ce35b4a | 105 | * wihtin $limit; can be use to replace the default $pf->get call. |
501a6db2 RB |
106 | * @param &$pf The filter |
107 | * @param $limit The PlLimit | |
108 | * @return The results of the filter | |
109 | */ | |
110 | protected function &getFilterResults(PlFilter &$pf, PlLimit $limit) | |
111 | { | |
b0a8daa8 FB |
112 | $res = $pf->get($limit); |
113 | return $res; | |
501a6db2 RB |
114 | } |
115 | ||
8ce35b4a RB |
116 | /** This function returns the values of the set, and sets $count with the |
117 | * total number of results. | |
24bb3936 RB |
118 | * @param $limit A PlLimit for selecting users |
119 | * @param $orders An optional array of PFO to use before the "default" ones | |
120 | * @return The result of $pf->get(); | |
121 | */ | |
122 | public function &get(PlLimit $limit = null, $orders = array()) | |
bd9b36fe | 123 | { |
24bb3936 RB |
124 | if (!is_array($orders)) { |
125 | $orders = array($orders); | |
126 | } | |
127 | ||
128 | $orders = array_merge($orders, $this->orders); | |
129 | ||
130 | $pf = $this->buildFilter($this->conds, $orders); | |
bd9b36fe | 131 | |
9a122520 | 132 | if (is_null($limit)) { |
4aa4899b | 133 | $limit = new PlLimit(self::DEFAULT_MAX_RES, 0); |
9a122520 | 134 | } |
501a6db2 | 135 | $it = $this->getFilterResults($pf, $limit); |
bd9b36fe | 136 | $this->count = $pf->getTotalCount(); |
8c4a0c30 | 137 | return $it; |
138 | } | |
139 | ||
e8e034fd RB |
140 | /** Return an array containing all pertinent parameters for this page |
141 | * Generated from $_GET, after some cleanup (remove 'n' (plat/al field | |
142 | * for the handler path) | |
8ce35b4a | 143 | */ |
35fa92e8 | 144 | public function args() |
145 | { | |
146 | $get = $_GET; | |
147 | unset($get['n']); | |
148 | return $get; | |
149 | } | |
150 | ||
e8e034fd RB |
151 | /** Convert an array into an URL query (?foo=bar) |
152 | * @param $args An associative array to convert to a query string | |
153 | * @param $encode Whether to url-encode the string | |
8ce35b4a | 154 | */ |
35fa92e8 | 155 | protected function encodeArgs(array $args, $encode = false) |
156 | { | |
157 | $qs = '?'; | |
a2aa8436 | 158 | $sep = '&'; |
35fa92e8 | 159 | foreach ($args as $k=>$v) { |
160 | if (!$encode) { | |
161 | $k = urlencode($k); | |
162 | $v = urlencode($v); | |
163 | } | |
164 | $qs .= "$k=$v$sep"; | |
165 | } | |
166 | return $encode ? urlencode($qs) : $qs; | |
167 | } | |
168 | ||
8c4a0c30 | 169 | public function count() |
170 | { | |
171 | return $this->count; | |
172 | } | |
173 | ||
8ce35b4a RB |
174 | /** Builds the view class from the given parameters |
175 | * @param $view A string ('profile' for 'ProfileView'); if null, | |
176 | * the default view is used. | |
177 | * @return A new PlView instance. | |
178 | */ | |
179 | private function &buildView($view) | |
8c4a0c30 | 180 | { |
181 | $view = strtolower($view); | |
182 | if (!$view || !class_exists($view . 'View') || !isset($this->mods[$view])) { | |
ff3eb9b7 | 183 | reset($this->mods); |
184 | $view = $this->default ? $this->default : key($this->mods); | |
8c4a0c30 | 185 | } |
186 | $this->mod = $view; | |
187 | $class = $view . 'View'; | |
188 | if (!class_exists($class)) { | |
189 | $view = null; | |
190 | } else { | |
8ce35b4a | 191 | $view = new $class($this, $this->modParams[$this->mod]); |
8c4a0c30 | 192 | if (!$view instanceof PlView) { |
193 | $view = null; | |
194 | } | |
195 | } | |
196 | return $view; | |
197 | } | |
198 | ||
8ce35b4a RB |
199 | /** Creates the view: sets the page template, assigns Smarty vars. |
200 | * @param $baseurl The base URL for this (for instance, "search/") | |
201 | * @param $page The page in which the view should be loaded | |
202 | * @param $view The name of the view; if null, the default one will be used. | |
203 | */ | |
204 | public function apply($baseurl, PlPage &$page, $view = null) | |
8c4a0c30 | 205 | { |
8ce35b4a | 206 | $view =& $this->buildView($view); |
8c4a0c30 | 207 | if (is_null($view)) { |
208 | return false; | |
209 | } | |
35fa92e8 | 210 | $args = $view->args(); |
7cb40d85 | 211 | $page->coreTpl('plset.tpl'); |
8c4a0c30 | 212 | $page->assign('plset_base', $baseurl); |
213 | $page->assign('plset_mods', $this->mods); | |
214 | $page->assign('plset_mod', $this->mod); | |
e8e034fd RB |
215 | $page->assign('plset_args', $this->encodeArgs($args)); |
216 | $page->assign('plset_args_enc', $this->encodeArgs($args, true)); | |
8c4a0c30 | 217 | foreach ($this->modParams[$this->mod] as $param=>$value) { |
218 | $page->assign($this->mod . '_' . $param, $value); | |
219 | } | |
220 | $page->assign('plset_content', $view->apply($page)); | |
221 | $page->assign('plset_count', $this->count); | |
222 | return true; | |
223 | } | |
224 | } | |
225 | ||
226 | interface PlView | |
227 | { | |
8ce35b4a RB |
228 | /** Constructs a new PlView |
229 | * @param $set The set | |
230 | * @param $params Parameters to tune the view (sort by score, include promo...) | |
231 | */ | |
232 | public function __construct(PlSet &$set, array $params); | |
233 | ||
234 | /** Applies the view to a page | |
235 | * The content of the set is fetched here. | |
236 | * @param $page Page to which the view will be applied | |
237 | * @return The name of the global view template (for displaying the view, | |
238 | * not the items of the set) | |
239 | */ | |
3efb6052 | 240 | public function apply(PlPage &$page); |
8ce35b4a | 241 | |
e8e034fd RB |
242 | /** As PlSet->args(), returns the ?foo=bar part of the URL for generating |
243 | * this PlSet, after adding the necessary components and removing useless ones. | |
8ce35b4a | 244 | */ |
35fa92e8 | 245 | public function args(); |
8c4a0c30 | 246 | } |
247 | ||
bd9b36fe RB |
248 | /** This class describes an Order as used in a PlView : |
249 | * - It is based on a PlFilterOrder | |
250 | * - It has a short identifier | |
251 | * - It has a full name, to display on the page | |
252 | */ | |
253 | class PlViewOrder | |
254 | { | |
4c27b6a2 | 255 | public $pfos = null; |
bd9b36fe RB |
256 | public $name = null; |
257 | public $displaytext = null; | |
258 | ||
259 | /** Build a PlViewOrder | |
260 | * @param $name Name of the order (key) | |
261 | * @param $displaytext Text to display | |
4c27b6a2 | 262 | * @param $pfos Array of PlFilterOrder for the order |
bd9b36fe | 263 | */ |
4c27b6a2 | 264 | public function __construct($name, $pfos, $displaytext = null) |
bd9b36fe | 265 | { |
4aa4899b | 266 | $this->name = $name; |
bd9b36fe RB |
267 | if (is_null($displaytext)) { |
268 | $this->displaytext = ucfirst($name); | |
269 | } else { | |
4aa4899b | 270 | $this->displaytext = $displaytext; |
bd9b36fe | 271 | } |
4c27b6a2 | 272 | $this->pfos = $pfos; |
bd9b36fe RB |
273 | } |
274 | } | |
275 | ||
8c4a0c30 | 276 | abstract class MultipageView implements PlView |
277 | { | |
278 | protected $set; | |
279 | ||
280 | public $pages = 1; | |
281 | public $page = 1; | |
282 | public $offset = 0; | |
283 | ||
8c4a0c30 | 284 | protected $entriesPerPage = 20; |
285 | protected $params = array(); | |
286 | ||
35fa92e8 | 287 | protected $sortkeys = array(); |
288 | protected $defaultkey = null; | |
289 | ||
5e92949d FB |
290 | protected $bound_field = null; |
291 | ||
bd9b36fe RB |
292 | /** Builds a MultipageView |
293 | * @param $set The associated PlSet | |
bd9b36fe RB |
294 | * @param $params Parameters of the view |
295 | */ | |
8ce35b4a | 296 | public function __construct(PlSet &$set, array $params) |
8c4a0c30 | 297 | { |
298 | $this->set =& $set; | |
299 | $this->page = Env::i('page', 1); | |
300 | $this->offset = $this->entriesPerPage * ($this->page - 1); | |
301 | $this->params = $params; | |
302 | } | |
303 | ||
bd9b36fe RB |
304 | /** Add an order to the view |
305 | */ | |
306 | protected function addSort(PlViewOrder &$pvo, $default = false) | |
5e92949d | 307 | { |
bd9b36fe | 308 | $this->sortkeys[$pvo->name] = $pvo; |
35fa92e8 | 309 | if (!$this->defaultkey || $default) { |
bd9b36fe | 310 | $this->defaultkey = $pvo->name; |
35fa92e8 | 311 | } |
312 | } | |
313 | ||
4c27b6a2 | 314 | /** Returns a list of PFO objects in accordance with the user's choice |
bd9b36fe | 315 | */ |
8c4a0c30 | 316 | public function order() |
317 | { | |
35fa92e8 | 318 | $order = Env::v('order', $this->defaultkey); |
319 | $invert = ($order{0} == '-'); | |
320 | if ($invert) { | |
321 | $order = substr($order, 1); | |
322 | } | |
4c27b6a2 RB |
323 | |
324 | $ordering = $this->sortkeys[$order]; | |
325 | if ($invert) { | |
326 | foreach ($ordering->pfos as $pfo) { | |
327 | $pfo->toggleDesc(); | |
35fa92e8 | 328 | } |
8c4a0c30 | 329 | } |
4c27b6a2 | 330 | return $ordering->pfos; |
bd9b36fe RB |
331 | } |
332 | ||
333 | /** Returns information on the order of bounds | |
334 | * @return * 1 if normal bounds | |
335 | * * -1 if inversed bounds | |
336 | * * 0 if bounds shouldn't be displayed | |
337 | */ | |
338 | public function bounds() | |
339 | { | |
340 | return null; | |
8c4a0c30 | 341 | } |
342 | ||
7efca70c RB |
343 | public function limit() |
344 | { | |
43343e2a | 345 | return new PlLimit($this->entriesPerPage, $this->offset); |
7efca70c RB |
346 | } |
347 | ||
bd9b36fe | 348 | /** Name of the template to use for displaying items of the view |
8ce35b4a | 349 | * e.g plview.minifiche.tpl, plview.trombi.pl, ... |
bd9b36fe | 350 | */ |
8c4a0c30 | 351 | abstract public function templateName(); |
352 | ||
bd9b36fe RB |
353 | /** Returns the value of a boundary of the current view (in order |
354 | * to show "from C to F") | |
355 | * @param $obj The boundary result whose value must be shown to the user | |
8ce35b4a | 356 | * (e.g a Profile, ...) |
4c27b6a2 | 357 | * @return The bound |
bd9b36fe | 358 | */ |
7efca70c | 359 | abstract protected function getBoundValue($obj); |
bd9b36fe | 360 | |
3efb6052 | 361 | public function apply(PlPage &$page) |
8c4a0c30 | 362 | { |
bd9b36fe | 363 | foreach ($this->order() as $order) { |
4c27b6a2 | 364 | if (!is_null($order)) { |
24bb3936 | 365 | $this->set->addSort($order); |
4c27b6a2 | 366 | } |
bd9b36fe RB |
367 | } |
368 | $res = $this->set->get($this->limit()); | |
369 | ||
5e92949d | 370 | $show_bounds = $this->bounds(); |
5e92949d | 371 | if ($show_bounds) { |
b9bd8dd3 SJ |
372 | $start = current($res); |
373 | $end = end($res); | |
5e92949d | 374 | if ($show_bounds == 1) { |
701c0b6f | 375 | $first = $this->getBoundValue($start); |
bd9b36fe | 376 | $last = $this->getBoundValue($end); |
5e92949d | 377 | } elseif ($show_bounds == -1) { |
bd9b36fe | 378 | $first = $this->getBoundValue($end); |
701c0b6f | 379 | $last = $this->getBoundValue($start); |
5e92949d FB |
380 | } |
381 | $page->assign('first', $first); | |
382 | $page->assign('last', $last); | |
383 | } | |
384 | ||
385 | $page->assign('show_bounds', $show_bounds); | |
35fa92e8 | 386 | $page->assign('order', Env::v('order', $this->defaultkey)); |
387 | $page->assign('orders', $this->sortkeys); | |
8c4a0c30 | 388 | $page->assign_by_ref('plview', $this); |
b0a8daa8 FB |
389 | if (is_array($res)) { |
390 | $page->assign('set_keys', array_keys($res)); | |
391 | } | |
5e92949d | 392 | $page->assign_by_ref('set', $res); |
8c4a0c30 | 393 | $count = $this->set->count(); |
394 | $this->pages = intval(ceil($count / $this->entriesPerPage)); | |
7cb40d85 | 395 | return PlPage::getCoreTpl('plview.multipage.tpl'); |
8c4a0c30 | 396 | } |
35fa92e8 | 397 | |
e8e034fd RB |
398 | /** Arguments are those needed by the set, minus 'page' and 'order' which |
399 | * will be set to new values in the html links. | |
400 | */ | |
35fa92e8 | 401 | public function args() |
402 | { | |
403 | $list = $this->set->args(); | |
404 | unset($list['page']); | |
405 | unset($list['order']); | |
406 | return $list; | |
eaf30d86 | 407 | } |
8c4a0c30 | 408 | } |
409 | ||
410 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
411 | ?> |