41e73da5fd0fe784eca6f01790b786fd479d6c1c
[platal.git] / classes / plset.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
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
22
23 /** UserSet is a light-weight Model/View tool for displaying a set of items
24 */
25 abstract class PlSet
26 {
27 const DEFAULT_MAX_RES = 20;
28
29 protected $conds = null;
30 protected $orders = array();
31 protected $limit = null;
32
33 protected $count = null;
34
35 // A list of available views
36 private $mods = array();
37 // An array of $view_name => array($parameters)
38 private $modParams = array();
39 // The current view name
40 private $mod = null;
41 // The default view name
42 private $default = null;
43
44 public function __construct(PlFilterCondition &$cond, $orders = null)
45 {
46 if ($cond instanceof PFC_And) {
47 $this->conds = $cond;
48 } else {
49 $this->conds = new PFC_And($cond);
50 }
51
52 if (!is_null($orders) && $orders instanceof PlFilterOrder) {
53 $this->addSort($orders);
54 } else if (is_array($orders)){
55 foreach ($orders as $order) {
56 $this->addSort($order);
57 }
58 }
59 }
60
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 */
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
84 /** Adds a new sort (on the PlFilter)
85 */
86 public function addSort(PlFilterOrder &$order)
87 {
88 $this->orders[] = $order;
89 }
90
91 /** Adds a new condition to the PlFilter
92 */
93 public function addCond(PlFilterCondition &$cond)
94 {
95 $this->conds->addChild($cond);
96 }
97
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 */
102 abstract protected function buildFilter(PlFilterCondition &$cond, $orders);
103
104 /** This function returns the results of the given filter
105 * wihtin $limit; can be use to replace the default $pf->get call.
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 {
112 $res = $pf->get($limit);
113 return $res;
114 }
115
116 /** This function returns the values of the set, and sets $count with the
117 * total number of results.
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())
123 {
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);
131
132 if (is_null($limit)) {
133 $limit = new PlLimit(self::DEFAULT_MAX_RES, 0);
134 }
135 $it = $this->getFilterResults($pf, $limit);
136 $this->count = $pf->getTotalCount();
137 return $it;
138 }
139
140 /** XXX ??
141 */
142 public function args()
143 {
144 $get = $_GET;
145 unset($get['n']);
146 return $get;
147 }
148
149 /** XXX?
150 */
151 protected function encodeArgs(array $args, $encode = false)
152 {
153 $qs = '?';
154 $sep = '&';
155 foreach ($args as $k=>$v) {
156 if (!$encode) {
157 $k = urlencode($k);
158 $v = urlencode($v);
159 }
160 $qs .= "$k=$v$sep";
161 }
162 return $encode ? urlencode($qs) : $qs;
163 }
164
165 public function count()
166 {
167 return $this->count;
168 }
169
170 /** Builds the view class from the given parameters
171 * @param $view A string ('profile' for 'ProfileView'); if null,
172 * the default view is used.
173 * @return A new PlView instance.
174 */
175 private function &buildView($view)
176 {
177 $view = strtolower($view);
178 if (!$view || !class_exists($view . 'View') || !isset($this->mods[$view])) {
179 reset($this->mods);
180 $view = $this->default ? $this->default : key($this->mods);
181 }
182 $this->mod = $view;
183 $class = $view . 'View';
184 if (!class_exists($class)) {
185 $view = null;
186 } else {
187 $view = new $class($this, $this->modParams[$this->mod]);
188 if (!$view instanceof PlView) {
189 $view = null;
190 }
191 }
192 return $view;
193 }
194
195 /** Creates the view: sets the page template, assigns Smarty vars.
196 * @param $baseurl The base URL for this (for instance, "search/")
197 * @param $page The page in which the view should be loaded
198 * @param $view The name of the view; if null, the default one will be used.
199 */
200 public function apply($baseurl, PlPage &$page, $view = null)
201 {
202 $view =& $this->buildView($view);
203 if (is_null($view)) {
204 return false;
205 }
206 $args = $view->args();
207 if (!isset($args['rechercher'])) {
208 $args['rechercher'] = 'Chercher';
209 }
210 $page->coreTpl('plset.tpl');
211 $page->assign('plset_base', $baseurl);
212 $page->assign('plset_mods', $this->mods);
213 $page->assign('plset_mod', $this->mod);
214 $page->assign('plset_search', $this->encodeArgs($args));
215 $page->assign('plset_search_enc', $this->encodeArgs($args, true));
216 foreach ($this->modParams[$this->mod] as $param=>$value) {
217 $page->assign($this->mod . '_' . $param, $value);
218 }
219 $page->assign('plset_content', $view->apply($page));
220 $page->assign('plset_count', $this->count);
221 return true;
222 }
223 }
224
225 interface PlView
226 {
227 /** Constructs a new PlView
228 * @param $set The set
229 * @param $params Parameters to tune the view (sort by score, include promo...)
230 */
231 public function __construct(PlSet &$set, array $params);
232
233 /** Applies the view to a page
234 * The content of the set is fetched here.
235 * @param $page Page to which the view will be applied
236 * @return The name of the global view template (for displaying the view,
237 * not the items of the set)
238 */
239 public function apply(PlPage &$page);
240
241 /** XXX?
242 */
243 public function args();
244 }
245
246 /** This class describes an Order as used in a PlView :
247 * - It is based on a PlFilterOrder
248 * - It has a short identifier
249 * - It has a full name, to display on the page
250 */
251 class PlViewOrder
252 {
253 public $pfos = null;
254 public $name = null;
255 public $displaytext = null;
256
257 /** Build a PlViewOrder
258 * @param $name Name of the order (key)
259 * @param $displaytext Text to display
260 * @param $pfos Array of PlFilterOrder for the order
261 */
262 public function __construct($name, $pfos, $displaytext = null)
263 {
264 $this->name = $name;
265 if (is_null($displaytext)) {
266 $this->displaytext = ucfirst($name);
267 } else {
268 $this->displaytext = $displaytext;
269 }
270 $this->pfos = $pfos;
271 }
272 }
273
274 abstract class MultipageView implements PlView
275 {
276 protected $set;
277
278 public $pages = 1;
279 public $page = 1;
280 public $offset = 0;
281
282 protected $entriesPerPage = 20;
283 protected $params = array();
284
285 protected $sortkeys = array();
286 protected $defaultkey = null;
287
288 protected $bound_field = null;
289
290 /** Builds a MultipageView
291 * @param $set The associated PlSet
292 * @param $params Parameters of the view
293 */
294 public function __construct(PlSet &$set, array $params)
295 {
296 $this->set =& $set;
297 $this->page = Env::i('page', 1);
298 $this->offset = $this->entriesPerPage * ($this->page - 1);
299 $this->params = $params;
300 }
301
302 /** Add an order to the view
303 */
304 protected function addSort(PlViewOrder &$pvo, $default = false)
305 {
306 $this->sortkeys[$pvo->name] = $pvo;
307 if (!$this->defaultkey || $default) {
308 $this->defaultkey = $pvo->name;
309 }
310 }
311
312 /** Returns a list of PFO objects in accordance with the user's choice
313 */
314 public function order()
315 {
316 $order = Env::v('order', $this->defaultkey);
317 $invert = ($order{0} == '-');
318 if ($invert) {
319 $order = substr($order, 1);
320 }
321
322 $ordering = $this->sortkeys[$order];
323 if ($invert) {
324 foreach ($ordering->pfos as $pfo) {
325 $pfo->toggleDesc();
326 }
327 }
328 return $ordering->pfos;
329 }
330
331 /** Returns information on the order of bounds
332 * @return * 1 if normal bounds
333 * * -1 if inversed bounds
334 * * 0 if bounds shouldn't be displayed
335 */
336 public function bounds()
337 {
338 return null;
339 }
340
341 public function limit()
342 {
343 return new PlLimit($this->entriesPerPage, $this->offset);
344 }
345
346 /** Name of the template to use for displaying items of the view
347 * e.g plview.minifiche.tpl, plview.trombi.pl, ...
348 */
349 abstract public function templateName();
350
351 /** Returns the value of a boundary of the current view (in order
352 * to show "from C to F")
353 * @param $obj The boundary result whose value must be shown to the user
354 * (e.g a Profile, ...)
355 * @return The bound
356 */
357 abstract protected function getBoundValue($obj);
358
359 public function apply(PlPage &$page)
360 {
361 foreach ($this->order() as $order) {
362 if (!is_null($order)) {
363 $this->set->addSort($order);
364 }
365 }
366 $res = $this->set->get($this->limit());
367
368 $show_bounds = $this->bounds();
369 if ($show_bounds) {
370 $start = current($res);
371 $end = end($res);
372 if ($show_bounds == 1) {
373 $first = $this->getBoundValue($start);
374 $last = $this->getBoundValue($end);
375 } elseif ($show_bounds == -1) {
376 $first = $this->getBoundValue($end);
377 $last = $this->getBoundValue($start);
378 }
379 $page->assign('first', $first);
380 $page->assign('last', $last);
381 }
382
383 $page->assign('show_bounds', $show_bounds);
384 $page->assign('order', Env::v('order', $this->defaultkey));
385 $page->assign('orders', $this->sortkeys);
386 $page->assign_by_ref('plview', $this);
387 if (is_array($res)) {
388 $page->assign('set_keys', array_keys($res));
389 }
390 $page->assign_by_ref('set', $res);
391 $count = $this->set->count();
392 $this->pages = intval(ceil($count / $this->entriesPerPage));
393 return PlPage::getCoreTpl('plview.multipage.tpl');
394 }
395
396 public function args()
397 {
398 $list = $this->set->args();
399 unset($list['page']);
400 unset($list['order']);
401 return $list;
402 }
403 }
404
405 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
406 ?>