source: trunk/spip/esqueleto-redcta/plugins/magusine/exec/gabarit.php @ 80

Last change on this file since 80 was 80, checked in by guille, 15 years ago

el plugin completo

  • Property svn:executable set to *
File size: 19.0 KB
Line 
1<?php
2
3include_spip('inc/xml-parser');
4//require(_DIR_PLUGIN_ARTY.'/inc/xml-parser.php');
5
6if (!defined("_ECRIRE_INC_VERSION")) return;
7include_spip('inc/presentation');
8
9function exec_gabarit() {
10
11  global $connect_statut;
12  if ($connect_statut != "0minirezo" ) {
13    echo "<p><b>"._T('arty:acces_a_la_page')."</b></p>";
14          fin_page();
15          exit;
16  }
17
18if ($_GET['mode']=="ajax") {
19        //print_r($_POST);
20        $gabarit=$_POST['gabarit'];
21        // correspondance entre conteneur et valeur numerique
22                $colonnes=array("contexte1" => 1,"contexte2" => 2,"corps" => 3,"reserve" =>0,"avance"=> 4);
23       
24        foreach(array_keys($colonnes) as $colonne) {
25        if (in_array($colonne, array_keys($_POST)))     {
26                foreach($_POST[$colonne] as $ordre => $bloc) {
27                       
28                        // est-ce qu'il y a un parametre pour ce bloc ?
29                        if ($_POST[$bloc]) {
30                                $parametre=addslashes($_POST[$bloc]);
31                                } else { $parametre =""; }
32                       
33                        $bloc=addslashes($bloc);
34                        // fait la correspondance entre le nom du conteneur et son equivalent numerique (0,1 etc.) pour entrer dans la base
35                        $colonnenum = $colonnes[$colonne];
36                        $ordre =(int) $ordre;
37                        $resultat = spip_query("SELECT id_bloc FROM spip_arty_gabarit_ordre WHERE gabarit='$gabarit' AND nom='$bloc'");
38                       
39                        //echo "sommaire : colonne :".$colonne." et bloc : ".$bloc." et ordre :".$ordre."\n";
40                        if(spip_num_rows($resultat)) {
41                                $row=spip_fetch_array($resultat);
42                                spip_query("UPDATE spip_arty_gabarit_ordre SET ordre=$ordre, conteneur=$colonnenum, param='$parametre' WHERE id_bloc=".$row['id_bloc']);
43                                       
44                        } else {
45                                spip_query("INSERT INTO spip_arty_gabarit_ordre (nom, ordre, conteneur, gabarit, param) VALUES ('$bloc', $ordre, $colonnenum, '$gabarit', '$parametre') ");
46
47                        }
48                }       
49        }
50       
51        }
52       
53echo _T("arty:le_changement_a_ete_effectue");   
54//print_r($_POST);
55return FALSE;
56}
57
58if ($_POST['action'] == 'supprimer') {
59  $gabarit = isset($_POST['gabarit']) ? addslashes($_POST['gabarit']) : false;
60 
61  if ($gabarit) {
62    //supprimer les blocs de gabarit_ordre
63    spip_query("DELETE FROM spip_arty_gabarit_ordre WHERE gabarit='$gabarit'");
64    //supprimer l'entrée du gabarit dans gabarit_perso
65    spip_query("DELETE FROM spip_arty_gabarit_perso WHERE nom='$gabarit'");
66    //supprimer les entrées du gabarit dans gabaritassoc
67    spip_query("DELETE FROM spip_arty_gabaritassoc WHERE gabarit='$gabarit'");
68    //echo _T("arty:gabarit_supprime");
69  }
70}
71
72  echo debut_page();
73  echo barre_onglets("arty", "gabarit");
74  echo debut_gauche();
75  echo debut_cadre_relief(_DIR_PLUGIN_ARTY.'/images/aide.png', true, "", _T('arty:info'));
76  echo _T("arty:sideinfo_gabarit");
77  echo fin_cadre_relief(true);
78       
79  $gabarits=array();
80  $gabaritpardefaut=array("sommaire","rubrique","article","groupe","mot","breve");
81  $ignore_liste=array(".","..",".DS_Store","^\.[.+]");
82        $rep = _DIR_PLUGIN_ARTY."definitions-gabarits";
83  $handle = opendir($rep);
84  while($fichier = readdir($handle)) {
85   
86    if (!in_array($fichier, $ignore_liste) && eregi('^[a-zA-Z0-9_-]*\.xml$',$fichier)) {
87      $p =& new xmlParser();
88                $p->parse($rep.'/'.$fichier);
89                if (!in_array(ereg_replace("\.xml$","",$fichier),$gabaritpardefaut)) {
90                 $gabarits[$p->output[0]['attrs']['TYPE']][]=ereg_replace("\.xml$","",$fichier);
91                }
92                $liste_gabarit[]=ereg_replace("\.xml$","",$fichier);
93      }         
94  }
95
96  //gabarits du dossier "pages-custom" (surclassage)
97  $rep_custom = _DIR_RACINE."pages-custom/gabarits";
98  if (file_exists($rep_custom)) {
99    $handle = opendir($rep_custom);
100    while($fichier = readdir($handle)) {
101      if (!in_array($fichier, $ignore_liste) && eregi('^[a-zA-Z0-9_-]*\.xml$',$fichier) && !eregi('^ajout-',$fichier) && !in_array($fichier, $liste_gabarit)) {
102        $p =& new xmlParser();
103                $p->parse($rep_custom.'/'.$fichier);
104                $nom_gabarit = ereg_replace("\.xml$","",$fichier);
105                $type_gabarit = $p->output[0]['attrs']['TYPE'];
106                if (!in_array($nom_gabarit, $gabaritpardefaut) && !in_array($nom_gabarit, $gabarits[$type_gabarit])) {
107                 $gabarits[$type_gabarit][] = ereg_replace("\.xml$","",$fichier);
108                }
109        $liste_gabarit[] = ereg_replace("\.xml$","",$fichier);
110      }
111     }
112   }
113   
114   //gabarits de la base de données
115  $query = spip_query('SELECT * FROM spip_arty_gabarit_perso');
116  while ($row = spip_fetch_array($query)) {
117    if (!in_array($row['nom'], $liste_gabarit)) {
118      $liste_gabarit[] = $row['nom'];
119      //$donnees = unserialize($row['donnees']);
120      $gabarits[$donnees[0]['attrs']['TYPE']][] = $row['nom'];
121    }
122  }
123 
124  if ($_POST['action'] == "deriver") {
125 
126    if (isset($_POST['nom_gabarit']) && isset($_POST['source'])) {
127      if (in_array($_POST['nom_gabarit'], $liste_gabarit)) {
128        echo _T("arty:nom_deja_pris");
129      } else {
130   
131        $source = addslashes($_POST['source']);
132       
133        //vérification de la présence d'un gabarit dans pages-custom (surclassage)
134        $rep = _DIR_PLUGIN_ARTY."definitions-gabarits/";
135        $rep_custom = _DIR_RACINE."pages-custom/gabarits/";
136       
137        if (file_exists($rep_custom.$source.".xml")) {
138          $chemin_gabarit = $rep_custom.$source.".xml";
139        } else if (file_exists($rep.$source.".xml")) {
140          $chemin_gabarit = $rep.$source.".xml";
141        }
142       
143        if (isset($chemin_gabarit)) {
144          $p =& new xmlParser();
145          $p->parse($chemin_gabarit);
146          $donnees = $p->output;
147        }
148       
149        if (isset($donnees)) {
150       
151          $type_source = addslashes($donnees[0]["attrs"]["TYPE"]);
152         
153          if ($type_source == "defaut") {
154            $donnees[0]["attrs"]["TYPE"] = $source;
155            $type_source = $source;
156          }
157         
158          $nom = addslashes(trim(ereg_replace("[^a-zA-Z0-9_-]", "", $_POST['nom_gabarit'])));
159                        if (!$nom) { $nom="sansnom"; } // eviter le gabarit sans nom
160          $donnees = serialize($donnees);
161         
162          $result = spip_query("SELECT * FROM spip_arty_gabarit_ordre WHERE gabarit='$source'");
163          $blocs = array();
164          while ($row = spip_fetch_array($result)) {
165            $blocs[] = array(
166              "nom"       => addslashes($row['nom']),
167              "ordre"     => (!is_nan((int) $row['ordre'])) ? (int) $row['ordre'] : 0,
168              "conteneur" => (!is_nan((int) $row['conteneur'])) ? (int) $row['conteneur'] : 0,
169              "gabarit"   => $nom,
170              "param"     => addslashes($row['param']),
171            );
172          }
173         
174          if ($blocs) {
175            $query = "INSERT INTO spip_arty_gabarit_ordre (nom, ordre, conteneur, gabarit, param) VALUES ";
176            for ($i=0; $i<count($blocs); $i++) {
177              $query .= "('".$blocs[$i]['nom']."', ".$blocs[$i]['ordre'].", ".$blocs[$i]['conteneur'].", '".$blocs[$i]['gabarit']."', '".$blocs[$i]['param']."')";
178              if ($i != count($blocs)-1) {
179                $query .= ", ";
180              }
181            }
182           
183            spip_query($query);
184            spip_query("INSERT INTO spip_arty_gabarit_perso (nom,type,donnees) VALUES ('$nom','$type_source','$source')");
185         
186            // echo _T("arty:nouveau_gabarit_cree");
187           
188            // si on vient de creer le gabarit a partir d'une page d'edition, associer le gabarit
189            if ($_POST['associer']){
190                $leid = (int) $_POST['associer'];
191               
192                // supprimer autre entree de gabarit
193                spip_query("DELETE FROM spip_arty_gabaritassoc WHERE type='$type_source' AND id=$leid");
194                spip_query("INSERT INTO spip_arty_gabaritassoc (gabarit,id,type) VALUES ('$nom','$leid','$type_source')");
195            }
196           
197            $nouveau_gabarit = $nom;
198            $liste_gabarit[] = $nom;
199            $donnees = unserialize($donnees);
200            $gabarits[$donnees[0]['attrs']['TYPE']][] = $nom;
201           
202          }
203        }
204      }
205    }
206  }
207 
208  echo debut_cadre_relief(_DIR_PLUGIN_ARTY.'/images/aide.png', true, "", _T('arty:nouveau_gabarit'));
209  echo "<p style='margin-top:0px; margin-bottom:2px;'>"._T("arty:deriver_gabarit")."</p>";
210
211  echo "<form action='".generer_url_ecrire('gabarit')."' method='post'>\n";
212  echo "<input type='hidden' name='action' value='deriver'/>";
213  echo "<input type='text' name='nom_gabarit' value='"._T('arty:nom_du_gabarit')."' /><br />";
214 
215  echo "<p style='margin-top:8px; margin-bottom:2px;'>"._T("arty:baser_gabarit_sur")."</p>";
216 
217  echo "<select id='source' name='source'>";
218 
219 
220  foreach($gabaritpardefaut as $un_gabarit) {
221    if ($un_gabarit != "sommaire") {
222      echo '<option name="'.$un_gabarit.'" value="'.$un_gabarit.'">'.$un_gabarit.'</option>';
223    }
224  }
225 
226  foreach($gabarits as $nom_type => $types_gabarits) {
227    if ($nom_type != "defaut") {
228      $nom_type = "(".$nom_type.")";
229
230      foreach($types_gabarits as $un_gabarit) {
231       
232        if ($nom_type!="()"){
233        echo '<option name="'.$un_gabarit.'" value="'.$un_gabarit.'">'.$un_gabarit.' '.$nom_type.'</option>';
234        }
235      }
236    }
237  }
238  echo "</select>";
239  echo "<br /><br /><input type='submit' value='"._T("arty:enregistrer")."' class='fondo' />";
240  echo "</form>";
241
242  echo fin_cadre_relief(true);
243 
244 
245  // pour la page principale : nom du gabarit en cours
246 
247  if (isset($nouveau_gabarit)) {
248    $gabarit = $nouveau_gabarit;
249  } else {
250    $gabarit=$_GET['gabarit'];
251  }
252  if(!in_array($gabarit, $liste_gabarit)) {
253        $gabarit="sommaire";
254  }
255 
256  $liste_nonassociee=array("sommaire","article","rubrique","mot","groupe-mots","breve","404","forum","login","recherche","auteur","webmaton-mosaique","webmaton-questions");
257 
258  if (!in_array($gabarit, $liste_nonassociee)){
259        lister_associations_gabarit($gabarit);
260        }
261  echo debut_droite();
262
263  echo "\n";
264   
265   
266  gros_titre(_T("arty:configuration_des_gabarits")." "._T("arty:".$gabarit));
267
268  // faire le menu deroulant
269 
270   echo "<div id='deroulant-gabarits'>"._T("arty:modifier_un_autre_gabarit").' : <select onchange="window.location.href=\''.generer_url_ecrire("gabarit", "gabarit=").'\'+this.options[this.selectedIndex].value;" class="selecteur-gabarit">';
271 
272    foreach ($gabaritpardefaut as $cegabarit) {
273            $selected = ($cegabarit == $gabarit) ? 'selected="selected"' : '';
274
275        echo "<option class='$type' value=\"".$cegabarit."\" $selected>"._T("arty:$cegabarit")."</option>";
276  }
277 
278  foreach($gabarits as $type) {
279        foreach($type as $cegabarit) {
280                    $selected = ($cegabarit == $gabarit) ? 'selected="selected"' : '';
281        echo "<option class='$type' value=\"".$cegabarit."\" $selected>"._T("arty:$cegabarit")."</option>";
282        }
283  }
284  echo "</select></div>";
285 
286  //print_r($gabarits);
287   
288  charger_gabarit(addslashes($gabarit));
289 
290echo fin_gauche();
291echo fin_page();
292}
293
294function charger_gabarit($gabarit) {
295       
296        //vérification de la présence d'un gabarit dans pages-custom (surclassage)
297        $rep = _DIR_PLUGIN_ARTY."definitions-gabarits/";
298        $rep_custom = _DIR_RACINE."pages-custom/gabarits/";
299       
300        if (file_exists($rep_custom.$gabarit.".xml")) {
301    $chemin_gabarit = $rep_custom.$gabarit.".xml";
302    $origine=$gabarit;
303  } else if (file_exists($rep.$gabarit.".xml")) {
304    $chemin_gabarit = $rep.$gabarit.".xml";
305    $origine=$gabarit;
306  } else {
307    $query = spip_query("SELECT * FROM spip_arty_gabarit_perso WHERE nom='$gabarit'");
308    if (spip_num_rows($query)) {
309      $row = spip_fetch_array($query);
310      $origine = $row['donnees'];
311      $from_db = $row['type'];
312     
313      //retrouve le gabarit d'origine de la declinaison
314      if (file_exists($rep_custom.$origine.".xml")) {
315        $chemin_gabarit = $rep_custom.$origine.".xml";
316                } else if (file_exists($rep.$origine.".xml")) {
317        $chemin_gabarit = $rep.$origine.".xml";
318                } else { 
319                echo _T("arty:gabarit_origine_non-trouve"); 
320                return false;
321                }
322    }
323  }
324               
325        if (isset($chemin_gabarit)) {
326    $p =& new xmlParser();
327    $p->parse($chemin_gabarit);
328    $donnees = $p->output;
329  }
330
331  // fichier d'injection simple de bloc sur base du gabarit d'origine (physique)
332        if (file_exists($rep_custom."ajout-".$origine.".xml")) {
333        $pajout =& new xmlParser();
334    $pajout->parse($rep_custom."ajout-".$origine.".xml");
335    $donneesajout = $pajout->output;
336    $donnees[0][child] = array_merge((array)$donnees[0][child], (array)$donneesajout[0][child]);
337        }
338 
339  if (!isset($donnees)) {
340    echo _T("arty:gabarit_non-trouve");
341    return false;
342  } 
343 
344  if (isset($from_db)) {
345    // formulaire de suppression
346    echo "<form action='".generer_url_ecrire('gabarit')."' method='post'>\n";
347    echo "<input type='hidden' name='action' value='supprimer'/>\n";
348    echo "<input type='hidden' name='gabarit' value=\"".stripslashes($gabarit)."\" />\n";
349    echo "<br /><input type='submit' value='"._T("arty:supprimer")."' class='fondo' />";
350    echo "</form>";
351  }
352 
353 $nom = $donnees[0]['attrs']['NOM'];
354 
355 $blocs =array();
356 
357 foreach($donnees[0]['child'] as $ordre => $bloc) {
358        $blocs[$bloc['content']]=array('ordre'=> $ordre, 'conteneur' => $bloc['attrs']['STATUT'], 'param' => $bloc['attrs']['PARAM'], 'paramdefaut' => $bloc['attrs']['PARAMDEFAUT'],'paramdescr' => $bloc['attrs']['PARAMDESCR'],'paramtext' => $bloc['attrs']['PARAMTEXT'], 'paramvalue' => $bloc['attrs']['PARAMVALUE']);
359 }
360 
361 $resultat=spip_query("SELECT * FROM spip_arty_gabarit_ordre WHERE gabarit='$gabarit'"); 
362  while ($row=spip_fetch_array($resultat)) {
363        if (in_array($row['nom'], array_keys($blocs))) {
364                $blocs[$row['nom']]['conteneur']=$row['conteneur'];
365                $blocs[$row['nom']]['ordre']=$row['ordre'];
366                $blocs[$row['nom']]['paramdefaut']=$row['param']; // peut crasher
367        } else {
368                spip_query("DELETE FROM spip_arty_gabarit_ordre WHERE id_bloc=".$row['id_bloc']);
369        }
370  }
371
372 uasort($blocs, "comparerblocs");
373
374 echo "<div id='general'>\n";
375 
376  //print_r($donnees);
377 echo "<div id='pageweb'><div id='bandeau'></div>";
378$colonnes = array("contexte1" => 1, "contexte2" =>2, "corps" =>3, "reserve" => 0, "avance" => 4);
379
380foreach( array_keys($colonnes) as $colonne) {
381 //boucle pour tous les conteneurs
382 if($colonne=='reserve') { echo "<br class='clearer'/><div id='footer'></div></div><div id='reserves'>";
383  }
384 
385 echo "<ul id='$colonne' class='conteneur_bloc'>\n";
386 
387 echo "<li><h3>"._T("arty:$colonne")."</h3></li>\n";
388 foreach($blocs as $bloc => $proprietes) {
389//on affiche le bloc
390if ($proprietes['conteneur']== $colonnes[$colonne]) {
391        echo "<li id='".$bloc."' class='bloc $colonne' >";
392        // poignée
393        echo "<div class='poignee'></div>"; 
394        echo "<span class='titre-bloc'>"._T("arty:".$bloc)."</span><br />";
395        if($proprietes['paramdescr']) {
396        echo "<p class='option-bloc'>"._T("arty:".$proprietes['paramdescr']);
397        }
398       
399        if (preg_match("/^champ/",$proprietes['param'])) {
400        // en cas de champ : montrer le champ
401        echo "<input type='text' name='$bloc' class='selecteur-param' value='";
402        echo $proprietes['paramdefaut'];
403        echo "' style='width:100%' />";
404        echo "</p>";
405       
406        } else if (preg_match("/^area/",$proprietes['param'])) {
407        // en cas de area : montrer le champ
408        echo "<textarea name='$bloc' class='selecteur-param' rows='4' style='width:100%'>";
409        echo $proprietes['paramdefaut'];
410        echo "</textarea>";
411        echo "</p>";
412       
413        } else if ($proprietes['param'] !="") { 
414        // en cas de parametre : montrer le selecteur
415                echo "<select name='$bloc' class='selecteur-param'>";
416               
417                if (ereg("SELECT",$proprietes['param'])) {
418                        $requete=spip_query($proprietes['param']);
419                        while ($row=spip_fetch_array($requete)) {
420                                echo "<option value='".$row[$proprietes['paramvalue']]."'>".$row[$proprietes['paramtext']]."</option>";
421                        }
422                               
423                } else {
424                        // c'est une liste separee par des virgules
425                        $options=explode(",",$proprietes['param']);
426                        foreach($options as $option) {
427                        echo "<option value='$option'";
428                        if ( trim(strval($option))==trim(strval($proprietes['paramdefaut'])) ) { echo "selected='selected'"; }
429                        echo ">"._T("arty:$option")."</option>";
430                       
431                        }
432                }
433               
434                echo "</select></p>";
435        }
436       
437        echo "</li>\n";
438}
439 }
440                echo "</ul>\n";
441       
442        if($colonne=='avance'){
443                echo "</div>\n";
444        }
445}       
446
447echo "<br class='clearer' />";
448  echo "<div id='bloc_sauver'>";
449        echo "<div id='reponse'></div>"; 
450        echo "<img src='"._DIR_IMG_PACK."searching.gif' id='search' style='visibility:hidden' />";
451        echo '<a href="javascript:sauver(\''.$gabarit.'\'';
452       
453        if ($_POST['retour']){
454        // si un retour vers une page est prevu
455                echo ",'".$_POST['retour']."');\">";
456                echo _T("arty:modifier_et_retour");
457        } else {
458        echo ');">'._T("arty:enregister");
459        }
460  echo "</a></div>";
461echo "</div>\n";
462}
463
464function testexist($nom, $liste) {
465        $return=FALSE;
466        foreach($liste as $bloc) {
467                if ($bloc['content']==$nom) {
468                $return=TRUE;   
469                }
470        }
471        return $return;
472}
473
474function comparerblocs ($a, $b) {
475    if ($a['ordre'] == $b['ordre']) return 0;
476    return ($a['ordre'] < $b['ordre']) ? -1 : 1;
477}
478
479function lister_associations_gabarit($gabarit)
480{
481
482  if ($_GET['action'] == 'delete_assoc_gabarit' && $_GET['id_check'] == $_SESSION['id_check']) {
483    $type = isset($_GET['type']) ? addslashes($_GET['type']) : false;
484    $id = isset($_GET['id']) ? (int) $_GET['id'] : false;
485    $theme = isset($_GET['gabarit']) ? addslashes($_GET['gabarit']) : false;
486    if ($type && $id && $theme) {
487      spip_query("DELETE FROM spip_arty_gabaritassoc WHERE type='$type' AND id=$id AND gabarit='$gabarit'");
488    }
489  }
490
491  echo "<div id=\"div_liste_assoc\">";
492  $result = spip_query("SELECT * FROM spip_arty_gabaritassoc WHERE gabarit='$gabarit'");
493 
494    echo debut_cadre_couleur("doc-24.gif", true, "", _T('arty:liste_assoc_gabarit'));
495    echo "<div class='liste'><table border='0' cellspacing='0' cellpadding='3' width='100%'>";
496    if (spip_num_rows($result) > 0) {
497    while ($row = spip_fetch_array($result)){
498      echo "<tr class='tr_liste'>";
499      $type = $row['type'];
500      $id = $row['id']; 
501      switch ($type) {
502        case "rubrique":
503        $rubrique = spip_fetch_array(spip_query("SELECT titre FROM spip_rubriques WHERE id_rubrique=$id"));
504        $titre = $rubrique['titre']; 
505        $url="?exec=naviguer&id_rubrique=$id";   
506        break;
507       
508        case "article":
509        $article = spip_fetch_array(spip_query("SELECT titre FROM spip_articles WHERE id_article=$id"));
510        $titre = $article['titre']; 
511        $url="?exec=articles&id_article=$id";     
512        break;
513       
514        case "groupe":
515        $groupe = spip_fetch_array(spip_query("SELECT titre FROM spip_groupes_mots WHERE id_groupe=$id"));
516        $titre = $groupe['titre'];
517        $url="?exec=mots_tous";
518        break;
519       
520        case "mot":
521        $mot = spip_fetch_array(spip_query("SELECT titre FROM spip_mots WHERE id_mot=$id"));
522        $titre = $mot['titre']; 
523        $url="?exec=mots_tous&id_mot=$id";     
524        break;
525      }
526       
527      $gabarit = $row['gabarit'];
528      echo "<td class=\"arial1\"><a href=\"$url\">$titre</a></td>";
529      echo "<td class=\"arial1\"> ($type $id)</td>";
530      echo "<td class=\"arial1\"><div style='text-align:right;'><a class=\"supprimer\" href='".generer_url_ecrire('gabarit')."&amp;action=delete_assoc_gabarit&amp;id_check=".$_SESSION['id_check']."&amp;id=$id&amp;gabarit=$gabarit&amp;type=$type'><img src='../dist/images/croix-rouge.gif' alt='X' width='7' height='7' align='bottom' /></a></div></td>";
531      echo "</tr>";
532    }
533    } else { echo "<tr><td class=\"arial1\">"._T("arty:pas_de_page_associee")."</td></tr>"; }
534    echo "</table></div>";
535   
536    echo fin_cadre_couleur(true);
537 
538  echo "</div>";
539}
540
541?>
Note: See TracBrowser for help on using the repository browser.