Integrates external readers to the rss feeds and the iCal calendars.
[platal.git] / classes / pltableeditor.php
CommitLineData
e7ae7df9 1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 Polytechnique.org *
e7ae7df9 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
eaf30d86 22class PLTableEditor
2b1ee50b 23{
e7ae7df9 24 // the plat/al name of the page
2b1ee50b 25 public $pl;
e7ae7df9 26 // the table name
2b1ee50b 27 public $table;
e7ae7df9 28 // joint tables to delete when deleting an entry
2b1ee50b 29 public $jtables = array();
2e7b5921 30 // sorting field
2b1ee50b 31 public $sort = array();
e7ae7df9 32 // the id field
2b1ee50b 33 public $idfield;
e7ae7df9 34 // possibility to edit the field
2b1ee50b 35 public $idfield_editable;
e7ae7df9 36 // vars
2b1ee50b 37 public $vars;
e7ae7df9 38 // number of displayed fields
2b1ee50b 39 public $nbfields;
c57685e7
PC
40 // a where clause to restrict table
41 public $whereclause;
a3828787 42 // the field for sorting entries
2b1ee50b 43 public $sortfield;
44 public $sortdesc = false;
de61dbcf 45 // action to do to delete row:
2b1ee50b 46 // null => delete effectively, false => no deletion, SQL
47 public $delete_action;
48 public $delete_message;
3851b0a6 49 // Should "Save" button return to the list view
2b1ee50b 50 public $auto_return = true;
3851b0a6 51
e7ae7df9 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)
57 */
2b1ee50b 58 public function __construct($plname, $table, $idfield, $editid=false)
1d10d3fd 59 {
e7ae7df9 60 $this->pl = $plname;
61 $this->table = $table;
62 $this->idfield = $idfield;
a3828787 63 $this->sortfield = $idfield;
e7ae7df9 64 $this->idfield_editable = $editid;
c57685e7 65 $this->whereclause = '1';
e7ae7df9 66 $r = XDB::iterator("SHOW 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'];
71 $a['display'] = true;
6682f91d 72
e7ae7df9 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';
79 }
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
a7de4ef7 84 $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 5, strlen($a['Type']) - 7)));
987f1bd2 85 if (count($a['List']) == 1) {
86 $a['Type'] = 'checkbox';
87 $a['Value'] = $a['List'][0];
88 } else {
89 $a['Type'] = 'set';
eaf30d86 90 }
e7ae7df9 91 }
92 elseif (substr($a['Type'],0,5) == 'enum(') {
93 // get the list of options
a7de4ef7 94 $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 6, strlen($a['Type']) - 8)));
e7ae7df9 95 $a['Type'] = 'enum';
96 }
b9cd8008 97 elseif (substr($a['Type'],0,10) == 'timestamp(' || $a['Type'] == 'datetime') {
e7ae7df9 98 $a['Type'] = 'timestamp';
99 }
100
101 $this->vars[$a['Field']] = $a;
102 }
103 $this->vars[$idfield]['desc'] = 'id';
104 }
2b1ee50b 105
1d10d3fd 106 // called before creating a new entry
2b1ee50b 107 private function prepare_new()
1d10d3fd 108 {
109 $entry = array();
110 foreach ($this->vars as $field => $descr) {
111 $entry[$field] = $descr['Default'];
112 }
113 return $this->prepare_edit($entry);
114 }
2b1ee50b 115
e7ae7df9 116 // called before editing $entry
eaf30d86 117 private function prepare_edit(&$entry)
2b1ee50b 118 {
e7ae7df9 119 foreach ($this->vars as $field => $descr) {
120 if ($descr['Type'] == 'set') {
121 // get the list of options selected
122 $selected = explode(',', $entry[$field]);
123 $entry[$field] = array();
124 foreach ($selected as $option)
125 $entry[$field][$option] = 1;
126 }
127 if ($descr['Type'] == 'timestamp') {
128 // set readable timestamp
129 $date =& $entry[$field];
6682f91d 130 $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);
e7ae7df9 131 }
b9cd8008 132 if ($descr['Type'] == 'date') {
133 $date =& $entry[$field];
134 $date = preg_replace('/([0-9]{4})-?([0-9]{2})-?([0-9]{2})/', '\3/\2/\1', $date);
135 }
e7ae7df9 136 }
137 return $entry;
138 }
eaf30d86 139
3851b0a6 140 // set whether the save button show redirect to list view or edit view
2b1ee50b 141 public function list_on_edit($var)
3851b0a6 142 {
143 $this->auto_return = $var;
144 }
2b1ee50b 145
e7ae7df9 146 // change display of a field
2b1ee50b 147 public function describe($name, $desc, $display)
148 {
e7ae7df9 149 $this->vars[$name]['desc'] = $desc;
150 $this->vars[$name]['display'] = $display;
151 }
eaf30d86 152
6682f91d 153 // add a join table, when deleting a row corresponding entries will be deleted in these tables
2b1ee50b 154 public function add_join_table($name,$joinid,$joindel,$joinextra="")
155 {
e7ae7df9 156 if ($joindel)
157 $this->jtables[$name] = array("joinid" => $joinid,"joinextra" => $joinextra?(" AND ".$joinextra):"");
158 }
eaf30d86 159
2e7b5921 160 // add a sort key
2b1ee50b 161 public function add_sort_field($key, $desc = false, $default = false)
2e7b5921 162 {
2b1ee50b 163 if ($default) {
164 $this->sortfield = $key . ($desc ? ' DESC' : '');
165 } else {
166 $this->sort[] = $key . ($desc ? ' DESC' : '');
de61dbcf 167 }
168 }
2b1ee50b 169
c57685e7
PC
170 // add a where clause to limit table listing
171 public function set_where_clause($whereclause="1")
172 {
173 $this->whereclause = $whereclause;
174 }
175
de61dbcf 176 // set an action when trying to delete row
2b1ee50b 177 public function on_delete($action = NULL, $message = NULL)
de61dbcf 178 {
2b1ee50b 179 $this->delete_action = $action;
180 $this->delete_message = $message;
2e7b5921 181 }
2b1ee50b 182
e7ae7df9 183 // call when done
2b1ee50b 184 public function apply(PlatalPage &$page, $action, $id = false)
185 {
8b1f8e12 186 $page->changeTpl('core/table-editor.tpl');
e7ae7df9 187 $list = true;
188 if ($action == 'delete') {
2b1ee50b 189 if (!isset($this->delete_action)) {
190 foreach ($this->jtables as $table => $j)
191 XDB::execute("DELETE FROM {$table} WHERE {$j['joinid']} = {?}{$j['joinextra']}", $id);
192 XDB::execute("DELETE FROM {$this->table} WHERE {$this->idfield} = {?}",$id);
193 $page->trig("L'entrée ".$id." a été supprimée.");
194 } else if ($this->delete_action) {
195 XDB::execute($this->delete_action, $id);
196 if (isset($this->delete_message)) {
197 $page->trig($this->delete_message);
198 } else {
199 $page->trig("L'entrée ".$id." a été supprimée.");
200 }
201 } else {
202 $page->trig("Impossible de supprimer l'entrée.");
203 }
e7ae7df9 204 }
205 if ($action == 'edit') {
c57685e7 206 $r = XDB::query("SELECT * FROM {$this->table} WHERE {$this->idfield} = {?} AND {$this->whereclause}",$id);
e7ae7df9 207 $entry = $r->fetchOneAssoc();
208 $page->assign('entry', $this->prepare_edit($entry));
209 $page->assign('id', $id);
210 $list = false;
211 }
a6613b2e 212 if ($action == 'massadd') {
213 $importer = new CSVImporter($this->table, $this->idfield_editable ? $this->idfield : null);
214 $fields = array();
215 foreach ($this->vars as $field=>$descr) {
216 if ($this->idfield_editable || $field != $this->idfield) {
217 $fields[] = $field;
218 $importer->describe($field, @$descr['desc']);
219 }
220 }
221 $page->assign('massadd', true);
222 $importer->apply($page, $this->pl . '/massadd', $fields);
223 $list = false;
224 }
e7ae7df9 225 if ($action == 'new') {
226 if (!$this->idfield_editable) {
227 $r = XDB::query("SELECT MAX({$this->idfield})+1 FROM {$this->table}");
228 $page->assign('id', $r->fetchOneCell());
1d10d3fd 229 $page->assign('entry', $this->prepare_new());
e7ae7df9 230 }
231 $list = false;
232 }
233 if ($action == 'update') {
234 $values = "";
235 $cancel = false;
236 foreach ($this->vars as $field => $descr) {
237 if ($values) $values .= ',';
238 if (($field == $this->idfield) && !$this->idfield_editable)
239 $val = "'".addslashes($id)."'";
240 elseif ($descr['Type'] == 'set') {
241 $val = "";
242 if (Post::has($field)) foreach (Post::v($field) as $option) {
243 if ($val) $val .= ',';
244 $val .= $option;
245 }
246 $val = "'".addslashes($val)."'";
987f1bd2 247 } elseif ($descr['Type'] == 'checkbox') {
248 $val = Post::has($field)?"'".addslashes($descr['Value'])."'":"''";
e7ae7df9 249 } elseif (Post::has($field)) {
6682f91d 250 $val = Post::v($field);
e7ae7df9 251 if ($descr['Type'] == 'timestamp') {
6682f91d 252 $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);
b9cd8008 253 }
254 elseif ($descr['Type'] == 'date') {
255 $val = preg_replace('/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})/', '\3-\2-\1', $val);
e7ae7df9 256 }
257 $val = "'".addslashes($val)."'";
258 } else {
259 $cancel = true;
6682f91d 260 $page->trig("Il manque le champ ".$field);
e7ae7df9 261 }
262 $values .= $val;
263 }
264 if (!$cancel) {
265 if ($this->idfield_editable && ($id != Post::v($this->idfield)) && $action != 'new')
c57685e7 266 XDB::execute("UPDATE {$this->table} SET {$this->idfield} = {?} WHERE {$this->idfield} = {?} AND {$this->whereclause}", Post::v($this->idfield), $id);
e7ae7df9 267 XDB::execute("REPLACE INTO {$this->table} VALUES ($values)");
268 if ($id !== false)
a7de4ef7 269 $page->trig("L'entrée ".$id." a été mise à jour.");
3851b0a6 270 else {
a7de4ef7 271 $page->trig("Une nouvelle entrée a été créée.");
3851b0a6 272 $id = XDB::insertId();
273 }
e7ae7df9 274 } else
a7de4ef7 275 $page->trig("Impossible de mette à jour.");
3851b0a6 276 if (!$this->auto_return) {
277 return $this->apply($page, 'edit', $id);
278 }
e7ae7df9 279 }
de61dbcf 280 if ($action == 'sort') {
2b1ee50b 281 $this->sortfield = $id;
de61dbcf 282 }
283 if ($action == 'sortdesc') {
2b1ee50b 284 $this->sortfield = $id.' DESC';
de61dbcf 285 }
e7ae7df9 286 if ($list) {
a3828787 287 // user can sort by field by clicking the title of the column
de61dbcf 288 if (isset($this->sortfield)) {
289 // add this sort order after the others (chosen by dev)
290 $this->add_sort_field($this->sortfield);
386b75ea 291 if (substr($this->sortfield,-5) == ' DESC') {
292 $this->sortfield = substr($this->sortfield,0,-5);
293 $this->sortdesc = true;
294 }
a3828787 295 }
2e7b5921 296 if (count($this->sort) > 0) {
297 $sort = 'ORDER BY ' . join($this->sort, ',');
298 }
c57685e7 299 $it = XDB::iterator("SELECT * FROM {$this->table} WHERE {$this->whereclause} $sort");
e7ae7df9 300 $this->nbfields = 0;
301 foreach ($this->vars as $field => $descr)
302 if ($descr['display']) $this->nbfields++;
303 $page->assign('list', $it);
304 }
305 $page->assign('t', $this);
306 }
307}
308
a7de4ef7 309// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
e7ae7df9 310?>