4 * Plugin générique de configuration pour SPIP
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 $
15 if (!defined("_ECRIRE_INC_VERSION")) return;
19 * Storage serialise dans extra de spip_auteurs ou autre
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)
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
32 * Pour utiliser une autre colonne, il faut renseigner @colonne
34 * - ~id_auteur@prefs/champ
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'
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
49 class cfg_depot_tablepack
52 * Les champs manipulés
55 var $champs = array();
58 * Si on passe par cfg_id, ça fait..
59 * Heu.. Quelque chose d'utile ?
62 var $champs_id = array();
65 * Les valeurs en dépôt
71 * Les différents paramètres : Tables, Colonnes, cfg_id, et Casier
77 * Pour gestion de l'affichage en succès ou échec
80 var $messages = array('message_ok'=>array(), 'message_erreur'=>array(), 'erreurs'=>array());
86 var $_arbre = array();
89 * Le WHERE permettant de retrouver l'Arbre
101 * Où on est dans l'arbre $this->_arbre
113 * Dépôt dans les attributs de la classe
115 * @param Array $params
117 function cfg_depot_tablepack($params=array())
119 foreach ($params as $o=>$v) {
125 * charge la base (racine) et le point de l'arbre sur lequel on se trouve (ici)
127 * @param boolean $lire # inutilisé
130 function charger($lire = false){
131 if (!$this->param
['colonne']) $this->param
['colonne'] = 'cfg';
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']);
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];
146 if (!$this->param
['cfg_id']) {
147 $this->messages
['message_erreur'][] = _T('cfg:id_manquant');
151 // verifier que la colonne existe
152 if (!$this->verifier_colonne()) {
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);
161 $this->_base
= ($d = sql_getfetsel($this->param
['colonne'], $this->param
['table'], $this->_where
)) ?
unserialize($d) : array();
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']);
171 * recuperer les valeurs.
178 if (!$this->charger(true)){
179 return array(true, null);
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];
191 // s'il y a des champs demandes, ne retourner que ceux-ci
192 if (count($this->champs
)){
194 foreach ($this->champs
as $name => $def) {
195 $val[$name] = $ici[$name];
199 return array(true, $ici);
204 * ecrit chaque enregistrement pour chaque champ.
211 if (!$this->charger()){
212 return array(false, $this->val
, $this->messages
);
217 foreach ($this->champs
as $name => $def) {
218 if (isset($def['id'])) continue;
219 $ici[$name] = $this->val
[$name];
225 $ok = sql_updateq($this->param
['table'], array($this->param
['colonne'] => serialize($this->_base
)), $this->_where
);
226 return array($ok, $ici);
231 * supprime chaque enregistrement pour chaque champ.
237 if (!$this->charger()){
238 return array(false, $this->val
, $this->messages
);
242 foreach ($this->champs
as $name => $def) {
243 if (isset($def['id'])) continue;
248 // supprimer les dossiers vides
249 for ($i = count($this->_arbre
); $i--; ) {
250 if ($this->_arbre
[$i][0][$this->_arbre
[$i][1]]) {
253 unset($this->_arbre
[$i][0][$this->_arbre
[$i][1]]);
255 $ok = sql_updateq($this->param
['table'], array($this->param
['colonne'] => serialize($this->_base
)), $this->_where
);
256 return array($ok, array());
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
266 * @param <type> $args
269 function charger_args($args){
270 $args = explode('/',$args);
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);
279 $id = $GLOBALS['auteur_session'] ?
$GLOBALS['auteur_session']['id_auteur'] : '';
282 // peut etre table:id:id/ si la table a 2 cles
284 list($table, $id) = explode(':',array_shift($args),2);
285 list($table, $colonne) = explode('@',$table);
286 list($table, $colid) = $this->get_table_id($table);
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);
294 $this->param
['casier'] = implode('/',$args);
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];
309 * se positionner dans le tableau arborescent
311 * @param <type> $base
312 * @param <type> $chemin
316 function & monte_arbre(&$base, $chemin){
320 if (!is_array($chemin)) {
321 $chemin = explode('/', $chemin);
323 if (!is_array($base)) {
327 foreach ($chemin as $dossier) {
328 if (!isset($base[$dossier])) {
329 $base[$dossier] = array();
331 $this->_arbre
[] = array(&$base, $dossier);
332 $base = &$base[$dossier];
341 * @param <type> $creer
344 function verifier_colonne($creer = false) {
345 if (!$this->param
['table'])
347 $col = sql_showtable($table = $this->param
['table']);
348 if (!is_array($col['field']) OR !array_key_exists($colonne = $this->param
['colonne'], $col['field'])) {
351 && sql_alter('TABLE '.$this->param
['table'] . ' ADD ' . $colonne . 'TEXT NOT NULL DEFAULT \'\'')) {
361 * Cherche le vrai nom d'une table ainsi que ses cles primaires
363 * @param <type> $table
366 function get_table_id($table) {
367 static $catab = array(
368 'tables_principales' => 'base/serial',
369 'tables_auxiliaires' => 'base/auxiliaires',
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])) {
377 preg_split('/\s*,\s*/', $GLOBALS[$categ][$nom]['key']['PRIMARY KEY']));
381 if ($try = table_objet($table)) {
382 return array('spip_' . $try, array(id_table_objet($table)));
384 return array(false, false);