Fix AX-Client for synchro
[platal.git] / classes / plset.php
CommitLineData
8c4a0c30 1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2007 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 */
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
89 public function &get($fields, $joins, $where, $groupby, $order, $limitcount = null, $limitfrom = null)
90 {
91 if (!is_null($limitcount)) {
92 if (!is_null($limitfrom)) {
93 $limitcount = "$limitfrom,$limitcount";
94 }
95 $limitcount = "LIMIT $limitcount";
96 }
97 $joins = $this->joins . ' ' . $joins;
98 $where = $where;
99 if (trim($this->where)) {
100 if (trim($where)) {
101 $where .= ' AND ';
102 }
103 $where .= $this->where;
104 }
105 if (!$groupby) {
106 $groupby = $this->groupby;
107 }
108 return $this->query($fields, $this->from, $joins, $where, $groupby, $order, $limitcount);
109 }
110
111 public function count()
112 {
113 return $this->count;
114 }
115
116 private function &buildView($view, $data)
117 {
118 $view = strtolower($view);
119 if (!$view || !class_exists($view . 'View') || !isset($this->mods[$view])) {
120 $view = $this->default ? $this->default : $this->mods[0];
121 }
122 $this->mod = $view;
123 $class = $view . 'View';
124 if (!class_exists($class)) {
125 $view = null;
126 } else {
127 $view = new $class($this, $data, $this->modParams[$this->mod]);
128 if (!$view instanceof PlView) {
129 $view = null;
130 }
131 }
132 return $view;
133 }
134
135 public function apply($baseurl, PlatalPage &$page, $view = null, $data = null)
136 {
137 $view =& $this->buildView($view, $data);
138 if (is_null($view)) {
139 return false;
140 }
90ccb062 141 if (empty($GLOBALS['IS_XNET_SITE'])) {
142 $page->changeTpl('core/plset.tpl');
143 } else {
144 new_group_open_page('core/plset.tpl');
145 }
8c4a0c30 146 $page->assign('plset_base', $baseurl);
147 $page->assign('plset_mods', $this->mods);
148 $page->assign('plset_mod', $this->mod);
149 foreach ($this->modParams[$this->mod] as $param=>$value) {
150 $page->assign($this->mod . '_' . $param, $value);
151 }
152 $page->assign('plset_content', $view->apply($page));
153 $page->assign('plset_count', $this->count);
154 return true;
155 }
156}
157
158interface PlView
159{
160 public function __construct(PlSet &$set, $data, array $params);
161 public function apply(PlatalPage &$page);
162}
163
164abstract class MultipageView implements PlView
165{
166 protected $set;
167
168 public $pages = 1;
169 public $page = 1;
170 public $offset = 0;
171
172 protected $order = array();
173 protected $entriesPerPage = 20;
174 protected $params = array();
175
176 public function __construct(PlSet &$set, $data, array $params)
177 {
178 $this->set =& $set;
179 $this->page = Env::i('page', 1);
180 $this->offset = $this->entriesPerPage * ($this->page - 1);
181 $this->params = $params;
182 }
183
184 public function joins()
185 {
186 return null;
187 }
188
189 public function where()
190 {
191 return null;
192 }
193
194 public function groupBy()
195 {
196 return null;
197 }
198
199 public function order()
200 {
201 foreach ($this->order as &$item) {
202 if ($item{0} == '-') {
203 $item = substr($item, 1) . ' DESC';
204 }
205 }
206 return implode(', ', $this->order);
207 }
208
209 abstract public function templateName();
210
211 public function apply(PlatalPage &$page)
212 {
213 $page->assign_by_ref('plview', $this);
214 $page->assign_by_ref('set',
215 $this->set->get($this->fields(),
216 $this->joins(),
217 $this->where(),
218 $this->groupBy(),
219 $this->order(),
220 $this->entriesPerPage,
221 $this->offset));
222 $count = $this->set->count();
223 $this->pages = intval(ceil($count / $this->entriesPerPage));
224
225 return 'include/plview.multipage.tpl';
226 }
227}
228
229// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
230?>