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: metapack.php 37604 2010-04-24 07:31:15Z esj@rezo.net $
15 if (!defined("_ECRIRE_INC_VERSION")) return;
18 * Retrouve et met a jour les donnees dans spip_meta (mode serialise)
22 class cfg_depot_metapack
25 * Les champs manipulés
28 var $champs = array();
31 * Si on passe par cfg_id, ça fait..
32 * Heu.. Quelque chose d'utile ?
35 var $champs_id = array();
38 * Les valeurs en dépôt
44 * Les différents paramètres : Tables, Colonnes, cfg_id, et Casier
50 * Pour gestion de l'affichage en succès ou échec
53 var $messages = array('message_ok'=>array(), 'message_erreur'=>array(), 'erreurs'=>array());
59 var $_arbre = array();
68 * Stockage interne dans les attributs de la classe
70 * @param Array $params
72 function cfg_depot_metapack($params=array())
74 foreach ($params as $o=>$v) {
81 * charge la base (racine) et le point de l'arbre sur lequel on se trouve (ici)
83 * @param boolean $lire
86 function charger($lire = false){
87 if ($lire && !isset($GLOBALS['meta'][$this->param
['nom']]))
89 $this->_base
= is_array($c = $GLOBALS['meta'][$this->param
['nom']]) ?
$c : @unserialize
($c);
90 $this->_arbre
= array();
91 $this->_ici
= &$this->_base
;
92 $this->_ici
= &$this->monte_arbre($this->_ici
, $this->param
['casier']);
93 $this->_ici
= &$this->monte_arbre($this->_ici
, isset($this->param
['cfg_id']) ?
$this->param
['cfg_id'] : '');
98 * recuperer les valeurs.
104 if (!$this->charger(true)){
105 return array(true, null); // pas de chargement = pas de valeur encore enregistrees
110 if (isset($this->param
['cfg_id'])) {
111 $cles = explode('/', $this->param
['cfg_id']);
112 foreach ($this->champs_id
as $i => $name) {
113 $ici[$name] = $cles[$i];
117 // s'il y a des champs demandes, les retourner... sinon, retourner la base
118 // (cas de lire_config('metapack::nom') tout court)
119 if (count($this->champs
)){
121 foreach ($this->champs
as $name => $def) {
122 $val[$name] = $ici[$name];
127 return array(true, $ici);
132 * ecrit une meta pour tous les champs
138 // si pas de champs : on ecrit directement (ecrire_meta(metapack::nom,$val))...
140 ecrire_meta($this->param
['nom'], serialize($this->val
));
141 if (defined('_COMPAT_CFG_192')) ecrire_metas();
142 return array(true, $this->val
);
145 if (!$this->charger()){
146 return array(false, $this->val
);
150 foreach ($this->champs
as $name => $def) {
151 if (isset($def['id'])) continue;
152 $ici[$name] = $this->val
[$name];
155 ecrire_meta($this->param
['nom'], serialize($this->_base
));
156 if (defined('_COMPAT_CFG_192')) ecrire_metas();
157 return array(true, $ici);
162 * supprime chaque enregistrement de meta pour chaque champ
167 // si pas de champs : on supprime directement (effacer_meta(metapack::nom))...
169 effacer_meta($this->param
['nom']);
170 if (defined('_COMPAT_CFG_192')) ecrire_metas();
171 return array(true, array());
174 if (!$this->charger()){
175 return array(false, $this->val
);
179 // supprimer les champs
180 foreach ($this->champs
as $name => $def) {
181 if (isset($def['id'])) continue;
185 // supprimer les dossiers vides
186 for ($i = count($this->_arbre
); $i--; ) {
187 if ($this->_arbre
[$i][0][$this->_arbre
[$i][1]]) {
190 unset($this->_arbre
[$i][0][$this->_arbre
[$i][1]]);
194 effacer_meta($this->param
['nom']);
196 ecrire_meta($this->param
['nom'], serialize($this->_base
));
198 if (defined('_COMPAT_CFG_192')) ecrire_metas();
200 return array(true, array());
205 * charger les arguments de lire_config(metapack::nom/casier/champ)
206 * il se peut qu'il n'y ait pas de champs si : lire_config(metapack::nom);
208 * @param string $args # $args = 'nom'; ici
211 function charger_args($args){
212 $args = explode('/',$args);
213 $this->param
['nom'] = array_shift($args);
214 if ($champ = array_pop($args)) {
215 $this->champs
= array($champ=>true);
217 $this->param
['casier'] = implode('/',$args);
223 * se positionner dans le tableau arborescent
225 * @param &Array $base
226 * @param string $chemin
229 function & monte_arbre(&$base, $chemin){
233 if (!is_array($chemin)) {
234 $chemin = explode('/', $chemin);
236 if (!is_array($base)) {
240 foreach ($chemin as $dossier) {
241 if (!isset($base[$dossier])) {
242 $base[$dossier] = array();
244 $this->_arbre
[] = array(&$base, $dossier);
245 $base = &$base[$dossier];