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