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