source: trunk/spip/esqueleto-redcta/plugins/crayons/tetecrayons.php @ 357

Last change on this file since 357 was 69, checked in by guille, 16 years ago

Se agrego Plugin Crayon y se actualizaron los esqueletos para que funcione

File size: 7.6 KB
Line 
1<?php
2
3/* inserer le css et le js externes pour Crayons dans le <head>
4 *
5 *  Crayons plugin for spip (c) Fil, toggg 2006-2007 -- licence GPL
6 */
7
8# mettre a true dans mes_options pour avoir les crayons non compresses
9@define('_DEBUG_CRAYONS', false);
10
11// Dire rapidement si ca vaut le coup de chercher des droits
12function analyse_droits_rapide_dist() {
13    return isset($GLOBALS['auteur_session']['statut']);
14}
15
16// Le pipeline header_prive (pour y tester les crayons)
17function Crayons_insert_head($head) {
18    include_spip('cfg_options');
19
20    //par défaut, pas de traitement sur les entetes
21    $res = $head;
22   
23    //vérifie la présence de cfg, si pas de cfg on cherche meme pas à faire de l'espace privé
24    if (!function_exists('lire_config')) {
25        return $res;
26    }
27   
28   
29    //vérifie que l'éditiojn de l'espace privé est autorisé
30    if (lire_config('crayons/espaceprive') == 'on') {
31        //determine les pages (exec) crayonnables
32        if (in_array(_request('exec'), explode(',',lire_config('crayons/exec_autorise')))) {
33            //Calcul des droits
34            include_spip('inc/crayons');
35            $res = Crayons_preparer_page($head, '*', wdgcfg(), 'head');
36        } 
37    }
38   
39    //retourne l'entete modifiée
40    return $res;
41       
42    /*
43    // Est-on autorise ?
44    include_spip('inc/autoriser');
45    switch (_request('exec')) {
46        case 'articles':
47            $droits = autoriser('modifier', 'article', _request('id_article'));
48            break;
49
50        default:
51            $droits=false;
52    }
53
54    if (!$droits)
55        return $head;
56    */
57
58}
59
60// Le pipeline affichage_final, execute a chaque hit sur toute la page
61function &Crayons_affichage_final(&$page) {
62
63    // ne pas se fatiguer si le visiteur n'a aucun droit
64    if (!(function_exists('analyse_droits_rapide')?analyse_droits_rapide():analyse_droits_rapide_dist()))
65        return $page;
66
67    // sinon regarder rapidement si la page a des classes crayon
68    if (strpos($page, 'crayon')===FALSE)
69        return $page;
70
71    // voir un peu plus precisement lesquelles
72    include_spip('inc/crayons');
73    if (!preg_match_all(_PREG_CRAYON, $page, $regs, PREG_SET_ORDER))
74        return $page;
75    $wdgcfg = wdgcfg();
76
77    // calculer les droits sur ces crayons
78    include_spip('inc/autoriser');
79    $droits = array();
80    $droits_accordes = 0;
81    foreach ($regs as $reg) {
82        list(,$crayon,$type,$champ,$id) = $reg;
83        if (autoriser('modifier', $type, $id, NULL, array('champ'=>$champ))) {
84            $droits['.' . $crayon]++;
85            $droits_accordes ++;
86        }
87    }
88    // et les signaler dans la page
89    if ($droits_accordes == count($regs)) // tous les droits
90        $page = Crayons_preparer_page($page, '*', $wdgcfg);
91    else if ($droits) // seulement certains droits, preciser lesquels
92        $page = Crayons_preparer_page($page, join(',',array_keys($droits)), $wdgcfg);
93
94    return $page;
95}
96
97function &Crayons_preparer_page(&$page, $droits, $wdgcfg = array(), $mode='page') {
98        lang_select($GLOBALS['auteur_session']['lang']);
99
100        $jsFile = generer_url_public('crayons.js');
101        if (_DEBUG_CRAYONS)
102                $jsFile = parametre_url($jsFile,'debug_crayons',1);
103        include_spip('inc/filtres'); // rien que pour direction_css() :(
104        $cssFile = direction_css(find_in_path('crayons.css'));
105
106        $config = var2js(array(
107                'imgPath' => dirname(find_in_path('images/crayon.png')),
108                'droits' => $droits,
109                'dir_racine' => _DIR_RACINE,
110                'self' => self('&'),
111                'txt' => array(
112                        'error' => _U('crayons:svp_copier_coller'),
113                        'sauvegarder' => $wdgcfg['msgAbandon'] ? _U('crayons:sauvegarder') : ''
114                ),
115                'img' => array(
116                        'searching' => array(
117                                'txt' => _U('crayons:veuillez_patienter')
118                        ),
119                        'crayon' => array(
120                                'txt' => _U('crayons:editer')
121                        ),
122                        'edit' => array(
123                                'txt' => _U('crayons:editer_tout')
124                        ),
125                        'img-changed' => array(
126                                'txt' => _U('crayons:deja_modifie')
127                        )
128                ),
129                'cfg' => $wdgcfg
130        ));
131
132
133        $incHead = <<<EOH
134
135<link rel="stylesheet" href="{$cssFile}" type="text/css" media="all" />
136<script src="{$jsFile}" type="text/javascript"></script>
137<script type="text/javascript"><!--
138    var configCrayons = new cQuery.prototype.cfgCrayons({$config});
139    ( window.jQuery ? jQuery : cQuery )(document).ready(cQuery.fn.crayonsstart);
140    // cQuery.ready() plante le jQuery.ready() sous MSIE
141//-->
142</script>
143EOH;
144
145        if ($mode == 'head')
146                return $page . $incHead;
147
148        $pos_head = strpos($page, '</head>');
149        if ($pos_head === false)
150                return $page;
151
152        return substr_replace($page, $incHead, $pos_head, 0);
153
154}
155
156// #EDIT{ps} pour appeler le crayon ps ;
157// si cette fonction est absente, balise_EDIT_dist() met a vide
158function balise_EDIT($p) {
159
160        $i_boucle = $p->nom_boucle ? $p->nom_boucle : $p->id_boucle;
161        // #EDIT hors boucle ? ne rien faire
162        if (!$type = $p->boucles[$i_boucle]->type_requete) {
163                $p->code = "''";
164                $p->interdire_scripts = false;
165                return $p;
166        }
167
168        // crayon sur une base distante 'nua:article-intro-5'
169        // on ne sait pas encore les gerer, mais au moins on les detecte
170        if ($distant = $p->boucles[$i_boucle]->sql_serveur)
171                $type = "$distant:$type";
172
173        // le compilateur 1.9.2 ne calcule pas primary pour les tables secondaires
174        if (!($primary = $p->boucles[$i_boucle]->primary)) {
175                list($nom, $desc) = trouver_def_table(
176                        $p->boucles[$i_boucle]->type_requete, $p->boucles[$i_boucle]);
177                $primary = $desc['key']['PRIMARY KEY'];
178        }
179        $primary = explode(',',$primary);
180        $id = array();
181        foreach($primary as $key)
182                $id[] = champ_sql(trim($key),$p);
183        $primary = implode(".'-'.",$id);
184        $p->code = "classe_boucle_crayon('"
185                . $type
186                ."',"
187                .sinon(interprete_argument_balise(1,$p),"''")
188                .","
189                . $primary
190                .").' '";
191        $p->interdire_scripts = false;
192        return $p;
193}
194
195function creer_le_crayon($type, $champ, $id) {
196        include_spip('inc/crayons');
197        include_spip('action/crayons_html');
198        $a = affiche_controleur("crayon $type-$champ-$id",
199                array('w' => 485, 'h' => 300, 'wh' => 500)
200        );
201        return $a['$erreur'] ? $a['$erreur'] : $a['$html'];
202}
203
204// #CRAYON{ps} pour creer le crayon ps SI ?edit=1;
205function balise_CRAYON($p) {
206
207        $i_boucle = $p->nom_boucle ? $p->nom_boucle : $p->id_boucle;
208        // #CRAYON hors boucle ? ne rien faire
209        if (!$type = $p->boucles[$i_boucle]->type_requete) {
210                $p->code = "''";
211                $p->interdire_scripts = false;
212                return $p;
213        }
214
215        // crayon sur une base distante 'nua:article-intro-5'
216        // on ne sait pas encore les gerer, mais au moins on les detecte
217        if ($distant = $p->boucles[$i_boucle]->sql_serveur)
218                $type = "$distant:$type";
219
220        // le compilateur 1.9.2 ne calcule pas primary pour les tables secondaires
221        if (!($primary = $p->boucles[$i_boucle]->primary)) {
222                list($nom, $desc) = trouver_def_table(
223                        $p->boucles[$i_boucle]->type_requete, $p->boucles[$i_boucle]);
224                $primary = $desc['key']['PRIMARY KEY'];
225        }
226        $primary = explode(',',$primary);
227        $id = array();
228        foreach($primary as $key)
229                $id[] = champ_sql(trim($key),$p);
230        $primary = implode(".'-'.",$id);
231        $p->code = "creer_le_crayon('"
232                . $type
233                ."',"
234                .sinon(interprete_argument_balise(1,$p),"''")
235                .","
236                . $primary
237                .").' '";
238        $p->interdire_scripts = false;
239        return $p;
240}
241
242
243// Donne la classe crayon en fonction
244// - du type de la boucle
245// (attention aux exceptions pour #EDIT dans les boucles HIERARCHIE et SITES)
246// - du champ demande (vide, + ou se terminant par + : (+)classe type--id)
247// - de l'id courant
248function classe_boucle_crayon($type, $champ, $id) {
249        $type = $type[strlen($type) - 1] == 's' ?
250                        substr($type, 0, -1) : 
251                        str_replace(
252                                array('hierarchie', 'syndication'),
253                                array('rubrique',   'site'),
254                                $type);
255
256        $plus = (substr($champ, -1) == '+' AND $champ = substr($champ, 0, -1))
257                ? " $type--$id"
258                : '';
259
260        return 'crayon ' . $type . '-' . $champ . '-' . $id . $plus;
261}
262
263?>
Note: See TracBrowser for help on using the repository browser.