Table editor also recognise date fields
[platal.git] / classes / PLTableEditor.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 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 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();
29 // the id field
30 var $idfield;
31 // possibility to edit the field
32 var $idfield_editable;
33 // vars
34 var $vars;
35 // number of displayed fields
36 var $nbfields;
37 /* table editor for platal
38 * $plname : the PLname of the page, ex: admin/payments
39 * $table : the table to edit, ex: profile_medals
40 * $idfield : the field of the table which is the id, ex: id
41 * $editid : is the id editable or not (if not, it is considered as an int)
42 */
43 function PLTableEditor($plname, $table, $idfield, $editid=false) {
44 $this->pl = $plname;
45 $this->table = $table;
46 $this->idfield = $idfield;
47 $this->idfield_editable = $editid;
48 $r = XDB::iterator("SHOW COLUMNS FROM $table");
49 $this->vars = array();
50 while ($a = $r->next()) {
51 // desc will be the title of the column
52 $a['desc'] = $a['Field'];
53 $a['display'] = true;
54
55 if (substr($a['Type'],0,8) == 'varchar(') {
56 // limit editing box size
57 $a['Size'] = $a['Maxlength'] = substr($a['Type'], 8, strlen($a['Type']) - 9);
58 if ($a['Size'] > 40) $a['Size'] = 40;
59 // if too big, put a textarea
60 $a['Type'] = ($a['Maxlength']<200)?'varchar':'varchar200';
61 }
62 elseif ($a['Type'] == 'text' || $a['Type'] == 'mediumtext')
63 $a['Type'] = 'textarea';
64 elseif (substr($a['Type'],0,4) == 'set(') {
65 // get the list of options
66 $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 5, strlen($a['Type']) - 7)));
67 $a['Type'] = 'set';
68 }
69 elseif (substr($a['Type'],0,5) == 'enum(') {
70 // get the list of options
71 $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 6, strlen($a['Type']) - 8)));
72 $a['Type'] = 'enum';
73 }
74 elseif (substr($a['Type'],0,10) == 'timestamp(' || $a['Type'] == 'datetime') {
75 $a['Type'] = 'timestamp';
76 }
77
78 $this->vars[$a['Field']] = $a;
79 }
80 $this->vars[$idfield]['desc'] = 'id';
81 }
82 // called before editing $entry
83 function prepare_edit(&$entry) {
84 foreach ($this->vars as $field => $descr) {
85 if ($descr['Type'] == 'set') {
86 // get the list of options selected
87 $selected = explode(',', $entry[$field]);
88 $entry[$field] = array();
89 foreach ($selected as $option)
90 $entry[$field][$option] = 1;
91 }
92 if ($descr['Type'] == 'timestamp') {
93 // set readable timestamp
94 $date =& $entry[$field];
95 $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);
96 }
97 if ($descr['Type'] == 'date') {
98 $date =& $entry[$field];
99 $date = preg_replace('/([0-9]{4})-?([0-9]{2})-?([0-9]{2})/', '\3/\2/\1', $date);
100 }
101 }
102 return $entry;
103 }
104 // change display of a field
105 function describe($name, $desc, $display) {
106 $this->vars[$name]['desc'] = $desc;
107 $this->vars[$name]['display'] = $display;
108 }
109 // add a join table, when deleting a row corresponding entries will be deleted in these tables
110 function add_join_table($name,$joinid,$joindel,$joinextra="") {
111 if ($joindel)
112 $this->jtables[$name] = array("joinid" => $joinid,"joinextra" => $joinextra?(" AND ".$joinextra):"");
113 }
114 // call when done
115 function apply(&$page, $action, $id = false) {
116 $page->changeTpl('table-editor.tpl');
117 $list = true;
118 if ($action == 'delete') {
119 foreach ($this->jtables as $table => $j)
120 XDB::execute("DELETE FROM {$table} WHERE {$j['joinid']} = {?}{$j['joinextra']}", $id);
121 XDB::execute("DELETE FROM {$this->table} WHERE {$this->idfield} = {?}",$id);
122 $page->trig("L'entrée ".$id." a été supprimée.");
123 }
124 if ($action == 'edit') {
125 $r = XDB::query("SELECT * FROM {$this->table} WHERE {$this->idfield} = {?}",$id);
126 $entry = $r->fetchOneAssoc();
127 $page->assign('entry', $this->prepare_edit($entry));
128 $page->assign('id', $id);
129 $list = false;
130 }
131 if ($action == 'new') {
132 if (!$this->idfield_editable) {
133 $r = XDB::query("SELECT MAX({$this->idfield})+1 FROM {$this->table}");
134 $page->assign('id', $r->fetchOneCell());
135 }
136 $list = false;
137 }
138 if ($action == 'update') {
139 $values = "";
140 $cancel = false;
141 foreach ($this->vars as $field => $descr) {
142 if ($values) $values .= ',';
143 if (($field == $this->idfield) && !$this->idfield_editable)
144 $val = "'".addslashes($id)."'";
145 elseif ($descr['Type'] == 'set') {
146 $val = "";
147 if (Post::has($field)) foreach (Post::v($field) as $option) {
148 if ($val) $val .= ',';
149 $val .= $option;
150 }
151 $val = "'".addslashes($val)."'";
152 } elseif (Post::has($field)) {
153 $val = Post::v($field);
154 if ($descr['Type'] == 'timestamp') {
155 $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);
156 }
157 elseif ($descr['Type'] == 'date') {
158 $val = preg_replace('/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})/', '\3-\2-\1', $val);
159 }
160 $val = "'".addslashes($val)."'";
161 } else {
162 $cancel = true;
163 $page->trig("Il manque le champ ".$field);
164 }
165 $values .= $val;
166 }
167 if (!$cancel) {
168 if ($this->idfield_editable && ($id != Post::v($this->idfield)) && $action != 'new')
169 XDB::execute("UPDATE {$this->table} SET {$this->idfield} = {?} WHERE {$this->idfield} = {?}", Post::v($this->idfield), $id);
170 XDB::execute("REPLACE INTO {$this->table} VALUES ($values)");
171 if ($id !== false)
172 $page->trig("L'entrée ".$id." a été mise à jour.");
173 else
174 $page->trig("Une nouvelle entrée a été créée.");
175 } else
176 $page->trig("Impossible de mette à jour.");
177 }
178 if ($list) {
179 $it = XDB::iterator("SELECT * FROM {$this->table}");
180 $this->nbfields = 0;
181 foreach ($this->vars as $field => $descr)
182 if ($descr['display']) $this->nbfields++;
183 $page->assign('list', $it);
184 }
185 $page->assign('t', $this);
186 }
187 }
188
189 ?>