| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2008 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 | /** UserSet is a light-weight Model/View tool for displaying a set of items |
| 23 | */ |
| 24 | class PlSet |
| 25 | { |
| 26 | private $from = null; |
| 27 | private $groupby = null; |
| 28 | private $joins = null; |
| 29 | private $where = null; |
| 30 | |
| 31 | private $count = null; |
| 32 | |
| 33 | private $mods = array(); |
| 34 | private $modParams = array(); |
| 35 | private $mod = null; |
| 36 | private $default = null; |
| 37 | |
| 38 | public function __construct($from, $joins = '', $where = '', $groupby = '') |
| 39 | { |
| 40 | $this->from = $from; |
| 41 | $this->joins = $joins; |
| 42 | $this->where = $where; |
| 43 | $this->groupby = $groupby; |
| 44 | } |
| 45 | |
| 46 | public function addMod($name, $description, $default = false, array $params = array()) |
| 47 | { |
| 48 | $name = strtolower($name); |
| 49 | $this->mods[$name] = $description; |
| 50 | $this->modParams[$name] = $params; |
| 51 | if ($default) { |
| 52 | $this->default = $name; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | public function rmMod($name) |
| 57 | { |
| 58 | $name = strtolower($name); |
| 59 | unset($this->mods[$name]); |
| 60 | } |
| 61 | |
| 62 | private function &query($fields, $from, $joins, $where, $groupby, $order, $limit) |
| 63 | { |
| 64 | if (trim($order)) { |
| 65 | $order = "ORDER BY $order"; |
| 66 | } |
| 67 | if (trim($where)) { |
| 68 | $where = "WHERE $where"; |
| 69 | } |
| 70 | if (trim($groupby)) { |
| 71 | $groupby = "GROUP BY $groupby"; |
| 72 | } |
| 73 | $query = "SELECT SQL_CALC_FOUND_ROWS |
| 74 | $fields |
| 75 | FROM $from |
| 76 | $joins |
| 77 | $where |
| 78 | $groupby |
| 79 | $order |
| 80 | $limit"; |
| 81 | // echo $query; |
| 82 | // print_r($this); |
| 83 | $it = XDB::query($query); |
| 84 | $it = $it->fetchAllAssoc(); |
| 85 | $count = XDB::query('SELECT FOUND_ROWS()'); |
| 86 | $this->count = intval($count->fetchOneCell()); |
| 87 | return $it; |
| 88 | } |
| 89 | |
| 90 | public function args() |
| 91 | { |
| 92 | $get = $_GET; |
| 93 | unset($get['n']); |
| 94 | return $get; |
| 95 | } |
| 96 | |
| 97 | protected function encodeArgs(array $args, $encode = false) |
| 98 | { |
| 99 | $qs = '?'; |
| 100 | $sep = '&'; |
| 101 | foreach ($args as $k=>$v) { |
| 102 | if (!$encode) { |
| 103 | $k = urlencode($k); |
| 104 | $v = urlencode($v); |
| 105 | } |
| 106 | $qs .= "$k=$v$sep"; |
| 107 | } |
| 108 | return $encode ? urlencode($qs) : $qs; |
| 109 | } |
| 110 | |
| 111 | public function &get($fields, $joins, $where, $groupby, $order, $limitcount = null, $limitfrom = null) |
| 112 | { |
| 113 | if (!is_null($limitcount)) { |
| 114 | if (!is_null($limitfrom)) { |
| 115 | $limitcount = "$limitfrom,$limitcount"; |
| 116 | } |
| 117 | $limitcount = "LIMIT $limitcount"; |
| 118 | } |
| 119 | $joins = $this->joins . ' ' . $joins; |
| 120 | if (trim($this->where)) { |
| 121 | if (trim($where)) { |
| 122 | $where .= ' AND '; |
| 123 | } |
| 124 | $where .= $this->where; |
| 125 | } |
| 126 | if (!$groupby) { |
| 127 | $groupby = $this->groupby; |
| 128 | } |
| 129 | return $this->query($fields, $this->from, $joins, $where, $groupby, $order, $limitcount); |
| 130 | } |
| 131 | |
| 132 | public function count() |
| 133 | { |
| 134 | return $this->count; |
| 135 | } |
| 136 | |
| 137 | private function &buildView($view, $data) |
| 138 | { |
| 139 | $view = strtolower($view); |
| 140 | if (!$view || !class_exists($view . 'View') || !isset($this->mods[$view])) { |
| 141 | reset($this->mods); |
| 142 | $view = $this->default ? $this->default : key($this->mods); |
| 143 | } |
| 144 | $this->mod = $view; |
| 145 | $class = $view . 'View'; |
| 146 | if (!class_exists($class)) { |
| 147 | $view = null; |
| 148 | } else { |
| 149 | $view = new $class($this, $data, $this->modParams[$this->mod]); |
| 150 | if (!$view instanceof PlView) { |
| 151 | $view = null; |
| 152 | } |
| 153 | } |
| 154 | return $view; |
| 155 | } |
| 156 | |
| 157 | public function apply($baseurl, PlPage &$page, $view = null, $data = null) |
| 158 | { |
| 159 | $view =& $this->buildView($view, $data); |
| 160 | if (is_null($view)) { |
| 161 | return false; |
| 162 | } |
| 163 | $args = $view->args(); |
| 164 | if (!isset($args['rechercher'])) { |
| 165 | $args['rechercher'] = 'Chercher'; |
| 166 | } |
| 167 | $page->changeTpl('core/plset.tpl'); |
| 168 | $page->assign('plset_base', $baseurl); |
| 169 | $page->assign('plset_mods', $this->mods); |
| 170 | $page->assign('plset_mod', $this->mod); |
| 171 | $page->assign('plset_search', $this->encodeArgs($args)); |
| 172 | $page->assign('plset_search_enc', $this->encodeArgs($args, true)); |
| 173 | foreach ($this->modParams[$this->mod] as $param=>$value) { |
| 174 | $page->assign($this->mod . '_' . $param, $value); |
| 175 | } |
| 176 | $page->assign('plset_content', $view->apply($page)); |
| 177 | $page->assign('plset_count', $this->count); |
| 178 | return true; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | interface PlView |
| 183 | { |
| 184 | public function __construct(PlSet &$set, $data, array $params); |
| 185 | public function apply(PlPage &$page); |
| 186 | public function args(); |
| 187 | } |
| 188 | |
| 189 | abstract class MultipageView implements PlView |
| 190 | { |
| 191 | protected $set; |
| 192 | |
| 193 | public $pages = 1; |
| 194 | public $page = 1; |
| 195 | public $offset = 0; |
| 196 | |
| 197 | protected $entriesPerPage = 20; |
| 198 | protected $params = array(); |
| 199 | |
| 200 | protected $sortkeys = array(); |
| 201 | protected $defaultkey = null; |
| 202 | |
| 203 | protected $bound_field = null; |
| 204 | |
| 205 | public function __construct(PlSet &$set, $data, array $params) |
| 206 | { |
| 207 | $this->set =& $set; |
| 208 | $this->page = Env::i('page', 1); |
| 209 | $this->offset = $this->entriesPerPage * ($this->page - 1); |
| 210 | $this->params = $params; |
| 211 | } |
| 212 | |
| 213 | public function joins() |
| 214 | { |
| 215 | return null; |
| 216 | } |
| 217 | |
| 218 | public function where() |
| 219 | { |
| 220 | return null; |
| 221 | } |
| 222 | |
| 223 | public function groupBy() |
| 224 | { |
| 225 | return null; |
| 226 | } |
| 227 | |
| 228 | public function bounds() |
| 229 | { |
| 230 | return null; |
| 231 | } |
| 232 | |
| 233 | protected function addSortKey($name, array $keys, $desc, $default = false) |
| 234 | { |
| 235 | $this->sortkeys[$name] = array('keys' => $keys, 'desc' => $desc); |
| 236 | if (!$this->defaultkey || $default) { |
| 237 | $this->defaultkey = $name; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | public function order() |
| 242 | { |
| 243 | $order = Env::v('order', $this->defaultkey); |
| 244 | $invert = ($order{0} == '-'); |
| 245 | if ($invert) { |
| 246 | $order = substr($order, 1); |
| 247 | } |
| 248 | $list = array(); |
| 249 | foreach ($this->sortkeys[$order]['keys'] as $item) { |
| 250 | $desc = ($item{0} == '-'); |
| 251 | if ($desc) { |
| 252 | $item = substr($item, 1); |
| 253 | } |
| 254 | if ($desc xor $invert) { |
| 255 | $item .= ' DESC'; |
| 256 | } |
| 257 | $list[] = $item; |
| 258 | } |
| 259 | return implode(', ', $list); |
| 260 | } |
| 261 | |
| 262 | abstract public function templateName(); |
| 263 | |
| 264 | public function apply(PlPage &$page) |
| 265 | { |
| 266 | $res = $this->set->get($this->fields(), |
| 267 | $this->joins(), |
| 268 | $this->where(), |
| 269 | $this->groupBy(), |
| 270 | $this->order(), |
| 271 | $this->entriesPerPage, |
| 272 | $this->offset); |
| 273 | $show_bounds = $this->bounds(); |
| 274 | $end = end($res); |
| 275 | if ($show_bounds) { |
| 276 | if ($show_bounds == 1) { |
| 277 | $first = $res[0][$this->bound_field]; |
| 278 | $last = $end[$this->bound_field]; |
| 279 | } elseif ($show_bounds == -1) { |
| 280 | $first = $end[$this->bound_field]; |
| 281 | $last = $res[0][$this->bound_field]; |
| 282 | } |
| 283 | $page->assign('first', $first); |
| 284 | $page->assign('last', $last); |
| 285 | } |
| 286 | |
| 287 | $page->assign('show_bounds', $show_bounds); |
| 288 | $page->assign('order', Env::v('order', $this->defaultkey)); |
| 289 | $page->assign('orders', $this->sortkeys); |
| 290 | $page->assign_by_ref('plview', $this); |
| 291 | $page->assign_by_ref('set', $res); |
| 292 | $count = $this->set->count(); |
| 293 | $this->pages = intval(ceil($count / $this->entriesPerPage)); |
| 294 | return 'include/plview.multipage.tpl'; |
| 295 | } |
| 296 | |
| 297 | public function args() |
| 298 | { |
| 299 | $list = $this->set->args(); |
| 300 | unset($list['page']); |
| 301 | unset($list['order']); |
| 302 | return $list; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 307 | ?> |