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