petite icone pour specifier le tri dans le table-editor
[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 */
52 function PLTableEditor($plname, $table, $idfield, $editid=false) {
53 $this->pl = $plname;
54 $this->table = $table;
55 $this->idfield = $idfield;
a3828787 56 $this->sortfield = $idfield;
e7ae7df9 57 $this->idfield_editable = $editid;
58 $r = XDB::iterator("SHOW COLUMNS FROM $table");
59 $this->vars = array();
60 while ($a = $r->next()) {
61 // desc will be the title of the column
62 $a['desc'] = $a['Field'];
63 $a['display'] = true;
64
65 if (substr($a['Type'],0,8) == 'varchar(') {
66 // limit editing box size
67 $a['Size'] = $a['Maxlength'] = substr($a['Type'], 8, strlen($a['Type']) - 9);
68 if ($a['Size'] > 40) $a['Size'] = 40;
69 // if too big, put a textarea
70 $a['Type'] = ($a['Maxlength']<200)?'varchar':'varchar200';
71 }
72 elseif ($a['Type'] == 'text' || $a['Type'] == 'mediumtext')
73 $a['Type'] = 'textarea';
74 elseif (substr($a['Type'],0,4) == 'set(') {
75 // get the list of options
76 $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 5, strlen($a['Type']) - 7)));
77 $a['Type'] = 'set';
78 }
79 elseif (substr($a['Type'],0,5) == 'enum(') {
80 // get the list of options
81 $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 6, strlen($a['Type']) - 8)));
82 $a['Type'] = 'enum';
83 }
b9cd8008 84 elseif (substr($a['Type'],0,10) == 'timestamp(' || $a['Type'] == 'datetime') {
e7ae7df9 85 $a['Type'] = 'timestamp';
86 }
87
88 $this->vars[$a['Field']] = $a;
89 }
90 $this->vars[$idfield]['desc'] = 'id';
91 }
92 // called before editing $entry
93 function prepare_edit(&$entry) {
94 foreach ($this->vars as $field => $descr) {
95 if ($descr['Type'] == 'set') {
96 // get the list of options selected
97 $selected = explode(',', $entry[$field]);
98 $entry[$field] = array();
99 foreach ($selected as $option)
100 $entry[$field][$option] = 1;
101 }
102 if ($descr['Type'] == 'timestamp') {
103 // set readable timestamp
104 $date =& $entry[$field];
105 $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);
106 }
b9cd8008 107 if ($descr['Type'] == 'date') {
108 $date =& $entry[$field];
109 $date = preg_replace('/([0-9]{4})-?([0-9]{2})-?([0-9]{2})/', '\3/\2/\1', $date);
110 }
e7ae7df9 111 }
112 return $entry;
113 }
114 // change display of a field
115 function describe($name, $desc, $display) {
116 $this->vars[$name]['desc'] = $desc;
117 $this->vars[$name]['display'] = $display;
118 }
119 // add a join table, when deleting a row corresponding entries will be deleted in these tables
120 function add_join_table($name,$joinid,$joindel,$joinextra="") {
121 if ($joindel)
122 $this->jtables[$name] = array("joinid" => $joinid,"joinextra" => $joinextra?(" AND ".$joinextra):"");
123 }
2e7b5921 124 // add a sort key
de61dbcf 125 function add_sort_field($key, $desc = false, $default = false)
2e7b5921 126 {
de61dbcf 127 if ($default) {
128 $this->sortfield = $key . ($desc ? ' DESC' : '');
129 } else {
130 $this->sort[] = $key . ($desc ? ' DESC' : '');
131 }
132 }
133 // set an action when trying to delete row
134 function on_delete($action = NULL, $message = NULL)
135 {
136 $this->delete_action = $action;
137 $this->delete_message = $message;
2e7b5921 138 }
e7ae7df9 139 // call when done
140 function apply(&$page, $action, $id = false) {
141 $page->changeTpl('table-editor.tpl');
142 $list = true;
143 if ($action == 'delete') {
de61dbcf 144 if (!isset($this->delete_action)) {
145 foreach ($this->jtables as $table => $j)
146 XDB::execute("DELETE FROM {$table} WHERE {$j['joinid']} = {?}{$j['joinextra']}", $id);
147 XDB::execute("DELETE FROM {$this->table} WHERE {$this->idfield} = {?}",$id);
148 $page->trig("L'entrée ".$id." a été supprimée.");
149 } else if ($this->delete_action) {
150 XDB::execute($this->delete_action, $id);
151 if (isset($this->delete_message)) {
152 $page->trig($this->delete_message);
153 } else {
154 $page->trig("L'entrée ".$id." a été supprimée.");
155 }
156 } else {
157 $page->trig("Impossible de supprimer l'entrée.");
158 }
e7ae7df9 159 }
160 if ($action == 'edit') {
161 $r = XDB::query("SELECT * FROM {$this->table} WHERE {$this->idfield} = {?}",$id);
162 $entry = $r->fetchOneAssoc();
163 $page->assign('entry', $this->prepare_edit($entry));
164 $page->assign('id', $id);
165 $list = false;
166 }
167 if ($action == 'new') {
168 if (!$this->idfield_editable) {
169 $r = XDB::query("SELECT MAX({$this->idfield})+1 FROM {$this->table}");
170 $page->assign('id', $r->fetchOneCell());
171 }
172 $list = false;
173 }
174 if ($action == 'update') {
175 $values = "";
176 $cancel = false;
177 foreach ($this->vars as $field => $descr) {
178 if ($values) $values .= ',';
179 if (($field == $this->idfield) && !$this->idfield_editable)
180 $val = "'".addslashes($id)."'";
181 elseif ($descr['Type'] == 'set') {
182 $val = "";
183 if (Post::has($field)) foreach (Post::v($field) as $option) {
184 if ($val) $val .= ',';
185 $val .= $option;
186 }
187 $val = "'".addslashes($val)."'";
188 } elseif (Post::has($field)) {
189 $val = Post::v($field);
190 if ($descr['Type'] == 'timestamp') {
b9cd8008 191 $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);
192 }
193 elseif ($descr['Type'] == 'date') {
194 $val = preg_replace('/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})/', '\3-\2-\1', $val);
e7ae7df9 195 }
196 $val = "'".addslashes($val)."'";
197 } else {
198 $cancel = true;
199 $page->trig("Il manque le champ ".$field);
200 }
201 $values .= $val;
202 }
203 if (!$cancel) {
204 if ($this->idfield_editable && ($id != Post::v($this->idfield)) && $action != 'new')
205 XDB::execute("UPDATE {$this->table} SET {$this->idfield} = {?} WHERE {$this->idfield} = {?}", Post::v($this->idfield), $id);
206 XDB::execute("REPLACE INTO {$this->table} VALUES ($values)");
207 if ($id !== false)
208 $page->trig("L'entrée ".$id." a été mise à jour.");
209 else
210 $page->trig("Une nouvelle entrée a été créée.");
211 } else
212 $page->trig("Impossible de mette à jour.");
213 }
de61dbcf 214 if ($action == 'sort') {
215 $this->sortfield = $id;
216 }
217 if ($action == 'sortdesc') {
218 $this->sortfield = $id.' DESC';
219 }
e7ae7df9 220 if ($list) {
a3828787 221 // user can sort by field by clicking the title of the column
de61dbcf 222 if (isset($this->sortfield)) {
223 // add this sort order after the others (chosen by dev)
224 $this->add_sort_field($this->sortfield);
386b75ea 225 if (substr($this->sortfield,-5) == ' DESC') {
226 $this->sortfield = substr($this->sortfield,0,-5);
227 $this->sortdesc = true;
228 }
a3828787 229 }
2e7b5921 230 if (count($this->sort) > 0) {
231 $sort = 'ORDER BY ' . join($this->sort, ',');
232 }
233 $it = XDB::iterator("SELECT * FROM {$this->table} $sort");
e7ae7df9 234 $this->nbfields = 0;
235 foreach ($this->vars as $field => $descr)
236 if ($descr['display']) $this->nbfields++;
237 $page->assign('list', $it);
238 }
239 $page->assign('t', $this);
240 }
241}
242
243?>