[SPIP] +2.1.12
[velocampus/web/www.git] / www / plugins / auto / cfg / cfg_fonctions.php
1 <?php
2
3 /**
4 * Plugin générique de configuration pour SPIP
5 *
6 * @license GNU/GPL
7 * @package plugins
8 * @subpackage cfg
9 * @category outils
10 * @copyright (c) toggg, marcimat 2007-2008
11 * @link http://www.spip-contrib.net/
12 * @version $Id$
13 */
14
15 if (!defined("_ECRIRE_INC_VERSION")) return;
16
17
18 # CONFIG
19
20 /**
21 * #CONFIG retourne lire_config()
22 *
23 * Le 3eme argument permet de controler la serialisation du resultat
24 * (mais ne sert que pour le depot 'meta') qui doit parfois deserialiser
25 *
26 * ex: |in_array{#CONFIG{toto,#ARRAY,1}}.
27 *
28 * Ceci n'affecte pas d'autres depots et |in_array{#CONFIG{toto/,#ARRAY}} sera equivalent
29 * car du moment qu'il y a un /, c'est le depot 'metapack' qui est appelle.
30 *
31 * @param Object $p Arbre syntaxique du compilo
32 * @return Object
33 */
34 if (!function_exists('balise_CONFIG')) {
35 function balise_CONFIG($p) {
36 if (!$arg = interprete_argument_balise(1,$p)) {
37 $arg = "''";
38 }
39 $sinon = interprete_argument_balise(2,$p);
40 $unserialize = sinon(interprete_argument_balise(3,$p),"false");
41
42 $p->code = 'lire_config(' . $arg . ',' .
43 ($sinon && $sinon != "''" ? $sinon : 'null') . ',' . $unserialize . ')';
44
45 return $p;
46 }
47 }
48
49 # CFG_CHEMIN
50
51 /**
52 * La balise CFG_CHEMIN retourne le chemin d'une image stockee
53 * par cfg.
54 *
55 * cfg stocke : 'config/vue/champ.ext' (ce qu'affiche #CONFIG)<br>
56 * #cfg_chemin retourne l'adresse complete : 'IMG/config/vue/champ.ext'
57 *
58 * @param Object $p Arbre syntaxique du compilo
59 * @return Object
60 */
61 function balise_CFG_CHEMIN_dist($p) {
62 if (!$arg = interprete_argument_balise(1,$p)) {
63 $arg = "''";
64 }
65 $sinon = interprete_argument_balise(2,$p);
66
67 $p->code = '($l = lire_config(' . $arg . ',' .
68 ($sinon && $sinon != "''" ? $sinon : 'null') . ')) ? _DIR_IMG . $l : null';
69
70 return $p;
71 }
72
73
74 /**
75 * #EDIT_CONFIG : Pour jouer avec les Crayons et les configurations
76 *
77 * <code>
78 * <div class="#EDIT_CONFIG{plugin/casier/cle}">
79 * #CONFIG{plugin/casier/cle}
80 * </div>
81 * </code>
82 *
83 * @param <type> $p
84 * @return <type>
85 */
86 function balise_EDIT_CONFIG_dist($p) {
87 $config = interprete_argument_balise(1,$p);
88 $instance = interprete_argument_balise(2,$p);
89 $instance = $instance ? $instance : "'0'";
90 $p->code = 'classe_config_crayon('.$config.', '.$instance.')';
91 $p->statut = 'php';
92 $p->interdire_scripts = false;
93 return $p;
94 }
95
96 /**
97 * Classe qui sera utilisé pour CFG dans le bloc éditable par Crayon
98 *
99 * @param string $config
100 * @param string $instance
101 * @return string
102 */
103 function classe_config_crayon($config, $instance) {
104 // pour n'avoir que des caracteres alphanumeriques,
105 // on transforme "depot::plugin/casier/cle" en "depot___plugin__casier__cle"
106 $config = cfg_config2crayon($config);
107 // return 'crayon composant-'.$composant.'-'.$instance.' type_pinceau';
108 return 'crayon config-'.$config.'-'.$instance . ' type_config';
109 }
110
111 /**
112 * Outil de manipulation des classes
113 *
114 * @param string $config classe pour CFG
115 * @return string classe pour Crayon
116 */
117 function cfg_config2crayon($config) {return str_replace(array('::','/'), array('___','__'), $config);}
118
119 /**
120 * Outil de manipulation des classes
121 *
122 * @param string $crayon classe pour Crayon
123 * @return string classe pour CFG
124 */
125 function cfg_crayon2config($crayon) {return str_replace(array('___','__'), array('::','/'), $crayon);}
126
127 /**
128 * autorisation de configurer
129 *
130 * @param string $faire
131 * @param string $type
132 * @param string $id
133 * @param string $qui
134 * @param string $opt
135 * @return boolean
136 */
137 function autoriser_config_crayonner_dist($faire, $type, $id, $qui, $opt) {
138 return autoriser('configurer', $type, $id, $qui, $opt);
139 }
140
141 /**
142 * autorisation de crayonner
143 *
144 * @param string $faire
145 * @param string $type
146 * @param string $id
147 * @param string $qui
148 * @param string $opt
149 * @return boolean
150 */
151 function autoriser_config_modifier_dist($faire, $type, $id, $qui, $opt) {
152 return autoriser('crayonner', $type, $id, $qui, $opt);
153 }
154
155
156 # CFG_ARBO
157
158 /**
159 * Affiche une arborescence du contenu d'un #CONFIG
160 *
161 * - #CFG_ARBO,
162 * - #CFG_ARBO{ma_meta},
163 * - #CFG_ARBO{~toto},
164 * - #CFG_ARBO{ma_meta/mon_casier},
165 * - #CFG_ARBO{ma_table:mon_id/mon_champ}
166 *
167 * @param Object $p
168 * @return Object
169 */
170 function balise_CFG_ARBO_dist($p) {
171 if (!$arg = interprete_argument_balise(1,$p)) {
172 $arg = "''";
173 }
174 $p->interdire_scripts = false;
175 $p->code = 'cfg_affiche_arborescence(' . $arg . ')';
176 return $p;
177 }
178
179 /**
180 * Fonction utilisée par le compilo pour {@see balise_CFG_ARBO_dist} :
181 * affiche l'arborescence qui est calculée par {@see cfg_affiche_sous_arborescence}
182 *
183 * @param string $cfg nom de la table meta
184 * @return string code html en sortie
185 */
186 function cfg_affiche_arborescence($cfg='') {
187
188 $sortie = '';
189 $hash = substr(md5(rand()*rand()),0,6);
190
191 // integration du css
192 // Suppression de cette inclusion des css arbo au profit d'une inclusion d'un fichier cfg.css dans le header prive
193 // $sortie .= "<style type='text/css'>\n"
194 // . ".cfg_arbo{}\n"
195 // . ".cfg_arbo h5{padding:0.2em 0.2em; margin:0.2em 0; cursor:pointer;}\n"
196 // . ".cfg_arbo ul{border:1px solid #ccc; margin:0; padding:0.2em 0.5em; list-style-type:none;}\n"
197 // . "</style>\n";
198
199 // integration du js
200 $sortie .= "<script type='text/javascript'><!--
201
202 jQuery(document).ready(function(){
203 function cfg_arbo(){
204 jQuery('#cfg_arbo_$hash ul').hide();
205 jQuery('#cfg_arbo_$hash h5 strong').remove();
206 jQuery('#cfg_arbo_$hash h5')
207 .prepend('<strong>[+] <\/strong>')
208 .unbind().toggle(
209 function () {
210 jQuery(this).children('strong').text('[-] ');
211 jQuery(this).next('ul').show();
212 },
213 function () {
214 jQuery(this).children('strong').text('[+] ');
215 jQuery(this).next('ul').hide();
216 });
217 }
218 setTimeout(cfg_arbo,100);
219 });
220 // --></script>\n";
221
222 $tableau = lire_config($cfg);
223 if ($c = @unserialize($tableau)) $tableau = $c;
224
225 if (empty($cfg)) $cfg = 'spip_meta';
226 // parcours des donnees
227 $sortie .=
228 "<div class='cfg_arbo' id='cfg_arbo_$hash'>\n" .
229 cfg_affiche_sous_arborescence($cfg, $tableau) .
230 "\n</div>\n";
231
232
233 return $sortie;
234 }
235
236 /**
237 * Calcul récursif de l'arborescence des configurations
238 *
239 * @param string $nom nom de la table meta
240 * @param Array $tableau configuration
241 * @return string code html en sortie
242 */
243 function cfg_affiche_sous_arborescence($nom, $tableau){
244 $sortie = "\n<h5>$nom</h5>\n";
245 $sortie .= "\n<ul>";
246 if (is_array($tableau)){
247 ksort($tableau);
248 foreach ($tableau as $tab=>$val){
249 if (is_array($val))
250 $sortie .= "<li>" . cfg_affiche_sous_arborescence($tab, $val) . "</li>";
251 elseif (false !== $v = @unserialize($val))
252 $sortie .= "<li>" . cfg_affiche_sous_arborescence($tab, $v) . "</li>";
253 else
254 $sortie .= "<li>$tab = " . htmlentities($val, ENT_COMPAT, $GLOBALS['meta']['charset']) ."</li>\n";
255
256 }
257 } else {
258 $sortie .= "<li>$nom = " . htmlentities($val, ENT_COMPAT, $GLOBALS['meta']['charset']) . "</li>";
259 }
260 $sortie .= "</ul>\n";
261 return $sortie;
262 }
263
264
265
266
267 if (!function_exists('filtre_cle_dist')) {
268 /**
269 * Cette fonction retourne une valeur dans un tableau arborescent
270 * en indiquant la cle souhaitee. On descend dans la profondeur de
271 * l'arborescence du tableau par des slash.
272 * Si on donne un chaine serialisee en entree a la place d'un tableau,
273 * la fonction tente de la deserialiser.
274 *
275 * Exemples :
276 * $x = array("a1"=>array("b1"=>array("c1"=>3), "b2"=>4), "a2"=>8);
277 * filtre_cle_dist($x, "a2") = 8
278 * filtre_cle_dist($x, "a1") = array("b1"=>array("c1"=>3), "b2"=>4)
279 * filtre_cle_dist($x, "a1/b2") = 4
280 * filtre_cle_dist($x, "a1/b1/c1") = 3
281 *
282 * Depuis un squelette SPIP : [(#TABLEAU|cle{a1/b1/c1})]
283 *
284 * @param array/string $tab : tableau ou tableau serialise
285 * @param string $chemin : chemin d'acces a une valeur du tableau tel que "cleA/cleB/cleC"
286 * @param string $defaut : valeur a retourner par defaut, si la cle n'est pas trouvee
287 *
288 * @return la valeur correspondant a la cle demandee, $defaut sinon
289 **/
290 function filtre_cle_dist($tab, $chemin, $defaut=null) {
291 if (!$tab) {
292 return $defaut;
293 }
294 if (!is_array($tab)) {
295 if (!is_string($tab)
296 or !$tab = @unserialize($tab)
297 or !is_array($tab)
298 ) {
299 return $defaut;
300 }
301 }
302 $position = &$tab;
303 $chemins = explode('/', $chemin);
304 foreach ($chemins as $cle) {
305 if (!isset($position[$cle])) {
306 return $defaut;
307 }
308 $position = $position[$cle];
309 }
310 return $position;
311 }
312 }
313
314
315
316
317 ?>