2 /***************************************************************************
3 * Copyright (C) 2003-2007 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 var $sortdesc = false
;
42 // action to do to delete row:
43 // null => delete effectively, false => no deletion, SQL
46 // Should "Save" button return to the list view
47 var $auto_return = true
;
49 /* table editor for platal
50 * $plname : the PLname of the page, ex: admin/payments
51 * $table : the table to edit, ex: profile_medals
52 * $idfield : the field of the table which is the id, ex: id
53 * $editid : is the id editable or not (if not, it is considered as an int)
55 function PLTableEditor($plname, $table, $idfield, $editid=false
)
58 $this->table
= $table;
59 $this->idfield
= $idfield;
60 $this->sortfield
= $idfield;
61 $this->idfield_editable
= $editid;
62 $r = XDB
::iterator("SHOW COLUMNS FROM $table");
63 $this->vars
= array();
64 while ($a = $r->next()) {
65 // desc will be the title of the column
66 $a['desc'] = $a['Field'];
69 if (substr($a['Type'],0,8) == 'varchar(') {
70 // limit editing box size
71 $a['Size'] = $a['Maxlength'] = substr($a['Type'], 8, strlen($a['Type']) - 9);
72 if ($a['Size'] > 40) $a['Size'] = 40;
73 // if too big, put a textarea
74 $a['Type'] = ($a['Maxlength']<200)?
'varchar':'varchar200';
76 elseif ($a['Type'] == 'text' ||
$a['Type'] == 'mediumtext')
77 $a['Type'] = 'textarea';
78 elseif (substr($a['Type'],0,4) == 'set(') {
79 // get the list of options
80 $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 5, strlen($a['Type']) - 7)));
81 if (count($a['List']) == 1) {
82 $a['Type'] = 'checkbox';
83 $a['Value'] = $a['List'][0];
88 elseif (substr($a['Type'],0,5) == 'enum(') {
89 // get the list of options
90 $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 6, strlen($a['Type']) - 8)));
93 elseif (substr($a['Type'],0,10) == 'timestamp(' ||
$a['Type'] == 'datetime') {
94 $a['Type'] = 'timestamp';
97 $this->vars
[$a['Field']] = $a;
99 $this->vars
[$idfield]['desc'] = 'id';
101 // called before creating a new entry
102 function prepare_new()
105 foreach ($this->vars
as $field => $descr) {
106 $entry[$field] = $descr['Default'];
108 return $this->prepare_edit($entry);
110 // called before editing $entry
111 function prepare_edit(&$entry) {
112 foreach ($this->vars
as $field => $descr) {
113 if ($descr['Type'] == 'set') {
114 // get the list of options selected
115 $selected = explode(',', $entry[$field]);
116 $entry[$field] = array();
117 foreach ($selected as $option)
118 $entry[$field][$option] = 1;
120 if ($descr['Type'] == 'timestamp') {
121 // set readable timestamp
122 $date =& $entry[$field];
123 $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);
125 if ($descr['Type'] == 'date') {
126 $date =& $entry[$field];
127 $date = preg_replace('/([0-9]{4})-?([0-9]{2})-?([0-9]{2})/', '\3/\2/\1', $date);
132 // set whether the save button show redirect to list view or edit view
133 function list_on_edit($var)
135 $this->auto_return
= $var;
137 // change display of a field
138 function describe($name, $desc, $display) {
139 $this->vars
[$name]['desc'] = $desc;
140 $this->vars
[$name]['display'] = $display;
142 // add a join table, when deleting a row corresponding entries will be deleted in these tables
143 function add_join_table($name,$joinid,$joindel,$joinextra="") {
145 $this->jtables
[$name] = array("joinid" => $joinid,"joinextra" => $joinextra?
(" AND ".$joinextra):"");
148 function add_sort_field($key, $desc = false
, $default = false
)
151 $this->sortfield
= $key . ($desc ?
' DESC' : '');
153 $this->sort
[] = $key . ($desc ?
' DESC' : '');
156 // set an action when trying to delete row
157 function on_delete($action = NULL
, $message = NULL
)
159 $this->delete_action
= $action;
160 $this->delete_message
= $message;
163 function apply(&$page, $action, $id = false
) {
164 $page->changeTpl('core/table-editor.tpl');
166 if ($action == 'delete') {
167 if (!isset($this->delete_action
)) {
168 foreach ($this->jtables
as $table => $j)
169 XDB
::execute("DELETE FROM {$table} WHERE {$j['joinid']} = {?}{$j['joinextra']}", $id);
170 XDB
::execute("DELETE FROM {$this->table} WHERE {$this->idfield} = {?}",$id);
171 $page->trig("L'entrée ".$id." a été supprimée.");
172 } else if ($this->delete_action
) {
173 XDB
::execute($this->delete_action
, $id);
174 if (isset($this->delete_message
)) {
175 $page->trig($this->delete_message
);
177 $page->trig("L'entrée ".$id." a été supprimée.");
180 $page->trig("Impossible de supprimer l'entrée.");
183 if ($action == 'edit') {
184 $r = XDB
::query("SELECT * FROM {$this->table} WHERE {$this->idfield} = {?}",$id);
185 $entry = $r->fetchOneAssoc();
186 $page->assign('entry', $this->prepare_edit($entry));
187 $page->assign('id', $id);
190 if ($action == 'massadd') {
191 $importer = new CSVImporter($this->table
, $this->idfield_editable ?
$this->idfield
: null
);
193 foreach ($this->vars
as $field=>$descr) {
194 if ($this->idfield_editable ||
$field != $this->idfield
) {
196 $importer->describe($field, @$descr['desc']);
199 $page->assign('massadd', true
);
200 $importer->apply($page, $this->pl
. '/massadd', $fields);
203 if ($action == 'new') {
204 if (!$this->idfield_editable
) {
205 $r = XDB
::query("SELECT MAX({$this->idfield})+1 FROM {$this->table}");
206 $page->assign('id', $r->fetchOneCell());
207 $page->assign('entry', $this->prepare_new());
211 if ($action == 'update') {
214 foreach ($this->vars
as $field => $descr) {
215 if ($values) $values .= ',';
216 if (($field == $this->idfield
) && !$this->idfield_editable
)
217 $val = "'".addslashes($id)."'";
218 elseif ($descr['Type'] == 'set') {
220 if (Post
::has($field)) foreach (Post
::v($field) as $option) {
221 if ($val) $val .= ',';
224 $val = "'".addslashes($val)."'";
225 } elseif ($descr['Type'] == 'checkbox') {
226 $val = Post
::has($field)?
"'".addslashes($descr['Value'])."'":"''";
227 } elseif (Post
::has($field)) {
228 $val = Post
::v($field);
229 if ($descr['Type'] == 'timestamp') {
230 $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);
232 elseif ($descr['Type'] == 'date') {
233 $val = preg_replace('/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})/', '\3-\2-\1', $val);
235 $val = "'".addslashes($val)."'";
238 $page->trig("Il manque le champ ".$field);
243 if ($this->idfield_editable
&& ($id != Post
::v($this->idfield
)) && $action != 'new')
244 XDB
::execute("UPDATE {$this->table} SET {$this->idfield} = {?} WHERE {$this->idfield} = {?}", Post
::v($this->idfield
), $id);
245 XDB
::execute("REPLACE INTO {$this->table} VALUES ($values)");
247 $page->trig("L'entrée ".$id." a été mise à jour.");
249 $page->trig("Une nouvelle entrée a été créée.");
250 $id = XDB
::insertId();
253 $page->trig("Impossible de mette à jour.");
254 if (!$this->auto_return
) {
255 return $this->apply($page, 'edit', $id);
258 if ($action == 'sort') {
259 $this->sortfield
= $id;
261 if ($action == 'sortdesc') {
262 $this->sortfield
= $id.' DESC';
265 // user can sort by field by clicking the title of the column
266 if (isset($this->sortfield
)) {
267 // add this sort order after the others (chosen by dev)
268 $this->add_sort_field($this->sortfield
);
269 if (substr($this->sortfield
,-5) == ' DESC') {
270 $this->sortfield
= substr($this->sortfield
,0,-5);
271 $this->sortdesc
= true
;
274 if (count($this->sort
) > 0) {
275 $sort = 'ORDER BY ' . join($this->sort
, ',');
277 $it = XDB
::iterator("SELECT * FROM {$this->table} $sort");
279 foreach ($this->vars
as $field => $descr)
280 if ($descr['display']) $this->nbfields++
;
281 $page->assign('list', $it);
283 $page->assign('t', $this);
287 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: