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