1fd155eadfb54288ea406f19b4150b99e0ab9665
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 /** This function builds the right kind of PlFilter from given data
76 * @param $cond The PlFilterCondition for the filter
77 * @param $orders An array of PlFilterOrder for the filter
79 abstract private static function buildFilter(PlFilterCondition
$cond, $orders);
81 public function &get(PlFilterLimit
$limit = null
)
83 $pf = self
::buildFilter($this->conds
, $this->orders
);
85 $it = $pf->get($limit);
86 $this->count
= $pf->getTotalCount();
90 public function args()
97 protected function encodeArgs(array $args, $encode = false
)
101 foreach ($args as $k=>$v) {
108 return $encode ?
urlencode($qs) : $qs;
111 public function count()
116 private function &buildView($view, $data)
118 $view = strtolower($view);
119 if (!$view ||
!class_exists($view . 'View') ||
!isset($this->mods
[$view])) {
121 $view = $this->default ?
$this->default : key($this->mods
);
124 $class = $view . 'View';
125 if (!class_exists($class)) {
128 $view = new $class($this, $data, $this->modParams
[$this->mod
]);
129 if (!$view instanceof PlView
) {
136 public function apply($baseurl, PlPage
&$page, $view = null
, $data = null
)
138 $view =& $this->buildView($view, $data);
139 if (is_null($view)) {
142 $args = $view->args();
143 if (!isset($args['rechercher'])) {
144 $args['rechercher'] = 'Chercher';
146 $page->coreTpl('plset.tpl');
147 $page->assign('plset_base', $baseurl);
148 $page->assign('plset_mods', $this->mods
);
149 $page->assign('plset_mod', $this->mod
);
150 $page->assign('plset_search', $this->encodeArgs($args));
151 $page->assign('plset_search_enc', $this->encodeArgs($args, true
));
152 foreach ($this->modParams
[$this->mod
] as $param=>$value) {
153 $page->assign($this->mod
. '_' . $param, $value);
155 $page->assign('plset_content', $view->apply($page));
156 $page->assign('plset_count', $this->count
);
163 public function __construct(PlSet
&$set, $data, array $params);
164 public function apply(PlPage
&$page);
165 public function args();
168 /** This class describes an Order as used in a PlView :
169 * - It is based on a PlFilterOrder
170 * - It has a short identifier
171 * - It has a full name, to display on the page
177 public $displaytext = null
;
179 /** Build a PlViewOrder
180 * @param $name Name of the order (key)
181 * @param $displaytext Text to display
182 * @param $pfo PlFilterOrder for the order
184 public function __construct($name, PlFilterOrder
&$pfo, $displaytext = null
)
187 if (is_null($displaytext)) {
188 $this->displaytext
= ucfirst($name);
190 $this->displaytext
= $displaytext;
196 abstract class MultipageView
implements PlView
204 protected $entriesPerPage = 20;
205 protected $params = array();
207 protected $sortkeys = array();
208 protected $defaultkey = null
;
210 protected $bound_field = null
;
212 /** Builds a MultipageView
213 * @param $set The associated PlSet
214 * @param $data Data for the PlSet
215 * @param $params Parameters of the view
217 public function __construct(PlSet
&$set, $data, array $params)
220 $this->page
= Env
::i('page', 1);
221 $this->offset
= $this->entriesPerPage
* ($this->page
- 1);
222 $this->params
= $params;
225 /** Add an order to the view
227 protected function addSort(PlViewOrder
&$pvo, $default = false
)
229 $this->sortkeys
[$pvo->name
] = $pvo;
230 if (!$this->defaultkey ||
$default) {
231 $this->defaultkey
= $pvo->name
;
235 /** Returns a list of PFO objects, the "default" one first
237 public function order()
239 $order = Env
::v('order', $this->defaultkey
);
240 $invert = ($order{0} == '-');
242 $order = substr($order, 1);
244 $list = array(0 => null
);
245 foreach ($this->sortkeys
as $name => $sort) {
246 $desc = $sort->pfo
->isDescending();;
248 $sort->pfo
->toggleDesc();
250 if ($name == $order) {
251 $list[0] = $sort->pfo
;
253 $list[] = $sort->pfo
;
259 /** Returns information on the order of bounds
260 * @return * 1 if normal bounds
261 * * -1 if inversed bounds
262 * * 0 if bounds shouldn't be displayed
264 public function bounds()
269 /** Name of the template to use for displaying items of the view
271 abstract public function templateName();
273 /** Returns the value of a boundary of the current view (in order
274 * to show "from C to F")
275 * @param $obj The boundary result whose value must be shown to the user
277 abstract private function getBoundValue($obj);
279 /** Applies the view to a page
280 * @param $page Page to which the view will be applied
282 public function apply(PlPage
&$page)
284 foreach ($this->order() as $order) {
285 $this->set
->addSort($order->pfo
);
287 $res = $this->set
->get($this->limit());
289 $show_bounds = $this->bounds();
292 if ($show_bounds == 1) {
293 $first = $this->getBoundValue($res[0]);
294 $last = $this->getBoundValue($end);
295 } elseif ($show_bounds == -1) {
296 $first = $this->getBoundValue($end);
297 $last = $this->getBoundValue($res[0]);
299 $page->assign('first', $first);
300 $page->assign('last', $last);
303 $page->assign('show_bounds', $show_bounds);
304 $page->assign('order', Env
::v('order', $this->defaultkey
));
305 $page->assign('orders', $this->sortkeys
);
306 $page->assign_by_ref('plview', $this);
307 $page->assign_by_ref('set', $res);
308 $count = $this->set
->count();
309 $this->pages
= intval(ceil($count / $this->entriesPerPage
));
310 return PlPage
::getCoreTpl('plview.multipage.tpl');
313 public function args()
315 $list = $this->set
->args();
316 unset($list['page']);
317 unset($list['order']);
322 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: