2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
23 /** UserSet is a light-weight Model/View tool for displaying a set of items
27 private $conds = null
;
28 private $orders = null
;
29 private $limit = null
;
31 protected $count = null
;
33 private $mods = array();
34 private $modParams = array();
36 private $default = null
;
38 public function __construct(PlFilterCondition
&$cond, $orders)
40 if ($cond instanceof PFC_And
) {
43 $this->conds
= new PFC_And($cond);
46 if ($orders instanceof PlFilterOrder
) {
47 $this->orders
= array($order);
49 foreach ($orders as $order) {
50 $this->orders
[] = $order;
55 public function addMod($name, $description, $default = false
, array $params = array())
57 $name = strtolower($name);
58 $this->mods
[$name] = $description;
59 $this->modParams
[$name] = $params;
61 $this->default = $name;
65 public function rmMod($name)
67 $name = strtolower($name);
68 unset($this->mods
[$name]);
71 public function addSort(PlFilterOrder
&$order)
73 $this->orders
[] = $order;
76 public function addCond(PlFilterCondition
&$cond)
78 $this->conds
->addChild($cond);
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
85 abstract protected function buildFilter(PlFilterCondition
&$cond, $orders);
87 public function &get(PlLimit
$limit = null
)
89 $pf = $this->buildFilter($this->conds
, $this->orders
);
91 if (is_null($limit)) {
92 $limit = new PlLimit(20, 0);
94 $it = $pf->get($limit);
95 $this->count
= $pf->getTotalCount();
99 public function args()
106 protected function encodeArgs(array $args, $encode = false
)
110 foreach ($args as $k=>$v) {
117 return $encode ?
urlencode($qs) : $qs;
120 public function count()
125 private function &buildView($view, $data)
127 $view = strtolower($view);
128 if (!$view ||
!class_exists($view . 'View') ||
!isset($this->mods
[$view])) {
130 $view = $this->default ?
$this->default : key($this->mods
);
133 $class = $view . 'View';
134 if (!class_exists($class)) {
137 $view = new $class($this, $data, $this->modParams
[$this->mod
]);
138 if (!$view instanceof PlView
) {
145 public function apply($baseurl, PlPage
&$page, $view = null
, $data = null
)
147 $view =& $this->buildView($view, $data);
148 if (is_null($view)) {
151 $args = $view->args();
152 if (!isset($args['rechercher'])) {
153 $args['rechercher'] = 'Chercher';
155 $page->coreTpl('plset.tpl');
156 $page->assign('plset_base', $baseurl);
157 $page->assign('plset_mods', $this->mods
);
158 $page->assign('plset_mod', $this->mod
);
159 $page->assign('plset_search', $this->encodeArgs($args));
160 $page->assign('plset_search_enc', $this->encodeArgs($args, true
));
161 foreach ($this->modParams
[$this->mod
] as $param=>$value) {
162 $page->assign($this->mod
. '_' . $param, $value);
164 $page->assign('plset_content', $view->apply($page));
165 $page->assign('plset_count', $this->count
);
172 public function __construct(PlSet
&$set, $data, array $params);
173 public function apply(PlPage
&$page);
174 public function args();
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
186 public $displaytext = null
;
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
193 public function __construct($name, PlFilterOrder
&$pfo, $displaytext = null
)
196 if (is_null($displaytext)) {
197 $this->displaytext
= ucfirst($name);
199 $this->displaytext
= $displaytext;
205 abstract class MultipageView
implements PlView
213 protected $entriesPerPage = 20;
214 protected $params = array();
216 protected $sortkeys = array();
217 protected $defaultkey = null
;
219 protected $bound_field = null
;
221 /** Builds a MultipageView
222 * @param $set The associated PlSet
223 * @param $data Data for the PlSet
224 * @param $params Parameters of the view
226 public function __construct(PlSet
&$set, $data, array $params)
229 $this->page
= Env
::i('page', 1);
230 $this->offset
= $this->entriesPerPage
* ($this->page
- 1);
231 $this->params
= $params;
234 /** Add an order to the view
236 protected function addSort(PlViewOrder
&$pvo, $default = false
)
238 $this->sortkeys
[$pvo->name
] = $pvo;
239 if (!$this->defaultkey ||
$default) {
240 $this->defaultkey
= $pvo->name
;
244 /** Returns a list of PFO objects, the "default" one first
246 public function order()
248 $order = Env
::v('order', $this->defaultkey
);
249 $invert = ($order{0} == '-');
251 $order = substr($order, 1);
254 if (count($this->sortkeys
)) {
257 foreach ($this->sortkeys
as $name => $sort) {
258 $desc = $sort->pfo
->isDescending();;
260 $sort->pfo
->toggleDesc();
262 if ($name == $order) {
263 $list[0] = $sort->pfo
;
265 $list[] = $sort->pfo
;
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
276 public function bounds()
281 public function limit()
286 /** Name of the template to use for displaying items of the view
288 abstract public function templateName();
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
294 abstract protected function getBoundValue($obj);
296 /** Applies the view to a page
297 * @param $page Page to which the view will be applied
299 public function apply(PlPage
&$page)
301 foreach ($this->order() as $order) {
302 $this->set
->addSort($order->pfo
);
304 $res = $this->set
->get($this->limit());
306 $show_bounds = $this->bounds();
309 if ($show_bounds == 1) {
310 $first = $this->getBoundValue($res[0]);
311 $last = $this->getBoundValue($end);
312 } elseif ($show_bounds == -1) {
313 $first = $this->getBoundValue($end);
314 $last = $this->getBoundValue($res[0]);
316 $page->assign('first', $first);
317 $page->assign('last', $last);
320 $page->assign('show_bounds', $show_bounds);
321 $page->assign('order', Env
::v('order', $this->defaultkey
));
322 $page->assign('orders', $this->sortkeys
);
323 $page->assign_by_ref('plview', $this);
324 $page->assign_by_ref('set', $res);
325 $count = $this->set
->count();
326 $this->pages
= intval(ceil($count / $this->entriesPerPage
));
327 return PlPage
::getCoreTpl('plview.multipage.tpl');
330 public function args()
332 $list = $this->set
->args();
333 unset($list['page']);
334 unset($list['order']);
339 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: