2 /***************************************************************************
3 * Copyright (C) 2003-2007 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
27 private $groupby = null
;
28 private $joins = null
;
29 private $where = null
;
31 private $count = null
;
33 private $mods = array();
34 private $modParams = array();
36 private $default = null
;
38 public function __construct($from, $joins = '', $where = '', $groupby = '')
41 $this->joins
= $joins;
42 $this->where
= $where;
43 $this->groupby
= $groupby;
46 public function addMod($name, $description, $default = false
, array $params = array())
48 $name = strtolower($name);
49 $this->mods
[$name] = $description;
50 $this->modParams
[$name] = $params;
52 $this->default = $name;
56 public function rmMod($name)
58 $name = strtolower($name);
59 unset($this->mods
[$name]);
62 private function &query($fields, $from, $joins, $where, $groupby, $order, $limit)
65 $order = "ORDER BY $order";
68 $where = "WHERE $where";
71 $groupby = "GROUP BY $groupby";
73 $query = "SELECT SQL_CALC_FOUND_ROWS
83 $it = XDB
::iterator($query);
84 $count = XDB
::query('SELECT FOUND_ROWS()');
85 $this->count
= intval($count->fetchOneCell());
89 public function args()
96 protected function encodeArgs(array $args, $encode = false
)
99 $sep = $encode ?
'&' : '&';
100 foreach ($args as $k=>$v) {
107 return $encode ?
urlencode($qs) : $qs;
110 public function &get($fields, $joins, $where, $groupby, $order, $limitcount = null
, $limitfrom = null
)
112 if (!is_null($limitcount)) {
113 if (!is_null($limitfrom)) {
114 $limitcount = "$limitfrom,$limitcount";
116 $limitcount = "LIMIT $limitcount";
118 $joins = $this->joins
. ' ' . $joins;
120 if (trim($this->where
)) {
124 $where .= $this->where
;
127 $groupby = $this->groupby
;
129 return $this->query($fields, $this->from
, $joins, $where, $groupby, $order, $limitcount);
132 public function count()
137 private function &buildView($view, $data)
139 $view = strtolower($view);
140 if (!$view ||
!class_exists($view . 'View') ||
!isset($this->mods
[$view])) {
142 $view = $this->default ?
$this->default : key($this->mods
);
145 $class = $view . 'View';
146 if (!class_exists($class)) {
149 $view = new $class($this, $data, $this->modParams
[$this->mod
]);
150 if (!$view instanceof PlView
) {
157 public function apply($baseurl, PlatalPage
&$page, $view = null
, $data = null
)
159 $view =& $this->buildView($view, $data);
160 if (is_null($view)) {
163 $args = $view->args();
164 $page->changeTpl('core/plset.tpl');
165 $page->assign('plset_base', $baseurl);
166 $page->assign('plset_mods', $this->mods
);
167 $page->assign('plset_mod', $this->mod
);
168 $page->assign('plset_search', $this->encodeArgs($args));
169 $page->assign('plset_search_enc', $this->encodeArgs($args, true
));
170 foreach ($this->modParams
[$this->mod
] as $param=>$value) {
171 $page->assign($this->mod
. '_' . $param, $value);
173 $page->assign('plset_content', $view->apply($page));
174 $page->assign('plset_count', $this->count
);
181 public function __construct(PlSet
&$set, $data, array $params);
182 public function apply(PlatalPage
&$page);
183 public function args();
186 abstract class MultipageView
implements PlView
194 protected $entriesPerPage = 20;
195 protected $params = array();
197 protected $sortkeys = array();
198 protected $defaultkey = null
;
200 public function __construct(PlSet
&$set, $data, array $params)
203 $this->page
= Env
::i('page', 1);
204 $this->offset
= $this->entriesPerPage
* ($this->page
- 1);
205 $this->params
= $params;
208 public function joins()
213 public function where()
218 public function groupBy()
223 protected function addSortKey($name, array $keys, $desc, $default = false
)
225 $this->sortkeys
[$name] = array('keys' => $keys, 'desc' => $desc);
226 if (!$this->defaultkey ||
$default) {
227 $this->defaultkey
= $name;
231 public function order()
233 $order = Env
::v('order', $this->defaultkey
);
234 $invert = ($order{0} == '-');
236 $order = substr($order, 1);
239 foreach ($this->sortkeys
[$order]['keys'] as $item) {
240 $desc = ($item{0} == '-');
242 $item = substr($item, 1);
244 if ($desc xor $invert) {
249 return implode(', ', $list);
252 abstract public function templateName();
254 public function apply(PlatalPage
&$page)
256 $page->assign('order', Env
::v('order', $this->defaultkey
));
257 $page->assign('orders', $this->sortkeys
);
258 $page->assign_by_ref('plview', $this);
259 $page->assign_by_ref('set',
260 $this->set
->get($this->fields(),
265 $this->entriesPerPage
,
267 $count = $this->set
->count();
268 $this->pages
= intval(ceil($count / $this->entriesPerPage
));
269 return 'include/plview.multipage.tpl';
272 public function args()
274 $list = $this->set
->args();
275 unset($list['page']);
276 unset($list['order']);
281 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: