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: table.php 36744 2010-03-29 02:31:19Z gilles.vincent@gmail.com $
15 if (!defined("_ECRIRE_INC_VERSION")) return;
18 * Retrouve et met a jour les colonnes d'une table.
19 * ici, cfg_id est obligatoire ...
27 * Les champs manipulés
30 var $champs = array();
33 * Si on passe par cfg_id, ça fait..
34 * Heu.. Quelque chose d'utile ?
37 var $champs_id = array();
40 * Les valeurs en dépôt
46 * Les différents paramètres : Tables, Colonnes, cfg_id, et Casier
52 * Pour gestion de l'affichage en succès ou échec
55 var $messages = array('message_ok'=>array(), 'message_erreur'=>array(), 'erreurs'=>array());
61 var $_arbre = array();
64 * Le WHERE permettant de retrouver l'Arbre
76 * Où on est dans l'arbre $this->_arbre
88 * Dépôt dans les attributs de la classe
90 * @param Array $params
92 function cfg_depot_table($params=array()){
93 foreach ($params as $o=>$v) {
99 * charge la base (racine) et le point de l'arbre sur lequel on se trouve (ici)
101 * @param boolean $creer # inutilisé
103 function charger($creer = false){
105 if (!$this->param
['table']) {
106 $this->messages
['message_erreur'][] = _T('cfg:nom_table_manquant');
110 // colid : nom de la colonne primary key
111 list($this->param
['table'], $colid) = $this->get_table_id($this->param
['table']);
113 // renseigner les liens id=valeur
114 $id = explode('/',$this->param
['cfg_id']);
115 foreach ($colid as $n=>$c) {
116 if (isset($id[$n])) {
117 $this->_id
[$c] = $id[$n];
121 if (!$this->param
['cfg_id'] AND !($this->param
['autoriser_absence_id'] == 'oui')) {
122 $this->messages
['message_erreur'][] = _T('cfg:id_manquant');
127 $this->_select
= array();
129 foreach ($this->champs
as $nom => $def) {
130 if (isset($def['id'])) {
133 $this->_select
[] = $nom;
136 $this->_select
[] = '*';
140 $this->_where
= array();
141 foreach ($this->_id
as $nom => $id) {
142 $this->_where
[] = $nom . '=' . sql_quote($id);
145 $this->_base
= ($d = sql_fetsel(
147 $this->param
['table'],
148 $this->_where
)) ?
$d : array();
150 $this->_existe
= count($this->_base
);
152 $this->_ici
= &$this->_base
;
157 * recuperer les valeurs.
163 if (!$this->charger()){
164 return array(false, $this->val
, $this->messages
);
168 if ($this->param
['cfg_id']) {
169 $cles = explode('/', $this->param
['cfg_id']);
170 foreach ($this->champs_id
as $i => $name) {
171 $this->_ici
[$name] = $cles[$i];
175 // s'il y a des champs demandes, ne retourner que ceux-ci
176 if (count($this->champs
)){
178 foreach ($this->champs
as $name => $def) {
179 $val[$name] = $this->_ici
[$name];
183 return array(true, $this->_ici
);
188 * ecrit chaque enregistrement pour chaque champ.
195 if (!$this->charger()){
196 return array(false, $this->val
, $this->messages
);
200 foreach ($this->champs
as $name => $def) {
201 if (isset($def['id'])) continue;
202 $this->_ici
[$name] = $this->val
[$name];
205 $this->_ici
= $this->val
;
209 if ($this->_existe
) {
210 $ok = sql_updateq($this->param
['table'], $this->_ici
, $this->_where
);
212 $ok = $id = sql_insertq($this->param
['table'], $this->_ici
);
216 if ($ok && (count($this->champs_id
)==1)) {
217 $this->_ici
[$nomid = $this->champs_id
[0]] = $this->_existe ?
$this->val
[$nomid] : $ok;
220 return array($ok, $this->_ici
);
225 * supprime chaque enregistrement pour chaque champ.
231 if (!$this->charger()){
232 return array(false, $this->val
, $this->messages
);
235 $ok = !$this->_existe ||
sql_delete($this->param
['table'], $this->_where
);
236 return array($ok, array());
241 * charger les arguments
242 * - lire_config(table::table@colonne:id
243 * - lire_config(table::table:id
245 * @param string $args
248 function charger_args($args){
250 list($table, $id) = explode(':',$args,2);
251 list($table, $colonne) = explode('@',$table);
252 list($table, $colid) = $this->get_table_id($table);
254 $this->param
['cfg_id'] = $id;
255 $this->param
['champs'] = $colonne ?
array($colonne=>true) : '';
256 $this->param
['table'] = $table ?
$table : 'spip_cfg';
258 // renseigner les liens id=valeur
259 $id = explode(':',$id);
260 foreach ($colid as $n=>$c) {
261 if (isset($id[$n])) {
262 $this->_id
[$c] = $id[$n];
271 * se positionner dans le tableau arborescent
273 * @param &Array $base
274 * @param string $chemin
277 function & monte_arbre(&$base, $chemin){
281 if (!is_array($chemin)) {
282 $chemin = explode('/', $chemin);
284 if (!is_array($base)) {
288 foreach ($chemin as $dossier) {
289 if (!isset($base[$dossier])) {
290 $base[$dossier] = array();
292 $this->_arbre
[] = array(&$base, $dossier);
293 $base = &$base[$dossier];
301 * Cherche le vrai nom d'une table
302 * ainsi que ses cles primaires
304 * @param string $table
307 function get_table_id($table) {
308 static $catab = array(
309 'tables_principales' => 'base/serial',
310 'tables_auxiliaires' => 'base/auxiliaires',
312 $try = array($table, 'spip_' . $table);
313 foreach ($catab as $categ => $catinc) {
314 include_spip($catinc);
315 foreach ($try as $nom) {
316 if (isset($GLOBALS[$categ][$nom])) {
318 preg_split('/\s*,\s*/', $GLOBALS[$categ][$nom]['key']['PRIMARY KEY']));
322 if ($try = table_objet($table)) {
323 return array('spip_' . $try, array(id_table_objet($table)));
325 return array(false, false);