[PLUGINS] +les plugins de bases
[ptitvelo/web/www.git] / www / plugins / zpip_v1 / z_pipelines.php
1 <?php
2 /*
3 * Plugin Zpip
4 * (c) 2008-2010 Cedric MORIN Yterium.net
5 * Distribue sous licence GPL
6 *
7 */
8
9 if (!defined("_ECRIRE_INC_VERSION")) return;
10
11 // demander a SPIP de definir 'type' dans le contexte du premier squelette
12 define('_DEFINIR_CONTEXTE_TYPE',true);
13 // verifier une seule fois que l'on peut utiliser APL si demande
14 if (defined('_Z_AJAX_PARALLEL_LOAD')) {
15 if (_request('var_zapl')=='non') {
16 include_spip('inc/cookie');
17 spip_setcookie('no_zapl',$_COOKIE['no_zapl']='no_zapl');
18 }
19 if (!isset($_COOKIE['no_zapl'])
20 AND !_IS_BOT
21 AND !_request('var_zajax')
22 AND _request('var_mode')!=="debug"
23 AND $_SERVER['REQUEST_METHOD'] == 'GET'
24 ) {
25 define('_Z_AJAX_PARALLEL_LOAD_OK',true);
26 $GLOBALS['marqueur'] .= ":Zapl";
27 }
28 }
29
30 /**
31 * Inutilise mais permet le chargement de ce fichier avant le decodage des urls
32 * et l'utilisation de _DEFINIR_CONTEXTE_TYPE
33 * @param array $flux
34 * @return array
35 */
36 function Z_declarer_url_objets($flux){
37 return $flux;
38 }
39
40 /**
41 * Fonction Page automatique a partir de contenu/page-xx
42 *
43 * @param array $flux
44 * @return array
45 */
46 function Z_styliser($flux){
47 $z_blocs = isset($GLOBALS['z_blocs'])?$GLOBALS['z_blocs']:array('contenu','navigation','extra','head');
48 $z_contenu = reset($z_blocs); // contenu par defaut
49
50 $squelette = $flux['data'];
51 $fond = $flux['args']['fond'];
52 $ext = $flux['args']['ext'];
53
54 // Ajax Parallel loading : ne pas calculer le bloc, mais renvoyer un js qui le loadera an ajax
55 if (defined('_Z_AJAX_PARALLEL_LOAD_OK')
56 AND $dir = explode('/',$fond)
57 AND count($dir)==2 // pas un sous repertoire
58 AND $dir = reset($dir)
59 AND in_array($dir,$z_blocs) // verifier deja qu'on est dans un bloc Z
60 AND in_array($dir,explode(',',_Z_AJAX_PARALLEL_LOAD)) // et dans un demande en APL
61 AND $pipe = find_in_path("$dir/z_apl.$ext") // et qui contient le squelette APL
62 ){
63 $flux['data'] = substr($pipe, 0, - strlen(".$ext"));
64 return $flux;
65 }
66
67 // gerer les squelettes non trouves
68 // -> router vers les /dist.html
69 // ou scaffolding ou page automatique les contenus
70 if (!$squelette){
71
72 // Cas de figure où on a déclaré type-composition.html dans un bloc, mais où type.html n'existe pas
73 if (isset($flux['args']['contexte']['composition'])
74 AND $dir = explode('/',$fond)
75 AND $dir = reset($dir)
76 AND in_array($dir,$z_blocs)
77 AND $f=find_in_path($fond."-".$flux['args']['contexte']['composition'].".$ext")){
78 $flux['data'] = substr($f,0,-strlen(".$ext"));
79 }
80
81 // si on est sur un ?page=XX non trouve
82 elseif ($flux['args']['contexte'][_SPIP_PAGE] == $fond OR $flux['args']['contexte']['type'] == $fond) {
83 // si c'est un objet spip, associe a une table, utiliser le fond homonyme
84 if (z_scaffoldable($fond)){
85 $flux['data'] = substr(find_in_path("objet.$ext"), 0, - strlen(".$ext"));
86 }
87 // sinon, brancher sur contenu/page-xx si elle existe
88 // si on est sur un ?page=XX non trouve
89 elseif ($flux['args']['contexte'][_SPIP_PAGE] == $fond) {
90 $base = "$z_contenu/page-".$fond.".".$ext;
91 if ($base = find_in_path($base)){
92 $flux['data'] = substr(find_in_path("page.$ext"), 0, - strlen(".$ext"));
93 }
94 }
95 }
96
97 // scaffolding :
98 // si c'est un fond de contenu d'un objet en base
99 // generer un fond automatique a la volee pour les webmestres
100 elseif (strncmp($fond, "$z_contenu/", strlen($z_contenu)+1)==0
101 AND include_spip('inc/autoriser')
102 AND isset($GLOBALS['visiteur_session']['id_auteur']) // performance
103 AND autoriser('webmestre')){
104 $type = substr($fond,strlen($z_contenu)+1);
105 if ($is = z_scaffoldable($type))
106 $flux['data'] = z_scaffolding($type,$is[0],$is[1],$is[2],$ext);
107 }
108
109 // sinon, si on demande un fond non trouve dans un des autres blocs
110 // et si il y a bien un contenu correspondant ou scaffoldable
111 // se rabbatre sur le dist.html du bloc concerne
112 else{
113 if ( $dir = explode('/',$fond)
114 AND $dir = reset($dir)
115 AND $dir !== $z_contenu
116 AND in_array($dir,$z_blocs)){
117 $type = substr($fond,strlen("$dir/"));
118 if (find_in_path("$z_contenu/$type.$ext") OR z_scaffoldable($type))
119 $flux['data'] = substr(find_in_path("$dir/dist.$ext"), 0, - strlen(".$ext"));
120 }
121 }
122 $squelette = $flux['data'];
123 }
124 if ($fond=='body' AND substr($squelette,-strlen($fond))==$fond){
125 if (isset($flux['args']['contexte']['type'])
126 AND (
127 (isset($flux['args']['contexte']['composition'])
128 AND file_exists(($f=$squelette."-".$flux['args']['contexte']['type']."-".$flux['args']['contexte']['composition']).".$ext"))
129 OR
130 file_exists(($f=$squelette."-".$flux['args']['contexte']['type']).".$ext")
131 ))
132 $flux['data'] = $f;
133 }
134 // chercher le fond correspondant a la composition
135 elseif (isset($flux['args']['contexte']['composition'])
136 AND substr($squelette,-strlen($fond))==$fond
137 AND $dir = explode('/',$fond)
138 AND $dir = reset($dir)
139 AND in_array($dir,$z_blocs)
140 AND $f=find_in_path($fond."-".$flux['args']['contexte']['composition'].".$ext")){
141 $flux['data'] = substr($f,0,-strlen(".$ext"));
142 }
143 return $flux;
144 }
145
146
147 /**
148 * Tester si un type est scaffoldable
149 * cad si il correspond bien a un objet en base
150 *
151 * @staticvar array $scaffoldable
152 * @param string $type
153 * @return bool
154 */
155 function z_scaffoldable($type){
156 static $scaffoldable = array();
157 if (isset($scaffoldable[$type]))
158 return $scaffoldable[$type];
159 if (preg_match(',[^\w],',$type))
160 return $scaffoldable[$type] = false;
161 if ($table = table_objet($type)
162 AND $type == objet_type($table)
163 AND $trouver_table = charger_fonction('trouver_table','base')
164 AND
165 ($desc = $trouver_table($table)
166 OR $desc = $trouver_table($table_sql = $GLOBALS['table_prefix']."_$table"))
167 )
168 return $scaffoldable[$type] = array($table,$desc['table'],$desc);
169 else
170 return $scaffoldable[$type] = false;
171 }
172
173
174 /**
175 * Generer a la volee un fond a partir d'une table de contenu
176 *
177 * @param string $type
178 * @param string $table
179 * @param string $table_sql
180 * @param array $desc
181 * @param string $ext
182 * @return string
183 */
184 function z_scaffolding($type,$table,$table_sql,$desc,$ext){
185 include_spip('public/interfaces');
186 $primary = id_table_objet($type);
187 if (!$primary AND isset($desc['key']["PRIMARY KEY"])){
188 $primary = $desc['key']["PRIMARY KEY"];
189 }
190
191 // reperer un titre
192 $titre = 'titre';
193 if (isset($GLOBALS['table_titre'][$table])){
194 $titre = explode(' ',$GLOBALS['table_titre'][$table]);
195 $titre = explode(',',reset($titre));
196 $titre = reset($titre);
197 }
198 if (isset($desc['field'][$titre])){
199 unset($desc['field'][$titre]);
200 $titre="<h1 class='h1 #EDIT{titre}'>#".strtoupper($titre)."</h1>";
201 }
202 else $titre="";
203
204 // reperer une date
205 $date = "date";
206 if (isset($GLOBALS['table_date'][$table]))
207 $date = $GLOBALS['table_date'][$table];
208 if (isset($desc['field'][$date])){
209 unset($desc['field'][$date]);
210 $date = strtoupper($date);
211 $date="<p class='info-publi'>[(#$date|nom_jour) ][(#$date|affdate)][, <span class='auteurs'><:par_auteur:> (#LESAUTEURS)</span>]</p>";
212 }
213 else $date = "";
214
215 $content = array();
216 foreach($desc['field'] as $champ=>$z){
217 if (!in_array($champ,array('maj','statut','idx',$primary))){
218 $content[] = "[<div><strong>$champ</strong><div class='#EDIT{".$champ."} $champ'>(#".strtoupper($champ)."|image_reduire{500,0})</div></div>]";
219 }
220 }
221 $content = implode("\n\t",$content);
222
223 $scaffold = "#CACHE{0}
224 <BOUCLE_contenu($table_sql){".$primary."}>
225 [(#REM) Fil d'Ariane ]
226 <p id='hierarchie'><a href='#URL_SITE_SPIP/'><:accueil_site:></a>[ &gt; <strong class='on'>(#TITRE|couper{80})</strong>]</p>
227
228 <div class='contenu-principal'>
229 <div class='cartouche'>
230 $titre
231 $date
232 </div>
233
234 $content
235
236 </div>
237
238 [<div class='notes surlignable'><h2 class='h2 pas_surlignable'><:info_notes:></h2>(#NOTES)</div>]
239 </BOUCLE_contenu>";
240
241 $dir = sous_repertoire(_DIR_CACHE,"scaffold",false);
242 $dir = sous_repertoire($dir,"contenu",false);
243 $f = $dir."$type";
244 ecrire_fichier("$f.$ext",$scaffold);
245 return $f;
246 }
247
248
249
250 /**
251 * Surcharger les intertires avant que le core ne les utilise
252 * pour y mettre la class h3
253 * une seule fois suffit !
254 *
255 * @param string $flux
256 * @return string
257 */
258 function Z_pre_propre($flux){
259 static $init = false;
260 if (!$init){
261 if (isset($GLOBALS['debut_intertitre']) and $GLOBALS['debut_intertitre']){
262 $intertitre = $GLOBALS['debut_intertitre'];
263 $class = extraire_attribut($GLOBALS['debut_intertitre'],'class');
264 $class = ($class ? " $class":"");
265 $GLOBALS['debut_intertitre'] = inserer_attribut($GLOBALS['debut_intertitre'], 'class', "h3$class");
266 foreach($GLOBALS['spip_raccourcis_typo'] as $k=>$v){
267 $GLOBALS['spip_raccourcis_typo'][$k] = str_replace($intertitre,$GLOBALS['debut_intertitre'],$GLOBALS['spip_raccourcis_typo'][$k]);
268 }
269 }
270 else {
271 $GLOBALS['debut_intertitre'] = '<h3 class="h3 spip">';
272 $GLOBALS['fin_intertitre'] = '</h3>';
273 }
274 $init = true;
275 }
276 return $flux;
277 }
278
279 /**
280 * Ajouter le inc-insert-head du theme si il existe
281 *
282 * @param string $flux
283 * @return string
284 */
285 function Z_insert_head($flux){
286 if (find_in_path('inc-insert-head.html')){
287 $flux .= recuperer_fond('inc-insert-head',array());
288 }
289 return $flux;
290 }
291
292 //
293 // fonction standard de calcul de la balise #INTRODUCTION
294 // mais retourne toujours dans un <p> comme propre
295 //
296 // http://doc.spip.org/@filtre_introduction_dist
297 if (!function_exists('filtre_introduction')){ // securite
298 function filtre_introduction($descriptif, $texte, $longueur, $connect) {
299 include_spip('public/composer');
300 $texte = filtre_introduction_dist($descriptif, $texte, $longueur, $connect);
301
302 if ($GLOBALS['toujours_paragrapher'] AND strpos($texte,"</p>")===FALSE)
303 // Fermer les paragraphes ; mais ne pas en creer si un seul
304 $texte = paragrapher($texte, $GLOBALS['toujours_paragrapher']);
305
306 return $texte;
307 }
308 }
309
310 /**
311 * Tester la presence sur une page
312 * @param object $p
313 * @return object
314 */
315 if (!function_exists('balise_SI_PAGE_dist')){
316 function balise_SI_PAGE_dist($p) {
317 $_page = interprete_argument_balise(1,$p);
318 $p->code = "(((\$Pile[0][_SPIP_PAGE]==(\$zp=$_page)) OR (\$Pile[0]['composition']==\$zp AND \$Pile[0]['type']=='page'))?' ':'')";
319 $p->interdire_scripts = false;
320 return $p;
321 }
322 }
323 ?>