8ef6ba84fc94bf31d3d1f28143cec94e34a912ee
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 ***************************************************************************/
22 /** UserSet is a light-weight Model/View tool for displaying a set of items
26 private $conds = null
;
27 private $orders = null
;
28 private $limit = null
;
30 protected $count = null
;
32 private $mods = array();
33 private $modParams = array();
35 private $default = null
;
37 public function __construct(PlFilterCondition
&$cond, $orders)
39 if ($cond instanceof PFC_And
) {
42 $this->conds
= new PFC_And($cond);
45 if ($orders instanceof PlFilterOrder
) {
46 $this->orders
= array($order);
48 foreach ($orders as $order) {
49 $this->orders
[] = $order;
54 public function addMod($name, $description, $default = false
, array $params = array())
56 $name = strtolower($name);
57 $this->mods
[$name] = $description;
58 $this->modParams
[$name] = $params;
60 $this->default = $name;
64 public function rmMod($name)
66 $name = strtolower($name);
67 unset($this->mods
[$name]);
70 public function addSort(PlFilterOrder
&$order)
72 $this->orders
[] = $order;
75 public function addCond(PlFilterCondition
&$cond)
77 $this->conds
->addChild($cond);
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
84 abstract protected function buildFilter(PlFilterCondition
&$cond, $orders);
86 public function &get(PlFilterLimit
$limit = null
)
88 $pf = $this->buildFilter($this->conds
, $this->orders
);
90 $it = $pf->get($limit);
91 $this->count
= $pf->getTotalCount();
95 public function args()
102 protected function encodeArgs(array $args, $encode = false
)
106 foreach ($args as $k=>$v) {
113 return $encode ?
urlencode($qs) : $qs;
116 public function count()
121 private function &buildView($view, $data)
123 $view = strtolower($view);
124 if (!$view ||
!class_exists($view . 'View') ||
!isset($this->mods
[$view])) {
126 $view = $this->default ?
$this->default : key($this->mods
);
129 $class = $view . 'View';
130 if (!class_exists($class)) {
133 $view = new $class($this, $data, $this->modParams
[$this->mod
]);
134 if (!$view instanceof PlView
) {
141 public function apply($baseurl, PlPage
&$page, $view = null
, $data = null
)
143 $view =& $this->buildView($view, $data);
144 if (is_null($view)) {
147 $args = $view->args();
148 if (!isset($args['rechercher'])) {
149 $args['rechercher'] = 'Chercher';
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);
160 $page->assign('plset_content', $view->apply($page));
161 $page->assign('plset_count', $this->count
);
168 public function __construct(PlSet
&$set, $data, array $params);
169 public function apply(PlPage
&$page);
170 public function args();
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
182 public $displaytext = null
;
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
189 public function __construct($name, PlFilterOrder
&$pfo, $displaytext = null
)
192 if (is_null($displaytext)) {
193 $this->displaytext
= ucfirst($name);
195 $this->displaytext
= $displaytext;
201 abstract class MultipageView
implements PlView
209 protected $entriesPerPage = 20;
210 protected $params = array();
212 protected $sortkeys = array();
213 protected $defaultkey = null
;
215 protected $bound_field = null
;
217 /** Builds a MultipageView
218 * @param $set The associated PlSet
219 * @param $data Data for the PlSet
220 * @param $params Parameters of the view
222 public function __construct(PlSet
&$set, $data, array $params)
225 $this->page
= Env
::i('page', 1);
226 $this->offset
= $this->entriesPerPage
* ($this->page
- 1);
227 $this->params
= $params;
230 /** Add an order to the view
232 protected function addSort(PlViewOrder
&$pvo, $default = false
)
234 $this->sortkeys
[$pvo->name
] = $pvo;
235 if (!$this->defaultkey ||
$default) {
236 $this->defaultkey
= $pvo->name
;
240 /** Returns a list of PFO objects, the "default" one first
242 public function order()
244 $order = Env
::v('order', $this->defaultkey
);
245 $invert = ($order{0} == '-');
247 $order = substr($order, 1);
250 if (count($this->sortkeys
)) {
253 foreach ($this->sortkeys
as $name => $sort) {
254 $desc = $sort->pfo
->isDescending();;
256 $sort->pfo
->toggleDesc();
258 if ($name == $order) {
259 $list[0] = $sort->pfo
;
261 $list[] = $sort->pfo
;
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
272 public function bounds()
277 public function limit()
282 /** Name of the template to use for displaying items of the view
284 abstract public function templateName();
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
290 abstract protected function getBoundValue($obj);
292 /** Applies the view to a page
293 * @param $page Page to which the view will be applied
295 public function apply(PlPage
&$page)
297 foreach ($this->order() as $order) {
298 $this->set
->addSort($order->pfo
);
300 $res = $this->set
->get($this->limit());
302 $show_bounds = $this->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]);
312 $page->assign('first', $first);
313 $page->assign('last', $last);
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');
326 public function args()
328 $list = $this->set
->args();
329 unset($list['page']);
330 unset($list['order']);
335 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: