nouveau table-editor (exit celui de diogenes), migration des dernieres pages admin
[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();
29 // the id field
30 var $idfield;
31 // possibility to edit the field
32 var $idfield_editable;
33 // vars
34 var $vars;
35 // number of displayed fields
36 var $nbfields;
37 /* table editor for platal
38 * $plname : the PLname of the page, ex: admin/payments
39 * $table : the table to edit, ex: profile_medals
40 * $idfield : the field of the table which is the id, ex: id
41 * $editid : is the id editable or not (if not, it is considered as an int)
42 */
43 function PLTableEditor($plname, $table, $idfield, $editid=false) {
44 $this->pl = $plname;
45 $this->table = $table;
46 $this->idfield = $idfield;
47 $this->idfield_editable = $editid;
48 $r = XDB::iterator("SHOW COLUMNS FROM $table");
49 $this->vars = array();
50 while ($a = $r->next()) {
51 // desc will be the title of the column
52 $a['desc'] = $a['Field'];
53 $a['display'] = true;
54
55 if (substr($a['Type'],0,8) == 'varchar(') {
56 // limit editing box size
57 $a['Size'] = $a['Maxlength'] = substr($a['Type'], 8, strlen($a['Type']) - 9);
58 if ($a['Size'] > 40) $a['Size'] = 40;
59 // if too big, put a textarea
60 $a['Type'] = ($a['Maxlength']<200)?'varchar':'varchar200';
61 }
62 elseif ($a['Type'] == 'text' || $a['Type'] == 'mediumtext')
63 $a['Type'] = 'textarea';
64 elseif (substr($a['Type'],0,4) == 'set(') {
65 // get the list of options
66 $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 5, strlen($a['Type']) - 7)));
67 $a['Type'] = 'set';
68 }
69 elseif (substr($a['Type'],0,5) == 'enum(') {
70 // get the list of options
71 $a['List'] = explode('§',str_replace("','","§",substr($a['Type'], 6, strlen($a['Type']) - 8)));
72 $a['Type'] = 'enum';
73 }
74 elseif (substr($a['Type'],0,10) == 'timestamp(') {
75 $a['Type'] = 'timestamp';
76 }
77
78 $this->vars[$a['Field']] = $a;
79 }
80 $this->vars[$idfield]['desc'] = 'id';
81 }
82 // called before editing $entry
83 function prepare_edit(&$entry) {
84 foreach ($this->vars as $field => $descr) {
85 if ($descr['Type'] == 'set') {
86 // get the list of options selected
87 $selected = explode(',', $entry[$field]);
88 $entry[$field] = array();
89 foreach ($selected as $option)
90 $entry[$field][$option] = 1;
91 }
92 if ($descr['Type'] == 'timestamp') {
93 // set readable timestamp
94 $date =& $entry[$field];
95 $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);
96 }
97 }
98 return $entry;
99 }
100 // change display of a field
101 function describe($name, $desc, $display) {
102 $this->vars[$name]['desc'] = $desc;
103 $this->vars[$name]['display'] = $display;
104 }
105 // add a join table, when deleting a row corresponding entries will be deleted in these tables
106 function add_join_table($name,$joinid,$joindel,$joinextra="") {
107 if ($joindel)
108 $this->jtables[$name] = array("joinid" => $joinid,"joinextra" => $joinextra?(" AND ".$joinextra):"");
109 }
110 // call when done
111 function apply(&$page, $action, $id = false) {
112 $page->changeTpl('table-editor.tpl');
113 $list = true;
114 if ($action == 'delete') {
115 foreach ($this->jtables as $table => $j)
116 XDB::execute("DELETE FROM {$table} WHERE {$j['joinid']} = {?}{$j['joinextra']}", $id);
117 XDB::execute("DELETE FROM {$this->table} WHERE {$this->idfield} = {?}",$id);
118 $page->trig("L'entrée ".$id." a été supprimée.");
119 }
120 if ($action == 'edit') {
121 $r = XDB::query("SELECT * FROM {$this->table} WHERE {$this->idfield} = {?}",$id);
122 $entry = $r->fetchOneAssoc();
123 $page->assign('entry', $this->prepare_edit($entry));
124 $page->assign('id', $id);
125 $list = false;
126 }
127 if ($action == 'new') {
128 if (!$this->idfield_editable) {
129 $r = XDB::query("SELECT MAX({$this->idfield})+1 FROM {$this->table}");
130 $page->assign('id', $r->fetchOneCell());
131 }
132 $list = false;
133 }
134 if ($action == 'update') {
135 $values = "";
136 $cancel = false;
137 foreach ($this->vars as $field => $descr) {
138 if ($values) $values .= ',';
139 if (($field == $this->idfield) && !$this->idfield_editable)
140 $val = "'".addslashes($id)."'";
141 elseif ($descr['Type'] == 'set') {
142 $val = "";
143 if (Post::has($field)) foreach (Post::v($field) as $option) {
144 if ($val) $val .= ',';
145 $val .= $option;
146 }
147 $val = "'".addslashes($val)."'";
148 } elseif (Post::has($field)) {
149 $val = Post::v($field);
150 if ($descr['Type'] == 'timestamp') {
151 $val = preg_replace('/([0-9]{2})\/([0-9]{2})\/([0-9]{4}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/', '\3\2\1\4\5\6', $val);
152 }
153 $val = "'".addslashes($val)."'";
154 } else {
155 $cancel = true;
156 $page->trig("Il manque le champ ".$field);
157 }
158 $values .= $val;
159 }
160 if (!$cancel) {
161 if ($this->idfield_editable && ($id != Post::v($this->idfield)) && $action != 'new')
162 XDB::execute("UPDATE {$this->table} SET {$this->idfield} = {?} WHERE {$this->idfield} = {?}", Post::v($this->idfield), $id);
163 XDB::execute("REPLACE INTO {$this->table} VALUES ($values)");
164 if ($id !== false)
165 $page->trig("L'entrée ".$id." a été mise à jour.");
166 else
167 $page->trig("Une nouvelle entrée a été créée.");
168 } else
169 $page->trig("Impossible de mette à jour.");
170 }
171 if ($list) {
172 $it = XDB::iterator("SELECT * FROM {$this->table}");
173 $this->nbfields = 0;
174 foreach ($this->vars as $field => $descr)
175 if ($descr['display']) $this->nbfields++;
176 $page->assign('list', $it);
177 }
178 $page->assign('t', $this);
179 }
180}
181
182?>