fc96f1735f787fd486557de36b5bc64108a55619
2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 results of the given filter
90 * wihtin $limit ; available when the PlFilter getter isn't the usual get
91 * @param &$pf The filter
92 * @param $limit The PlLimit
93 * @return The results of the filter
95 protected function &getFilterResults(PlFilter
&$pf, PlLimit
$limit)
97 return $pf->get($limit);
100 /** This function returns the values of the set
101 * @param $limit A PlLimit for selecting users
102 * @param $orders An optional array of PFO to use before the "default" ones
103 * @return The result of $pf->get();
105 public function &get(PlLimit
$limit = null
, $orders = array())
107 if (!is_array($orders)) {
108 $orders = array($orders);
111 $orders = array_merge($orders, $this->orders
);
113 $pf = $this->buildFilter($this->conds
, $orders);
115 if (is_null($limit)) {
116 $limit = new PlLimit(self
::DEFAULT_MAX_RES
, 0);
118 $it = $this->getFilterResults($pf, $limit);
119 $this->count
= $pf->getTotalCount();
123 public function args()
130 protected function encodeArgs(array $args, $encode = false
)
134 foreach ($args as $k=>$v) {
141 return $encode ?
urlencode($qs) : $qs;
144 public function count()
149 private function &buildView($view, $data)
151 $view = strtolower($view);
152 if (!$view ||
!class_exists($view . 'View') ||
!isset($this->mods
[$view])) {
154 $view = $this->default ?
$this->default : key($this->mods
);
157 $class = $view . 'View';
158 if (!class_exists($class)) {
161 $view = new $class($this, $data, $this->modParams
[$this->mod
]);
162 if (!$view instanceof PlView
) {
169 public function apply($baseurl, PlPage
&$page, $view = null
, $data = null
)
171 $view =& $this->buildView($view, $data);
172 if (is_null($view)) {
175 $args = $view->args();
176 if (!isset($args['rechercher'])) {
177 $args['rechercher'] = 'Chercher';
179 $page->coreTpl('plset.tpl');
180 $page->assign('plset_base', $baseurl);
181 $page->assign('plset_mods', $this->mods
);
182 $page->assign('plset_mod', $this->mod
);
183 $page->assign('plset_search', $this->encodeArgs($args));
184 $page->assign('plset_search_enc', $this->encodeArgs($args, true
));
185 foreach ($this->modParams
[$this->mod
] as $param=>$value) {
186 $page->assign($this->mod
. '_' . $param, $value);
188 $page->assign('plset_content', $view->apply($page));
189 $page->assign('plset_count', $this->count
);
196 public function __construct(PlSet
&$set, $data, array $params);
197 public function apply(PlPage
&$page);
198 public function args();
201 /** This class describes an Order as used in a PlView :
202 * - It is based on a PlFilterOrder
203 * - It has a short identifier
204 * - It has a full name, to display on the page
210 public $displaytext = null
;
212 /** Build a PlViewOrder
213 * @param $name Name of the order (key)
214 * @param $displaytext Text to display
215 * @param $pfos Array of PlFilterOrder for the order
217 public function __construct($name, $pfos, $displaytext = null
)
220 if (is_null($displaytext)) {
221 $this->displaytext
= ucfirst($name);
223 $this->displaytext
= $displaytext;
229 abstract class MultipageView
implements PlView
237 protected $entriesPerPage = 20;
238 protected $params = array();
240 protected $sortkeys = array();
241 protected $defaultkey = null
;
243 protected $bound_field = null
;
245 /** Builds a MultipageView
246 * @param $set The associated PlSet
247 * @param $data Data for the PlSet
248 * @param $params Parameters of the view
250 public function __construct(PlSet
&$set, $data, array $params)
253 $this->page
= Env
::i('page', 1);
254 $this->offset
= $this->entriesPerPage
* ($this->page
- 1);
255 $this->params
= $params;
258 /** Add an order to the view
260 protected function addSort(PlViewOrder
&$pvo, $default = false
)
262 $this->sortkeys
[$pvo->name
] = $pvo;
263 if (!$this->defaultkey ||
$default) {
264 $this->defaultkey
= $pvo->name
;
268 /** Returns a list of PFO objects in accordance with the user's choice
270 public function order()
272 $order = Env
::v('order', $this->defaultkey
);
273 $invert = ($order{0} == '-');
275 $order = substr($order, 1);
278 $ordering = $this->sortkeys
[$order];
280 foreach ($ordering->pfos
as $pfo) {
284 return $ordering->pfos
;
287 /** Returns information on the order of bounds
288 * @return * 1 if normal bounds
289 * * -1 if inversed bounds
290 * * 0 if bounds shouldn't be displayed
292 public function bounds()
297 public function limit()
299 return new PlLimit($this->entriesPerPage
, $this->offset
);
302 /** Name of the template to use for displaying items of the view
304 abstract public function templateName();
306 /** Returns the value of a boundary of the current view (in order
307 * to show "from C to F")
308 * @param $obj The boundary result whose value must be shown to the user
311 abstract protected function getBoundValue($obj);
313 /** Applies the view to a page
314 * @param $page Page to which the view will be applied
316 public function apply(PlPage
&$page)
318 foreach ($this->order() as $order) {
319 if (!is_null($order)) {
320 $this->set
->addSort($order);
323 $res = $this->set
->get($this->limit());
325 $show_bounds = $this->bounds();
327 $start = current($res);
329 if ($show_bounds == 1) {
330 $first = $this->getBoundValue($start);
331 $last = $this->getBoundValue($end);
332 } elseif ($show_bounds == -1) {
333 $first = $this->getBoundValue($end);
334 $last = $this->getBoundValue($start);
336 $page->assign('first', $first);
337 $page->assign('last', $last);
340 $page->assign('show_bounds', $show_bounds);
341 $page->assign('order', Env
::v('order', $this->defaultkey
));
342 $page->assign('orders', $this->sortkeys
);
343 $page->assign_by_ref('plview', $this);
344 $page->assign_by_ref('set', $res);
345 $count = $this->set
->count();
346 $this->pages
= intval(ceil($count / $this->entriesPerPage
));
347 return PlPage
::getCoreTpl('plview.multipage.tpl');
350 public function args()
352 $list = $this->set
->args();
353 unset($list['page']);
354 unset($list['order']);
359 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: