Modify PlSet to allow for custom PlFilter getters
[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 private $mods = array();
36 private $modParams = array();
37 private $mod = null;
38 private $default = null;
39
40 public function __construct(PlFilterCondition &$cond, $orders = null)
41 {
42 if ($cond instanceof PFC_And) {
43 $this->conds = $cond;
44 } else {
45 $this->conds = new PFC_And($cond);
46 }
47
48 if (!is_null($orders) && $orders instanceof PlFilterOrder) {
49 $this->addSort($orders);
50 } else if (is_array($orders)){
51 foreach ($orders as $order) {
52 $this->addSort($order);
53 }
54 }
55 }
56
57 public function addMod($name, $description, $default = false, array $params = array())
58 {
59 $name = strtolower($name);
60 $this->mods[$name] = $description;
61 $this->modParams[$name] = $params;
62 if ($default) {
63 $this->default = $name;
64 }
65 }
66
67 public function rmMod($name)
68 {
69 $name = strtolower($name);
70 unset($this->mods[$name]);
71 }
72
73 public function addSort(PlFilterOrder &$order)
74 {
75 $this->orders[] = $order;
76 }
77
78 public function addCond(PlFilterCondition &$cond)
79 {
80 $this->conds->addChild($cond);
81 }
82
83 /** This function builds the right kind of PlFilter from given data
84 * @param $cond The PlFilterCondition for the filter
85 * @param $orders An array of PlFilterOrder for the filter
86 */
87 abstract protected function buildFilter(PlFilterCondition &$cond, $orders);
88
89 /** This function returns the results of the given filter
90 * wihtin $limit ; available when the PlFilter getter isn't the usual get
91 * @param &$pf The filter
92 * @param $limit The PlLimit
93 * @return The results of the filter
94 */
95 protected function &getFilterResults(PlFilter &$pf, PlLimit $limit)
96 {
97 return $pf->get($limit);
98 }
99
100 /** This function returns the values of the set
101 * @param $limit A PlLimit for selecting users
102 * @param $orders An optional array of PFO to use before the "default" ones
103 * @return The result of $pf->get();
104 */
105 public function &get(PlLimit $limit = null, $orders = array())
106 {
107 if (!is_array($orders)) {
108 $orders = array($orders);
109 }
110
111 $orders = array_merge($orders, $this->orders);
112
113 $pf = $this->buildFilter($this->conds, $orders);
114
115 if (is_null($limit)) {
116 $limit = new PlLimit(self::DEFAULT_MAX_RES, 0);
117 }
118 $it = $this->getFilterResults($pf, $limit);
119 $this->count = $pf->getTotalCount();
120 return $it;
121 }
122
123 public function args()
124 {
125 $get = $_GET;
126 unset($get['n']);
127 return $get;
128 }
129
130 protected function encodeArgs(array $args, $encode = false)
131 {
132 $qs = '?';
133 $sep = '&';
134 foreach ($args as $k=>$v) {
135 if (!$encode) {
136 $k = urlencode($k);
137 $v = urlencode($v);
138 }
139 $qs .= "$k=$v$sep";
140 }
141 return $encode ? urlencode($qs) : $qs;
142 }
143
144 public function count()
145 {
146 return $this->count;
147 }
148
149 private function &buildView($view, $data)
150 {
151 $view = strtolower($view);
152 if (!$view || !class_exists($view . 'View') || !isset($this->mods[$view])) {
153 reset($this->mods);
154 $view = $this->default ? $this->default : key($this->mods);
155 }
156 $this->mod = $view;
157 $class = $view . 'View';
158 if (!class_exists($class)) {
159 $view = null;
160 } else {
161 $view = new $class($this, $data, $this->modParams[$this->mod]);
162 if (!$view instanceof PlView) {
163 $view = null;
164 }
165 }
166 return $view;
167 }
168
169 public function apply($baseurl, PlPage &$page, $view = null, $data = null)
170 {
171 $view =& $this->buildView($view, $data);
172 if (is_null($view)) {
173 return false;
174 }
175 $args = $view->args();
176 if (!isset($args['rechercher'])) {
177 $args['rechercher'] = 'Chercher';
178 }
179 $page->coreTpl('plset.tpl');
180 $page->assign('plset_base', $baseurl);
181 $page->assign('plset_mods', $this->mods);
182 $page->assign('plset_mod', $this->mod);
183 $page->assign('plset_search', $this->encodeArgs($args));
184 $page->assign('plset_search_enc', $this->encodeArgs($args, true));
185 foreach ($this->modParams[$this->mod] as $param=>$value) {
186 $page->assign($this->mod . '_' . $param, $value);
187 }
188 $page->assign('plset_content', $view->apply($page));
189 $page->assign('plset_count', $this->count);
190 return true;
191 }
192 }
193
194 interface PlView
195 {
196 public function __construct(PlSet &$set, $data, array $params);
197 public function apply(PlPage &$page);
198 public function args();
199 }
200
201 /** This class describes an Order as used in a PlView :
202 * - It is based on a PlFilterOrder
203 * - It has a short identifier
204 * - It has a full name, to display on the page
205 */
206 class PlViewOrder
207 {
208 public $pfos = null;
209 public $name = null;
210 public $displaytext = null;
211
212 /** Build a PlViewOrder
213 * @param $name Name of the order (key)
214 * @param $displaytext Text to display
215 * @param $pfos Array of PlFilterOrder for the order
216 */
217 public function __construct($name, $pfos, $displaytext = null)
218 {
219 $this->name = $name;
220 if (is_null($displaytext)) {
221 $this->displaytext = ucfirst($name);
222 } else {
223 $this->displaytext = $displaytext;
224 }
225 $this->pfos = $pfos;
226 }
227 }
228
229 abstract class MultipageView implements PlView
230 {
231 protected $set;
232
233 public $pages = 1;
234 public $page = 1;
235 public $offset = 0;
236
237 protected $entriesPerPage = 20;
238 protected $params = array();
239
240 protected $sortkeys = array();
241 protected $defaultkey = null;
242
243 protected $bound_field = null;
244
245 /** Builds a MultipageView
246 * @param $set The associated PlSet
247 * @param $data Data for the PlSet
248 * @param $params Parameters of the view
249 */
250 public function __construct(PlSet &$set, $data, array $params)
251 {
252 $this->set =& $set;
253 $this->page = Env::i('page', 1);
254 $this->offset = $this->entriesPerPage * ($this->page - 1);
255 $this->params = $params;
256 }
257
258 /** Add an order to the view
259 */
260 protected function addSort(PlViewOrder &$pvo, $default = false)
261 {
262 $this->sortkeys[$pvo->name] = $pvo;
263 if (!$this->defaultkey || $default) {
264 $this->defaultkey = $pvo->name;
265 }
266 }
267
268 /** Returns a list of PFO objects in accordance with the user's choice
269 */
270 public function order()
271 {
272 $order = Env::v('order', $this->defaultkey);
273 $invert = ($order{0} == '-');
274 if ($invert) {
275 $order = substr($order, 1);
276 }
277
278 $ordering = $this->sortkeys[$order];
279 if ($invert) {
280 foreach ($ordering->pfos as $pfo) {
281 $pfo->toggleDesc();
282 }
283 }
284 return $ordering->pfos;
285 }
286
287 /** Returns information on the order of bounds
288 * @return * 1 if normal bounds
289 * * -1 if inversed bounds
290 * * 0 if bounds shouldn't be displayed
291 */
292 public function bounds()
293 {
294 return null;
295 }
296
297 public function limit()
298 {
299 return new PlLimit($this->entriesPerPage, $this->offset);
300 }
301
302 /** Name of the template to use for displaying items of the view
303 */
304 abstract public function templateName();
305
306 /** Returns the value of a boundary of the current view (in order
307 * to show "from C to F")
308 * @param $obj The boundary result whose value must be shown to the user
309 * @return The bound
310 */
311 abstract protected function getBoundValue($obj);
312
313 /** Applies the view to a page
314 * @param $page Page to which the view will be applied
315 */
316 public function apply(PlPage &$page)
317 {
318 foreach ($this->order() as $order) {
319 if (!is_null($order)) {
320 $this->set->addSort($order);
321 }
322 }
323 $res = $this->set->get($this->limit());
324
325 $show_bounds = $this->bounds();
326 if ($show_bounds) {
327 $start = current($res);
328 $end = end($res);
329 if ($show_bounds == 1) {
330 $first = $this->getBoundValue($start);
331 $last = $this->getBoundValue($end);
332 } elseif ($show_bounds == -1) {
333 $first = $this->getBoundValue($end);
334 $last = $this->getBoundValue($start);
335 }
336 $page->assign('first', $first);
337 $page->assign('last', $last);
338 }
339
340 $page->assign('show_bounds', $show_bounds);
341 $page->assign('order', Env::v('order', $this->defaultkey));
342 $page->assign('orders', $this->sortkeys);
343 $page->assign_by_ref('plview', $this);
344 $page->assign_by_ref('set', $res);
345 $count = $this->set->count();
346 $this->pages = intval(ceil($count / $this->entriesPerPage));
347 return PlPage::getCoreTpl('plview.multipage.tpl');
348 }
349
350 public function args()
351 {
352 $list = $this->set->args();
353 unset($list['page']);
354 unset($list['order']);
355 return $list;
356 }
357 }
358
359 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
360 ?>