Commit | Line | Data |
---|---|---|
e7ae7df9 | 1 | <?php |
2 | /*************************************************************************** | |
e92ecb8c | 3 | * Copyright (C) 2003-2011 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 | ||
eaf30d86 | 22 | class PLTableEditor |
2b1ee50b | 23 | { |
e7ae7df9 | 24 | // the plat/al name of the page |
2b1ee50b | 25 | public $pl; |
e7ae7df9 | 26 | // the table name |
2b1ee50b | 27 | public $table; |
e7ae7df9 | 28 | // joint tables to delete when deleting an entry |
2b1ee50b | 29 | public $jtables = array(); |
b08bfbce PC |
30 | /** joint tables to add optional infos |
31 | * associative array : keys are the tables names, values are the joint | |
32 | * clauses | |
33 | * @see add_option_fields | |
34 | */ | |
35 | public $otables = array(); | |
36 | /** optional fields | |
37 | * These fields are used to display additionnal infos in listing (an only | |
38 | * there). The additionnal infos are retreived from other tables. This var | |
39 | * is an associative array : keys are the sql name of the fields | |
40 | * (table.field) where table must be in $otables, values are a list of the | |
7381c10d PC |
41 | * name used in $vars, the description and the type of the field, and the |
42 | * var it should precede. | |
b08bfbce PC |
43 | */ |
44 | public $ofields = array(); | |
2e7b5921 | 45 | // sorting field |
2b1ee50b | 46 | public $sort = array(); |
e7ae7df9 | 47 | // the id field |
2b1ee50b | 48 | public $idfield; |
e7ae7df9 | 49 | // possibility to edit the field |
2b1ee50b | 50 | public $idfield_editable; |
e7ae7df9 | 51 | // vars |
2b1ee50b | 52 | public $vars; |
e7ae7df9 | 53 | // number of displayed fields |
2b1ee50b | 54 | public $nbfields; |
c57685e7 PC |
55 | // a where clause to restrict table |
56 | public $whereclause; | |
a3828787 | 57 | // the field for sorting entries |
2b1ee50b | 58 | public $sortfield; |
59 | public $sortdesc = false; | |
de61dbcf | 60 | // action to do to delete row: |
2b1ee50b | 61 | // null => delete effectively, false => no deletion, SQL |
62 | public $delete_action; | |
63 | public $delete_message; | |
3851b0a6 | 64 | // Should "Save" button return to the list view |
2b1ee50b | 65 | public $auto_return = true; |
3851b0a6 | 66 | |
e7ae7df9 | 67 | /* table editor for platal |
68 | * $plname : the PLname of the page, ex: admin/payments | |
69 | * $table : the table to edit, ex: profile_medals | |
70 | * $idfield : the field of the table which is the id, ex: id | |
71 | * $editid : is the id editable or not (if not, it is considered as an int) | |
72 | */ | |
80c3b579 | 73 | public function __construct($plname, $table, $idfield, $editid = false, $idfield2 = false) |
1d10d3fd | 74 | { |
e7ae7df9 | 75 | $this->pl = $plname; |
76 | $this->table = $table; | |
77 | $this->idfield = $idfield; | |
80c3b579 | 78 | $this->idfield2 = $idfield2; |
a3828787 | 79 | $this->sortfield = $idfield; |
e7ae7df9 | 80 | $this->idfield_editable = $editid; |
c57685e7 | 81 | $this->whereclause = '1'; |
669cb0e0 | 82 | $r = XDB::iterator("SHOW FULL COLUMNS FROM $table"); |
e7ae7df9 | 83 | $this->vars = array(); |
84 | while ($a = $r->next()) { | |
85 | // desc will be the title of the column | |
86 | $a['desc'] = $a['Field']; | |
87 | $a['display'] = true; | |
6682f91d | 88 | |
e7ae7df9 | 89 | if (substr($a['Type'],0,8) == 'varchar(') { |
90 | // limit editing box size | |
91 | $a['Size'] = $a['Maxlength'] = substr($a['Type'], 8, strlen($a['Type']) - 9); | |
92 | if ($a['Size'] > 40) $a['Size'] = 40; | |
93 | // if too big, put a textarea | |
94 | $a['Type'] = ($a['Maxlength']<200)?'varchar':'varchar200'; | |
95 | } | |
96 | elseif ($a['Type'] == 'text' || $a['Type'] == 'mediumtext') | |
97 | $a['Type'] = 'textarea'; | |
98 | elseif (substr($a['Type'],0,4) == 'set(') { | |
99 | // get the list of options | |
a7de4ef7 | 100 | $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 5, strlen($a['Type']) - 7))); |
987f1bd2 | 101 | if (count($a['List']) == 1) { |
102 | $a['Type'] = 'checkbox'; | |
103 | $a['Value'] = $a['List'][0]; | |
104 | } else { | |
105 | $a['Type'] = 'set'; | |
eaf30d86 | 106 | } |
e7ae7df9 | 107 | } |
108 | elseif (substr($a['Type'],0,5) == 'enum(') { | |
109 | // get the list of options | |
a7de4ef7 | 110 | $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 6, strlen($a['Type']) - 8))); |
e7ae7df9 | 111 | $a['Type'] = 'enum'; |
112 | } | |
b9cd8008 | 113 | elseif (substr($a['Type'],0,10) == 'timestamp(' || $a['Type'] == 'datetime') { |
e7ae7df9 | 114 | $a['Type'] = 'timestamp'; |
115 | } | |
669cb0e0 | 116 | elseif ($a['Comment'] == 'ip_address') { |
117 | $a['Type']='ip_address'; | |
118 | } | |
e7ae7df9 | 119 | |
120 | $this->vars[$a['Field']] = $a; | |
121 | } | |
122 | $this->vars[$idfield]['desc'] = 'id'; | |
123 | } | |
2b1ee50b | 124 | |
1d10d3fd | 125 | // called before creating a new entry |
2b1ee50b | 126 | private function prepare_new() |
1d10d3fd | 127 | { |
128 | $entry = array(); | |
129 | foreach ($this->vars as $field => $descr) { | |
130 | $entry[$field] = $descr['Default']; | |
131 | } | |
132 | return $this->prepare_edit($entry); | |
133 | } | |
2b1ee50b | 134 | |
e7ae7df9 | 135 | // called before editing $entry |
eaf30d86 | 136 | private function prepare_edit(&$entry) |
2b1ee50b | 137 | { |
e7ae7df9 | 138 | foreach ($this->vars as $field => $descr) { |
139 | if ($descr['Type'] == 'set') { | |
140 | // get the list of options selected | |
141 | $selected = explode(',', $entry[$field]); | |
142 | $entry[$field] = array(); | |
143 | foreach ($selected as $option) | |
144 | $entry[$field][$option] = 1; | |
145 | } | |
146 | if ($descr['Type'] == 'timestamp') { | |
147 | // set readable timestamp | |
148 | $date =& $entry[$field]; | |
6682f91d | 149 | $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 | 150 | } |
b9cd8008 | 151 | if ($descr['Type'] == 'date') { |
152 | $date =& $entry[$field]; | |
153 | $date = preg_replace('/([0-9]{4})-?([0-9]{2})-?([0-9]{2})/', '\3/\2/\1', $date); | |
154 | } | |
669cb0e0 | 155 | if ($descr['Type'] == 'ip_address') { |
156 | $ip = & $entry[$field]; | |
157 | $ip = long2ip($ip); | |
158 | } | |
e7ae7df9 | 159 | } |
160 | return $entry; | |
161 | } | |
eaf30d86 | 162 | |
3851b0a6 | 163 | // set whether the save button show redirect to list view or edit view |
2b1ee50b | 164 | public function list_on_edit($var) |
3851b0a6 | 165 | { |
166 | $this->auto_return = $var; | |
167 | } | |
2b1ee50b | 168 | |
e7ae7df9 | 169 | // change display of a field |
2b1ee50b | 170 | public function describe($name, $desc, $display) |
171 | { | |
e7ae7df9 | 172 | $this->vars[$name]['desc'] = $desc; |
173 | $this->vars[$name]['display'] = $display; | |
174 | } | |
eaf30d86 | 175 | |
6682f91d | 176 | // add a join table, when deleting a row corresponding entries will be deleted in these tables |
2b1ee50b | 177 | public function add_join_table($name,$joinid,$joindel,$joinextra="") |
178 | { | |
e7ae7df9 | 179 | if ($joindel) |
180 | $this->jtables[$name] = array("joinid" => $joinid,"joinextra" => $joinextra?(" AND ".$joinextra):""); | |
181 | } | |
eaf30d86 | 182 | |
b08bfbce PC |
183 | /** Add optional table |
184 | * @see add_option_field | |
185 | * @param $name the table sql name | |
186 | * @param $jointclause the full joint clause. Use t as main table alias | |
187 | * name. | |
188 | */ | |
189 | public function add_option_table($name, $jointclause) | |
190 | { | |
191 | $this->otables[$name] = $jointclause; | |
192 | } | |
193 | ||
194 | /** Add optional field | |
195 | * These fields are used to display additionnal infos in listing (and only | |
196 | * there). The additionnal infos are retreived from other tables. | |
197 | * @param $sqlname is the full sql name (table.field) where table must be | |
198 | * added with add_option_table | |
199 | * @param $name the name used for sort (please make it different from | |
200 | * other fields in main table or subtables) | |
201 | * @param $desc the description displayed as column header | |
202 | * @param $type the typed used for display | |
203 | */ | |
7381c10d | 204 | public function add_option_field($sqlname, $name, $desc, $type = null, $nextvar = null) |
b08bfbce | 205 | { |
7381c10d | 206 | $this->ofields[$sqlname] = array($name, $desc, $type, $nextvar); |
b08bfbce PC |
207 | } |
208 | ||
2e7b5921 | 209 | // add a sort key |
2b1ee50b | 210 | public function add_sort_field($key, $desc = false, $default = false) |
2e7b5921 | 211 | { |
2b1ee50b | 212 | if ($default) { |
213 | $this->sortfield = $key . ($desc ? ' DESC' : ''); | |
214 | } else { | |
215 | $this->sort[] = $key . ($desc ? ' DESC' : ''); | |
de61dbcf | 216 | } |
217 | } | |
2b1ee50b | 218 | |
c57685e7 PC |
219 | // add a where clause to limit table listing |
220 | public function set_where_clause($whereclause="1") | |
221 | { | |
222 | $this->whereclause = $whereclause; | |
223 | } | |
ef138fdc | 224 | |
de61dbcf | 225 | // set an action when trying to delete row |
2b1ee50b | 226 | public function on_delete($action = NULL, $message = NULL) |
de61dbcf | 227 | { |
2b1ee50b | 228 | $this->delete_action = $action; |
229 | $this->delete_message = $message; | |
2e7b5921 | 230 | } |
2b1ee50b | 231 | |
e7ae7df9 | 232 | // call when done |
ed4f7de0 | 233 | public function apply(PlPage $page, $action, $id = false, $id2 = false) |
2b1ee50b | 234 | { |
7cb40d85 | 235 | $page->coreTpl('table-editor.tpl'); |
e7ae7df9 | 236 | $list = true; |
4744b162 | 237 | if ($action == 'delete' && $id !== false) { |
40d428d8 | 238 | S::assert_xsrf_token(); |
e61326ed | 239 | |
2b1ee50b | 240 | if (!isset($this->delete_action)) { |
241 | foreach ($this->jtables as $table => $j) | |
242 | XDB::execute("DELETE FROM {$table} WHERE {$j['joinid']} = {?}{$j['joinextra']}", $id); | |
80c3b579 SJ |
243 | $where = XDB::format("{$this->idfield} = {?}", $id); |
244 | if ($this->idfield2) { | |
245 | $where .= XDB::format(" AND {$this->idfield2} = {?}", $id2); | |
246 | } | |
247 | XDB::rawExecute("DELETE FROM {$this->table} WHERE " . $where); | |
248 | $page->trigSuccess("L'entrée " . $id . (($id2) ? '-' . $id2 : '') . ' a été supprimée.'); | |
2b1ee50b | 249 | } else if ($this->delete_action) { |
250 | XDB::execute($this->delete_action, $id); | |
251 | if (isset($this->delete_message)) { | |
a7d35093 | 252 | $page->trigSuccess($this->delete_message); |
2b1ee50b | 253 | } else { |
a7d35093 | 254 | $page->trigSuccess("L'entrée ".$id." a été supprimée."); |
669cb0e0 | 255 | } |
2b1ee50b | 256 | } else { |
a7d35093 | 257 | $page->trigError("Impossible de supprimer l'entrée."); |
2b1ee50b | 258 | } |
e7ae7df9 | 259 | } |
4744b162 | 260 | if ($action == 'edit' && $id !== false) { |
c57685e7 | 261 | $r = XDB::query("SELECT * FROM {$this->table} WHERE {$this->idfield} = {?} AND {$this->whereclause}",$id); |
e7ae7df9 | 262 | $entry = $r->fetchOneAssoc(); |
263 | $page->assign('entry', $this->prepare_edit($entry)); | |
264 | $page->assign('id', $id); | |
265 | $list = false; | |
266 | } | |
a6613b2e | 267 | if ($action == 'massadd') { |
268 | $importer = new CSVImporter($this->table, $this->idfield_editable ? $this->idfield : null); | |
269 | $fields = array(); | |
270 | foreach ($this->vars as $field=>$descr) { | |
271 | if ($this->idfield_editable || $field != $this->idfield) { | |
272 | $fields[] = $field; | |
273 | $importer->describe($field, @$descr['desc']); | |
274 | } | |
275 | } | |
276 | $page->assign('massadd', true); | |
277 | $importer->apply($page, $this->pl . '/massadd', $fields); | |
278 | $list = false; | |
279 | } | |
e7ae7df9 | 280 | if ($action == 'new') { |
281 | if (!$this->idfield_editable) { | |
1d10d3fd | 282 | $page->assign('entry', $this->prepare_new()); |
e7ae7df9 | 283 | } |
284 | $list = false; | |
285 | } | |
40d428d8 VZ |
286 | if ($action == 'update') { |
287 | S::assert_xsrf_token(); | |
288 | ||
e7ae7df9 | 289 | $cancel = false; |
4c603b95 FB |
290 | $values = array(); |
291 | $new = false; | |
e7ae7df9 | 292 | foreach ($this->vars as $field => $descr) { |
4c603b95 FB |
293 | $val = null; |
294 | $new = ($id === false || $id === null); | |
295 | if ($field == $this->idfield && !$this->idfield_editable) { | |
296 | if ($new) { | |
297 | $val = XDB::fetchOneCell("SELECT MAX({$field}) + 1 | |
298 | FROM {$this->table}"); | |
4744b162 | 299 | } else { |
4c603b95 | 300 | continue; |
4744b162 PC |
301 | } |
302 | } elseif ($descr['Type'] == 'set') { | |
4c603b95 FB |
303 | $val = new PlFlagset(); |
304 | if (Post::has($field)) { | |
305 | foreach (Post::v($field) as $option) { | |
306 | $val->addFlag($option); | |
307 | } | |
e7ae7df9 | 308 | } |
987f1bd2 | 309 | } elseif ($descr['Type'] == 'checkbox') { |
4c603b95 | 310 | $val = Post::has($field)? $descr['Value'] : ""; |
e7ae7df9 | 311 | } elseif (Post::has($field)) { |
6682f91d | 312 | $val = Post::v($field); |
e7ae7df9 | 313 | if ($descr['Type'] == 'timestamp') { |
6682f91d | 314 | $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); |
4c603b95 | 315 | } else if ($descr['Type'] == 'date') { |
b9cd8008 | 316 | $val = preg_replace('/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})/', '\3-\2-\1', $val); |
4c603b95 | 317 | } elseif ($descr['Type'] == 'ip_address') { |
669cb0e0 | 318 | $val = ip2long($val); |
319 | } | |
e7ae7df9 | 320 | } else { |
321 | $cancel = true; | |
a7d35093 | 322 | $page->trigError("Il manque le champ ".$field); |
e7ae7df9 | 323 | } |
4c603b95 | 324 | $values[$field] = XDB::escape($val); |
e7ae7df9 | 325 | } |
326 | if (!$cancel) { | |
4c603b95 FB |
327 | if (!$new) { |
328 | $update = array(); | |
329 | foreach ($values as $field => $value) { | |
330 | $update[] = $field . ' = ' . $value; | |
331 | } | |
332 | $update = implode(', ', $update); | |
acfa828e FB |
333 | XDB::rawExecute("UPDATE {$this->table} |
334 | SET {$update} | |
335 | WHERE {$this->idfield} = " . XDB::escape($id) . " | |
336 | AND {$this->whereclause}"); | |
4c603b95 FB |
337 | } else { |
338 | $fields = implode(', ', array_keys($values)); | |
339 | $values = implode(', ', $values); | |
acfa828e FB |
340 | XDB::rawExecute("INSERT INTO {$this->table} ({$fields}) |
341 | VALUES ({$values})"); | |
4c603b95 | 342 | } |
3aff822a | 343 | if ($id !== false && $id !== null) { |
a7d35093 | 344 | $page->trigSuccess("L'entrée ".$id." a été mise à jour."); |
3aff822a | 345 | } else { |
a7d35093 | 346 | $page->trigSuccess("Une nouvelle entrée a été créée."); |
3851b0a6 | 347 | $id = XDB::insertId(); |
348 | } | |
4c603b95 | 349 | } else { |
a7d35093 | 350 | $page->trigError("Impossible de mettre à jour."); |
4c603b95 | 351 | } |
3851b0a6 | 352 | if (!$this->auto_return) { |
353 | return $this->apply($page, 'edit', $id); | |
354 | } | |
e7ae7df9 | 355 | } |
de61dbcf | 356 | if ($action == 'sort') { |
2b1ee50b | 357 | $this->sortfield = $id; |
de61dbcf | 358 | } |
359 | if ($action == 'sortdesc') { | |
2b1ee50b | 360 | $this->sortfield = $id.' DESC'; |
de61dbcf | 361 | } |
e7ae7df9 | 362 | if ($list) { |
a3828787 | 363 | // user can sort by field by clicking the title of the column |
de61dbcf | 364 | if (isset($this->sortfield)) { |
365 | // add this sort order after the others (chosen by dev) | |
366 | $this->add_sort_field($this->sortfield); | |
386b75ea | 367 | if (substr($this->sortfield,-5) == ' DESC') { |
368 | $this->sortfield = substr($this->sortfield,0,-5); | |
369 | $this->sortdesc = true; | |
370 | } | |
a3828787 | 371 | } |
2e7b5921 | 372 | if (count($this->sort) > 0) { |
373 | $sort = 'ORDER BY ' . join($this->sort, ','); | |
374 | } | |
b08bfbce PC |
375 | // optional infos columns |
376 | $optional_fields = ''; | |
7381c10d PC |
377 | if (count($this->ofields)) { |
378 | $order = array(); | |
379 | foreach ($this->vars as $i => $aliasname) { | |
380 | $order[sprintf('%f',count($order))] = $i; | |
381 | } | |
382 | // delta for indexing optional columns between existing ones | |
383 | $changeorder = 0.5; | |
384 | foreach ($this->ofields as $sqlname => $ofieldvalues) { | |
385 | list($aliasname, $desc, $type, $nextvar) = $ofieldvalues; | |
386 | $optional_fields .= ', '.$sqlname.' AS '.$aliasname; | |
387 | $this->describe($aliasname, $desc, true); | |
388 | $this->vars[$aliasname]['optional'] = true; | |
389 | if (isset($type)) { | |
390 | $this->vars[$aliasname]['Type'] = $type; | |
391 | } | |
392 | if (isset($nextvar) && isset($this->vars[$nextvar]) && $nextvar != $aliasname) { | |
393 | $nextkey = array_search($nextvar, $order); | |
394 | $order[sprintf('%f',$nextkey - $changeorder)] = $aliasname; | |
395 | $changeorder = $changeorder / 2; | |
396 | } else { | |
397 | $order[sprintf('%f',count($order))] = $aliasname; | |
398 | } | |
399 | $this->vars[$aliasname]['Field'] = $aliasname; | |
400 | } | |
401 | if ($changeorder != 0.5) { | |
402 | ksort($order); | |
403 | $orderedvars = array(); | |
404 | foreach ($order as $aliasname) { | |
405 | $orderedvars[$aliasname] = $this->vars[$aliasname]; | |
406 | } | |
407 | $this->vars = $orderedvars; | |
408 | } | |
b08bfbce PC |
409 | } |
410 | $optional_joints = ''; | |
411 | foreach ($this->otables as $tablename => $jointclause) { | |
412 | $optional_joints .= ' LEFT JOIN '.$tablename.' ON ('.$jointclause.')'; | |
413 | } | |
414 | $it = XDB::iterator("SELECT t.* {$optional_fields} FROM {$this->table} AS t {$optional_joints} WHERE {$this->whereclause} $sort"); | |
e7ae7df9 | 415 | $this->nbfields = 0; |
416 | foreach ($this->vars as $field => $descr) | |
417 | if ($descr['display']) $this->nbfields++; | |
418 | $page->assign('list', $it); | |
419 | } | |
420 | $page->assign('t', $this); | |
421 | } | |
422 | } | |
423 | ||
a7de4ef7 | 424 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
e7ae7df9 | 425 | ?> |