e7ae7df9 |
1 | <?php |
2 | /*************************************************************************** |
5ddeb07c |
3 | * Copyright (C) 2003-2007 Polytechnique.org * |
e7ae7df9 |
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 | class PLTableEditor { |
23 | // the plat/al name of the page |
24 | var $pl; |
25 | // the table name |
26 | var $table; |
27 | // joint tables to delete when deleting an entry |
28 | var $jtables = array(); |
2e7b5921 |
29 | // sorting field |
30 | var $sort = array(); |
e7ae7df9 |
31 | // the id field |
32 | var $idfield; |
33 | // possibility to edit the field |
34 | var $idfield_editable; |
35 | // vars |
36 | var $vars; |
37 | // number of displayed fields |
38 | var $nbfields; |
a3828787 |
39 | // the field for sorting entries |
40 | var $sortfield; |
386b75ea |
41 | var $sortdesc = false; |
de61dbcf |
42 | // action to do to delete row: |
43 | // null => delete effectively, false => no deletion, SQL |
44 | var $delete_action; |
45 | var $delete_message; |
3851b0a6 |
46 | // Should "Save" button return to the list view |
47 | var $auto_return = true; |
48 | |
e7ae7df9 |
49 | /* table editor for platal |
50 | * $plname : the PLname of the page, ex: admin/payments |
51 | * $table : the table to edit, ex: profile_medals |
52 | * $idfield : the field of the table which is the id, ex: id |
53 | * $editid : is the id editable or not (if not, it is considered as an int) |
54 | */ |
1d10d3fd |
55 | function PLTableEditor($plname, $table, $idfield, $editid=false) |
56 | { |
e7ae7df9 |
57 | $this->pl = $plname; |
58 | $this->table = $table; |
59 | $this->idfield = $idfield; |
a3828787 |
60 | $this->sortfield = $idfield; |
e7ae7df9 |
61 | $this->idfield_editable = $editid; |
62 | $r = XDB::iterator("SHOW COLUMNS FROM $table"); |
63 | $this->vars = array(); |
64 | while ($a = $r->next()) { |
65 | // desc will be the title of the column |
66 | $a['desc'] = $a['Field']; |
67 | $a['display'] = true; |
6682f91d |
68 | |
e7ae7df9 |
69 | if (substr($a['Type'],0,8) == 'varchar(') { |
70 | // limit editing box size |
71 | $a['Size'] = $a['Maxlength'] = substr($a['Type'], 8, strlen($a['Type']) - 9); |
72 | if ($a['Size'] > 40) $a['Size'] = 40; |
73 | // if too big, put a textarea |
74 | $a['Type'] = ($a['Maxlength']<200)?'varchar':'varchar200'; |
75 | } |
76 | elseif ($a['Type'] == 'text' || $a['Type'] == 'mediumtext') |
77 | $a['Type'] = 'textarea'; |
78 | elseif (substr($a['Type'],0,4) == 'set(') { |
79 | // get the list of options |
a7de4ef7 |
80 | $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 5, strlen($a['Type']) - 7))); |
987f1bd2 |
81 | if (count($a['List']) == 1) { |
82 | $a['Type'] = 'checkbox'; |
83 | $a['Value'] = $a['List'][0]; |
84 | } else { |
85 | $a['Type'] = 'set'; |
86 | } |
e7ae7df9 |
87 | } |
88 | elseif (substr($a['Type'],0,5) == 'enum(') { |
89 | // get the list of options |
a7de4ef7 |
90 | $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 6, strlen($a['Type']) - 8))); |
e7ae7df9 |
91 | $a['Type'] = 'enum'; |
92 | } |
b9cd8008 |
93 | elseif (substr($a['Type'],0,10) == 'timestamp(' || $a['Type'] == 'datetime') { |
e7ae7df9 |
94 | $a['Type'] = 'timestamp'; |
95 | } |
96 | |
97 | $this->vars[$a['Field']] = $a; |
98 | } |
99 | $this->vars[$idfield]['desc'] = 'id'; |
100 | } |
1d10d3fd |
101 | // called before creating a new entry |
102 | function prepare_new() |
103 | { |
104 | $entry = array(); |
105 | foreach ($this->vars as $field => $descr) { |
106 | $entry[$field] = $descr['Default']; |
107 | } |
108 | return $this->prepare_edit($entry); |
109 | } |
e7ae7df9 |
110 | // called before editing $entry |
111 | function prepare_edit(&$entry) { |
112 | foreach ($this->vars as $field => $descr) { |
113 | if ($descr['Type'] == 'set') { |
114 | // get the list of options selected |
115 | $selected = explode(',', $entry[$field]); |
116 | $entry[$field] = array(); |
117 | foreach ($selected as $option) |
118 | $entry[$field][$option] = 1; |
119 | } |
120 | if ($descr['Type'] == 'timestamp') { |
121 | // set readable timestamp |
122 | $date =& $entry[$field]; |
6682f91d |
123 | $date = preg_replace('/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/', '\3/\2/\1 \4:\5:\6', $date); |
e7ae7df9 |
124 | } |
b9cd8008 |
125 | if ($descr['Type'] == 'date') { |
126 | $date =& $entry[$field]; |
127 | $date = preg_replace('/([0-9]{4})-?([0-9]{2})-?([0-9]{2})/', '\3/\2/\1', $date); |
128 | } |
e7ae7df9 |
129 | } |
130 | return $entry; |
131 | } |
3851b0a6 |
132 | // set whether the save button show redirect to list view or edit view |
133 | function list_on_edit($var) |
134 | { |
135 | $this->auto_return = $var; |
136 | } |
e7ae7df9 |
137 | // change display of a field |
138 | function describe($name, $desc, $display) { |
139 | $this->vars[$name]['desc'] = $desc; |
140 | $this->vars[$name]['display'] = $display; |
141 | } |
6682f91d |
142 | // add a join table, when deleting a row corresponding entries will be deleted in these tables |
e7ae7df9 |
143 | function add_join_table($name,$joinid,$joindel,$joinextra="") { |
144 | if ($joindel) |
145 | $this->jtables[$name] = array("joinid" => $joinid,"joinextra" => $joinextra?(" AND ".$joinextra):""); |
146 | } |
2e7b5921 |
147 | // add a sort key |
de61dbcf |
148 | function add_sort_field($key, $desc = false, $default = false) |
2e7b5921 |
149 | { |
de61dbcf |
150 | if ($default) { |
151 | $this->sortfield = $key . ($desc ? ' DESC' : ''); |
152 | } else { |
153 | $this->sort[] = $key . ($desc ? ' DESC' : ''); |
154 | } |
155 | } |
156 | // set an action when trying to delete row |
157 | function on_delete($action = NULL, $message = NULL) |
158 | { |
159 | $this->delete_action = $action; |
160 | $this->delete_message = $message; |
2e7b5921 |
161 | } |
e7ae7df9 |
162 | // call when done |
163 | function apply(&$page, $action, $id = false) { |
8b1f8e12 |
164 | $page->changeTpl('core/table-editor.tpl'); |
e7ae7df9 |
165 | $list = true; |
166 | if ($action == 'delete') { |
de61dbcf |
167 | if (!isset($this->delete_action)) { |
168 | foreach ($this->jtables as $table => $j) |
169 | XDB::execute("DELETE FROM {$table} WHERE {$j['joinid']} = {?}{$j['joinextra']}", $id); |
170 | XDB::execute("DELETE FROM {$this->table} WHERE {$this->idfield} = {?}",$id); |
a7de4ef7 |
171 | $page->trig("L'entrée ".$id." a été supprimée."); |
de61dbcf |
172 | } else if ($this->delete_action) { |
173 | XDB::execute($this->delete_action, $id); |
174 | if (isset($this->delete_message)) { |
175 | $page->trig($this->delete_message); |
176 | } else { |
a7de4ef7 |
177 | $page->trig("L'entrée ".$id." a été supprimée."); |
de61dbcf |
178 | } |
179 | } else { |
a7de4ef7 |
180 | $page->trig("Impossible de supprimer l'entrée."); |
de61dbcf |
181 | } |
e7ae7df9 |
182 | } |
183 | if ($action == 'edit') { |
184 | $r = XDB::query("SELECT * FROM {$this->table} WHERE {$this->idfield} = {?}",$id); |
185 | $entry = $r->fetchOneAssoc(); |
186 | $page->assign('entry', $this->prepare_edit($entry)); |
187 | $page->assign('id', $id); |
188 | $list = false; |
189 | } |
a6613b2e |
190 | if ($action == 'massadd') { |
191 | $importer = new CSVImporter($this->table, $this->idfield_editable ? $this->idfield : null); |
192 | $fields = array(); |
193 | foreach ($this->vars as $field=>$descr) { |
194 | if ($this->idfield_editable || $field != $this->idfield) { |
195 | $fields[] = $field; |
196 | $importer->describe($field, @$descr['desc']); |
197 | } |
198 | } |
199 | $page->assign('massadd', true); |
200 | $importer->apply($page, $this->pl . '/massadd', $fields); |
201 | $list = false; |
202 | } |
e7ae7df9 |
203 | if ($action == 'new') { |
204 | if (!$this->idfield_editable) { |
205 | $r = XDB::query("SELECT MAX({$this->idfield})+1 FROM {$this->table}"); |
206 | $page->assign('id', $r->fetchOneCell()); |
1d10d3fd |
207 | $page->assign('entry', $this->prepare_new()); |
e7ae7df9 |
208 | } |
209 | $list = false; |
210 | } |
211 | if ($action == 'update') { |
212 | $values = ""; |
213 | $cancel = false; |
214 | foreach ($this->vars as $field => $descr) { |
215 | if ($values) $values .= ','; |
216 | if (($field == $this->idfield) && !$this->idfield_editable) |
217 | $val = "'".addslashes($id)."'"; |
218 | elseif ($descr['Type'] == 'set') { |
219 | $val = ""; |
220 | if (Post::has($field)) foreach (Post::v($field) as $option) { |
221 | if ($val) $val .= ','; |
222 | $val .= $option; |
223 | } |
224 | $val = "'".addslashes($val)."'"; |
987f1bd2 |
225 | } elseif ($descr['Type'] == 'checkbox') { |
226 | $val = Post::has($field)?"'".addslashes($descr['Value'])."'":"''"; |
e7ae7df9 |
227 | } elseif (Post::has($field)) { |
6682f91d |
228 | $val = Post::v($field); |
e7ae7df9 |
229 | if ($descr['Type'] == 'timestamp') { |
6682f91d |
230 | $val = preg_replace('/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})/', '\3\2\1\4\5\6', $val); |
b9cd8008 |
231 | } |
232 | elseif ($descr['Type'] == 'date') { |
233 | $val = preg_replace('/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})/', '\3-\2-\1', $val); |
e7ae7df9 |
234 | } |
235 | $val = "'".addslashes($val)."'"; |
236 | } else { |
237 | $cancel = true; |
6682f91d |
238 | $page->trig("Il manque le champ ".$field); |
e7ae7df9 |
239 | } |
240 | $values .= $val; |
241 | } |
242 | if (!$cancel) { |
243 | if ($this->idfield_editable && ($id != Post::v($this->idfield)) && $action != 'new') |
244 | XDB::execute("UPDATE {$this->table} SET {$this->idfield} = {?} WHERE {$this->idfield} = {?}", Post::v($this->idfield), $id); |
245 | XDB::execute("REPLACE INTO {$this->table} VALUES ($values)"); |
246 | if ($id !== false) |
a7de4ef7 |
247 | $page->trig("L'entrée ".$id." a été mise à jour."); |
3851b0a6 |
248 | else { |
a7de4ef7 |
249 | $page->trig("Une nouvelle entrée a été créée."); |
3851b0a6 |
250 | $id = XDB::insertId(); |
251 | } |
e7ae7df9 |
252 | } else |
a7de4ef7 |
253 | $page->trig("Impossible de mette à jour."); |
3851b0a6 |
254 | if (!$this->auto_return) { |
255 | return $this->apply($page, 'edit', $id); |
256 | } |
e7ae7df9 |
257 | } |
de61dbcf |
258 | if ($action == 'sort') { |
259 | $this->sortfield = $id; |
260 | } |
261 | if ($action == 'sortdesc') { |
262 | $this->sortfield = $id.' DESC'; |
263 | } |
e7ae7df9 |
264 | if ($list) { |
a3828787 |
265 | // user can sort by field by clicking the title of the column |
de61dbcf |
266 | if (isset($this->sortfield)) { |
267 | // add this sort order after the others (chosen by dev) |
268 | $this->add_sort_field($this->sortfield); |
386b75ea |
269 | if (substr($this->sortfield,-5) == ' DESC') { |
270 | $this->sortfield = substr($this->sortfield,0,-5); |
271 | $this->sortdesc = true; |
272 | } |
a3828787 |
273 | } |
2e7b5921 |
274 | if (count($this->sort) > 0) { |
275 | $sort = 'ORDER BY ' . join($this->sort, ','); |
276 | } |
277 | $it = XDB::iterator("SELECT * FROM {$this->table} $sort"); |
e7ae7df9 |
278 | $this->nbfields = 0; |
279 | foreach ($this->vars as $field => $descr) |
280 | if ($descr['display']) $this->nbfields++; |
281 | $page->assign('list', $it); |
282 | } |
283 | $page->assign('t', $this); |
284 | } |
285 | } |
286 | |
a7de4ef7 |
287 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
e7ae7df9 |
288 | ?> |