source: trunk/spip/esqueleto-redcta/plugins/cfg/depots/table.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: 5.4 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_table: storage naturel dans une table
9 */
10
11if (!defined("_ECRIRE_INC_VERSION")) return;
12
13
14
15// cfg_table retrouve et met a jour les colonnes d'une table
16// ici, cfg_id est obligatoire ...
17class cfg_depot_table
18{
19        var $champs = array();
20        var $champs_id = array();
21        var $val = array();
22        var $param = array();
23        var $messages = array('message_ok'=>array(), 'message_erreur'=>array(), 'erreurs'=>array());
24       
25        var $_arbre = array();
26        var $_id = array();
27        var $_base = null;
28        var $_ici = null;
29       
30        // version du depot
31        var $version = 2;
32       
33        function cfg_depot_table($params=array())
34        {
35                foreach ($params as $o=>$v) {
36                        $this->$o = $v;
37                }
38        }
39       
40        // charge la base (racine) et le point de l'arbre sur lequel on se trouve (ici)
41        function charger($creer = false){
42               
43                if (!$this->param['table']) {
44                        $this->messages['message_erreur'][] = _T('cfg:nom_table_manquant');
45                        return false;
46                }
47               
48                // colid : nom de la colonne primary key
49                list($this->param['table'], $colid) = $this->get_table_id($this->param['table']);
50               
51                // renseigner les liens id=valeur
52                $id = explode('/',$this->param['cfg_id']);
53                foreach ($colid as $n=>$c) {
54                        if (isset($id[$n])) {
55                                $this->_id[$c] = $id[$n];
56                        }
57                }
58               
59                if (!$this->param['cfg_id'] AND !($this->param['autoriser_absence_id'] == 'oui')) {
60                        $this->messages['message_erreur'][] = _T('cfg:id_manquant');
61                        return false;
62                }
63
64                // select
65                $this->_select = array();
66                if ($this->champs){
67                        foreach ($this->champs as $nom => $def) {
68                                if (isset($def['id'])) {
69                                        continue;
70                                }
71                                $this->_select[] = $nom;
72                        }
73                } else {
74                        $this->_select[] = '*'; 
75                }
76               
77                // where
78                $this->_where = array();
79                foreach ($this->_id as $nom => $id) {
80                        $this->_where[] = $nom . '=' . sql_quote($id);
81                }
82                       
83                $this->_base = ($d = sql_fetsel(
84                        $this->_select, 
85                        $this->param['table'], 
86                        $this->_where)) ? $d : array();
87               
88                $this->_existe = count($this->_base);
89                       
90                $this->_ici = &$this->_base;
91        return true;   
92        }
93       
94        // recuperer les valeurs.
95        function lire()
96        {
97                // charger
98                if (!$this->charger()){
99                        return array(false, $this->val, $this->messages);       
100                }
101
102        // utile ??
103        if ($this->param['cfg_id']) {
104                $cles = explode('/', $this->param['cfg_id']);
105                        foreach ($this->champs_id as $i => $name) {
106                                $this->_ici[$name] = $cles[$i];
107                    }
108        }
109       
110        // s'il y a des champs demandes, ne retourner que ceux-ci
111        if (count($this->champs)){
112                $val = array();
113                        foreach ($this->champs as $name => $def) {
114                                $val[$name] = $this->_ici[$name];
115                        }
116                        $this->_ici = $val;
117        }
118            return array(true, $this->_ici);
119        }
120
121
122        // ecrit une entree pour tous les champs
123        function ecrire()
124        {
125                // charger
126                if (!$this->charger()){
127                        return array(false, $this->val, $this->messages);       
128                }
129
130                if ($this->champs){
131                        foreach ($this->champs as $name => $def) {
132                                if (isset($def['id'])) continue;
133                                $this->_ici[$name] = $this->val[$name];
134                        }
135                } else {
136                        $this->_ici = $this->val;       
137                }       
138                       
139                // update
140                if ($this->_existe) {   
141                    $ok = sql_updateq($this->param['table'], $this->_ici, $this->_where );
142            } else {
143                        $ok = $id = sql_insertq($this->param['table'], $this->_ici);
144            }
145
146                // remettre l'id
147                if ($ok && (count($this->champs_id)==1)) {
148                        $this->_ici[$nomid = $this->champs_id[0]] = $this->_existe ? $this->val[$nomid] : $ok;
149                }
150                       
151                return array($ok, $this->_ici);
152        }
153       
154       
155        // supprime chaque enregistrement de meta pour chaque champ
156        function effacer(){
157                // charger
158                if (!$this->charger()){
159                        return array(false, $this->val, $this->messages);       
160                }
161               
162                $ok = !$this->_existe || sql_delete($this->param['table'], $this->_where );     
163                return array($ok, array());
164        }
165       
166       
167        // charger les arguments
168        // lire_config(table::table@colonne:id
169        // lire_config(table::table:id
170        function charger_args($args){
171
172                list($table, $id) = explode(':',$args,2);
173                list($table, $colonne) = explode('@',$table);
174                list($table, $colid) = $this->get_table_id($table);
175               
176                $this->param['cfg_id'] = $id;
177                $this->param['champs'] = $colonne ? array($colonne=>true) : '';
178                $this->param['table'] = $table ? $table : 'spip_cfg';
179               
180                // renseigner les liens id=valeur
181                $id = explode(':',$id);
182                foreach ($colid as $n=>$c) {
183                        if (isset($id[$n])) {
184                                $this->_id[$c] = $id[$n];
185                        }
186                }
187               
188                return true;   
189        }
190       
191       
192        // se positionner dans le tableau arborescent
193        function & monte_arbre(&$base, $chemin){
194                if (!$chemin) {
195                        return $base;
196                }
197                if (!is_array($chemin)) {
198                        $chemin = explode('/', $chemin);
199                }
200                if (!is_array($base)) {
201                        $base = array();
202                }
203               
204                foreach ($chemin as $dossier) {
205                        if (!isset($base[$dossier])) {
206                                $base[$dossier] = array();
207                        }
208                        $this->_arbre[] = array(&$base, $dossier);
209                        $base = &$base[$dossier];
210                }
211               
212                return $base;
213        }
214       
215
216        //
217        // Cherche le vrai nom d'une table
218        // ainsi que ses cles primaires
219        //
220        function get_table_id($table) { 
221                static $catab = array(
222                        'tables_principales' => 'base/serial',
223                        'tables_auxiliaires' => 'base/auxiliaires',
224                );
225                $try = array($table, 'spip_' . $table);
226                foreach ($catab as $categ => $catinc) {
227                        include_spip($catinc);
228                        foreach ($try as $nom) {
229                                if (isset($GLOBALS[$categ][$nom])) {
230                                        return array($nom,
231                                                preg_split('/\s*,\s*/', $GLOBALS[$categ][$nom]['key']['PRIMARY KEY']));
232                                }
233                        }
234                }
235                if ($try = table_objet($table)) {
236                        return array('spip_' . $try, array(id_table_objet($table)));
237                }
238                return array(false, false);
239        }
240
241}
242
243
244
245
246
247
248
249?>
Note: See TracBrowser for help on using the repository browser.