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 $res = $pf->get($limit);
101 /** This function returns the values of the set
102 * @param $limit A PlLimit for selecting users
103 * @param $orders An optional array of PFO to use before the "default" ones
104 * @return The result of $pf->get();
106 public function &get(PlLimit
$limit = null
, $orders = array())
108 if (!is_array($orders)) {
109 $orders = array($orders);
112 $orders = array_merge($orders, $this->orders
);
114 $pf = $this->buildFilter($this->conds
, $orders);
116 if (is_null($limit)) {
117 $limit = new PlLimit(self
::DEFAULT_MAX_RES
, 0);
119 $it = $this->getFilterResults($pf, $limit);
120 $this->count
= $pf->getTotalCount();
124 public function args()
131 protected function encodeArgs(array $args, $encode = false
)
135 foreach ($args as $k=>$v) {
142 return $encode ?
urlencode($qs) : $qs;
145 public function count()
150 private function &buildView($view, $data)
152 $view = strtolower($view);
153 if (!$view ||
!class_exists($view . 'View') ||
!isset($this->mods
[$view])) {
155 $view = $this->default ?
$this->default : key($this->mods
);
158 $class = $view . 'View';
159 if (!class_exists($class)) {
162 $view = new $class($this, $data, $this->modParams
[$this->mod
]);
163 if (!$view instanceof PlView
) {
170 public function apply($baseurl, PlPage
&$page, $view = null
, $data = null
)
172 $view =& $this->buildView($view, $data);
173 if (is_null($view)) {
176 $args = $view->args();
177 if (!isset($args['rechercher'])) {
178 $args['rechercher'] = 'Chercher';
180 $page->coreTpl('plset.tpl');
181 $page->assign('plset_base', $baseurl);
182 $page->assign('plset_mods', $this->mods
);
183 $page->assign('plset_mod', $this->mod
);
184 $page->assign('plset_search', $this->encodeArgs($args));
185 $page->assign('plset_search_enc', $this->encodeArgs($args, true
));
186 foreach ($this->modParams
[$this->mod
] as $param=>$value) {
187 $page->assign($this->mod
. '_' . $param, $value);
189 $page->assign('plset_content', $view->apply($page));
190 $page->assign('plset_count', $this->count
);
197 public function __construct(PlSet
&$set, $data, array $params);
198 public function apply(PlPage
&$page);
199 public function args();
202 /** This class describes an Order as used in a PlView :
203 * - It is based on a PlFilterOrder
204 * - It has a short identifier
205 * - It has a full name, to display on the page
211 public $displaytext = null
;
213 /** Build a PlViewOrder
214 * @param $name Name of the order (key)
215 * @param $displaytext Text to display
216 * @param $pfos Array of PlFilterOrder for the order
218 public function __construct($name, $pfos, $displaytext = null
)
221 if (is_null($displaytext)) {
222 $this->displaytext
= ucfirst($name);
224 $this->displaytext
= $displaytext;
230 abstract class MultipageView
implements PlView
238 protected $entriesPerPage = 20;
239 protected $params = array();
241 protected $sortkeys = array();
242 protected $defaultkey = null
;
244 protected $bound_field = null
;
246 /** Builds a MultipageView
247 * @param $set The associated PlSet
248 * @param $data Data for the PlSet
249 * @param $params Parameters of the view
251 public function __construct(PlSet
&$set, $data, array $params)
254 $this->page
= Env
::i('page', 1);
255 $this->offset
= $this->entriesPerPage
* ($this->page
- 1);
256 $this->params
= $params;
259 /** Add an order to the view
261 protected function addSort(PlViewOrder
&$pvo, $default = false
)
263 $this->sortkeys
[$pvo->name
] = $pvo;
264 if (!$this->defaultkey ||
$default) {
265 $this->defaultkey
= $pvo->name
;
269 /** Returns a list of PFO objects in accordance with the user's choice
271 public function order()
273 $order = Env
::v('order', $this->defaultkey
);
274 $invert = ($order{0} == '-');
276 $order = substr($order, 1);
279 $ordering = $this->sortkeys
[$order];
281 foreach ($ordering->pfos
as $pfo) {
285 return $ordering->pfos
;
288 /** Returns information on the order of bounds
289 * @return * 1 if normal bounds
290 * * -1 if inversed bounds
291 * * 0 if bounds shouldn't be displayed
293 public function bounds()
298 public function limit()
300 return new PlLimit($this->entriesPerPage
, $this->offset
);
303 /** Name of the template to use for displaying items of the view
305 abstract public function templateName();
307 /** Returns the value of a boundary of the current view (in order
308 * to show "from C to F")
309 * @param $obj The boundary result whose value must be shown to the user
312 abstract protected function getBoundValue($obj);
314 /** Applies the view to a page
315 * @param $page Page to which the view will be applied
317 public function apply(PlPage
&$page)
319 foreach ($this->order() as $order) {
320 if (!is_null($order)) {
321 $this->set
->addSort($order);
324 $res = $this->set
->get($this->limit());
326 $show_bounds = $this->bounds();
328 $start = current($res);
330 if ($show_bounds == 1) {
331 $first = $this->getBoundValue($start);
332 $last = $this->getBoundValue($end);
333 } elseif ($show_bounds == -1) {
334 $first = $this->getBoundValue($end);
335 $last = $this->getBoundValue($start);
337 $page->assign('first', $first);
338 $page->assign('last', $last);
341 $page->assign('show_bounds', $show_bounds);
342 $page->assign('order', Env
::v('order', $this->defaultkey
));
343 $page->assign('orders', $this->sortkeys
);
344 $page->assign_by_ref('plview', $this);
345 if (is_array($res)) {
346 $page->assign('set_keys', array_keys($res));
348 $page->assign_by_ref('set', $res);
349 $count = $this->set
->count();
350 $this->pages
= intval(ceil($count / $this->entriesPerPage
));
351 return PlPage
::getCoreTpl('plview.multipage.tpl');
354 public function args()
356 $list = $this->set
->args();
357 unset($list['page']);
358 unset($list['order']);
363 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: