3 * Plugin générique de configuration pour SPIP
9 * @copyright (c) toggg, marcimat 2007-2008
10 * @link http://www.spip-contrib.net/
11 * @version $Id: cfg_config.php 53409 2011-10-13 20:42:57Z yffic@lefourneau.com $
14 if (!defined("_ECRIRE_INC_VERSION")) return;
17 * charge le depot qui va bien en fonction de l'argument demande
21 * - metapack::prefixe_plugin
22 * - metapack::prefixe/casier/champ
23 * - tablepack::auteur@extra:8/prefixe/casier/champ
24 * - tablepack::~id_auteur@extra/prefixe/casier/champ
26 * en l'absence du nom de depot (gauche des ::) cette fonction prendra comme suit :
27 * - ~ en premier caractere : tablepack
28 * - : present avant un / : tablepack
34 function cfg_charger_depot($args){
35 $r = explode('::',$args,2);
37 list($depot,$args) = $r;
39 // si un seul argument, il faut trouver le depot
40 $depot = cfg_charger_depot_args($args);
42 $depot = new cfg_depot($depot);
43 $depot->charger_args($args);
47 function cfg_charger_depot_args($args){
52 (list($head, ) = explode('/',$args,2)) &&
53 (strpos($head,':') !== false)) {
56 if (strpos($args,'/') !== false)
65 * cette classe charge les fonctions de lecture et ecriture d'un depot (dans depots/)
67 * Ces depots ont une version qui evoluera en fonction si des changements d'api apparaissent
69 * version 2 (fonctions)
71 * - lire, ecrire, effacer
79 * Le nom de la classe du dépôt
92 * Constructeur de la classe
94 * 'depot' est le nom du fichier php stocke dans /depots/{depot}.php
95 * qui contient une classe 'cfg_depot_{depot}'
97 * $params est un tableau de parametres passes a la classe cfg_depot_{depot} qui peut contenir :
101 * 'balise' => 'select|textarea|input', // nom de la balise
102 * 'type' => 'checkbox|hidden|text...', // type d'un input
103 * 'tableau' => bool, // est-ce un champ tableau name="champ[]" ?
104 * 'cfg' => 'xx', // classe css commencant par css_xx
105 * 'id' => y, // cle du tableau 'champs_id' (emplacement qui possede ce champ)
107 * 'champs_id' => array(
108 * cle => 'nom' // nom d'un champ de type id
111 * 'parametre_cfg' => 'valeur' // les parametres <!-- param=valeur --> passes dans les formulaires cfg
114 * 'nom' => 'valeur' // les valeurs des champs sont stockes dedans
119 * @param string $depot
120 * @param Array $params
122 function cfg_depot($depot='metapack', $params=array()){
123 if (!isset($params['param'])) {
124 $params['param'] = array();
127 include_spip('depots/'.$depot);
128 if (!class_exists($class = 'cfg_depot_'.$depot)) {
129 die("CFG ne trouve pas le dépot $depot");
131 $this->depot
= new $class($params);
132 $this->version
= $this->depot
->version
;
137 * ajoute les parametres transmis dans l'objet du depot
139 * @param Array $params
141 function add_params($params){
142 foreach ($params as $o=>$v) {
143 $this->depot
->$o = $v;
148 * récupérer les enregistrements des différents champs.
149 * @param Array $params
152 function lire($params = array()){
153 $this->add_params($params);
154 return $this->depot
->lire(); // array($ok, $val, $messages)
158 * ecrit chaque enregistrement pour chaque champ.
159 * @param Array $params
162 function ecrire($params = array()){
163 $this->add_params($params);
164 return $this->depot
->ecrire(); // array($ok, $val, $messages)
168 * supprime chaque enregistrement pour chaque champ.
169 * @param Array $params
172 function effacer($params = array()){
173 $this->add_params($params);
174 return $this->depot
->effacer(); // array($ok, $val, $messages)
178 * Lecture de la configuration
180 * @param boolean $unserialize
183 function lire_config($unserialize=true){
184 list($ok, $s) = $this->depot
->lire($unserialize);
185 if ($ok && ($nom = $this->nom_champ())) {
193 * enregistrer une configuration
195 * @param mixed $valeur
198 function ecrire_config($valeur){
199 if ($nom = $this->nom_champ()) {
200 $this->depot
->val
= array($nom=>$valeur);
202 $this->depot
->val
= $valeur;
204 list($ok, $s) = $this->depot
->ecrire();
209 * supprimer une config
213 function effacer_config(){
214 if ($nom = $this->nom_champ()){
215 $this->depot
->val
[$nom] = false;
217 $this->depot
->val
= null;
219 list($ok, $s) = $this->depot
->effacer();
225 * le nom d'un champ s'il est dans le dépôt
226 * @return boolean|string
228 function nom_champ(){
229 if (count($this->depot
->champs
)==1){
230 foreach ($this->depot
->champs
as $nom=>$def){
238 * charge les arguments d'un lire/ecrire/effacer_config
239 * dans le depot : lire_config($args = 'metapack::prefixe/casier/champ');
244 function charger_args($args){
245 if (method_exists($this->depot
, 'charger_args')){
246 return $this->depot
->charger_args($args);
255 * Lecture de la configuration
257 * lire_config() permet de recuperer une config depuis le php<br>
258 * memes arguments que la balise (forcement)<br>
259 * $cfg: la config, lire_config('montruc') est un tableau<br>
260 * lire_config('montruc/sub') est l'element "sub" de cette config
261 * comme la balise pour ~, ~id_auteur ou table:id<br>
263 * $unserialize est mis par l'histoire, et affecte le depot 'meta'
265 * @param string $cfg la config
266 * @param mixed $def un defaut optionnel
267 * @param boolean $unserialize n'affecte que le depot 'meta'
270 if (!function_exists('lire_config')) {
271 function lire_config($cfg='', $def=null, $unserialize=true) {
272 $depot = cfg_charger_depot($cfg);
273 $r = $depot->lire_config($unserialize);
274 if (is_null($r)) return $def;
280 * enregistrer une configuration
283 * @param mixed $valeur
286 if (!function_exists('ecrire_config')) {
287 function ecrire_config($cfg='', $valeur=null){
288 $depot = cfg_charger_depot($cfg);
289 return $depot->ecrire_config($valeur);
295 * supprimer une config
300 if (!function_exists('effacer_config')) {
301 function effacer_config($cfg=''){
302 $depot = cfg_charger_depot($cfg);
303 return $depot->effacer_config();