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