a2ea5333934c677ca1afcae28e7b8937fa269569
2 /***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
23 // the plat/al name of the page
27 // joint tables to delete when deleting an entry
28 var $jtables = array();
33 // possibility to edit the field
34 var $idfield_editable;
37 // number of displayed fields
39 // the field for sorting entries
41 // action to do to delete row:
42 // null => delete effectively, false => no deletion, SQL
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)
51 function PLTableEditor($plname, $table, $idfield, $editid=false
) {
53 $this->table
= $table;
54 $this->idfield
= $idfield;
55 $this->sortfield
= $idfield;
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'];
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';
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)));
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)));
83 elseif (substr($a['Type'],0,10) == 'timestamp(' ||
$a['Type'] == 'datetime') {
84 $a['Type'] = 'timestamp';
87 $this->vars
[$a['Field']] = $a;
89 $this->vars
[$idfield]['desc'] = 'id';
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;
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);
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);
113 // change display of a field
114 function describe($name, $desc, $display) {
115 $this->vars
[$name]['desc'] = $desc;
116 $this->vars
[$name]['display'] = $display;
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="") {
121 $this->jtables
[$name] = array("joinid" => $joinid,"joinextra" => $joinextra?
(" AND ".$joinextra):"");
124 function add_sort_field($key, $desc = false
, $default = false
)
127 $this->sortfield
= $key . ($desc ?
' DESC' : '');
129 $this->sort
[] = $key . ($desc ?
' DESC' : '');
132 // set an action when trying to delete row
133 function on_delete($action = NULL
, $message = NULL
)
135 $this->delete_action
= $action;
136 $this->delete_message
= $message;
139 function apply(&$page, $action, $id = false
) {
140 $page->changeTpl('table-editor.tpl');
142 if ($action == 'delete') {
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
);
153 $page->trig("L'entrée ".$id." a été supprimée.");
156 $page->trig("Impossible de supprimer l'entrée.");
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);
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());
173 if ($action == 'update') {
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') {
182 if (Post
::has($field)) foreach (Post
::v($field) as $option) {
183 if ($val) $val .= ',';
186 $val = "'".addslashes($val)."'";
187 } elseif (Post
::has($field)) {
188 $val = Post
::v($field);
189 if ($descr['Type'] == 'timestamp') {
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);
192 elseif ($descr['Type'] == 'date') {
193 $val = preg_replace('/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})/', '\3-\2-\1', $val);
195 $val = "'".addslashes($val)."'";
198 $page->trig("Il manque le champ ".$field);
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)");
207 $page->trig("L'entrée ".$id." a été mise à jour.");
209 $page->trig("Une nouvelle entrée a été créée.");
211 $page->trig("Impossible de mette à jour.");
213 if ($action == 'sort') {
214 $this->sortfield
= $id;
216 if ($action == 'sortdesc') {
217 $this->sortfield
= $id.' DESC';
220 // user can sort by field by clicking the title of the column
221 if (isset($this->sortfield
)) {
222 // add this sort order after the others (chosen by dev)
223 $this->add_sort_field($this->sortfield
);
225 if (count($this->sort
) > 0) {
226 $sort = 'ORDER BY ' . join($this->sort
, ',');
228 $it = XDB
::iterator("SELECT * FROM {$this->table} $sort");
230 foreach ($this->vars
as $field => $descr)
231 if ($descr['display']) $this->nbfields++
;
232 $page->assign('list', $it);
234 $page->assign('t', $this);