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 | * Liste les types d'objets qui ont une composition |
---|
145 | * utilise la valeur en cache meta sauf si demande de recalcul |
---|
146 | * ou pas encore definie |
---|
147 | * |
---|
148 | * @staticvar array $liste |
---|
149 | * @return array |
---|
150 | */ |
---|
151 | function compositions_types(){ |
---|
152 | static $liste = null; |
---|
153 | if (is_null($liste)) { |
---|
154 | if ($GLOBALS['var_mode'] OR !isset($GLOBALS['meta']['compositions_types'])){ |
---|
155 | include_spip('inc/compositions'); |
---|
156 | compositions_cacher(); |
---|
157 | } |
---|
158 | $liste = explode(',',$GLOBALS['meta']['compositions_types']); |
---|
159 | } |
---|
160 | return $liste; |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | * Renvoie la composition qui s'applique à un objet |
---|
165 | * en tenant compte, le cas échéant, de la composition héritée |
---|
166 | * si etoile=true on renvoi dire le champ sql |
---|
167 | * |
---|
168 | * @param string $type |
---|
169 | * @param integer $id |
---|
170 | * @param string $serveur |
---|
171 | * @param bool $etoile |
---|
172 | * @return string |
---|
173 | */ |
---|
174 | function compositions_determiner($type, $id, $serveur='', $etoile = false){ |
---|
175 | include_spip('base/abstract_sql'); |
---|
176 | $table = table_objet($type); |
---|
177 | $table_sql = table_objet_sql($type); |
---|
178 | $_id_table = id_table_objet($type); |
---|
179 | |
---|
180 | $retour = ''; |
---|
181 | |
---|
182 | $trouver_table = charger_fonction('trouver_table', 'base'); |
---|
183 | $desc = $trouver_table($table,$serveur); |
---|
184 | if (isset($desc['field']['composition']) AND $id){ |
---|
185 | $composition = sql_getfetsel('composition', $table_sql, "$_id_table=".intval($id), '', '', '', '', $serveur); |
---|
186 | if ($composition != '') |
---|
187 | $retour = $composition; |
---|
188 | elseif (!$etoile AND isset($desc['field']['id_rubrique'])) { |
---|
189 | $_id_rubrique = ($type == 'rubrique') ? 'id_parent' : 'id_rubrique'; |
---|
190 | $id_rubrique = sql_getfetsel($_id_rubrique,$table_sql,"$_id_table=".intval($id),'','','','',$serveur); |
---|
191 | $retour = compositions_heriter($type, $id_rubrique, $serveur); |
---|
192 | } |
---|
193 | else |
---|
194 | $retour = ''; |
---|
195 | } |
---|
196 | return ($retour == '-') ? '' : $retour; |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * Renvoie la composition héritée par un objet selon sa rubrique |
---|
201 | * |
---|
202 | * @param string $type |
---|
203 | * @param integer $id_rubrique |
---|
204 | * @param string $serveur |
---|
205 | * @return string |
---|
206 | */ |
---|
207 | function compositions_heriter($type, $id_rubrique, $serveur=''){ |
---|
208 | if ($type=='syndic') $type='site'; //grml |
---|
209 | if (intval($id_rubrique) < 1) return ''; |
---|
210 | if($infos_rubrique = sql_fetsel(array('id_parent','composition'),'spip_rubriques','id_rubrique='.intval($id_rubrique),'','','','',$serveur)) { |
---|
211 | if ( |
---|
212 | $infos_rubrique['composition'] != '' |
---|
213 | AND $infos = compositions_lister_disponibles('rubrique') |
---|
214 | AND isset($infos['rubrique'][$infos_rubrique['composition']]) |
---|
215 | AND isset($infos['rubrique'][$infos_rubrique['composition']]['branche']) |
---|
216 | AND isset($infos['rubrique'][$infos_rubrique['composition']]['branche'][$type]) |
---|
217 | ) |
---|
218 | return $infos['rubrique'][$infos_rubrique['composition']]['branche'][$type]; |
---|
219 | else |
---|
220 | return compositions_heriter($type, $infos_rubrique['id_parent'],$serveur); |
---|
221 | } |
---|
222 | return ''; |
---|
223 | } |
---|
224 | |
---|
225 | /** |
---|
226 | * #COMPOSITION |
---|
227 | * Renvoie la composition s'appliquant à un objet |
---|
228 | * en tenant compte, le cas échéant, de l'héritage. |
---|
229 | * |
---|
230 | * Sans precision, l'objet et son identifiant sont pris |
---|
231 | * dans la boucle en cours, mais l'on peut spécifier notre recherche |
---|
232 | * en passant objet et id_objet en argument de la balise : |
---|
233 | * #COMPOSITION{article, 8} |
---|
234 | * |
---|
235 | * #COMPOSITION* renvoie toujours le champs brut, sans tenir compte de l'heritage |
---|
236 | * |
---|
237 | * @param array $p AST au niveau de la balise |
---|
238 | * @return array AST->code modifié pour calculer le nom de la composition |
---|
239 | */ |
---|
240 | function balise_COMPOSITION_dist($p) { |
---|
241 | $_composition = ""; |
---|
242 | if ($_objet = interprete_argument_balise(1, $p)) { |
---|
243 | $_id_objet = interprete_argument_balise(2, $p); |
---|
244 | } else { |
---|
245 | $_composition = champ_sql('composition',$p); |
---|
246 | $_id_objet = champ_sql($p->boucles[$p->id_boucle]->primary, $p); |
---|
247 | $_objet = "objet_type('" . $p->boucles[$p->id_boucle]->id_table . "')"; |
---|
248 | } |
---|
249 | // si on veut le champ brut, et qu'on l'a sous la main, inutile d'invoquer toute la machinerie |
---|
250 | if ($_composition AND $p->etoile) |
---|
251 | $p->code = $_composition; |
---|
252 | else { |
---|
253 | $connect = $p->boucles[$p->id_boucle]->sql_serveur; |
---|
254 | $p->code = "compositions_determiner($_objet, $_id_objet, '$connect', ".($p->etoile?'true':'false').")"; |
---|
255 | // ne declencher l'usine a gaz que si composition est vide ... |
---|
256 | if ($_composition) |
---|
257 | $p->code = "((\$zc=$_composition)?\$zc:".$p->code.")"; |
---|
258 | } |
---|
259 | return $p; |
---|
260 | } |
---|
261 | |
---|
262 | /** |
---|
263 | * Indique si la composition d'un objet est verrouillée ou non, |
---|
264 | * auquel cas, seul le webmaster peut la modifier |
---|
265 | * |
---|
266 | * @param string $type |
---|
267 | * @param integer $id |
---|
268 | * @param string $serveur |
---|
269 | * @return string |
---|
270 | */ |
---|
271 | function compositions_verrouiller($type, $id, $serveur=''){ |
---|
272 | $config = unserialize($GLOBALS['meta']['compositions']); |
---|
273 | if ($config['tout_verrouiller'] == 'oui') |
---|
274 | return true; |
---|
275 | |
---|
276 | include_spip('base/abstract_sql'); |
---|
277 | $table = table_objet($type); |
---|
278 | $table_sql = table_objet_sql($type); |
---|
279 | $_id_table = id_table_objet($type); |
---|
280 | |
---|
281 | $trouver_table = charger_fonction('trouver_table', 'base'); |
---|
282 | $desc = $trouver_table($table,$serveur); |
---|
283 | if (isset($desc['field']['composition_lock']) AND $id){ |
---|
284 | $lock = sql_getfetsel('composition_lock', $table_sql, "$_id_table=".intval($id), '', '', '', '', $serveur); |
---|
285 | if ($lock) |
---|
286 | return true; |
---|
287 | elseif (isset($desc['field']['id_rubrique'])) { |
---|
288 | $id_rubrique = sql_getfetsel('id_rubrique', $table_sql, "$_id_table=".intval($id), '', '', '', '', $serveur); |
---|
289 | return compositions_verrou_branche($id_rubrique, $serveur); |
---|
290 | } |
---|
291 | else |
---|
292 | return false; |
---|
293 | } |
---|
294 | else return false; |
---|
295 | } |
---|
296 | |
---|
297 | /** |
---|
298 | * Indique si les objets d'une branche sont verrouillés |
---|
299 | * @param integer $id_rubrique |
---|
300 | * @param string $serveur |
---|
301 | * @return string |
---|
302 | */ |
---|
303 | function compositions_verrou_branche($id_rubrique, $serveur=''){ |
---|
304 | |
---|
305 | if (intval($id_rubrique) < 1) return false; |
---|
306 | if($infos_rubrique = sql_fetsel(array('id_parent','composition_branche_lock'),'spip_rubriques','id_rubrique='.intval($id_rubrique),'','','','',$serveur)) { |
---|
307 | if ($infos_rubrique['composition_branche_lock']) |
---|
308 | return true; |
---|
309 | else |
---|
310 | return compositions_verrou_branche($infos_rubrique['id_parent'],$serveur); |
---|
311 | } |
---|
312 | return ''; |
---|
313 | } |
---|
314 | ?> |
---|