| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2010 Polytechnique.org * |
| 4 | * http://opensource.polytechnique.org/ * |
| 5 | * * |
| 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. * |
| 10 | * * |
| 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. * |
| 15 | * * |
| 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 * |
| 18 | * Foundation, Inc., * |
| 19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | |
| 23 | /** UserSet is a light-weight Model/View tool for displaying a set of items |
| 24 | */ |
| 25 | abstract class PlSet |
| 26 | { |
| 27 | const DEFAULT_MAX_RES = 20; |
| 28 | |
| 29 | protected $conds = null; |
| 30 | protected $orders = array(); |
| 31 | protected $limit = null; |
| 32 | |
| 33 | protected $count = null; |
| 34 | |
| 35 | private $mods = array(); |
| 36 | private $modParams = array(); |
| 37 | private $mod = null; |
| 38 | private $default = null; |
| 39 | |
| 40 | public function __construct(PlFilterCondition &$cond, $orders = null) |
| 41 | { |
| 42 | if ($cond instanceof PFC_And) { |
| 43 | $this->conds = $cond; |
| 44 | } else { |
| 45 | $this->conds = new PFC_And($cond); |
| 46 | } |
| 47 | |
| 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); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | public function addMod($name, $description, $default = false, array $params = array()) |
| 58 | { |
| 59 | $name = strtolower($name); |
| 60 | $this->mods[$name] = $description; |
| 61 | $this->modParams[$name] = $params; |
| 62 | if ($default) { |
| 63 | $this->default = $name; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public function rmMod($name) |
| 68 | { |
| 69 | $name = strtolower($name); |
| 70 | unset($this->mods[$name]); |
| 71 | } |
| 72 | |
| 73 | public function addSort(PlFilterOrder &$order) |
| 74 | { |
| 75 | $this->orders[] = $order; |
| 76 | } |
| 77 | |
| 78 | public function addCond(PlFilterCondition &$cond) |
| 79 | { |
| 80 | $this->conds->addChild($cond); |
| 81 | } |
| 82 | |
| 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 |
| 86 | */ |
| 87 | abstract protected function buildFilter(PlFilterCondition &$cond, $orders); |
| 88 | |
| 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(); |
| 93 | */ |
| 94 | public function &get(PlLimit $limit = null, $orders = array()) |
| 95 | { |
| 96 | if (!is_array($orders)) { |
| 97 | $orders = array($orders); |
| 98 | } |
| 99 | |
| 100 | $orders = array_merge($orders, $this->orders); |
| 101 | |
| 102 | $pf = $this->buildFilter($this->conds, $orders); |
| 103 | |
| 104 | if (is_null($limit)) { |
| 105 | $limit = new PlLimit(self::DEFAULT_MAX_RES, 0); |
| 106 | } |
| 107 | $it = $pf->get($limit); |
| 108 | $this->count = $pf->getTotalCount(); |
| 109 | return $it; |
| 110 | } |
| 111 | |
| 112 | public function args() |
| 113 | { |
| 114 | $get = $_GET; |
| 115 | unset($get['n']); |
| 116 | return $get; |
| 117 | } |
| 118 | |
| 119 | protected function encodeArgs(array $args, $encode = false) |
| 120 | { |
| 121 | $qs = '?'; |
| 122 | $sep = '&'; |
| 123 | foreach ($args as $k=>$v) { |
| 124 | if (!$encode) { |
| 125 | $k = urlencode($k); |
| 126 | $v = urlencode($v); |
| 127 | } |
| 128 | $qs .= "$k=$v$sep"; |
| 129 | } |
| 130 | return $encode ? urlencode($qs) : $qs; |
| 131 | } |
| 132 | |
| 133 | public function count() |
| 134 | { |
| 135 | return $this->count; |
| 136 | } |
| 137 | |
| 138 | private function &buildView($view, $data) |
| 139 | { |
| 140 | $view = strtolower($view); |
| 141 | if (!$view || !class_exists($view . 'View') || !isset($this->mods[$view])) { |
| 142 | reset($this->mods); |
| 143 | $view = $this->default ? $this->default : key($this->mods); |
| 144 | } |
| 145 | $this->mod = $view; |
| 146 | $class = $view . 'View'; |
| 147 | if (!class_exists($class)) { |
| 148 | $view = null; |
| 149 | } else { |
| 150 | $view = new $class($this, $data, $this->modParams[$this->mod]); |
| 151 | if (!$view instanceof PlView) { |
| 152 | $view = null; |
| 153 | } |
| 154 | } |
| 155 | return $view; |
| 156 | } |
| 157 | |
| 158 | public function apply($baseurl, PlPage &$page, $view = null, $data = null) |
| 159 | { |
| 160 | $view =& $this->buildView($view, $data); |
| 161 | if (is_null($view)) { |
| 162 | return false; |
| 163 | } |
| 164 | $args = $view->args(); |
| 165 | if (!isset($args['rechercher'])) { |
| 166 | $args['rechercher'] = 'Chercher'; |
| 167 | } |
| 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); |
| 176 | } |
| 177 | $page->assign('plset_content', $view->apply($page)); |
| 178 | $page->assign('plset_count', $this->count); |
| 179 | return true; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | interface PlView |
| 184 | { |
| 185 | public function __construct(PlSet &$set, $data, array $params); |
| 186 | public function apply(PlPage &$page); |
| 187 | public function args(); |
| 188 | } |
| 189 | |
| 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 |
| 194 | */ |
| 195 | class PlViewOrder |
| 196 | { |
| 197 | public $pfos = null; |
| 198 | public $name = null; |
| 199 | public $displaytext = null; |
| 200 | |
| 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 |
| 205 | */ |
| 206 | public function __construct($name, $pfos, $displaytext = null) |
| 207 | { |
| 208 | $this->name = $name; |
| 209 | if (is_null($displaytext)) { |
| 210 | $this->displaytext = ucfirst($name); |
| 211 | } else { |
| 212 | $this->displaytext = $displaytext; |
| 213 | } |
| 214 | $this->pfos = $pfos; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | abstract class MultipageView implements PlView |
| 219 | { |
| 220 | protected $set; |
| 221 | |
| 222 | public $pages = 1; |
| 223 | public $page = 1; |
| 224 | public $offset = 0; |
| 225 | |
| 226 | protected $entriesPerPage = 20; |
| 227 | protected $params = array(); |
| 228 | |
| 229 | protected $sortkeys = array(); |
| 230 | protected $defaultkey = null; |
| 231 | |
| 232 | protected $bound_field = null; |
| 233 | |
| 234 | /** Builds a MultipageView |
| 235 | * @param $set The associated PlSet |
| 236 | * @param $data Data for the PlSet |
| 237 | * @param $params Parameters of the view |
| 238 | */ |
| 239 | public function __construct(PlSet &$set, $data, array $params) |
| 240 | { |
| 241 | $this->set =& $set; |
| 242 | $this->page = Env::i('page', 1); |
| 243 | $this->offset = $this->entriesPerPage * ($this->page - 1); |
| 244 | $this->params = $params; |
| 245 | } |
| 246 | |
| 247 | /** Add an order to the view |
| 248 | */ |
| 249 | protected function addSort(PlViewOrder &$pvo, $default = false) |
| 250 | { |
| 251 | $this->sortkeys[$pvo->name] = $pvo; |
| 252 | if (!$this->defaultkey || $default) { |
| 253 | $this->defaultkey = $pvo->name; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** Returns a list of PFO objects in accordance with the user's choice |
| 258 | */ |
| 259 | public function order() |
| 260 | { |
| 261 | $order = Env::v('order', $this->defaultkey); |
| 262 | $invert = ($order{0} == '-'); |
| 263 | if ($invert) { |
| 264 | $order = substr($order, 1); |
| 265 | } |
| 266 | |
| 267 | $ordering = $this->sortkeys[$order]; |
| 268 | if ($invert) { |
| 269 | foreach ($ordering->pfos as $pfo) { |
| 270 | $pfo->toggleDesc(); |
| 271 | } |
| 272 | } |
| 273 | return $ordering->pfos; |
| 274 | } |
| 275 | |
| 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 |
| 280 | */ |
| 281 | public function bounds() |
| 282 | { |
| 283 | return null; |
| 284 | } |
| 285 | |
| 286 | public function limit() |
| 287 | { |
| 288 | return new PlLimit($this->entriesPerPage, $this->offset); |
| 289 | } |
| 290 | |
| 291 | /** Name of the template to use for displaying items of the view |
| 292 | */ |
| 293 | abstract public function templateName(); |
| 294 | |
| 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 |
| 298 | * @return The bound |
| 299 | */ |
| 300 | abstract protected function getBoundValue($obj); |
| 301 | |
| 302 | /** Applies the view to a page |
| 303 | * @param $page Page to which the view will be applied |
| 304 | */ |
| 305 | public function apply(PlPage &$page) |
| 306 | { |
| 307 | foreach ($this->order() as $order) { |
| 308 | if (!is_null($order)) { |
| 309 | $this->set->addSort($order); |
| 310 | } |
| 311 | } |
| 312 | $res = $this->set->get($this->limit()); |
| 313 | |
| 314 | $show_bounds = $this->bounds(); |
| 315 | if ($show_bounds) { |
| 316 | $start = current($res); |
| 317 | $end = end($res); |
| 318 | if ($show_bounds == 1) { |
| 319 | $first = $this->getBoundValue($start); |
| 320 | $last = $this->getBoundValue($end); |
| 321 | } elseif ($show_bounds == -1) { |
| 322 | $first = $this->getBoundValue($end); |
| 323 | $last = $this->getBoundValue($start); |
| 324 | } |
| 325 | $page->assign('first', $first); |
| 326 | $page->assign('last', $last); |
| 327 | } |
| 328 | |
| 329 | $page->assign('show_bounds', $show_bounds); |
| 330 | $page->assign('order', Env::v('order', $this->defaultkey)); |
| 331 | $page->assign('orders', $this->sortkeys); |
| 332 | $page->assign_by_ref('plview', $this); |
| 333 | $page->assign_by_ref('set', $res); |
| 334 | $count = $this->set->count(); |
| 335 | $this->pages = intval(ceil($count / $this->entriesPerPage)); |
| 336 | return PlPage::getCoreTpl('plview.multipage.tpl'); |
| 337 | } |
| 338 | |
| 339 | public function args() |
| 340 | { |
| 341 | $list = $this->set->args(); |
| 342 | unset($list['page']); |
| 343 | unset($list['order']); |
| 344 | return $list; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 349 | ?> |