baac0728522934dae287cd9a9b77887f516892c9
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 const DEFAULT_MAX_RES
= 20;
29 protected $conds = null
;
30 protected $orders = array();
31 protected $limit = null
;
33 protected $count = null
;
35 private $mods = array();
36 private $modParams = array();
38 private $default = null
;
40 public function __construct(PlFilterCondition
&$cond, $orders = null
)
42 if ($cond instanceof PFC_And
) {
45 $this->conds
= new PFC_And($cond);
48 if (!is_null($orders) && $orders instanceof PlFilterOrder
) {
49 $this->addSort($orders);
50 } else if (is_array($orders)){
51 foreach ($orders as $order) {
52 $this->addSort($order);
57 public function addMod($name, $description, $default = false
, array $params = array())
59 $name = strtolower($name);
60 $this->mods
[$name] = $description;
61 $this->modParams
[$name] = $params;
63 $this->default = $name;
67 public function rmMod($name)
69 $name = strtolower($name);
70 unset($this->mods
[$name]);
73 public function addSort(PlFilterOrder
&$order)
75 $this->orders
[] = $order;
78 public function addCond(PlFilterCondition
&$cond)
80 $this->conds
->addChild($cond);
83 /** This function builds the right kind of PlFilter from given data
84 * @param $cond The PlFilterCondition for the filter
85 * @param $orders An array of PlFilterOrder for the filter
87 abstract protected function buildFilter(PlFilterCondition
&$cond, $orders);
89 /** This function returns the values of the set
90 * @param $limit A PlLimit for selecting users
91 * @param $orders An optional array of PFO to use before the "default" ones
92 * @return The result of $pf->get();
94 public function &get(PlLimit
$limit = null
, $orders = array())
96 if (!is_array($orders)) {
97 $orders = array($orders);
100 $orders = array_merge($orders, $this->orders
);
102 $pf = $this->buildFilter($this->conds
, $orders);
104 if (is_null($limit)) {
105 $limit = new PlLimit(self
::DEFAULT_MAX_RES
, 0);
107 $it = $pf->get($limit);
108 $this->count
= $pf->getTotalCount();
112 public function args()
119 protected function encodeArgs(array $args, $encode = false
)
123 foreach ($args as $k=>$v) {
130 return $encode ?
urlencode($qs) : $qs;
133 public function count()
138 private function &buildView($view, $data)
140 $view = strtolower($view);
141 if (!$view ||
!class_exists($view . 'View') ||
!isset($this->mods
[$view])) {
143 $view = $this->default ?
$this->default : key($this->mods
);
146 $class = $view . 'View';
147 if (!class_exists($class)) {
150 $view = new $class($this, $data, $this->modParams
[$this->mod
]);
151 if (!$view instanceof PlView
) {
158 public function apply($baseurl, PlPage
&$page, $view = null
, $data = null
)
160 $view =& $this->buildView($view, $data);
161 if (is_null($view)) {
164 $args = $view->args();
165 if (!isset($args['rechercher'])) {
166 $args['rechercher'] = 'Chercher';
168 $page->coreTpl('plset.tpl');
169 $page->assign('plset_base', $baseurl);
170 $page->assign('plset_mods', $this->mods
);
171 $page->assign('plset_mod', $this->mod
);
172 $page->assign('plset_search', $this->encodeArgs($args));
173 $page->assign('plset_search_enc', $this->encodeArgs($args, true
));
174 foreach ($this->modParams
[$this->mod
] as $param=>$value) {
175 $page->assign($this->mod
. '_' . $param, $value);
177 $page->assign('plset_content', $view->apply($page));
178 $page->assign('plset_count', $this->count
);
185 public function __construct(PlSet
&$set, $data, array $params);
186 public function apply(PlPage
&$page);
187 public function args();
190 /** This class describes an Order as used in a PlView :
191 * - It is based on a PlFilterOrder
192 * - It has a short identifier
193 * - It has a full name, to display on the page
199 public $displaytext = null
;
201 /** Build a PlViewOrder
202 * @param $name Name of the order (key)
203 * @param $displaytext Text to display
204 * @param $pfos Array of PlFilterOrder for the order
206 public function __construct($name, $pfos, $displaytext = null
)
209 if (is_null($displaytext)) {
210 $this->displaytext
= ucfirst($name);
212 $this->displaytext
= $displaytext;
218 abstract class MultipageView
implements PlView
226 protected $entriesPerPage = 20;
227 protected $params = array();
229 protected $sortkeys = array();
230 protected $defaultkey = null
;
232 protected $bound_field = null
;
234 /** Builds a MultipageView
235 * @param $set The associated PlSet
236 * @param $data Data for the PlSet
237 * @param $params Parameters of the view
239 public function __construct(PlSet
&$set, $data, array $params)
242 $this->page
= Env
::i('page', 1);
243 $this->offset
= $this->entriesPerPage
* ($this->page
- 1);
244 $this->params
= $params;
247 /** Add an order to the view
249 protected function addSort(PlViewOrder
&$pvo, $default = false
)
251 $this->sortkeys
[$pvo->name
] = $pvo;
252 if (!$this->defaultkey ||
$default) {
253 $this->defaultkey
= $pvo->name
;
257 /** Returns a list of PFO objects in accordance with the user's choice
259 public function order()
261 $order = Env
::v('order', $this->defaultkey
);
262 $invert = ($order{0} == '-');
264 $order = substr($order, 1);
267 $ordering = $this->sortkeys
[$order];
269 foreach ($ordering->pfos
as $pfo) {
273 return $ordering->pfos
;
276 /** Returns information on the order of bounds
277 * @return * 1 if normal bounds
278 * * -1 if inversed bounds
279 * * 0 if bounds shouldn't be displayed
281 public function bounds()
286 public function limit()
291 /** Name of the template to use for displaying items of the view
293 abstract public function templateName();
295 /** Returns the value of a boundary of the current view (in order
296 * to show "from C to F")
297 * @param $obj The boundary result whose value must be shown to the user
300 abstract protected function getBoundValue($obj);
302 /** Applies the view to a page
303 * @param $page Page to which the view will be applied
305 public function apply(PlPage
&$page)
307 foreach ($this->order() as $order) {
308 if (!is_null($order)) {
309 $this->set
->addSort($order);
312 $res = $this->set
->get($this->limit());
314 $show_bounds = $this->bounds();
317 if ($show_bounds == 1) {
318 $first = $this->getBoundValue($res[0]);
319 $last = $this->getBoundValue($end);
320 } elseif ($show_bounds == -1) {
321 $first = $this->getBoundValue($end);
322 $last = $this->getBoundValue($res[0]);
324 $page->assign('first', $first);
325 $page->assign('last', $last);
328 $page->assign('show_bounds', $show_bounds);
329 $page->assign('order', Env
::v('order', $this->defaultkey
));
330 $page->assign('orders', $this->sortkeys
);
331 $page->assign_by_ref('plview', $this);
332 $page->assign_by_ref('set', $res);
333 $count = $this->set
->count();
334 $this->pages
= intval(ceil($count / $this->entriesPerPage
));
335 return PlPage
::getCoreTpl('plview.multipage.tpl');
338 public function args()
340 $list = $this->set
->args();
341 unset($list['page']);
342 unset($list['order']);
347 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: