Add license headers
[platal.git] / classes / pltableeditor.php
CommitLineData
e7ae7df9 1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
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
22class PLTableEditor {
23 // the plat/al name of the page
24 var $pl;
25 // the table name
26 var $table;
27 // joint tables to delete when deleting an entry
28 var $jtables = array();
2e7b5921 29 // sorting field
30 var $sort = array();
e7ae7df9 31 // the id field
32 var $idfield;
33 // possibility to edit the field
34 var $idfield_editable;
35 // vars
36 var $vars;
37 // number of displayed fields
38 var $nbfields;
a3828787 39 // the field for sorting entries
40 var $sortfield;
386b75ea 41 var $sortdesc = false;
de61dbcf 42 // action to do to delete row:
43 // null => delete effectively, false => no deletion, SQL
44 var $delete_action;
45 var $delete_message;
e7ae7df9 46 /* table editor for platal
47 * $plname : the PLname of the page, ex: admin/payments
48 * $table : the table to edit, ex: profile_medals
49 * $idfield : the field of the table which is the id, ex: id
50 * $editid : is the id editable or not (if not, it is considered as an int)
51 */
1d10d3fd 52 function PLTableEditor($plname, $table, $idfield, $editid=false)
53 {
e7ae7df9 54 $this->pl = $plname;
55 $this->table = $table;
56 $this->idfield = $idfield;
a3828787 57 $this->sortfield = $idfield;
e7ae7df9 58 $this->idfield_editable = $editid;
59 $r = XDB::iterator("SHOW COLUMNS FROM $table");
60 $this->vars = array();
61 while ($a = $r->next()) {
62 // desc will be the title of the column
63 $a['desc'] = $a['Field'];
64 $a['display'] = true;
6682f91d 65
e7ae7df9 66 if (substr($a['Type'],0,8) == 'varchar(') {
67 // limit editing box size
68 $a['Size'] = $a['Maxlength'] = substr($a['Type'], 8, strlen($a['Type']) - 9);
69 if ($a['Size'] > 40) $a['Size'] = 40;
70 // if too big, put a textarea
71 $a['Type'] = ($a['Maxlength']<200)?'varchar':'varchar200';
72 }
73 elseif ($a['Type'] == 'text' || $a['Type'] == 'mediumtext')
74 $a['Type'] = 'textarea';
75 elseif (substr($a['Type'],0,4) == 'set(') {
76 // get the list of options
77 $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 5, strlen($a['Type']) - 7)));
987f1bd2 78 if (count($a['List']) == 1) {
79 $a['Type'] = 'checkbox';
80 $a['Value'] = $a['List'][0];
81 } else {
82 $a['Type'] = 'set';
83 }
e7ae7df9 84 }
85 elseif (substr($a['Type'],0,5) == 'enum(') {
86 // get the list of options
87 $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 6, strlen($a['Type']) - 8)));
88 $a['Type'] = 'enum';
89 }
b9cd8008 90 elseif (substr($a['Type'],0,10) == 'timestamp(' || $a['Type'] == 'datetime') {
e7ae7df9 91 $a['Type'] = 'timestamp';
92 }
93
94 $this->vars[$a['Field']] = $a;
95 }
96 $this->vars[$idfield]['desc'] = 'id';
97 }
1d10d3fd 98 // called before creating a new entry
99 function prepare_new()
100 {
101 $entry = array();
102 foreach ($this->vars as $field => $descr) {
103 $entry[$field] = $descr['Default'];
104 }
105 return $this->prepare_edit($entry);
106 }
e7ae7df9 107 // called before editing $entry
108 function prepare_edit(&$entry) {
109 foreach ($this->vars as $field => $descr) {
110 if ($descr['Type'] == 'set') {
111 // get the list of options selected
112 $selected = explode(',', $entry[$field]);
113 $entry[$field] = array();
114 foreach ($selected as $option)
115 $entry[$field][$option] = 1;
116 }
117 if ($descr['Type'] == 'timestamp') {
118 // set readable timestamp
119 $date =& $entry[$field];
6682f91d 120 $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 121 }
b9cd8008 122 if ($descr['Type'] == 'date') {
123 $date =& $entry[$field];
124 $date = preg_replace('/([0-9]{4})-?([0-9]{2})-?([0-9]{2})/', '\3/\2/\1', $date);
125 }
e7ae7df9 126 }
127 return $entry;
128 }
129 // change display of a field
130 function describe($name, $desc, $display) {
131 $this->vars[$name]['desc'] = $desc;
132 $this->vars[$name]['display'] = $display;
133 }
6682f91d 134 // add a join table, when deleting a row corresponding entries will be deleted in these tables
e7ae7df9 135 function add_join_table($name,$joinid,$joindel,$joinextra="") {
136 if ($joindel)
137 $this->jtables[$name] = array("joinid" => $joinid,"joinextra" => $joinextra?(" AND ".$joinextra):"");
138 }
2e7b5921 139 // add a sort key
de61dbcf 140 function add_sort_field($key, $desc = false, $default = false)
2e7b5921 141 {
de61dbcf 142 if ($default) {
143 $this->sortfield = $key . ($desc ? ' DESC' : '');
144 } else {
145 $this->sort[] = $key . ($desc ? ' DESC' : '');
146 }
147 }
148 // set an action when trying to delete row
149 function on_delete($action = NULL, $message = NULL)
150 {
151 $this->delete_action = $action;
152 $this->delete_message = $message;
2e7b5921 153 }
e7ae7df9 154 // call when done
155 function apply(&$page, $action, $id = false) {
156 $page->changeTpl('table-editor.tpl');
157 $list = true;
158 if ($action == 'delete') {
de61dbcf 159 if (!isset($this->delete_action)) {
160 foreach ($this->jtables as $table => $j)
161 XDB::execute("DELETE FROM {$table} WHERE {$j['joinid']} = {?}{$j['joinextra']}", $id);
162 XDB::execute("DELETE FROM {$this->table} WHERE {$this->idfield} = {?}",$id);
163 $page->trig("L'entrée ".$id." a été supprimée.");
164 } else if ($this->delete_action) {
165 XDB::execute($this->delete_action, $id);
166 if (isset($this->delete_message)) {
167 $page->trig($this->delete_message);
168 } else {
169 $page->trig("L'entrée ".$id." a été supprimée.");
170 }
171 } else {
172 $page->trig("Impossible de supprimer l'entrée.");
173 }
e7ae7df9 174 }
175 if ($action == 'edit') {
176 $r = XDB::query("SELECT * FROM {$this->table} WHERE {$this->idfield} = {?}",$id);
177 $entry = $r->fetchOneAssoc();
178 $page->assign('entry', $this->prepare_edit($entry));
179 $page->assign('id', $id);
180 $list = false;
181 }
182 if ($action == 'new') {
183 if (!$this->idfield_editable) {
184 $r = XDB::query("SELECT MAX({$this->idfield})+1 FROM {$this->table}");
185 $page->assign('id', $r->fetchOneCell());
1d10d3fd 186 $page->assign('entry', $this->prepare_new());
e7ae7df9 187 }
188 $list = false;
189 }
190 if ($action == 'update') {
191 $values = "";
192 $cancel = false;
193 foreach ($this->vars as $field => $descr) {
194 if ($values) $values .= ',';
195 if (($field == $this->idfield) && !$this->idfield_editable)
196 $val = "'".addslashes($id)."'";
197 elseif ($descr['Type'] == 'set') {
198 $val = "";
199 if (Post::has($field)) foreach (Post::v($field) as $option) {
200 if ($val) $val .= ',';
201 $val .= $option;
202 }
203 $val = "'".addslashes($val)."'";
987f1bd2 204 } elseif ($descr['Type'] == 'checkbox') {
205 $val = Post::has($field)?"'".addslashes($descr['Value'])."'":"''";
e7ae7df9 206 } elseif (Post::has($field)) {
6682f91d 207 $val = Post::v($field);
e7ae7df9 208 if ($descr['Type'] == 'timestamp') {
6682f91d 209 $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 210 }
211 elseif ($descr['Type'] == 'date') {
212 $val = preg_replace('/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})/', '\3-\2-\1', $val);
e7ae7df9 213 }
214 $val = "'".addslashes($val)."'";
215 } else {
216 $cancel = true;
6682f91d 217 $page->trig("Il manque le champ ".$field);
e7ae7df9 218 }
219 $values .= $val;
220 }
221 if (!$cancel) {
222 if ($this->idfield_editable && ($id != Post::v($this->idfield)) && $action != 'new')
223 XDB::execute("UPDATE {$this->table} SET {$this->idfield} = {?} WHERE {$this->idfield} = {?}", Post::v($this->idfield), $id);
224 XDB::execute("REPLACE INTO {$this->table} VALUES ($values)");
225 if ($id !== false)
226 $page->trig("L'entrée ".$id." a été mise à jour.");
227 else
228 $page->trig("Une nouvelle entrée a été créée.");
229 } else
230 $page->trig("Impossible de mette à jour.");
231 }
de61dbcf 232 if ($action == 'sort') {
233 $this->sortfield = $id;
234 }
235 if ($action == 'sortdesc') {
236 $this->sortfield = $id.' DESC';
237 }
e7ae7df9 238 if ($list) {
a3828787 239 // user can sort by field by clicking the title of the column
de61dbcf 240 if (isset($this->sortfield)) {
241 // add this sort order after the others (chosen by dev)
242 $this->add_sort_field($this->sortfield);
386b75ea 243 if (substr($this->sortfield,-5) == ' DESC') {
244 $this->sortfield = substr($this->sortfield,0,-5);
245 $this->sortdesc = true;
246 }
a3828787 247 }
2e7b5921 248 if (count($this->sort) > 0) {
249 $sort = 'ORDER BY ' . join($this->sort, ',');
250 }
251 $it = XDB::iterator("SELECT * FROM {$this->table} $sort");
e7ae7df9 252 $this->nbfields = 0;
253 foreach ($this->vars as $field => $descr)
254 if ($descr['display']) $this->nbfields++;
255 $page->assign('list', $it);
256 }
257 $page->assign('t', $this);
258 }
259}
260
261?>