2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 ***************************************************************************/
24 // the plat/al name of the page
28 // joint tables to delete when deleting an entry
29 public $jtables = array();
31 public $sort = array();
34 // possibility to edit the field
35 public $idfield_editable;
38 // number of displayed fields
40 // a where clause to restrict table
42 // the field for sorting entries
44 public $sortdesc = false
;
45 // action to do to delete row:
46 // null => delete effectively, false => no deletion, SQL
47 public $delete_action;
48 public $delete_message;
49 // Should "Save" button return to the list view
50 public $auto_return = true
;
52 /* table editor for platal
53 * $plname : the PLname of the page, ex: admin/payments
54 * $table : the table to edit, ex: profile_medals
55 * $idfield : the field of the table which is the id, ex: id
56 * $editid : is the id editable or not (if not, it is considered as an int)
58 public function __construct($plname, $table, $idfield, $editid=false
)
61 $this->table
= $table;
62 $this->idfield
= $idfield;
63 $this->sortfield
= $idfield;
64 $this->idfield_editable
= $editid;
65 $this->whereclause
= '1';
66 $r = XDB
::iterator("SHOW FULL COLUMNS FROM $table");
67 $this->vars
= array();
68 while ($a = $r->next()) {
69 // desc will be the title of the column
70 $a['desc'] = $a['Field'];
73 if (substr($a['Type'],0,8) == 'varchar(') {
74 // limit editing box size
75 $a['Size'] = $a['Maxlength'] = substr($a['Type'], 8, strlen($a['Type']) - 9);
76 if ($a['Size'] > 40) $a['Size'] = 40;
77 // if too big, put a textarea
78 $a['Type'] = ($a['Maxlength']<200)?
'varchar':'varchar200';
80 elseif ($a['Type'] == 'text' ||
$a['Type'] == 'mediumtext')
81 $a['Type'] = 'textarea';
82 elseif (substr($a['Type'],0,4) == 'set(') {
83 // get the list of options
84 $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 5, strlen($a['Type']) - 7)));
85 if (count($a['List']) == 1) {
86 $a['Type'] = 'checkbox';
87 $a['Value'] = $a['List'][0];
92 elseif (substr($a['Type'],0,5) == 'enum(') {
93 // get the list of options
94 $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 6, strlen($a['Type']) - 8)));
97 elseif (substr($a['Type'],0,10) == 'timestamp(' ||
$a['Type'] == 'datetime') {
98 $a['Type'] = 'timestamp';
100 elseif ($a['Comment'] == 'ip_address') {
101 $a['Type']='ip_address';
104 $this->vars
[$a['Field']] = $a;
106 $this->vars
[$idfield]['desc'] = 'id';
109 // called before creating a new entry
110 private function prepare_new()
113 foreach ($this->vars
as $field => $descr) {
114 $entry[$field] = $descr['Default'];
116 return $this->prepare_edit($entry);
119 // called before editing $entry
120 private function prepare_edit(&$entry)
122 foreach ($this->vars
as $field => $descr) {
123 if ($descr['Type'] == 'set') {
124 // get the list of options selected
125 $selected = explode(',', $entry[$field]);
126 $entry[$field] = array();
127 foreach ($selected as $option)
128 $entry[$field][$option] = 1;
130 if ($descr['Type'] == 'timestamp') {
131 // set readable timestamp
132 $date =& $entry[$field];
133 $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);
135 if ($descr['Type'] == 'date') {
136 $date =& $entry[$field];
137 $date = preg_replace('/([0-9]{4})-?([0-9]{2})-?([0-9]{2})/', '\3/\2/\1', $date);
139 if ($descr['Type'] == 'ip_address') {
140 $ip = & $entry[$field];
147 // set whether the save button show redirect to list view or edit view
148 public function list_on_edit($var)
150 $this->auto_return
= $var;
153 // change display of a field
154 public function describe($name, $desc, $display)
156 $this->vars
[$name]['desc'] = $desc;
157 $this->vars
[$name]['display'] = $display;
160 // add a join table, when deleting a row corresponding entries will be deleted in these tables
161 public function add_join_table($name,$joinid,$joindel,$joinextra="")
164 $this->jtables
[$name] = array("joinid" => $joinid,"joinextra" => $joinextra?
(" AND ".$joinextra):"");
168 public function add_sort_field($key, $desc = false
, $default = false
)
171 $this->sortfield
= $key . ($desc ?
' DESC' : '');
173 $this->sort
[] = $key . ($desc ?
' DESC' : '');
177 // add a where clause to limit table listing
178 public function set_where_clause($whereclause="1")
180 $this->whereclause
= $whereclause;
183 // set an action when trying to delete row
184 public function on_delete($action = NULL
, $message = NULL
)
186 $this->delete_action
= $action;
187 $this->delete_message
= $message;
191 public function apply(PlatalPage
&$page, $action, $id = false
)
193 $page->changeTpl('core/table-editor.tpl');
195 if ($action == 'delete') {
196 S
::assert_xsrf_token();
198 if (!isset($this->delete_action
)) {
199 foreach ($this->jtables
as $table => $j)
200 XDB
::execute("DELETE FROM {$table} WHERE {$j['joinid']} = {?}{$j['joinextra']}", $id);
201 XDB
::execute("DELETE FROM {$this->table} WHERE {$this->idfield} = {?}",$id);
202 $page->trigSuccess("L'entrée ".$id." a été supprimée.");
203 } else if ($this->delete_action
) {
204 XDB
::execute($this->delete_action
, $id);
205 if (isset($this->delete_message
)) {
206 $page->trigSuccess($this->delete_message
);
208 $page->trigSuccess("L'entrée ".$id." a été supprimée.");
211 $page->trigError("Impossible de supprimer l'entrée.");
214 if ($action == 'edit') {
215 $r = XDB
::query("SELECT * FROM {$this->table} WHERE {$this->idfield} = {?} AND {$this->whereclause}",$id);
216 $entry = $r->fetchOneAssoc();
217 $page->assign('entry', $this->prepare_edit($entry));
218 $page->assign('id', $id);
221 if ($action == 'massadd') {
222 $importer = new CSVImporter($this->table
, $this->idfield_editable ?
$this->idfield
: null
);
224 foreach ($this->vars
as $field=>$descr) {
225 if ($this->idfield_editable ||
$field != $this->idfield
) {
227 $importer->describe($field, @$descr['desc']);
230 $page->assign('massadd', true
);
231 $importer->apply($page, $this->pl
. '/massadd', $fields);
234 if ($action == 'new') {
235 if (!$this->idfield_editable
) {
236 $r = XDB
::query("SELECT MAX({$this->idfield})+1 FROM {$this->table}");
237 $page->assign('id', $r->fetchOneCell());
238 $page->assign('entry', $this->prepare_new());
242 if ($action == 'update') {
243 S
::assert_xsrf_token();
247 foreach ($this->vars
as $field => $descr) {
248 if ($values) $values .= ',';
249 if (($field == $this->idfield
) && !$this->idfield_editable
)
250 $val = "'".addslashes($id)."'";
251 elseif ($descr['Type'] == 'set') {
253 if (Post
::has($field)) foreach (Post
::v($field) as $option) {
254 if ($val) $val .= ',';
257 $val = "'".addslashes($val)."'";
258 } elseif ($descr['Type'] == 'checkbox') {
259 $val = Post
::has($field)?
"'".addslashes($descr['Value'])."'":"''";
260 } elseif (Post
::has($field)) {
261 $val = Post
::v($field);
262 if ($descr['Type'] == 'timestamp') {
263 $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);
265 elseif ($descr['Type'] == 'date') {
266 $val = preg_replace('/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})/', '\3-\2-\1', $val);
268 elseif ($descr['Type'] == 'ip_address') {
269 $val = ip2long($val);
271 $val = "'".addslashes($val)."'";
274 $page->trigError("Il manque le champ ".$field);
279 if ($this->idfield_editable
&& ($id != Post
::v($this->idfield
)) && $action != 'new')
280 XDB
::execute("UPDATE {$this->table} SET {$this->idfield} = {?} WHERE {$this->idfield} = {?} AND {$this->whereclause}", Post
::v($this->idfield
), $id);
281 XDB
::execute("REPLACE INTO {$this->table} VALUES ($values)");
283 $page->trigSuccess("L'entrée ".$id." a été mise à jour.");
285 $page->trigSuccess("Une nouvelle entrée a été créée.");
286 $id = XDB
::insertId();
289 $page->trigError("Impossible de mettre à jour.");
290 if (!$this->auto_return
) {
291 return $this->apply($page, 'edit', $id);
294 if ($action == 'sort') {
295 $this->sortfield
= $id;
297 if ($action == 'sortdesc') {
298 $this->sortfield
= $id.' DESC';
301 // user can sort by field by clicking the title of the column
302 if (isset($this->sortfield
)) {
303 // add this sort order after the others (chosen by dev)
304 $this->add_sort_field($this->sortfield
);
305 if (substr($this->sortfield
,-5) == ' DESC') {
306 $this->sortfield
= substr($this->sortfield
,0,-5);
307 $this->sortdesc
= true
;
310 if (count($this->sort
) > 0) {
311 $sort = 'ORDER BY ' . join($this->sort
, ',');
313 $it = XDB
::iterator("SELECT * FROM {$this->table} WHERE {$this->whereclause} $sort");
315 foreach ($this->vars
as $field => $descr)
316 if ($descr['display']) $this->nbfields++
;
317 $page->assign('list', $it);
319 $page->assign('t', $this);
323 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: