source: trunk/spip/esqueleto-redcta/plugins/crayons/action/crayons_html.php @ 69

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

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

File size: 10.9 KB
Line 
1<?php
2
3if (!defined("_ECRIRE_INC_VERSION")) return;
4
5function affiche_controleur($class, $c=null) {
6        $return = array('$erreur'=>'');
7
8        if (preg_match(_PREG_CRAYON, $class, $regs)) {
9                list(,$nomcrayon,$type,$champ,$id) = $regs;
10                $regs[] = $class;
11                include_spip('inc/autoriser');
12                if (!autoriser('modifier',$type, $id, NULL, array('champ'=>$champ))) {
13                        $return['$erreur'] = "$type $id: " . _U('crayons:non_autorise');
14                } else {
15                        $f = charger_fonction($type.'_'.$champ, 'controleurs', true)
16                        OR $f = charger_fonction($champ, 'controleurs', true)
17                        OR $f = charger_fonction($type, 'controleurs', true)
18                        OR $f = 'controleur_dist';
19                        list($html,$status) = $f($regs, $c);
20                        if ($status) {
21                                $return['$erreur'] = $html;
22                        } else {
23                                $return['$html'] = $html;
24                        }
25                }
26        } else {
27                $return['$erreur'] = _U('crayons:donnees_mal_formatees');
28        }
29        return $return;
30}
31
32function controleur_dist($regs, $c=null) {
33        list( , $nomcrayon, $type, $champ, $id, $class) = $regs;
34        $options = array(
35                'class' => $class
36        );
37        // Si le controleur est un squelette html, on va chercher
38        // les champs qu'il lui faut dans la table demandee
39        // Attention, un controleur multi-tables ne fonctionnera
40        // que si les champs ont le meme nom dans toutes les tables
41        // (par exemple: hyperlien est ok, mais pas nom)
42        if (($fichier = find_in_path(
43        ($controleur = 'controleurs/' . $type . '_' . $champ) . '.html'))
44        || ($fichier = find_in_path(
45        ($controleur = 'controleurs/' . $champ) .'.html'))) {
46                if (!lire_fichier($fichier, $controldata))
47                        die('erreur lecture controleur');
48                if (preg_match_all('/\bname=(["\'])#ENV\{name_(\w+)\}\1/',
49                $controldata, $matches, PREG_PATTERN_ORDER)) {
50                        $champ = $matches[2];
51                }
52        } else {
53                $controleur = '';
54        }
55
56        $valeur = valeur_colonne_table($type, $champ, $id);
57
58        #spip_log("$valeur = valeur_colonne_table($type, $champ, $id);");
59        #spip_log($champ);
60
61        if ($valeur === false) {
62                return array("$type $id $champ: " . _U('crayons:pas_de_valeur'), 6);
63        }
64/*      if (is_scalar($valeur)) {
65                $valeur = array($champ => $valeur);
66        }*/
67
68        // type du crayon (a revoir quand le core aura type ses donnees)
69        $inputAttrs = array();
70        if ($controleur) {
71                $options['hauteurMini'] = 80; // base de hauteur mini
72                $option['inmode'] = 'controleur';
73                $options['controleur'] = $controleur;
74        } else
75        // si la valeur fait plusieurs lignes on doit mettre un textarea
76        if (
77        preg_match(",[\n\r],", $valeur[$champ])
78                OR
79        // on regarde le type tel que defini dans serial
80        // (attention il y avait des blob dans les vieux spip)
81        ($sqltype = colonne_table($type, $champ)) &&
82           ( in_array($sqltype['type'] , array('mediumtext', 'longblob', 'longtext')) ||
83           (($sqltype['type'] == 'text' || $sqltype['type'] == 'blob') && in_array($champ, array('descriptif', 'bio'))))) {
84                $options['hauteurMini'] = 80; // hauteur mini d'un textarea
85                $option['inmode'] = 'texte';
86        } else { // ligne, hauteur naturelle
87                $options['hauteurMaxi'] = 0;
88                $option['inmode'] = 'ligne';
89                if ($sqltype['long']) {
90                        $inputAttrs['maxlength'] = is_array($sqltype['long']) ?
91                                        $sqltype['long'][0] : $sqltype['long'];
92                }
93        }
94
95        $crayon = new Crayon($nomcrayon, $valeur, $options, $c);
96        $inputAttrs['style'] = join($crayon->styles);
97
98        if (!$controleur) {
99                $inputAttrs['style'] .= 'width:' . $crayon->largeur . 'px;' .
100                 ($crayon->hauteur ? ' height:' . $crayon->hauteur . 'px;' : '');
101        }
102
103        $html = $controleur ? $crayon->formulaire(null, $inputAttrs) :
104                                        $crayon->formulaire($option['inmode'], $inputAttrs);
105        $status = NULL;
106
107        return array($html,$status);
108}
109
110// Definition des crayons
111class Crayon {
112        // le nom du crayon "type-modele-id" comme "article-introduction-237"
113        var $name;
114        // type, a priori une table, extrait du nom
115        var $type;
116        // modele, un champ comme "texte" ou un modele, extrait du nom
117        var $modele;
118        // l'identificateur dans le type, comme un numero d'article
119        var $id;
120        // la ou les valeurs des champs du crayon, tableau associatif champ => valeur
121        var $texts = array();
122        // une cle unique pour chaque crayon demande
123        var $key;
124        // un md5 associe aux valeurs pour verifier et detecter si elles changent
125        var $md5;
126        // dimensions indicatives
127        var $largeurMini = 170;
128        var $largeurMaxi = 700;
129        var $hauteurMini = 80;
130        var $hauteurMaxi = 700;
131        var $largeur;
132        // le mode d'entree: texte, ligne ou controleur
133        var $inmode = '';
134        // eventuellement le fond modele pour le controleur
135        var $controleur = '';
136        var $styles = array();
137
138        // le constructeur du crayon
139        // $name : son nom
140        // $texts : tableau associatif des valeurs ou valeur unique si crayon monochamp
141        // $options : options directes du crayon (developpement)
142        function Crayon($name, $texts = array(), $options = array(), $c=null) {
143                $this->name = $name;
144                list($this->type, $this->modele, $this->id) = explode('-', $this->name, 3);
145                if (is_scalar($texts) || is_null($texts)) {
146                        $texts = array($this->modele => $texts);
147                }
148                $this->texts = $texts;
149                $this->key = strtr(uniqid('wid', true), '.', '_');
150                $this->md5 = $this->md5();
151                foreach ($options as $opt=>$val) {
152                        $this->$opt = $val;
153                }
154                $this->dimension($c);
155                $this->css();
156        }
157
158        // calcul du md5 associe aux valeurs
159        function md5() {
160                return md5(serialize($this->texts));
161        }
162
163        // dimensions indicatives
164        function dimension($c) {
165                // largeur du crayon
166                $this->largeur = min(max(intval(_request('w', $c)),
167                                        $this->largeurMini), $this->largeurMaxi);
168                // hauteur maxi d'un textarea selon wh: window height
169                $maxheight = min(max(intval(_request('wh', $c)) - 50, 400), $this->hauteurMaxi);
170                $this->hauteur = min(max(intval(_request('h', $c)), $this->hauteurMini), $maxheight);
171        }
172
173        // recuperer les elements de style
174        function css() {
175                foreach(array('color', 'font-size', 'font-family', 'font-weight', 'line-height') as $property) {
176                        if (null !== ($p = _request($property)))
177                                $this->styles[] = "$property:$p;";
178                }
179               
180                $property = 'background-color';
181                if (!$p = _request($property)
182                OR $p == 'transparent') {
183                        $p = 'white';
184                }
185                $this->styles[] = "$property:$p;";
186        }
187
188        // formulaire standard
189        function formulaire($contexte = array(), $inputAttrs = array()) {
190                return
191                        $this->code() .
192                        $this->input($contexte, $inputAttrs);
193        }
194
195        // balises input type hidden d'identification du crayon
196        function code() {
197                return
198                 '<input type="hidden" class="crayon-id" name="crayons[]"'
199                .' value="'.$this->key.'" />'."\n"
200                . '<input type="hidden" name="name_'.$this->key
201                .'" value="'.$this->name.'" />'."\n"
202                . '<input type="hidden" name="class_' . $this->key
203                . '" value="' . $this->class . '" />' . "\n"
204                . '<input type="hidden" name="md5_'.$this->key
205                .'" value="'.$this->md5.'" />'."\n"
206                . '<input type="hidden" name="fields_'.$this->key
207                .'" value="'.join(',',array_keys($this->texts)).'" />'
208                ."\n"
209                ;
210        }
211
212/*
213 Fabriquer les balises des champs d'apres un modele controleurs/(type_)modele.html
214        $contexte est un tableau (nom=>valeur) qui sera enrichi puis passe à recuperer_fond
215*/
216        function fond($contexte = array()) {
217                include_spip('inc/filtres');
218                $contexte['id_' . $this->type] = $this->id;
219                $contexte['lang'] = $GLOBALS['spip_lang'];
220                $contexte['key'] = $this->key;
221                $contexte['largeur'] = $this->largeur;
222                $contexte['hauteur'] = $this->hauteur;
223                $contexte['self'] = _request('self');
224                foreach ($this->texts as $champ => $val) {
225                        $contexte['name_' . $champ] = 'content_' . $this->key . '_' . $champ;
226                }
227                $contexte['style'] = join(' ',$this->styles);
228                include_spip('public/assembler');
229                return recuperer_fond($this->controleur, $contexte);
230        }
231
232/*
233 Fabriquer les balises du ou des champs
234        $spec est soit un scalaire 'ligne' ou 'texte' précisant le type de balise
235        soit un array($champ=>array('type'=>'...', 'attrs'=>array(attributs specifique du champs)))
236        $attrs est un tableau (attr=>val) d'attributs communs ou pour le champs unique
237*/
238        function input($spec = 'ligne', $attrs = array()) {
239                if ($this->controleur) {
240                        return $this->fond($spec);
241                }
242                include_spip('inc/filtres');
243                $return = '';
244                foreach ($this->texts as $champ => $val) {
245                        $type = is_array($spec) ? $spec[$champ]['type'] : $spec;
246                        switch ($type) {
247                                case 'texte':
248                                        $id = uniqid('wid');
249                                        $input = '<textarea style="width:100%;" class="crayon-active"'
250                                        . ' name="content_'.$this->key.'_'.$champ.'" id="'.$id.'">'
251                                        . entites_html($val)
252                                        . "</textarea>\n";
253
254                                        // petit truc crado pour mettre la barre typo si demandee
255                                        // pour faire propre il faudra reprogrammer la bt en jquery
256                                        $meta_crayon = unserialize($GLOBALS['meta']['crayons']);
257                                        if ($meta_crayon['barretypo']) {
258                                                include_spip('inc/barre');
259                                                $input = "<span style='width:100%; height:20px;'>"
260                                                        . afficher_barre("document.getElementById('$id')")
261                                                        . '</span>'
262                                                        . $input;
263                                        }
264
265                                        break;
266                                case 'ligne':
267                                default:
268                                        $input = '<input class="crayon-active" type="text"'
269                                        . ' name="content_'.$this->key.'_'.$champ.'"'
270                                        . ' value="'
271                                        . entites_html($val)
272                                        . '" />'."\n";
273                        }
274                        if (is_array($spec) && isset($spec[$champ]['attrs'])) {
275                                foreach ($spec[$champ]['attrs'] as $attr=>$val) {
276                                        $input = inserer_attribut($input, $attr, $val);
277                                }
278                        }
279
280                        foreach ($attrs as $attr=>$val) {
281                                $input = inserer_attribut($input, $attr, $val);
282                        }
283                        $return .= $input;
284                }
285                return $return;
286        }
287
288}
289
290
291/*
292        Fabriquer les boutons du formulaire
293*/
294function crayons_boutons($boutons = array()) {
295        $crayonsImgPath = dirname(url_absolue(find_in_path('images/cancel.png')));
296        $boutons['submit'] = array('ok', texte_backend(_T('bouton_enregistrer')));
297        $boutons['cancel'] = array('cancel', texte_backend(_T('crayons:annuler')));
298
299        $html = '';
300        foreach ($boutons as $bnam => $bdef) if ($bdef) {
301                $html .= '<a class="crayon-' . $bnam .
302                        '" title="' . $bdef[1] . '"';
303                if (!empty($bdef[2])) {
304                        $html .= ' href="' . $bdef[2] . '"';
305                }
306                $html .= '><img src="' . $crayonsImgPath . '/' .
307                        $bdef[0] . '.png" width="20" height="20" /></a>';
308        }
309
310        if ($html)
311                return '<div class="crayon-boutons"><div>'.$html.'</div></div>';
312}
313
314function crayons_formulaire($html) {
315        if (!$html)
316                return '';
317
318        include_spip('inc/filtres');
319        return liens_absolus(
320                '<form class="formulaire_crayon" method="post" action="'
321                . url_absolue(parametre_url(self(),'action', 'crayons_store'))
322                . '" enctype="multipart/form-data">'
323                . $html
324                . crayons_boutons()
325                . '</form>'
326        );
327}
328
329//
330// Un Crayon avec une verification de code de securite
331//
332class SecureCrayon extends Crayon {
333
334        function SecureCrayon($name, $text='') {
335                parent::Crayon($name, $text);
336        }
337
338        function code() {
339                $code = parent::code();
340                $secu = md5($GLOBALS['meta']['alea_ephemere']. '=' . $this->name);
341
342                return
343                        $code
344                        .'<input type="hidden" name="secu_'.$this->key.'" value="'.$secu.'" />'."\n";
345        }
346}
347
348function action_crayons_html_dist() {
349
350        header("Content-Type: text/html; charset=".$GLOBALS['meta']['charset']);
351
352        // CONTROLEUR
353        // on affiche le formulaire demande
354        include_spip('inc/crayons');
355        lang_select($GLOBALS['auteur_session']['lang']);
356        $return = affiche_controleur(_request('class'));
357        $return['$html'] = crayons_formulaire($return['$html']);
358
359        echo var2js($return);
360        exit;
361}
362
363?>
Note: See TracBrowser for help on using the repository browser.