[SPIP] +2.1.12
[velocampus/web/www.git] / www / plugins / auto / cfg / depots / table.php
1 <?php
2
3 /**
4 * Plugin générique de configuration pour SPIP
5 *
6 * @license GNU/GPL
7 * @package plugins
8 * @subpackage cfg
9 * @category outils
10 * @copyright (c) toggg, marcimat 2007-2008
11 * @link http://www.spip-contrib.net/
12 * @version $Id: table.php 36744 2010-03-29 02:31:19Z gilles.vincent@gmail.com $
13 */
14
15 if (!defined("_ECRIRE_INC_VERSION")) return;
16
17 /**
18 * Retrouve et met a jour les colonnes d'une table.
19 * ici, cfg_id est obligatoire ...
20 *
21 * @package plugins
22 * @subpackage cfg
23 */
24 class cfg_depot_table
25 {
26 /**
27 * Les champs manipulés
28 * @var Array
29 */
30 var $champs = array();
31
32 /**
33 * Si on passe par cfg_id, ça fait..
34 * Heu.. Quelque chose d'utile ?
35 * @var Array
36 */
37 var $champs_id = array();
38
39 /**
40 * Les valeurs en dépôt
41 * @var Array
42 */
43 var $val = array();
44
45 /**
46 * Les différents paramètres : Tables, Colonnes, cfg_id, et Casier
47 * @var Array
48 */
49 var $param = array();
50
51 /**
52 * Pour gestion de l'affichage en succès ou échec
53 * @var Array
54 */
55 var $messages = array('message_ok'=>array(), 'message_erreur'=>array(), 'erreurs'=>array());
56
57 /**
58 * Arbre
59 * @var Array
60 */
61 var $_arbre = array();
62
63 /**
64 * Le WHERE permettant de retrouver l'Arbre
65 * @var Array
66 */
67 var $_id = array();
68
69 /**
70 * Base de l'arbre
71 * @var Array
72 */
73 var $_base = null;
74
75 /**
76 * Où on est dans l'arbre $this->_arbre
77 * @var &Array
78 */
79 var $_ici = null;
80
81 /**
82 * version du depot
83 * @var int
84 */
85 var $version = 2;
86
87 /**
88 * Dépôt dans les attributs de la classe
89 *
90 * @param Array $params
91 */
92 function cfg_depot_table($params=array()){
93 foreach ($params as $o=>$v) {
94 $this->$o = $v;
95 }
96 }
97
98 /**
99 * charge la base (racine) et le point de l'arbre sur lequel on se trouve (ici)
100 *
101 * @param boolean $creer # inutilisé
102 */
103 function charger($creer = false){
104
105 if (!$this->param['table']) {
106 $this->messages['message_erreur'][] = _T('cfg:nom_table_manquant');
107 return false;
108 }
109
110 // colid : nom de la colonne primary key
111 list($this->param['table'], $colid) = $this->get_table_id($this->param['table']);
112
113 // renseigner les liens id=valeur
114 $id = explode('/',$this->param['cfg_id']);
115 foreach ($colid as $n=>$c) {
116 if (isset($id[$n])) {
117 $this->_id[$c] = $id[$n];
118 }
119 }
120
121 if (!$this->param['cfg_id'] AND !($this->param['autoriser_absence_id'] == 'oui')) {
122 $this->messages['message_erreur'][] = _T('cfg:id_manquant');
123 return false;
124 }
125
126 // select
127 $this->_select = array();
128 if ($this->champs){
129 foreach ($this->champs as $nom => $def) {
130 if (isset($def['id'])) {
131 continue;
132 }
133 $this->_select[] = $nom;
134 }
135 } else {
136 $this->_select[] = '*';
137 }
138
139 // where
140 $this->_where = array();
141 foreach ($this->_id as $nom => $id) {
142 $this->_where[] = $nom . '=' . sql_quote($id);
143 }
144
145 $this->_base = ($d = sql_fetsel(
146 $this->_select,
147 $this->param['table'],
148 $this->_where)) ? $d : array();
149
150 $this->_existe = count($this->_base);
151
152 $this->_ici = &$this->_base;
153 return true;
154 }
155
156 /**
157 * recuperer les valeurs.
158 *
159 * @return Array
160 */
161 function lire() {
162 // charger
163 if (!$this->charger()){
164 return array(false, $this->val, $this->messages);
165 }
166
167 // utile ??
168 if ($this->param['cfg_id']) {
169 $cles = explode('/', $this->param['cfg_id']);
170 foreach ($this->champs_id as $i => $name) {
171 $this->_ici[$name] = $cles[$i];
172 }
173 }
174
175 // s'il y a des champs demandes, ne retourner que ceux-ci
176 if (count($this->champs)){
177 $val = array();
178 foreach ($this->champs as $name => $def) {
179 $val[$name] = $this->_ici[$name];
180 }
181 $this->_ici = $val;
182 }
183 return array(true, $this->_ici);
184 }
185
186
187 /**
188 * ecrit chaque enregistrement pour chaque champ.
189 *
190 * @return Array
191 */
192 function ecrire()
193 {
194 // charger
195 if (!$this->charger()){
196 return array(false, $this->val, $this->messages);
197 }
198
199 if ($this->champs){
200 foreach ($this->champs as $name => $def) {
201 if (isset($def['id'])) continue;
202 $this->_ici[$name] = $this->val[$name];
203 }
204 } else {
205 $this->_ici = $this->val;
206 }
207
208 // update
209 if ($this->_existe) {
210 $ok = sql_updateq($this->param['table'], $this->_ici, $this->_where );
211 } else {
212 $ok = $id = sql_insertq($this->param['table'], $this->_ici);
213 }
214
215 // remettre l'id
216 if ($ok && (count($this->champs_id)==1)) {
217 $this->_ici[$nomid = $this->champs_id[0]] = $this->_existe ? $this->val[$nomid] : $ok;
218 }
219
220 return array($ok, $this->_ici);
221 }
222
223
224 /**
225 * supprime chaque enregistrement pour chaque champ.
226 *
227 * @return Array
228 */
229 function effacer(){
230 // charger
231 if (!$this->charger()){
232 return array(false, $this->val, $this->messages);
233 }
234
235 $ok = !$this->_existe || sql_delete($this->param['table'], $this->_where );
236 return array($ok, array());
237 }
238
239
240 /**
241 * charger les arguments
242 * - lire_config(table::table@colonne:id
243 * - lire_config(table::table:id
244 *
245 * @param string $args
246 * @return boolean
247 */
248 function charger_args($args){
249
250 list($table, $id) = explode(':',$args,2);
251 list($table, $colonne) = explode('@',$table);
252 list($table, $colid) = $this->get_table_id($table);
253
254 $this->param['cfg_id'] = $id;
255 $this->param['champs'] = $colonne ? array($colonne=>true) : '';
256 $this->param['table'] = $table ? $table : 'spip_cfg';
257
258 // renseigner les liens id=valeur
259 $id = explode(':',$id);
260 foreach ($colid as $n=>$c) {
261 if (isset($id[$n])) {
262 $this->_id[$c] = $id[$n];
263 }
264 }
265
266 return true;
267 }
268
269
270 /**
271 * se positionner dans le tableau arborescent
272 *
273 * @param &Array $base
274 * @param string $chemin
275 * @return &Array
276 */
277 function & monte_arbre(&$base, $chemin){
278 if (!$chemin) {
279 return $base;
280 }
281 if (!is_array($chemin)) {
282 $chemin = explode('/', $chemin);
283 }
284 if (!is_array($base)) {
285 $base = array();
286 }
287
288 foreach ($chemin as $dossier) {
289 if (!isset($base[$dossier])) {
290 $base[$dossier] = array();
291 }
292 $this->_arbre[] = array(&$base, $dossier);
293 $base = &$base[$dossier];
294 }
295
296 return $base;
297 }
298
299
300 /**
301 * Cherche le vrai nom d'une table
302 * ainsi que ses cles primaires
303 *
304 * @param string $table
305 * @return Array
306 */
307 function get_table_id($table) {
308 static $catab = array(
309 'tables_principales' => 'base/serial',
310 'tables_auxiliaires' => 'base/auxiliaires',
311 );
312 $try = array($table, 'spip_' . $table);
313 foreach ($catab as $categ => $catinc) {
314 include_spip($catinc);
315 foreach ($try as $nom) {
316 if (isset($GLOBALS[$categ][$nom])) {
317 return array($nom,
318 preg_split('/\s*,\s*/', $GLOBALS[$categ][$nom]['key']['PRIMARY KEY']));
319 }
320 }
321 }
322 if ($try = table_objet($table)) {
323 return array('spip_' . $try, array(id_table_objet($table)));
324 }
325 return array(false, false);
326 }
327
328 }
329
330
331
332
333
334
335
336 ?>