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