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