1 | <?php |
---|
2 | /* |
---|
3 | * Plugin Compositions |
---|
4 | * (c) 2007-2009 Cedric Morin |
---|
5 | * Distribue sous licence GPL |
---|
6 | * |
---|
7 | */ |
---|
8 | |
---|
9 | define('_COMPOSITIONS_MATCH','-([^0-9][^.]*)'); |
---|
10 | |
---|
11 | /** |
---|
12 | * Retrouver le nom du dossier ou sont stockees les compositions |
---|
13 | * reglage par defaut, ou valeur personalisee via cfg |
---|
14 | * |
---|
15 | * @return string |
---|
16 | */ |
---|
17 | function compositions_chemin(){ |
---|
18 | $config_chemin = 'compositions/'; |
---|
19 | if (defined('_DIR_PLUGIN_Z') OR defined('_DIR_PLUGIN_ZCORE')) |
---|
20 | $config_chemin = (isset($GLOBALS['z_blocs'])?reset($GLOBALS['z_blocs']):'contenu/'); |
---|
21 | |
---|
22 | if (isset($GLOBALS['meta']['compositions'])){ |
---|
23 | $config = unserialize($GLOBALS['meta']['compositions']); |
---|
24 | if (isset ($config['chemin_compositions'])){ |
---|
25 | $config_chemin = rtrim($config['chemin_compositions'],'/').'/'; |
---|
26 | } |
---|
27 | } |
---|
28 | |
---|
29 | return $config_chemin; |
---|
30 | } |
---|
31 | |
---|
32 | /** |
---|
33 | * Tester si la stylisation auto est activee |
---|
34 | * @return string |
---|
35 | */ |
---|
36 | function compositions_styliser_auto(){ |
---|
37 | $config_styliser = true; |
---|
38 | if (defined('_DIR_PLUGIN_Z') OR defined('_DIR_PLUGIN_ZCORE')){ |
---|
39 | $config_styliser = false; // Z s'occupe de styliser les compositions |
---|
40 | } |
---|
41 | elseif (isset($GLOBALS['meta']['compositions'])){ |
---|
42 | $config = unserialize($GLOBALS['meta']['compositions']); |
---|
43 | $config_styliser = $config['styliser_auto'] != 'non'; |
---|
44 | } |
---|
45 | return $config_styliser?' ':''; |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * Lister les compositions disponibles : toutes ou pour un type donne |
---|
50 | * Si informer est a false, on ne charge pas les infos du xml |
---|
51 | * |
---|
52 | * @param string $type |
---|
53 | * @param bool $informer |
---|
54 | * @return array |
---|
55 | */ |
---|
56 | function compositions_lister_disponibles($type, $informer=true){ |
---|
57 | include_spip('inc/compositions'); |
---|
58 | $type = preg_replace(',\W,','',$type); |
---|
59 | if ($type=='syndic') $type='site'; //grml |
---|
60 | if (!strlen($type)) $type="[a-z0-9]+"; |
---|
61 | |
---|
62 | |
---|
63 | // rechercher les skel du type article-truc.html |
---|
64 | // truc ne doit pas commencer par un chiffre pour eviter de confondre avec article-12.html |
---|
65 | $match = "/($type)("._COMPOSITIONS_MATCH.")?[.]html$"; |
---|
66 | |
---|
67 | // lister les compositions disponibles |
---|
68 | $liste = find_all_in_path(compositions_chemin(),$match); |
---|
69 | $res = array(); |
---|
70 | if (count($liste)){ |
---|
71 | foreach($liste as $s) { |
---|
72 | $base = preg_replace(',[.]html$,i','',$s); |
---|
73 | if (preg_match(",$match,ims",$s,$regs) |
---|
74 | AND ($composition = !$informer |
---|
75 | OR $composition = compositions_charger_infos($base))) |
---|
76 | $res[$regs[1]][$regs[3]] = $composition; |
---|
77 | // retenir les skels qui ont un xml associe |
---|
78 | } |
---|
79 | } |
---|
80 | // Pipeline compositions_lister_disponibles |
---|
81 | if ($type=="[a-z0-9]+") |
---|
82 | $type = ''; |
---|
83 | $res = pipeline('compositions_lister_disponibles',array( |
---|
84 | 'args'=>array('type' => $type,'informer' => $informer), |
---|
85 | 'data'=> $res |
---|
86 | ) |
---|
87 | ); |
---|
88 | return $res; |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * Liste les id d'un type donne utilisant une composition donnee |
---|
93 | * |
---|
94 | * @param string $type |
---|
95 | * @param string $composition |
---|
96 | * @return array |
---|
97 | */ |
---|
98 | function compositions_lister_utilisations($type,$composition){ |
---|
99 | $table_sql = table_objet_sql($type); |
---|
100 | $_id_table_objet = id_table_objet($type); |
---|
101 | return sql_allfetsel("$_id_table_objet as id,titre", $table_sql, "composition=".sql_quote($composition)); |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * Selectionner le fond en fonction du type et de la composition |
---|
106 | * en prenant en compte la configuration pour le chemin |
---|
107 | * et le fait que la composition a pu etre supprimee |
---|
108 | * |
---|
109 | * @param string $composition |
---|
110 | * @param string $type |
---|
111 | * @param string $defaut |
---|
112 | * @param string $ext |
---|
113 | * @param bool $fullpath |
---|
114 | * @param string $vide |
---|
115 | * @return string |
---|
116 | */ |
---|
117 | function compositions_selectionner($composition,$type,$defaut="",$ext="html",$fullpath = false, $vide="composition-vide"){ |
---|
118 | if ($type=='syndic') $type='site'; //grml |
---|
119 | $fond = compositions_chemin() . $type; |
---|
120 | |
---|
121 | // regarder si compositions/article-xxx est disponible |
---|
122 | if (strlen($composition) |
---|
123 | AND $f = find_in_path("$fond-$composition.$ext")) |
---|
124 | return $fullpath ? $f : $fond . "-$composition"; |
---|
125 | else |
---|
126 | // sinon regarder si compositions/article-defaut est disponible |
---|
127 | if (strlen($defaut) |
---|
128 | AND $f = find_in_path("$fond-$defaut.$ext")) |
---|
129 | return $fullpath ? $f : $fond . "-$defaut"; |
---|
130 | |
---|
131 | // se rabattre sur compositions/article si disponible |
---|
132 | if ($f = find_in_path("$fond.$ext")) |
---|
133 | return $fullpath ? $f : $fond; |
---|
134 | |
---|
135 | // sinon une composition vide pour ne pas generer d'erreur |
---|
136 | if ($vide AND $f = find_in_path("$vide.$ext")) |
---|
137 | return $fullpath ? $f : $vide; |
---|
138 | |
---|
139 | // rien mais ca fera une erreur dans le squelette si appele en filtre |
---|
140 | return ''; |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * Decrire une composition pour un objet |
---|
145 | * @param string $type |
---|
146 | * @param string $composition |
---|
147 | * @return array|bool|string |
---|
148 | */ |
---|
149 | function compositions_decrire($type, $composition){ |
---|
150 | static $compositions = array(); |
---|
151 | if ($type=='syndic') $type='site'; //grml |
---|
152 | if (isset($compositions[$type][$composition])) |
---|
153 | return $compositions[$type][$composition]; |
---|
154 | $ext = "html"; |
---|
155 | $fond = compositions_chemin() . $type; |
---|
156 | if (strlen($composition) |
---|
157 | AND $f = find_in_path("$fond-$composition.$ext") |
---|
158 | AND $desc = compositions_charger_infos($f)) |
---|
159 | return $compositions[$type][$composition] = $desc; |
---|
160 | return $compositions[$type][$composition] = false; |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * Un filtre a utiliser sur [(#COMPOSITION|classe_composition{#ENV{type}})] |
---|
165 | * pour poser des classes generiques sur le <body> |
---|
166 | * si une balise <class> est definie dans la composition c'est elle qui est appliquee |
---|
167 | * sinon on pose le nom de la composition comme classe |
---|
168 | * |
---|
169 | * @param string $composition |
---|
170 | * @param string $type |
---|
171 | * @return string |
---|
172 | */ |
---|
173 | function classe_composition($composition,$type){ |
---|
174 | if ($desc = compositions_decrire($type, $composition) |
---|
175 | AND isset($desc['class']) |
---|
176 | AND strlen($desc['class'])) |
---|
177 | return $desc['class']; |
---|
178 | return $composition; |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * Liste les types d'objets qui ont une composition |
---|
183 | * utilise la valeur en cache meta sauf si demande de recalcul |
---|
184 | * ou pas encore definie |
---|
185 | * |
---|
186 | * @staticvar array $liste |
---|
187 | * @return array |
---|
188 | */ |
---|
189 | function compositions_types(){ |
---|
190 | static $liste = null; |
---|
191 | if (is_null($liste)) { |
---|
192 | if ($GLOBALS['var_mode'] OR !isset($GLOBALS['meta']['compositions_types'])){ |
---|
193 | include_spip('inc/compositions'); |
---|
194 | compositions_cacher(); |
---|
195 | } |
---|
196 | $liste = explode(',',$GLOBALS['meta']['compositions_types']); |
---|
197 | } |
---|
198 | return $liste; |
---|
199 | } |
---|
200 | |
---|
201 | /** |
---|
202 | * Renvoie la composition qui s'applique à un objet |
---|
203 | * en tenant compte, le cas échéant, de la composition héritée |
---|
204 | * si etoile=true on renvoi dire le champ sql |
---|
205 | * |
---|
206 | * @param string $type |
---|
207 | * @param integer $id |
---|
208 | * @param string $serveur |
---|
209 | * @param bool $etoile |
---|
210 | * @return string |
---|
211 | */ |
---|
212 | function compositions_determiner($type, $id, $serveur='', $etoile = false){ |
---|
213 | static $composition = array(); |
---|
214 | |
---|
215 | if (isset($composition[$etoile][$serveur][$type][$id])) |
---|
216 | return $composition[$etoile][$serveur][$type][$id]; |
---|
217 | |
---|
218 | include_spip('base/abstract_sql'); |
---|
219 | $table = table_objet($type); |
---|
220 | $table_sql = table_objet_sql($type); |
---|
221 | $_id_table = id_table_objet($type); |
---|
222 | |
---|
223 | $retour = ''; |
---|
224 | |
---|
225 | $trouver_table = charger_fonction('trouver_table', 'base'); |
---|
226 | $desc = $trouver_table($table,$serveur); |
---|
227 | if (isset($desc['field']['composition']) AND $id){ |
---|
228 | $select = "composition"; |
---|
229 | if (isset($desc['field']['id_rubrique'])) |
---|
230 | $select .= "," . (($type == 'rubrique') ? 'id_parent' : 'id_rubrique as id_parent'); |
---|
231 | $row = sql_fetsel($select, $table_sql, "$_id_table=".intval($id), '', '', '', '', $serveur); |
---|
232 | if ($row['composition'] != '') |
---|
233 | $retour = $row['composition']; |
---|
234 | elseif (!$etoile |
---|
235 | AND isset($row['id_parent']) |
---|
236 | AND $row['id_parent']) |
---|
237 | $retour = compositions_heriter($type, $row['id_parent'], $serveur); |
---|
238 | } |
---|
239 | return $composition[$etoile][$serveur][$type][$id] = (($retour == '-') ? '' : $retour); |
---|
240 | } |
---|
241 | |
---|
242 | /** |
---|
243 | * Renvoie la composition héritée par un objet selon sa rubrique |
---|
244 | * |
---|
245 | * @param string $type |
---|
246 | * @param integer $id_rubrique |
---|
247 | * @param string $serveur |
---|
248 | * @return string |
---|
249 | */ |
---|
250 | function compositions_heriter($type, $id_rubrique, $serveur=''){ |
---|
251 | if ($type=='syndic') $type='site'; //grml |
---|
252 | if (intval($id_rubrique) < 1) return ''; |
---|
253 | $infos = null; |
---|
254 | $id_parent = $id_rubrique; |
---|
255 | do { |
---|
256 | $row = sql_fetsel(array('id_parent','composition'),'spip_rubriques','id_rubrique='.intval($id_parent),'','','','',$serveur); |
---|
257 | if (strlen($row['composition']) AND is_null($infos)) |
---|
258 | $infos = compositions_lister_disponibles('rubrique'); |
---|
259 | } |
---|
260 | while ($id_parent = $row['id_parent'] |
---|
261 | AND |
---|
262 | (!strlen($row['composition']) OR !isset($infos['rubrique'][$row['composition']]['branche'][$type]))); |
---|
263 | |
---|
264 | if (strlen($row['composition']) AND isset($infos['rubrique'][$row['composition']]['branche'][$type])) |
---|
265 | return $infos['rubrique'][$row['composition']]['branche'][$type]; |
---|
266 | |
---|
267 | return ''; |
---|
268 | } |
---|
269 | |
---|
270 | /** |
---|
271 | * #COMPOSITION |
---|
272 | * Renvoie la composition s'appliquant à un objet |
---|
273 | * en tenant compte, le cas échéant, de l'héritage. |
---|
274 | * |
---|
275 | * Sans precision, l'objet et son identifiant sont pris |
---|
276 | * dans la boucle en cours, mais l'on peut spécifier notre recherche |
---|
277 | * en passant objet et id_objet en argument de la balise : |
---|
278 | * #COMPOSITION{article, 8} |
---|
279 | * |
---|
280 | * #COMPOSITION* renvoie toujours le champs brut, sans tenir compte de l'heritage |
---|
281 | * |
---|
282 | * @param array $p AST au niveau de la balise |
---|
283 | * @return array AST->code modifié pour calculer le nom de la composition |
---|
284 | */ |
---|
285 | function balise_COMPOSITION_dist($p) { |
---|
286 | $_composition = ""; |
---|
287 | if ($_objet = interprete_argument_balise(1, $p)) { |
---|
288 | $_id_objet = interprete_argument_balise(2, $p); |
---|
289 | } else { |
---|
290 | $_composition = champ_sql('composition',$p); |
---|
291 | $_id_objet = champ_sql($p->boucles[$p->id_boucle]->primary, $p); |
---|
292 | $_objet = "objet_type('" . $p->boucles[$p->id_boucle]->id_table . "')"; |
---|
293 | } |
---|
294 | // si on veut le champ brut, et qu'on l'a sous la main, inutile d'invoquer toute la machinerie |
---|
295 | if ($_composition AND $p->etoile) |
---|
296 | $p->code = $_composition; |
---|
297 | else { |
---|
298 | $connect = $p->boucles[$p->id_boucle]->sql_serveur; |
---|
299 | $p->code = "compositions_determiner($_objet, $_id_objet, '$connect', ".($p->etoile?'true':'false').")"; |
---|
300 | // ne declencher l'usine a gaz que si composition est vide ... |
---|
301 | if ($_composition) |
---|
302 | $p->code = "((\$zc=$_composition)?(\$zc=='-'?'':\$zc):".$p->code.")"; |
---|
303 | } |
---|
304 | return $p; |
---|
305 | } |
---|
306 | |
---|
307 | /** |
---|
308 | * Indique si la composition d'un objet est verrouillée ou non, |
---|
309 | * auquel cas, seul le webmaster peut la modifier |
---|
310 | * |
---|
311 | * @param string $type |
---|
312 | * @param integer $id |
---|
313 | * @param string $serveur |
---|
314 | * @return string |
---|
315 | */ |
---|
316 | function compositions_verrouiller($type, $id, $serveur=''){ |
---|
317 | $config = unserialize($GLOBALS['meta']['compositions']); |
---|
318 | if ($config['tout_verrouiller'] == 'oui') |
---|
319 | return true; |
---|
320 | |
---|
321 | include_spip('base/abstract_sql'); |
---|
322 | $table = table_objet($type); |
---|
323 | $table_sql = table_objet_sql($type); |
---|
324 | $_id_table = id_table_objet($type); |
---|
325 | |
---|
326 | $trouver_table = charger_fonction('trouver_table', 'base'); |
---|
327 | $desc = $trouver_table($table,$serveur); |
---|
328 | if (isset($desc['field']['composition_lock']) AND $id){ |
---|
329 | $lock = sql_getfetsel('composition_lock', $table_sql, "$_id_table=".intval($id), '', '', '', '', $serveur); |
---|
330 | if ($lock) |
---|
331 | return true; |
---|
332 | elseif (isset($desc['field']['id_rubrique'])) { |
---|
333 | $id_rubrique = sql_getfetsel('id_rubrique', $table_sql, "$_id_table=".intval($id), '', '', '', '', $serveur); |
---|
334 | return compositions_verrou_branche($id_rubrique, $serveur); |
---|
335 | } |
---|
336 | else |
---|
337 | return false; |
---|
338 | } |
---|
339 | else return false; |
---|
340 | } |
---|
341 | |
---|
342 | /** |
---|
343 | * Indique si les objets d'une branche sont verrouillés |
---|
344 | * @param integer $id_rubrique |
---|
345 | * @param string $serveur |
---|
346 | * @return string |
---|
347 | */ |
---|
348 | function compositions_verrou_branche($id_rubrique, $serveur=''){ |
---|
349 | |
---|
350 | if (intval($id_rubrique) < 1) return false; |
---|
351 | if($infos_rubrique = sql_fetsel(array('id_parent','composition_branche_lock'),'spip_rubriques','id_rubrique='.intval($id_rubrique),'','','','',$serveur)) { |
---|
352 | if ($infos_rubrique['composition_branche_lock']) |
---|
353 | return true; |
---|
354 | else |
---|
355 | return compositions_verrou_branche($infos_rubrique['id_parent'],$serveur); |
---|
356 | } |
---|
357 | return ''; |
---|
358 | } |
---|
359 | ?> |
---|