107328c80c48ab1bf66a7646d3ecc6e8ecfaacaf
[velocampus/web/www.git] / www / plugins / auto / cfg / inc / cfg_config.php
1 <?php
2 /**
3 * Plugin générique de configuration pour SPIP
4 *
5 * @license GNU/GPL
6 * @package plugins
7 * @subpackage cfg
8 * @category outils
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 $
12 */
13
14 if (!defined("_ECRIRE_INC_VERSION")) return;
15
16 /**
17 * charge le depot qui va bien en fonction de l'argument demande
18 *
19 * exemples :
20 * - meta::description
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
25 *
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
29 * - sinon metapack
30 *
31 * @param string $args
32 * @return Object
33 */
34 function cfg_charger_depot($args){
35 $r = explode('::',$args,2);
36 if (count($r) > 1)
37 list($depot,$args) = $r;
38 else {
39 // si un seul argument, il faut trouver le depot
40 $depot = cfg_charger_depot_args($args);
41 }
42 $depot = new cfg_depot($depot);
43 $depot->charger_args($args);
44 return $depot;
45 }
46
47 function cfg_charger_depot_args($args){
48
49 if ($args[0] == '~'){
50 return'tablepack';
51 } elseif (
52 (list($head, ) = explode('/',$args,2)) &&
53 (strpos($head,':') !== false)) {
54 return'tablepack';
55 } else {
56 if (strpos($args,'/') !== false)
57 return'metapack';
58 else
59 return'meta';
60 }
61 }
62
63
64 /**
65 * cette classe charge les fonctions de lecture et ecriture d'un depot (dans depots/)
66 *
67 * Ces depots ont une version qui evoluera en fonction si des changements d'api apparaissent
68 *
69 * version 2 (fonctions)
70 * - charger_args
71 * - lire, ecrire, effacer
72 *
73 * @package plugins
74 * @subpackage cfg
75 */
76 class cfg_depot{
77
78 /**
79 * Le nom de la classe du dépôt
80 * @var string
81 */
82 var $nom;
83
84 /**
85 * Le dépôt
86 * @var Object
87 */
88 var $depot;
89
90 /*
91 *
92 * Constructeur de la classe
93 *
94 * 'depot' est le nom du fichier php stocke dans /depots/{depot}.php
95 * qui contient une classe 'cfg_depot_{depot}'
96 *
97 * $params est un tableau de parametres passes a la classe cfg_depot_{depot} qui peut contenir :
98 * <code>
99 * 'champs' => array(
100 * 'nom'=>array(
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)
106 * ),
107 * 'champs_id' => array(
108 * cle => 'nom' // nom d'un champ de type id
109 * ),
110 * 'param' => array(
111 * 'parametre_cfg' => 'valeur' // les parametres <!-- param=valeur --> passes dans les formulaires cfg
112 * ),
113 * 'val' => array(
114 * 'nom' => 'valeur' // les valeurs des champs sont stockes dedans
115 * )
116 * );
117 * </code>
118 *
119 * @param string $depot
120 * @param Array $params
121 */
122 function cfg_depot($depot='metapack', $params=array()){
123 if (!isset($params['param'])) {
124 $params['param'] = array();
125 }
126
127 include_spip('depots/'.$depot);
128 if (!class_exists($class = 'cfg_depot_'.$depot)) {
129 die("CFG ne trouve pas le d&eacute;pot $depot");
130 }
131 $this->depot = new $class($params);
132 $this->version = $this->depot->version;
133 $this->nom = $depot;
134 }
135
136 /**
137 * ajoute les parametres transmis dans l'objet du depot
138 *
139 * @param Array $params
140 */
141 function add_params($params){
142 foreach ($params as $o=>$v) {
143 $this->depot->$o = $v;
144 }
145 }
146
147 /**
148 * récupérer les enregistrements des différents champs.
149 * @param Array $params
150 * @return Array
151 */
152 function lire($params = array()){
153 $this->add_params($params);
154 return $this->depot->lire(); // array($ok, $val, $messages)
155 }
156
157 /**
158 * ecrit chaque enregistrement pour chaque champ.
159 * @param Array $params
160 * @return Array
161 */
162 function ecrire($params = array()){
163 $this->add_params($params);
164 return $this->depot->ecrire(); // array($ok, $val, $messages)
165 }
166
167 /**
168 * supprime chaque enregistrement pour chaque champ.
169 * @param Array $params
170 * @return Array
171 */
172 function effacer($params = array()){
173 $this->add_params($params);
174 return $this->depot->effacer(); // array($ok, $val, $messages)
175 }
176
177 /**
178 * Lecture de la configuration
179 *
180 * @param boolean $unserialize
181 * @return string
182 */
183 function lire_config($unserialize=true){
184 list($ok, $s) = $this->depot->lire($unserialize);
185 if ($ok && ($nom = $this->nom_champ())) {
186 return $s[$nom];
187 } elseif ($ok) {
188 return $s;
189 }
190 }
191
192 /**
193 * enregistrer une configuration
194 *
195 * @param mixed $valeur
196 * @return boolean
197 */
198 function ecrire_config($valeur){
199 if ($nom = $this->nom_champ()) {
200 $this->depot->val = array($nom=>$valeur);
201 } else {
202 $this->depot->val = $valeur;
203 }
204 list($ok, $s) = $this->depot->ecrire();
205 return $ok;
206 }
207
208 /**
209 * supprimer une config
210 *
211 * @return boolean
212 */
213 function effacer_config(){
214 if ($nom = $this->nom_champ()){
215 $this->depot->val[$nom] = false;
216 } else {
217 $this->depot->val = null;
218 }
219 list($ok, $s) = $this->depot->effacer();
220 return $ok;
221
222 }
223
224 /**
225 * le nom d'un champ s'il est dans le dépôt
226 * @return boolean|string
227 */
228 function nom_champ(){
229 if (count($this->depot->champs)==1){
230 foreach ($this->depot->champs as $nom=>$def){
231 return $nom;
232 }
233 }
234 return false;
235 }
236
237 /**
238 * charge les arguments d'un lire/ecrire/effacer_config
239 * dans le depot : lire_config($args = 'metapack::prefixe/casier/champ');
240 *
241 * @param Array $args
242 * @return boolean
243 */
244 function charger_args($args){
245 if (method_exists($this->depot, 'charger_args')){
246 return $this->depot->charger_args($args);
247 }
248 return false;
249 }
250 }
251
252
253
254 /**
255 * Lecture de la configuration
256 *
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>
262 *
263 * $unserialize est mis par l'histoire, et affecte le depot 'meta'
264 *
265 * @param string $cfg la config
266 * @param mixed $def un defaut optionnel
267 * @param boolean $unserialize n'affecte que le depot 'meta'
268 * @return string
269 */
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;
275 return $r;
276 }
277 }
278
279 /**
280 * enregistrer une configuration
281 *
282 * @param string $cfg
283 * @param mixed $valeur
284 * @return boolean
285 */
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);
290 }
291 }
292
293
294 /**
295 * supprimer une config
296 *
297 * @param string $cfg
298 * @return boolean
299 */
300 if (!function_exists('effacer_config')) {
301 function effacer_config($cfg=''){
302 $depot = cfg_charger_depot($cfg);
303 return $depot->effacer_config();
304 }
305 }
306
307
308
309 ?>