[SPIP] +2.1.12
[velocampus/web/www.git] / www / plugins / auto / cfg / depots / tablepack.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: tablepack.php 36744 2010-03-29 02:31:19Z gilles.vincent@gmail.com $
13 */
14
15 if (!defined("_ECRIRE_INC_VERSION")) return;
16
17
18 /**
19 * Storage serialise dans extra de spip_auteurs ou autre
20 *
21 *
22 * Cette classe retrouve et met a jour les donnees serialisees dans une colonne d'une table
23 * par défaut : colonne 'cfg' et table 'spip_auteurs'
24 * ici, cfg_id est obligatoire ... peut-être mappé sur l'auteur courant (a voir)
25 *
26 *
27 * pour #CONFIG{xxx} ou lire_config('xxx') si xxx demarre par
28 * - ~ on utilise la colonne 'cfg' de spip_auteurs
29 * - ~ tout court veut dire l'auteur connecte,
30 * - ~123 celui de l'auteur 123
31 *
32 * Pour utiliser une autre colonne, il faut renseigner @colonne
33 * - ~@extra/champ ou
34 * - ~id_auteur@prefs/champ
35 *
36 * Pour recuperer des valeurs d'une table particuliere,
37 * il faut utiliser 'table:id/champ' ou 'table@colonne:id/champ'
38 * - table:123 contenu de la colonne 'cfg' de l'enregistrement id 123 de "table"
39 * - rubriques@extra:3/qqc rubrique 3, colonne extra, champ 'qqc'
40 *
41 * "table" est un nom de table ou un raccourci comme "article"
42 * on peut croiser plusieurs id comme spip_auteurs_articles:6:123
43 * (mais il n'y a pas d'extra dans spip_auteurs_articles ...)
44 * Le 2eme argument de la balise est la valeur defaut comme pour la dist
45 *
46 * @package plugins
47 * @subpackage cfg
48 */
49 class cfg_depot_tablepack
50 {
51 /**
52 * Les champs manipulés
53 * @var Array
54 */
55 var $champs = array();
56
57 /**
58 * Si on passe par cfg_id, ça fait..
59 * Heu.. Quelque chose d'utile ?
60 * @var Array
61 */
62 var $champs_id = array();
63
64 /**
65 * Les valeurs en dépôt
66 * @var Array
67 */
68 var $val = array();
69
70 /**
71 * Les différents paramètres : Tables, Colonnes, cfg_id, et Casier
72 * @var Array
73 */
74 var $param = array();
75
76 /**
77 * Pour gestion de l'affichage en succès ou échec
78 * @var Array
79 */
80 var $messages = array('message_ok'=>array(), 'message_erreur'=>array(), 'erreurs'=>array());
81
82 /**
83 * Arbre
84 * @var Array
85 */
86 var $_arbre = array();
87
88 /**
89 * Le WHERE permettant de retrouver l'Arbre
90 * @var Array
91 */
92 var $_id = array();
93
94 /**
95 * Base de l'arbre
96 * @var Array
97 */
98 var $_base = null;
99
100 /**
101 * Où on est dans l'arbre $this->_arbre
102 * @var &Array
103 */
104 var $_ici = null;
105
106 /**
107 * version du depot
108 * @var int
109 */
110 var $version = 2;
111
112 /**
113 * Dépôt dans les attributs de la classe
114 *
115 * @param Array $params
116 */
117 function cfg_depot_tablepack($params=array())
118 {
119 foreach ($params as $o=>$v) {
120 $this->$o = $v;
121 }
122 }
123
124 /**
125 * charge la base (racine) et le point de l'arbre sur lequel on se trouve (ici)
126 *
127 * @param boolean $lire # inutilisé
128 * @return <type>
129 */
130 function charger($lire = false){
131 if (!$this->param['colonne']) $this->param['colonne'] = 'cfg';
132
133 // colid : nom de la colonne primary key
134 if ($this->param['table']) {
135 list($this->param['table'], $colid) = $this->get_table_id($this->param['table']);
136
137 // renseigner les liens id=valeur
138 $id = explode('/',$this->param['cfg_id']);
139 foreach ($colid as $n=>$c) {
140 if (isset($id[$n])) {
141 $this->_id[$c] = $id[$n];
142 }
143 }
144 }
145
146 if (!$this->param['cfg_id']) {
147 $this->messages['message_erreur'][] = _T('cfg:id_manquant');
148 return false;
149 }
150
151 // verifier que la colonne existe
152 if (!$this->verifier_colonne()) {
153 return false;
154 } else {
155 // recuperer la valeur du champ de la table sql
156 $this->_where = array();
157 foreach ($this->_id as $nom => $id) {
158 $this->_where[] = $nom . '=' . sql_quote($id);
159 }
160
161 $this->_base = ($d = sql_getfetsel($this->param['colonne'], $this->param['table'], $this->_where)) ? unserialize($d) : array();
162 }
163 $this->_arbre = array();
164 $this->_ici = &$this->_base;
165 $this->_ici = &$this->monte_arbre($this->_ici, $this->param['nom']);
166 $this->_ici = &$this->monte_arbre($this->_ici, $this->param['casier']);
167 return true;
168 }
169
170 /**
171 * recuperer les valeurs.
172 *
173 * @return Array
174 */
175 function lire()
176 {
177 // charger
178 if (!$this->charger(true)){
179 return array(true, null);
180 }
181 $ici = &$this->_ici;
182
183 // utile ??
184 if ($this->param['cfg_id']) {
185 $cles = explode('/', $this->param['cfg_id']);
186 foreach ($this->champs_id as $i => $name) {
187 $ici[$name] = $cles[$i];
188 }
189 }
190
191 // s'il y a des champs demandes, ne retourner que ceux-ci
192 if (count($this->champs)){
193 $val = array();
194 foreach ($this->champs as $name => $def) {
195 $val[$name] = $ici[$name];
196 }
197 $ici = $val;
198 }
199 return array(true, $ici);
200 }
201
202
203 /**
204 * ecrit chaque enregistrement pour chaque champ.
205 *
206 * @return Array
207 */
208 function ecrire()
209 {
210 // charger
211 if (!$this->charger()){
212 return array(false, $this->val, $this->messages);
213 }
214 $ici = &$this->_ici;
215
216 if ($this->champs){
217 foreach ($this->champs as $name => $def) {
218 if (isset($def['id'])) continue;
219 $ici[$name] = $this->val[$name];
220 }
221 } else {
222 $ici = $this->val;
223 }
224
225 $ok = sql_updateq($this->param['table'], array($this->param['colonne'] => serialize($this->_base)), $this->_where);
226 return array($ok, $ici);
227 }
228
229
230 /**
231 * supprime chaque enregistrement pour chaque champ.
232 *
233 * @return Array
234 */
235 function effacer(){
236 // charger
237 if (!$this->charger()){
238 return array(false, $this->val, $this->messages);
239 }
240 $ici = &$this->_ici;
241 if ($this->champs){
242 foreach ($this->champs as $name => $def) {
243 if (isset($def['id'])) continue;
244 unset($ici[$name]);
245 }
246 }
247
248 // supprimer les dossiers vides
249 for ($i = count($this->_arbre); $i--; ) {
250 if ($this->_arbre[$i][0][$this->_arbre[$i][1]]) {
251 break;
252 }
253 unset($this->_arbre[$i][0][$this->_arbre[$i][1]]);
254 }
255 $ok = sql_updateq($this->param['table'], array($this->param['colonne'] => serialize($this->_base)), $this->_where);
256 return array($ok, array());
257 }
258
259
260 /**
261 * charger les arguments de
262 * - lire_config(tablepack::table@colonne:id/nom/casier/champ)
263 * - lire_config(tablepack::~id_auteur@colonne/chemin/champ)
264 * - lire_config(tablepack::~@colonne/chemin/champ
265 *
266 * @param <type> $args
267 * @return <type>
268 */
269 function charger_args($args){
270 $args = explode('/',$args);
271 // cas ~id_auteur/
272 if ($args[0][0] == '~'){
273 $table = 'spip_auteurs';
274 $colid = array('id_auteur');
275 list($auteur, $colonne) = explode('@',array_shift($args));
276 if (count($auteur)>1){
277 $id = substr($auteur,1);
278 } else {
279 $id = $GLOBALS['auteur_session'] ? $GLOBALS['auteur_session']['id_auteur'] : '';
280 }
281 // cas table:id/
282 // peut etre table:id:id/ si la table a 2 cles
283 } else {
284 list($table, $id) = explode(':',array_shift($args),2);
285 list($table, $colonne) = explode('@',$table);
286 list($table, $colid) = $this->get_table_id($table);
287 }
288 $this->param['cfg_id'] = $id;
289 $this->param['colonne'] = $colonne ? $colonne : 'cfg';
290 $this->param['table'] = $table ? $table : 'spip_auteurs';
291 if ($champ = array_pop($args)) {
292 $this->champs = array($champ=>true);
293 }
294 $this->param['casier'] = implode('/',$args);
295
296 // renseigner les liens id=valeur
297 $id = explode(':',$id);
298 foreach ($colid as $n=>$c) {
299 if (isset($id[$n])) {
300 $this->_id[$c] = $id[$n];
301 }
302 }
303
304 return true;
305 }
306
307
308 /**
309 * se positionner dans le tableau arborescent
310 *
311 * @param <type> $base
312 * @param <type> $chemin
313 * @return <type>
314 */
315
316 function & monte_arbre(&$base, $chemin){
317 if (!$chemin) {
318 return $base;
319 }
320 if (!is_array($chemin)) {
321 $chemin = explode('/', $chemin);
322 }
323 if (!is_array($base)) {
324 $base = array();
325 }
326
327 foreach ($chemin as $dossier) {
328 if (!isset($base[$dossier])) {
329 $base[$dossier] = array();
330 }
331 $this->_arbre[] = array(&$base, $dossier);
332 $base = &$base[$dossier];
333 }
334
335 return $base;
336 }
337
338
339 /**
340 *
341 * @param <type> $creer
342 * @return <type>
343 */
344 function verifier_colonne($creer = false) {
345 if (!$this->param['table'])
346 return false;
347 $col = sql_showtable($table = $this->param['table']);
348 if (!is_array($col['field']) OR !array_key_exists($colonne = $this->param['colonne'], $col['field'])) {
349 if ($creer
350 && $colonne
351 && sql_alter('TABLE '.$this->param['table'] . ' ADD ' . $colonne . 'TEXT NOT NULL DEFAULT \'\'')) {
352 return true;
353 }
354 return false;
355 }
356 return true;
357 }
358
359
360 /**
361 * Cherche le vrai nom d'une table ainsi que ses cles primaires
362 *
363 * @param <type> $table
364 * @return <type>
365 */
366 function get_table_id($table) {
367 static $catab = array(
368 'tables_principales' => 'base/serial',
369 'tables_auxiliaires' => 'base/auxiliaires',
370 );
371 $try = array($table, 'spip_' . $table);
372 foreach ($catab as $categ => $catinc) {
373 include_spip($catinc);
374 foreach ($try as $nom) {
375 if (isset($GLOBALS[$categ][$nom])) {
376 return array($nom,
377 preg_split('/\s*,\s*/', $GLOBALS[$categ][$nom]['key']['PRIMARY KEY']));
378 }
379 }
380 }
381 if ($try = table_objet($table)) {
382 return array('spip_' . $try, array(id_table_objet($table)));
383 }
384 return array(false, false);
385 }
386
387 }
388
389
390 ?>