e7ae7df9 |
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(); |
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; |
de61dbcf |
41 | // action to do to delete row: |
42 | // null => delete effectively, false => no deletion, SQL |
43 | var $delete_action; |
44 | var $delete_message; |
e7ae7df9 |
45 | /* table editor for platal |
46 | * $plname : the PLname of the page, ex: admin/payments |
47 | * $table : the table to edit, ex: profile_medals |
48 | * $idfield : the field of the table which is the id, ex: id |
49 | * $editid : is the id editable or not (if not, it is considered as an int) |
50 | */ |
51 | function PLTableEditor($plname, $table, $idfield, $editid=false) { |
52 | $this->pl = $plname; |
53 | $this->table = $table; |
54 | $this->idfield = $idfield; |
a3828787 |
55 | $this->sortfield = $idfield; |
e7ae7df9 |
56 | $this->idfield_editable = $editid; |
57 | $r = XDB::iterator("SHOW COLUMNS FROM $table"); |
58 | $this->vars = array(); |
59 | while ($a = $r->next()) { |
60 | // desc will be the title of the column |
61 | $a['desc'] = $a['Field']; |
62 | $a['display'] = true; |
63 | |
64 | if (substr($a['Type'],0,8) == 'varchar(') { |
65 | // limit editing box size |
66 | $a['Size'] = $a['Maxlength'] = substr($a['Type'], 8, strlen($a['Type']) - 9); |
67 | if ($a['Size'] > 40) $a['Size'] = 40; |
68 | // if too big, put a textarea |
69 | $a['Type'] = ($a['Maxlength']<200)?'varchar':'varchar200'; |
70 | } |
71 | elseif ($a['Type'] == 'text' || $a['Type'] == 'mediumtext') |
72 | $a['Type'] = 'textarea'; |
73 | elseif (substr($a['Type'],0,4) == 'set(') { |
74 | // get the list of options |
75 | $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 5, strlen($a['Type']) - 7))); |
76 | $a['Type'] = 'set'; |
77 | } |
78 | elseif (substr($a['Type'],0,5) == 'enum(') { |
79 | // get the list of options |
80 | $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 6, strlen($a['Type']) - 8))); |
81 | $a['Type'] = 'enum'; |
82 | } |
b9cd8008 |
83 | elseif (substr($a['Type'],0,10) == 'timestamp(' || $a['Type'] == 'datetime') { |
e7ae7df9 |
84 | $a['Type'] = 'timestamp'; |
85 | } |
86 | |
87 | $this->vars[$a['Field']] = $a; |
88 | } |
89 | $this->vars[$idfield]['desc'] = 'id'; |
90 | } |
91 | // called before editing $entry |
92 | function prepare_edit(&$entry) { |
93 | foreach ($this->vars as $field => $descr) { |
94 | if ($descr['Type'] == 'set') { |
95 | // get the list of options selected |
96 | $selected = explode(',', $entry[$field]); |
97 | $entry[$field] = array(); |
98 | foreach ($selected as $option) |
99 | $entry[$field][$option] = 1; |
100 | } |
101 | if ($descr['Type'] == 'timestamp') { |
102 | // set readable timestamp |
103 | $date =& $entry[$field]; |
104 | $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); |
105 | } |
b9cd8008 |
106 | if ($descr['Type'] == 'date') { |
107 | $date =& $entry[$field]; |
108 | $date = preg_replace('/([0-9]{4})-?([0-9]{2})-?([0-9]{2})/', '\3/\2/\1', $date); |
109 | } |
e7ae7df9 |
110 | } |
111 | return $entry; |
112 | } |
113 | // change display of a field |
114 | function describe($name, $desc, $display) { |
115 | $this->vars[$name]['desc'] = $desc; |
116 | $this->vars[$name]['display'] = $display; |
117 | } |
118 | // add a join table, when deleting a row corresponding entries will be deleted in these tables |
119 | function add_join_table($name,$joinid,$joindel,$joinextra="") { |
120 | if ($joindel) |
121 | $this->jtables[$name] = array("joinid" => $joinid,"joinextra" => $joinextra?(" AND ".$joinextra):""); |
122 | } |
2e7b5921 |
123 | // add a sort key |
de61dbcf |
124 | function add_sort_field($key, $desc = false, $default = false) |
2e7b5921 |
125 | { |
de61dbcf |
126 | if ($default) { |
127 | $this->sortfield = $key . ($desc ? ' DESC' : ''); |
128 | } else { |
129 | $this->sort[] = $key . ($desc ? ' DESC' : ''); |
130 | } |
131 | } |
132 | // set an action when trying to delete row |
133 | function on_delete($action = NULL, $message = NULL) |
134 | { |
135 | $this->delete_action = $action; |
136 | $this->delete_message = $message; |
2e7b5921 |
137 | } |
e7ae7df9 |
138 | // call when done |
139 | function apply(&$page, $action, $id = false) { |
140 | $page->changeTpl('table-editor.tpl'); |
141 | $list = true; |
142 | if ($action == 'delete') { |
de61dbcf |
143 | if (!isset($this->delete_action)) { |
144 | foreach ($this->jtables as $table => $j) |
145 | XDB::execute("DELETE FROM {$table} WHERE {$j['joinid']} = {?}{$j['joinextra']}", $id); |
146 | XDB::execute("DELETE FROM {$this->table} WHERE {$this->idfield} = {?}",$id); |
147 | $page->trig("L'entrée ".$id." a été supprimée."); |
148 | } else if ($this->delete_action) { |
149 | XDB::execute($this->delete_action, $id); |
150 | if (isset($this->delete_message)) { |
151 | $page->trig($this->delete_message); |
152 | } else { |
153 | $page->trig("L'entrée ".$id." a été supprimée."); |
154 | } |
155 | } else { |
156 | $page->trig("Impossible de supprimer l'entrée."); |
157 | } |
e7ae7df9 |
158 | } |
159 | if ($action == 'edit') { |
160 | $r = XDB::query("SELECT * FROM {$this->table} WHERE {$this->idfield} = {?}",$id); |
161 | $entry = $r->fetchOneAssoc(); |
162 | $page->assign('entry', $this->prepare_edit($entry)); |
163 | $page->assign('id', $id); |
164 | $list = false; |
165 | } |
166 | if ($action == 'new') { |
167 | if (!$this->idfield_editable) { |
168 | $r = XDB::query("SELECT MAX({$this->idfield})+1 FROM {$this->table}"); |
169 | $page->assign('id', $r->fetchOneCell()); |
170 | } |
171 | $list = false; |
172 | } |
173 | if ($action == 'update') { |
174 | $values = ""; |
175 | $cancel = false; |
176 | foreach ($this->vars as $field => $descr) { |
177 | if ($values) $values .= ','; |
178 | if (($field == $this->idfield) && !$this->idfield_editable) |
179 | $val = "'".addslashes($id)."'"; |
180 | elseif ($descr['Type'] == 'set') { |
181 | $val = ""; |
182 | if (Post::has($field)) foreach (Post::v($field) as $option) { |
183 | if ($val) $val .= ','; |
184 | $val .= $option; |
185 | } |
186 | $val = "'".addslashes($val)."'"; |
187 | } elseif (Post::has($field)) { |
188 | $val = Post::v($field); |
189 | if ($descr['Type'] == 'timestamp') { |
b9cd8008 |
190 | $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); |
191 | } |
192 | elseif ($descr['Type'] == 'date') { |
193 | $val = preg_replace('/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})/', '\3-\2-\1', $val); |
e7ae7df9 |
194 | } |
195 | $val = "'".addslashes($val)."'"; |
196 | } else { |
197 | $cancel = true; |
198 | $page->trig("Il manque le champ ".$field); |
199 | } |
200 | $values .= $val; |
201 | } |
202 | if (!$cancel) { |
203 | if ($this->idfield_editable && ($id != Post::v($this->idfield)) && $action != 'new') |
204 | XDB::execute("UPDATE {$this->table} SET {$this->idfield} = {?} WHERE {$this->idfield} = {?}", Post::v($this->idfield), $id); |
205 | XDB::execute("REPLACE INTO {$this->table} VALUES ($values)"); |
206 | if ($id !== false) |
207 | $page->trig("L'entrée ".$id." a été mise à jour."); |
208 | else |
209 | $page->trig("Une nouvelle entrée a été créée."); |
210 | } else |
211 | $page->trig("Impossible de mette à jour."); |
212 | } |
de61dbcf |
213 | if ($action == 'sort') { |
214 | $this->sortfield = $id; |
215 | } |
216 | if ($action == 'sortdesc') { |
217 | $this->sortfield = $id.' DESC'; |
218 | } |
e7ae7df9 |
219 | if ($list) { |
a3828787 |
220 | // user can sort by field by clicking the title of the column |
de61dbcf |
221 | if (isset($this->sortfield)) { |
222 | // add this sort order after the others (chosen by dev) |
223 | $this->add_sort_field($this->sortfield); |
a3828787 |
224 | } |
2e7b5921 |
225 | if (count($this->sort) > 0) { |
226 | $sort = 'ORDER BY ' . join($this->sort, ','); |
227 | } |
228 | $it = XDB::iterator("SELECT * FROM {$this->table} $sort"); |
e7ae7df9 |
229 | $this->nbfields = 0; |
230 | foreach ($this->vars as $field => $descr) |
231 | if ($descr['display']) $this->nbfields++; |
232 | $page->assign('list', $it); |
233 | } |
234 | $page->assign('t', $this); |
235 | } |
236 | } |
237 | |
238 | ?> |