[SPIP] +2.1.12
[velocampus/web/www.git] / www / plugins / auto / cfg / depots / metapack.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: metapack.php 37604 2010-04-24 07:31:15Z esj@rezo.net $
13 */
14
15 if (!defined("_ECRIRE_INC_VERSION")) return;
16
17 /**
18 * Retrouve et met a jour les donnees dans spip_meta (mode serialise)
19 * @package plugins
20 * @subpackage cfg
21 */
22 class cfg_depot_metapack
23 {
24 /**
25 * Les champs manipulés
26 * @var Array
27 */
28 var $champs = array();
29
30 /**
31 * Si on passe par cfg_id, ça fait..
32 * Heu.. Quelque chose d'utile ?
33 * @var Array
34 */
35 var $champs_id = array();
36
37 /**
38 * Les valeurs en dépôt
39 * @var Array
40 */
41 var $val = array();
42
43 /**
44 * Les différents paramètres : Tables, Colonnes, cfg_id, et Casier
45 * @var Array
46 */
47 var $param = array();
48
49 /**
50 * Pour gestion de l'affichage en succès ou échec
51 * @var Array
52 */
53 var $messages = array('message_ok'=>array(), 'message_erreur'=>array(), 'erreurs'=>array());
54
55 /**
56 * Arbre
57 * @var Array
58 */
59 var $_arbre = array();
60
61 /**
62 * version du depot
63 * @var int
64 */
65 var $version = 2;
66
67 /**
68 * Stockage interne dans les attributs de la classe
69 *
70 * @param Array $params
71 */
72 function cfg_depot_metapack($params=array())
73 {
74 foreach ($params as $o=>$v) {
75 $this->$o = $v;
76 }
77 }
78
79
80 /**
81 * charge la base (racine) et le point de l'arbre sur lequel on se trouve (ici)
82 *
83 * @param boolean $lire
84 * @return boolean
85 */
86 function charger($lire = false){
87 if ($lire && !isset($GLOBALS['meta'][$this->param['nom']]))
88 return false;
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'] : '');
94 return true;
95 }
96
97 /**
98 * recuperer les valeurs.
99 *
100 * @return Array
101 */
102 function lire()
103 {
104 if (!$this->charger(true)){
105 return array(true, null); // pas de chargement = pas de valeur encore enregistrees
106 }
107 $ici = &$this->_ici;
108
109 // utile ??
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];
114 }
115 }
116
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)){
120 $val = array();
121 foreach ($this->champs as $name => $def) {
122 $val[$name] = $ici[$name];
123 }
124 $ici = $val;
125 }
126
127 return array(true, $ici);
128 }
129
130
131 /**
132 * ecrit une meta pour tous les champs
133 *
134 * @return Array
135 */
136 function ecrire()
137 {
138 // si pas de champs : on ecrit directement (ecrire_meta(metapack::nom,$val))...
139 if (!$this->champs){
140 ecrire_meta($this->param['nom'], serialize($this->val));
141 if (defined('_COMPAT_CFG_192')) ecrire_metas();
142 return array(true, $this->val);
143 }
144
145 if (!$this->charger()){
146 return array(false, $this->val);
147 }
148 $ici = &$this->_ici;
149
150 foreach ($this->champs as $name => $def) {
151 if (isset($def['id'])) continue;
152 $ici[$name] = $this->val[$name];
153 }
154
155 ecrire_meta($this->param['nom'], serialize($this->_base));
156 if (defined('_COMPAT_CFG_192')) ecrire_metas();
157 return array(true, $ici);
158 }
159
160
161 /**
162 * supprime chaque enregistrement de meta pour chaque champ
163 *
164 * @return Array
165 */
166 function effacer(){
167 // si pas de champs : on supprime directement (effacer_meta(metapack::nom))...
168 if (!$this->champs){
169 effacer_meta($this->param['nom']);
170 if (defined('_COMPAT_CFG_192')) ecrire_metas();
171 return array(true, array());
172 }
173
174 if (!$this->charger()){
175 return array(false, $this->val);
176 }
177 $ici = &$this->_ici;
178
179 // supprimer les champs
180 foreach ($this->champs as $name => $def) {
181 if (isset($def['id'])) continue;
182 unset($ici[$name]);
183 }
184
185 // supprimer les dossiers vides
186 for ($i = count($this->_arbre); $i--; ) {
187 if ($this->_arbre[$i][0][$this->_arbre[$i][1]]) {
188 break;
189 }
190 unset($this->_arbre[$i][0][$this->_arbre[$i][1]]);
191 }
192
193 if (!$this->_base) {
194 effacer_meta($this->param['nom']);
195 } else {
196 ecrire_meta($this->param['nom'], serialize($this->_base));
197 }
198 if (defined('_COMPAT_CFG_192')) ecrire_metas();
199
200 return array(true, array());
201 }
202
203
204 /**
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);
207 *
208 * @param string $args # $args = 'nom'; ici
209 * @return boolean
210 */
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);
216 }
217 $this->param['casier'] = implode('/',$args);
218 return true;
219 }
220
221
222 /**
223 * se positionner dans le tableau arborescent
224 *
225 * @param &Array $base
226 * @param string $chemin
227 * @return &Array
228 */
229 function & monte_arbre(&$base, $chemin){
230 if (!$chemin) {
231 return $base;
232 }
233 if (!is_array($chemin)) {
234 $chemin = explode('/', $chemin);
235 }
236 if (!is_array($base)) {
237 $base = array();
238 }
239
240 foreach ($chemin as $dossier) {
241 if (!isset($base[$dossier])) {
242 $base[$dossier] = array();
243 }
244 $this->_arbre[] = array(&$base, $dossier);
245 $base = &$base[$dossier];
246 }
247
248 return $base;
249 }
250 }
251
252
253
254 ?>