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 ***************************************************************************/
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 // the field for sorting entries
42 public $sortdesc = false
;
43 // action to do to delete row:
44 // null => delete effectively, false => no deletion, SQL
45 public $delete_action;
46 public $delete_message;
47 // Should "Save" button return to the list view
48 public $auto_return = true
;
50 /* table editor for platal
51 * $plname : the PLname of the page, ex: admin/payments
52 * $table : the table to edit, ex: profile_medals
53 * $idfield : the field of the table which is the id, ex: id
54 * $editid : is the id editable or not (if not, it is considered as an int)
56 public function __construct($plname, $table, $idfield, $editid=false
)
59 $this->table
= $table;
60 $this->idfield
= $idfield;
61 $this->sortfield
= $idfield;
62 $this->idfield_editable
= $editid;
63 $r = XDB
::iterator("SHOW COLUMNS FROM $table");
64 $this->vars
= array();
65 while ($a = $r->next()) {
66 // desc will be the title of the column
67 $a['desc'] = $a['Field'];
70 if (substr($a['Type'],0,8) == 'varchar(') {
71 // limit editing box size
72 $a['Size'] = $a['Maxlength'] = substr($a['Type'], 8, strlen($a['Type']) - 9);
73 if ($a['Size'] > 40) $a['Size'] = 40;
74 // if too big, put a textarea
75 $a['Type'] = ($a['Maxlength']<200)?
'varchar':'varchar200';
77 elseif ($a['Type'] == 'text' ||
$a['Type'] == 'mediumtext')
78 $a['Type'] = 'textarea';
79 elseif (substr($a['Type'],0,4) == 'set(') {
80 // get the list of options
81 $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 5, strlen($a['Type']) - 7)));
82 if (count($a['List']) == 1) {
83 $a['Type'] = 'checkbox';
84 $a['Value'] = $a['List'][0];
89 elseif (substr($a['Type'],0,5) == 'enum(') {
90 // get the list of options
91 $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 6, strlen($a['Type']) - 8)));
94 elseif (substr($a['Type'],0,10) == 'timestamp(' ||
$a['Type'] == 'datetime') {
95 $a['Type'] = 'timestamp';
98 $this->vars
[$a['Field']] = $a;
100 $this->vars
[$idfield]['desc'] = 'id';
103 // called before creating a new entry
104 private function prepare_new()
107 foreach ($this->vars
as $field => $descr) {
108 $entry[$field] = $descr['Default'];
110 return $this->prepare_edit($entry);
113 // called before editing $entry
114 private function prepare_edit(&$entry)
116 foreach ($this->vars
as $field => $descr) {
117 if ($descr['Type'] == 'set') {
118 // get the list of options selected
119 $selected = explode(',', $entry[$field]);
120 $entry[$field] = array();
121 foreach ($selected as $option)
122 $entry[$field][$option] = 1;
124 if ($descr['Type'] == 'timestamp') {
125 // set readable timestamp
126 $date =& $entry[$field];
127 $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);
129 if ($descr['Type'] == 'date') {
130 $date =& $entry[$field];
131 $date = preg_replace('/([0-9]{4})-?([0-9]{2})-?([0-9]{2})/', '\3/\2/\1', $date);
137 // set whether the save button show redirect to list view or edit view
138 public function list_on_edit($var)
140 $this->auto_return
= $var;
143 // change display of a field
144 public function describe($name, $desc, $display)
146 $this->vars
[$name]['desc'] = $desc;
147 $this->vars
[$name]['display'] = $display;
150 // add a join table, when deleting a row corresponding entries will be deleted in these tables
151 public function add_join_table($name,$joinid,$joindel,$joinextra="")
154 $this->jtables
[$name] = array("joinid" => $joinid,"joinextra" => $joinextra?
(" AND ".$joinextra):"");
158 public function add_sort_field($key, $desc = false
, $default = false
)
161 $this->sortfield
= $key . ($desc ?
' DESC' : '');
163 $this->sort
[] = $key . ($desc ?
' DESC' : '');
167 // set an action when trying to delete row
168 public function on_delete($action = NULL
, $message = NULL
)
170 $this->delete_action
= $action;
171 $this->delete_message
= $message;
175 public function apply(PlatalPage
&$page, $action, $id = false
)
177 $page->changeTpl('core/table-editor.tpl');
179 if ($action == 'delete') {
180 if (!isset($this->delete_action
)) {
181 foreach ($this->jtables
as $table => $j)
182 XDB
::execute("DELETE FROM {$table} WHERE {$j['joinid']} = {?}{$j['joinextra']}", $id);
183 XDB
::execute("DELETE FROM {$this->table} WHERE {$this->idfield} = {?}",$id);
184 $page->trig("L'entrée ".$id." a été supprimée.");
185 } else if ($this->delete_action
) {
186 XDB
::execute($this->delete_action
, $id);
187 if (isset($this->delete_message
)) {
188 $page->trig($this->delete_message
);
190 $page->trig("L'entrée ".$id." a été supprimée.");
193 $page->trig("Impossible de supprimer l'entrée.");
196 if ($action == 'edit') {
197 $r = XDB
::query("SELECT * FROM {$this->table} WHERE {$this->idfield} = {?}",$id);
198 $entry = $r->fetchOneAssoc();
199 $page->assign('entry', $this->prepare_edit($entry));
200 $page->assign('id', $id);
203 if ($action == 'massadd') {
204 $importer = new CSVImporter($this->table
, $this->idfield_editable ?
$this->idfield
: null
);
206 foreach ($this->vars
as $field=>$descr) {
207 if ($this->idfield_editable ||
$field != $this->idfield
) {
209 $importer->describe($field, @$descr['desc']);
212 $page->assign('massadd', true
);
213 $importer->apply($page, $this->pl
. '/massadd', $fields);
216 if ($action == 'new') {
217 if (!$this->idfield_editable
) {
218 $r = XDB
::query("SELECT MAX({$this->idfield})+1 FROM {$this->table}");
219 $page->assign('id', $r->fetchOneCell());
220 $page->assign('entry', $this->prepare_new());
224 if ($action == 'update') {
227 foreach ($this->vars
as $field => $descr) {
228 if ($values) $values .= ',';
229 if (($field == $this->idfield
) && !$this->idfield_editable
)
230 $val = "'".addslashes($id)."'";
231 elseif ($descr['Type'] == 'set') {
233 if (Post
::has($field)) foreach (Post
::v($field) as $option) {
234 if ($val) $val .= ',';
237 $val = "'".addslashes($val)."'";
238 } elseif ($descr['Type'] == 'checkbox') {
239 $val = Post
::has($field)?
"'".addslashes($descr['Value'])."'":"''";
240 } elseif (Post
::has($field)) {
241 $val = Post
::v($field);
242 if ($descr['Type'] == 'timestamp') {
243 $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);
245 elseif ($descr['Type'] == 'date') {
246 $val = preg_replace('/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})/', '\3-\2-\1', $val);
248 $val = "'".addslashes($val)."'";
251 $page->trig("Il manque le champ ".$field);
256 if ($this->idfield_editable
&& ($id != Post
::v($this->idfield
)) && $action != 'new')
257 XDB
::execute("UPDATE {$this->table} SET {$this->idfield} = {?} WHERE {$this->idfield} = {?}", Post
::v($this->idfield
), $id);
258 XDB
::execute("REPLACE INTO {$this->table} VALUES ($values)");
260 $page->trig("L'entrée ".$id." a été mise à jour.");
262 $page->trig("Une nouvelle entrée a été créée.");
263 $id = XDB
::insertId();
266 $page->trig("Impossible de mette à jour.");
267 if (!$this->auto_return
) {
268 return $this->apply($page, 'edit', $id);
271 if ($action == 'sort') {
272 $this->sortfield
= $id;
274 if ($action == 'sortdesc') {
275 $this->sortfield
= $id.' DESC';
278 // user can sort by field by clicking the title of the column
279 if (isset($this->sortfield
)) {
280 // add this sort order after the others (chosen by dev)
281 $this->add_sort_field($this->sortfield
);
282 if (substr($this->sortfield
,-5) == ' DESC') {
283 $this->sortfield
= substr($this->sortfield
,0,-5);
284 $this->sortdesc
= true
;
287 if (count($this->sort
) > 0) {
288 $sort = 'ORDER BY ' . join($this->sort
, ',');
290 $it = XDB
::iterator("SELECT * FROM {$this->table} $sort");
292 foreach ($this->vars
as $field => $descr)
293 if ($descr['display']) $this->nbfields++
;
294 $page->assign('list', $it);
296 $page->assign('t', $this);
300 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: