Base layout of the core.
[platal.git] / classes / plset.php
CommitLineData
8c4a0c30 1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 Polytechnique.org *
8c4a0c30 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 */
24class 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::iterator($query);
84 $count = XDB::query('SELECT FOUND_ROWS()');
85 $this->count = intval($count->fetchOneCell());
86 return $it;
87 }
88
35fa92e8 89 public function args()
90 {
91 $get = $_GET;
92 unset($get['n']);
93 return $get;
94 }
95
96 protected function encodeArgs(array $args, $encode = false)
97 {
98 $qs = '?';
a2aa8436 99 $sep = '&';
35fa92e8 100 foreach ($args as $k=>$v) {
101 if (!$encode) {
102 $k = urlencode($k);
103 $v = urlencode($v);
104 }
105 $qs .= "$k=$v$sep";
106 }
107 return $encode ? urlencode($qs) : $qs;
108 }
109
8c4a0c30 110 public function &get($fields, $joins, $where, $groupby, $order, $limitcount = null, $limitfrom = null)
111 {
112 if (!is_null($limitcount)) {
113 if (!is_null($limitfrom)) {
114 $limitcount = "$limitfrom,$limitcount";
115 }
116 $limitcount = "LIMIT $limitcount";
117 }
118 $joins = $this->joins . ' ' . $joins;
8c4a0c30 119 if (trim($this->where)) {
120 if (trim($where)) {
121 $where .= ' AND ';
122 }
123 $where .= $this->where;
124 }
125 if (!$groupby) {
126 $groupby = $this->groupby;
127 }
128 return $this->query($fields, $this->from, $joins, $where, $groupby, $order, $limitcount);
129 }
130
131 public function count()
132 {
133 return $this->count;
134 }
135
136 private function &buildView($view, $data)
137 {
138 $view = strtolower($view);
139 if (!$view || !class_exists($view . 'View') || !isset($this->mods[$view])) {
ff3eb9b7 140 reset($this->mods);
141 $view = $this->default ? $this->default : key($this->mods);
8c4a0c30 142 }
143 $this->mod = $view;
144 $class = $view . 'View';
145 if (!class_exists($class)) {
146 $view = null;
147 } else {
148 $view = new $class($this, $data, $this->modParams[$this->mod]);
149 if (!$view instanceof PlView) {
150 $view = null;
151 }
152 }
153 return $view;
154 }
155
04334c61 156 public function apply($baseurl, PlPage &$page, $view = null, $data = null)
8c4a0c30 157 {
158 $view =& $this->buildView($view, $data);
159 if (is_null($view)) {
160 return false;
161 }
35fa92e8 162 $args = $view->args();
a2aa8436 163 if (!isset($args['rechercher'])) {
164 $args['rechercher'] = 'Chercher';
165 }
1490093c 166 $page->changeTpl('core/plset.tpl');
8c4a0c30 167 $page->assign('plset_base', $baseurl);
168 $page->assign('plset_mods', $this->mods);
169 $page->assign('plset_mod', $this->mod);
35fa92e8 170 $page->assign('plset_search', $this->encodeArgs($args));
171 $page->assign('plset_search_enc', $this->encodeArgs($args, true));
8c4a0c30 172 foreach ($this->modParams[$this->mod] as $param=>$value) {
173 $page->assign($this->mod . '_' . $param, $value);
174 }
175 $page->assign('plset_content', $view->apply($page));
176 $page->assign('plset_count', $this->count);
177 return true;
178 }
179}
180
181interface PlView
182{
183 public function __construct(PlSet &$set, $data, array $params);
04334c61 184 public function apply(PlPage &$page);
35fa92e8 185 public function args();
8c4a0c30 186}
187
188abstract class MultipageView implements PlView
189{
190 protected $set;
191
192 public $pages = 1;
193 public $page = 1;
194 public $offset = 0;
195
8c4a0c30 196 protected $entriesPerPage = 20;
197 protected $params = array();
198
35fa92e8 199 protected $sortkeys = array();
200 protected $defaultkey = null;
201
8c4a0c30 202 public function __construct(PlSet &$set, $data, array $params)
203 {
204 $this->set =& $set;
205 $this->page = Env::i('page', 1);
206 $this->offset = $this->entriesPerPage * ($this->page - 1);
207 $this->params = $params;
208 }
209
210 public function joins()
211 {
212 return null;
213 }
214
215 public function where()
216 {
217 return null;
218 }
219
220 public function groupBy()
221 {
222 return null;
223 }
224
35fa92e8 225 protected function addSortKey($name, array $keys, $desc, $default = false)
226 {
227 $this->sortkeys[$name] = array('keys' => $keys, 'desc' => $desc);
228 if (!$this->defaultkey || $default) {
229 $this->defaultkey = $name;
230 }
231 }
232
8c4a0c30 233 public function order()
234 {
35fa92e8 235 $order = Env::v('order', $this->defaultkey);
236 $invert = ($order{0} == '-');
237 if ($invert) {
238 $order = substr($order, 1);
239 }
240 $list = array();
241 foreach ($this->sortkeys[$order]['keys'] as $item) {
242 $desc = ($item{0} == '-');
243 if ($desc) {
244 $item = substr($item, 1);
8c4a0c30 245 }
35fa92e8 246 if ($desc xor $invert) {
247 $item .= ' DESC';
248 }
249 $list[] = $item;
8c4a0c30 250 }
35fa92e8 251 return implode(', ', $list);
8c4a0c30 252 }
253
254 abstract public function templateName();
255
04334c61 256 public function apply(PlPage &$page)
8c4a0c30 257 {
35fa92e8 258 $page->assign('order', Env::v('order', $this->defaultkey));
259 $page->assign('orders', $this->sortkeys);
8c4a0c30 260 $page->assign_by_ref('plview', $this);
261 $page->assign_by_ref('set',
262 $this->set->get($this->fields(),
263 $this->joins(),
264 $this->where(),
265 $this->groupBy(),
266 $this->order(),
267 $this->entriesPerPage,
268 $this->offset));
269 $count = $this->set->count();
270 $this->pages = intval(ceil($count / $this->entriesPerPage));
8c4a0c30 271 return 'include/plview.multipage.tpl';
272 }
35fa92e8 273
274 public function args()
275 {
276 $list = $this->set->args();
277 unset($list['page']);
278 unset($list['order']);
279 return $list;
eaf30d86 280 }
8c4a0c30 281}
282
283// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
284?>