source: trunk/spip/esqueleto-redcta/plugins/cfg/depots/metapack.php @ 358

Last change on this file since 358 was 152, checked in by guille, 15 years ago

se agregaron svn_update y cfg para el 2.0

File size: 4.5 KB
Line 
1<?php
2
3/*
4 * Plugin CFG pour SPIP
5 * (c) toggg 2007, distribue sous licence GNU/GPL
6 * Documentation et contact: http://www.spip-contrib.net/
7 *
8 * classe cfg_metapack: storage serialise dans spip_meta
9 */
10
11if (!defined("_ECRIRE_INC_VERSION")) return;
12
13
14
15// cfg_metapack retrouve et met a jour les donnees dans spip_meta
16class cfg_depot_metapack
17{
18        var $champs = array();
19        var $champs_id = array();
20        var $val = array();
21        var $param = array();
22        var $messages = array('message_ok'=>array(), 'message_erreur'=>array(), 'erreurs'=>array());
23       
24        var $_arbre = array();
25       
26        // version du depot
27        var $version = 2;
28       
29        function cfg_depot_metapack($params=array())
30        {
31                foreach ($params as $o=>$v) {
32                        $this->$o = $v;
33                }       
34        }
35       
36
37        // charge la base (racine) et le point de l'arbre sur lequel on se trouve (ici)
38        function charger($lire = false){
39                if ($lire && !isset($GLOBALS['meta'][$this->param['nom']])) 
40                        return false;
41                $this->_base = is_array($c = $GLOBALS['meta'][$this->param['nom']]) ? $c : @unserialize($c);
42                $this->_arbre = array();
43                $this->_ici = &$this->_base;
44        $this->_ici = &$this->monte_arbre($this->_ici, $this->param['casier']);
45        $this->_ici = &$this->monte_arbre($this->_ici, $this->param['cfg_id']);
46        return true;   
47        }
48       
49        // recuperer les valeurs.
50        function lire()
51        {
52                if (!$this->charger(true)){
53                        return array(true, null); // pas de chargement = pas de valeur encore enregistrees
54                }
55                $ici = &$this->_ici;
56       
57        // utile ??
58        if ($this->param['cfg_id']) {
59                $cles = explode('/', $this->param['cfg_id']);
60                        foreach ($this->champs_id as $i => $name) {
61                                $ici[$name] = $cles[$i];
62                    }
63        }
64       
65        // s'il y a des champs demandes, les retourner... sinon, retourner la base
66        // (cas de lire_config('metapack::nom') tout court)
67        if (count($this->champs)){
68                $val = array();
69                        foreach ($this->champs as $name => $def) {
70                                $val[$name] = $ici[$name];
71                        }
72                        $ici = $val;
73        }
74
75            return array(true, $ici);
76        }
77
78
79        // ecrit une meta pour tous les champs
80        function ecrire()
81        {
82                // si pas de champs : on ecrit directement (ecrire_meta(metapack::nom,$val))...
83                if (!$this->champs){
84                        ecrire_meta($this->param['nom'], serialize($this->val));
85                        if (defined('_COMPAT_CFG_192')) ecrire_metas();
86                        return array(true, $this->val);
87                }
88               
89                if (!$this->charger()){
90                        return array(false, $this->val);       
91                }
92                $ici = &$this->_ici;
93               
94                foreach ($this->champs as $name => $def) {
95                        if (isset($def['id'])) continue;
96                        $ici[$name] = $this->val[$name];
97                }
98
99                ecrire_meta($this->param['nom'], serialize($this->_base));
100                if (defined('_COMPAT_CFG_192')) ecrire_metas();
101                return array(true, $ici);
102        }
103       
104       
105        // supprime chaque enregistrement de meta pour chaque champ
106        function effacer(){
107                // si pas de champs : on supprime directement (effacer_meta(metapack::nom))...
108                if (!$this->champs){
109                        effacer_meta($this->param['nom']);
110                        if (defined('_COMPAT_CFG_192')) ecrire_metas();
111                        return array(true, array());
112                }
113               
114                if (!$this->charger()){
115                        return array(false, $this->val);       
116                }
117                $ici = &$this->_ici;
118
119                // supprimer les champs
120                foreach ($this->champs as $name => $def) {
121                        if (isset($def['id'])) continue;
122                        unset($ici[$name]);
123                }
124
125                // supprimer les dossiers vides
126                for ($i = count($this->_arbre); $i--; ) {
127                        if ($this->_arbre[$i][0][$this->_arbre[$i][1]]) {
128                                break;
129                        }
130                        unset($this->_arbre[$i][0][$this->_arbre[$i][1]]);
131                }
132               
133                if (!$this->_base) {
134                    effacer_meta($this->param['nom']);
135                } else {
136                    ecrire_meta($this->param['nom'], serialize($this->_base));
137            }           
138                if (defined('_COMPAT_CFG_192')) ecrire_metas();
139               
140                return array(true, array());
141        }
142       
143       
144        // charger les arguments de lire_config(metapack::nom/casier/champ)
145        // $args = 'nom'; ici
146        // il se peut qu'il n'y ait pas de champs si : lire_config(metapack::nom);
147        function charger_args($args){
148                $args = explode('/',$args);
149                $this->param['nom'] = array_shift($args);
150                if ($champ = array_pop($args)) {
151                        $this->champs = array($champ=>true);
152                }
153                $this->param['casier'] = implode('/',$args);
154                return true;   
155        }
156       
157       
158        // se positionner dans le tableau arborescent
159        function & monte_arbre(&$base, $chemin){
160                if (!$chemin) {
161                        return $base;
162                }
163                if (!is_array($chemin)) {
164                        $chemin = explode('/', $chemin);
165                }
166                if (!is_array($base)) {
167                        $base = array();
168                }
169               
170                foreach ($chemin as $dossier) {
171                        if (!isset($base[$dossier])) {
172                                $base[$dossier] = array();
173                        }
174                        $this->_arbre[] = array(&$base, $dossier);
175                        $base = &$base[$dossier];
176                }
177               
178                return $base;
179        }
180}
181
182
183
184?>
Note: See TracBrowser for help on using the repository browser.