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