source: trunk/spip/esqueleto-redcta/plugins/cfg/depots/tablepack.php @ 152

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

se agregaron svn_update y cfg para el 2.0

File size: 7.6 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_extrapack: storage serialise dans extra de spip_auteurs ou autre
9 */
10
11if (!defined("_ECRIRE_INC_VERSION")) return;
12
13
14
15// cfg_tablepack retrouve et met a jour les donnees serialisees dans une colonne d'une table
16// par défaut : colonne 'cfg' et table 'spip_auteurs'
17// ici, cfg_id est obligatoire ... peut-être mappé sur l'auteur courant (a voir)
18//
19//
20// pour #CONFIG{xxx} ou lire_config('xxx') si xxx demarre par
21// ~ on utilise la colonne 'cfg' de spip_auteurs
22//   ~ tout court veut dire l'auteur connecte,
23//   ~123 celui de l'auteur 123
24
25// Pour utiliser une autre colonne, il faut renseigner @colonne
26//   ~@extra/champ ou
27//   ~id_auteur@prefs/champ
28//
29// Pour recuperer des valeurs d'une table particuliere,
30// il faut utiliser 'table:id/champ' ou 'table@colonne:id/champ'
31//   table:123 contenu de la colonne 'cfg' de l'enregistrement id 123 de "table"
32//   rubriques@extra:3/qqc  rubrique 3, colonne extra, champ 'qqc'
33//
34// "table" est un nom de table ou un raccourci comme "article"
35// on peut croiser plusieurs id comme spip_auteurs_articles:6:123
36// (mais il n'y a pas d'extra dans spip_auteurs_articles ...)
37// Le 2eme argument de la balise est la valeur defaut comme pour la dist
38//
39
40class cfg_depot_tablepack
41{
42        var $champs = array();
43        var $champs_id = array();
44        var $val = array();
45        var $param = array();
46        var $messages = array('message_ok'=>array(), 'message_erreur'=>array(), 'erreurs'=>array());
47       
48        var $_arbre = array();
49        var $_id = array();
50        var $_base = null;
51        var $_ici = null;
52       
53        // version du depot
54        var $version = 2;
55       
56        function cfg_depot_tablepack($params=array())
57        {
58                foreach ($params as $o=>$v) {
59                        $this->$o = $v;
60                }       
61        }
62       
63        // charge la base (racine) et le point de l'arbre sur lequel on se trouve (ici)
64        function charger($lire = false){
65                if (!$this->param['colonne'])   $this->param['colonne'] = 'cfg';
66
67                // colid : nom de la colonne primary key
68                if ($this->param['table']) {
69                        list($this->param['table'], $colid) = $this->get_table_id($this->param['table']);
70
71                        // renseigner les liens id=valeur
72                        $id = explode('/',$this->param['cfg_id']);
73                        foreach ($colid as $n=>$c) {
74                                if (isset($id[$n])) {
75                                        $this->_id[$c] = $id[$n];
76                                }
77                        }
78                }
79               
80                if (!$this->param['cfg_id']) {
81                        $this->messages['message_erreur'][] = _T('cfg:id_manquant');
82                        return false;
83                }
84               
85                // verifier que la colonne existe
86                if (!$this->verifier_colonne()) {
87                        return false;
88                } else {
89                        // recuperer la valeur du champ de la table sql
90                        $this->_where = array();
91                        foreach ($this->_id as $nom => $id) {
92                                $this->_where[] = $nom . '=' . sql_quote($id);
93                        }
94                       
95                        $this->_base = ($d = sql_getfetsel($this->param['colonne'], $this->param['table'], $this->_where)) ? unserialize($d) : array();
96                }       
97                $this->_arbre = array();
98                $this->_ici = &$this->_base;
99        $this->_ici = &$this->monte_arbre($this->_ici, $this->param['nom']);
100        $this->_ici = &$this->monte_arbre($this->_ici, $this->param['casier']);
101        return true;   
102        }
103       
104        // recuperer les valeurs.
105        function lire()
106        {
107                // charger
108                if (!$this->charger(true)){
109                        return array(true, null);       
110                }
111                $ici = &$this->_ici;
112
113        // utile ??
114        if ($this->param['cfg_id']) {
115                $cles = explode('/', $this->param['cfg_id']);
116                        foreach ($this->champs_id as $i => $name) {
117                                $ici[$name] = $cles[$i];
118                    }
119        }
120       
121        // s'il y a des champs demandes, ne retourner que ceux-ci
122        if (count($this->champs)){
123                $val = array();
124                        foreach ($this->champs as $name => $def) {
125                                $val[$name] = $ici[$name];
126                        }
127                        $ici = $val;
128        }
129            return array(true, $ici);
130        }
131
132
133        // ecrit une entree pour tous les champs
134        function ecrire()
135        {
136                // charger
137                if (!$this->charger()){
138                        return array(false, $this->val, $this->messages);       
139                }
140                $ici = &$this->_ici;
141               
142                if ($this->champs){
143                        foreach ($this->champs as $name => $def) {
144                                if (isset($def['id'])) continue;
145                                $ici[$name] = $this->val[$name];
146                        }
147                } else {
148                        $ici = $this->val;     
149                }
150
151                $ok = sql_updateq($this->param['table'], array($this->param['colonne'] => serialize($this->_base)), $this->_where);     
152                return array($ok, $ici);
153        }
154       
155       
156        // supprime chaque enregistrement de meta pour chaque champ
157        function effacer(){
158                // charger
159                if (!$this->charger()){
160                        return array(false, $this->val, $this->messages);       
161                }
162                $ici = &$this->_ici;
163                if ($this->champs){
164                        foreach ($this->champs as $name => $def) {
165                                if (isset($def['id'])) continue;
166                                unset($ici[$name]);
167                        }
168                }
169                       
170                // supprimer les dossiers vides
171                for ($i = count($this->_arbre); $i--; ) {
172                        if ($this->_arbre[$i][0][$this->_arbre[$i][1]]) {
173                                break;
174                        }
175                        unset($this->_arbre[$i][0][$this->_arbre[$i][1]]);
176                }
177                $ok = sql_updateq($this->param['table'], array($this->param['colonne'] => serialize($this->_base)), $this->_where);     
178                return array($ok, array());
179        }
180       
181       
182        // charger les arguments
183        // lire_config(tablepack::table@colonne:id/nom/casier/champ
184        // lire_config(tablepack::~id_auteur@colonne/chemin/champ
185        // lire_config(tablepack::~@colonne/chemin/champ
186        function charger_args($args){
187                $args = explode('/',$args);
188                // cas ~id_auteur/
189                if ($args[0][0] == '~'){
190                        $table = 'spip_auteurs';
191                        $colid = array('id_auteur');
192                        list($auteur, $colonne) = explode('@',array_shift($args));
193                        if (count($auteur)>1){
194                                $id = substr($auteur,1);
195                        } else {
196                                $id = $GLOBALS['auteur_session'] ? $GLOBALS['auteur_session']['id_auteur'] : '';
197                        }
198                // cas table:id/
199                // peut etre table:id:id/ si la table a 2 cles
200                } else {
201                        list($table, $id) = explode(':',array_shift($args),2);
202                        list($table, $colonne) = explode('@',$table);
203                        list($table, $colid) = $this->get_table_id($table);
204                }
205                $this->param['cfg_id'] = $id;
206                $this->param['colonne'] = $colonne ? $colonne : 'cfg';
207                $this->param['table'] = $table ? $table : 'spip_auteurs';
208                if ($champ = array_pop($args)) {
209                        $this->champs = array($champ=>true);
210                }
211                $this->param['casier'] = implode('/',$args);
212               
213                // renseigner les liens id=valeur
214                $id = explode(':',$id);
215                foreach ($colid as $n=>$c) {
216                        if (isset($id[$n])) {
217                                $this->_id[$c] = $id[$n];
218                        }
219                }
220
221                return true;   
222        }
223       
224       
225        // se positionner dans le tableau arborescent
226        function & monte_arbre(&$base, $chemin){
227                if (!$chemin) {
228                        return $base;
229                }
230                if (!is_array($chemin)) {
231                        $chemin = explode('/', $chemin);
232                }
233                if (!is_array($base)) {
234                        $base = array();
235                }
236               
237                foreach ($chemin as $dossier) {
238                        if (!isset($base[$dossier])) {
239                                $base[$dossier] = array();
240                        }
241                        $this->_arbre[] = array(&$base, $dossier);
242                        $base = &$base[$dossier];
243                }
244               
245                return $base;
246        }
247       
248       
249        function verifier_colonne($creer = false) {
250                if (!$this->param['table'])
251                        return false;
252                $col = sql_showtable($table = $this->param['table']);
253                if (!is_array($col['field']) OR !array_key_exists($colonne = $this->param['colonne'], $col['field'])) {
254                        if ($creer
255                        && $colonne
256                        && sql_alter('TABLE '.$this->param['table'] . ' ADD ' . $colonne . 'TEXT NOT NULL DEFAULT \'\'')) {
257                                return true;
258                        }
259                        return false;
260                }
261                return true;
262        }
263
264       
265        //
266        // Cherche le vrai nom d'une table
267        // ainsi que ses cles primaires
268        //
269        function get_table_id($table) { 
270                static $catab = array(
271                        'tables_principales' => 'base/serial',
272                        'tables_auxiliaires' => 'base/auxiliaires',
273                );
274                $try = array($table, 'spip_' . $table);
275                foreach ($catab as $categ => $catinc) {
276                        include_spip($catinc);
277                        foreach ($try as $nom) {
278                                if (isset($GLOBALS[$categ][$nom])) {
279                                        return array($nom,
280                                                preg_split('/\s*,\s*/', $GLOBALS[$categ][$nom]['key']['PRIMARY KEY']));
281                                }
282                        }
283                }
284                if ($try = table_objet($table)) {
285                        return array('spip_' . $try, array(id_table_objet($table)));
286                }
287                return array(false, false);
288        }
289
290}
291
292
293?>
Note: See TracBrowser for help on using the repository browser.