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