4 * Plugin générique de configuration pour SPIP
10 * @copyright (c) toggg, marcimat 2007-2008
11 * @link http://www.spip-contrib.net/
15 if (!defined("_ECRIRE_INC_VERSION")) return;
21 * #CONFIG retourne lire_config()
23 * Le 3eme argument permet de controler la serialisation du resultat
24 * (mais ne sert que pour le depot 'meta') qui doit parfois deserialiser
26 * ex: |in_array{#CONFIG{toto,#ARRAY,1}}.
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.
31 * @param Object $p Arbre syntaxique du compilo
34 if (!function_exists('balise_CONFIG')) {
35 function balise_CONFIG($p) {
36 if (!$arg = interprete_argument_balise(1,$p)) {
39 $sinon = interprete_argument_balise(2,$p);
40 $unserialize = sinon(interprete_argument_balise(3,$p),"false");
42 $p->code
= 'lire_config(' . $arg . ',' .
43 ($sinon && $sinon != "''" ?
$sinon : 'null') . ',' . $unserialize . ')';
52 * La balise CFG_CHEMIN retourne le chemin d'une image stockee
55 * cfg stocke : 'config/vue/champ.ext' (ce qu'affiche #CONFIG)<br>
56 * #cfg_chemin retourne l'adresse complete : 'IMG/config/vue/champ.ext'
58 * @param Object $p Arbre syntaxique du compilo
61 function balise_CFG_CHEMIN_dist($p) {
62 if (!$arg = interprete_argument_balise(1,$p)) {
65 $sinon = interprete_argument_balise(2,$p);
67 $p->code
= '($l = lire_config(' . $arg . ',' .
68 ($sinon && $sinon != "''" ?
$sinon : 'null') . ')) ? _DIR_IMG . $l : null';
75 * #EDIT_CONFIG : Pour jouer avec les Crayons et les configurations
78 * <div class="#EDIT_CONFIG{plugin/casier/cle}">
79 * #CONFIG{plugin/casier/cle}
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.')';
92 $p->interdire_scripts
= false;
97 * Classe qui sera utilisé pour CFG dans le bloc éditable par Crayon
99 * @param string $config
100 * @param string $instance
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';
112 * Outil de manipulation des classes
114 * @param string $config classe pour CFG
115 * @return string classe pour Crayon
117 function cfg_config2crayon($config) {return str_replace(array('::','/'), array('___','__'), $config);}
120 * Outil de manipulation des classes
122 * @param string $crayon classe pour Crayon
123 * @return string classe pour CFG
125 function cfg_crayon2config($crayon) {return str_replace(array('___','__'), array('::','/'), $crayon);}
128 * autorisation de configurer
130 * @param string $faire
131 * @param string $type
137 function autoriser_config_crayonner_dist($faire, $type, $id, $qui, $opt) {
138 return autoriser('configurer', $type, $id, $qui, $opt);
142 * autorisation de crayonner
144 * @param string $faire
145 * @param string $type
151 function autoriser_config_modifier_dist($faire, $type, $id, $qui, $opt) {
152 return autoriser('crayonner', $type, $id, $qui, $opt);
159 * Affiche une arborescence du contenu d'un #CONFIG
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}
170 function balise_CFG_ARBO_dist($p) {
171 if (!$arg = interprete_argument_balise(1,$p)) {
174 $p->interdire_scripts
= false;
175 $p->code
= 'cfg_affiche_arborescence(' . $arg . ')';
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}
183 * @param string $cfg nom de la table meta
184 * @return string code html en sortie
186 function cfg_affiche_arborescence($cfg='') {
189 $hash = substr(md5(rand()*rand()),0,6);
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"
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"
200 $sortie .= "<script type='text/javascript'><!--
202 jQuery(document).ready(function(){
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>')
210 jQuery(this).children('strong').text('[-] ');
211 jQuery(this).next('ul').show();
214 jQuery(this).children('strong').text('[+] ');
215 jQuery(this).next('ul').hide();
218 setTimeout(cfg_arbo,100);
222 $tableau = lire_config($cfg);
223 if ($c = @unserialize
($tableau)) $tableau = $c;
225 if (empty($cfg)) $cfg = 'spip_meta';
226 // parcours des donnees
228 "<div class='cfg_arbo' id='cfg_arbo_$hash'>\n" .
229 cfg_affiche_sous_arborescence($cfg, $tableau) .
237 * Calcul récursif de l'arborescence des configurations
239 * @param string $nom nom de la table meta
240 * @param Array $tableau configuration
241 * @return string code html en sortie
243 function cfg_affiche_sous_arborescence($nom, $tableau){
244 $sortie = "\n<h5>$nom</h5>\n";
246 if (is_array($tableau)){
248 foreach ($tableau as $tab=>$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>";
254 $sortie .= "<li>$tab = " . htmlentities($val, ENT_COMPAT
, $GLOBALS['meta']['charset']) ."</li>\n";
258 $sortie .= "<li>$nom = " . htmlentities($val, ENT_COMPAT
, $GLOBALS['meta']['charset']) . "</li>";
260 $sortie .= "</ul>\n";
267 if (!function_exists('filtre_cle_dist')) {
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.
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
282 * Depuis un squelette SPIP : [(#TABLEAU|cle{a1/b1/c1})]
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
288 * @return la valeur correspondant a la cle demandee, $defaut sinon
290 function filtre_cle_dist($tab, $chemin, $defaut=null) {
294 if (!is_array($tab)) {
296 or !$tab = @unserialize
($tab)
303 $chemins = explode('/', $chemin);
304 foreach ($chemins as $cle) {
305 if (!isset($position[$cle])) {
308 $position = $position[$cle];