[SPIP] v3.2.1-->v3.2.2
[lhc/web/www.git] / www / ecrire / inc / puce_statut.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2019 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
12
13 /**
14 * Gestion des puces de statut sur les objets
15 * ainsi que des puces de changement rapide de statut.
16 *
17 * @package SPIP\Core\PuceStatut
18 **/
19
20 if (!defined('_ECRIRE_INC_VERSION')) {
21 return;
22 }
23
24 if (!defined('_ACTIVER_PUCE_RAPIDE')) {
25 /**
26 * Activer le changement rapide de statut sur les listes d'objets ?
27 *
28 * Peut ralentir un site sur des listes très longues.
29 *
30 * @var bool
31 **/
32 define('_ACTIVER_PUCE_RAPIDE', true);
33 }
34
35 /**
36 * Afficher la puce statut d'un objet
37 *
38 * Utilise une fonction spécifique pour un type d'objet si elle existe, tel que
39 * puce_statut_$type_dist(), sinon tente avec puce_statut_changement_rapide().
40 *
41 * @see puce_statut_changement_rapide()
42 *
43 * @param int $id_objet
44 * Identifiant de l'objet
45 * @param string $statut
46 * Statut actuel de l'objet
47 * @param int $id_parent
48 * Identifiant du parent
49 * @param string $type
50 * Type d'objet
51 * @param bool $ajax
52 * Indique s'il ne faut renvoyer que le coeur du menu car on est
53 * dans une requete ajax suite à un post de changement rapide
54 * @param bool $menu_rapide
55 * Indique si l'on peut changer le statut, ou si on l'affiche simplement
56 * @return string
57 * Code HTML de l'image de puce de statut à insérer (et du menu de changement si présent)
58 */
59 function inc_puce_statut_dist(
60 $id_objet,
61 $statut,
62 $id_parent,
63 $type,
64 $ajax = false,
65 $menu_rapide = _ACTIVER_PUCE_RAPIDE
66 ) {
67 static $f_puce_statut = array();
68 $type = objet_type($type);
69 // cas prioritaire : fonction perso, qui permet aussi de gerer les cas historiques
70 if (!isset($f_puce_statut[$type]) or is_null($f_puce_statut[$type])) {
71 $f_puce_statut[$type] = charger_fonction($type, 'puce_statut', true);
72 }
73 if ($f_puce_statut[$type]) {
74 return $f_puce_statut[$type]($id_objet, $statut, $id_parent, $type, $ajax, $menu_rapide);
75 }
76
77 // si statut_image trouve quelque chose (et '' est quelque chose)
78 // composer une puce, avec si possible changement rapide
79 elseif (!is_null($puce = puce_statut_changement_rapide($id_objet, $statut, $id_parent, $type, $ajax, $menu_rapide))) {
80 return $puce;
81 } // sinon fausse puce avec le type de l'image
82 else {
83 return http_img_pack("$type-16.png", '');
84 }
85 }
86
87 /**
88 * Récuperer l'image correspondant au statut pour un objet éditorial indiqué
89 *
90 * Retrouve l'image correspondant au statut, telle que declarée dans
91 * `declarer_tables_objets_sql` sous la forme :
92 *
93 * ```
94 * array(
95 * 'imagepardefaut.png',
96 * 'statut1' => 'imagestatut1.png',
97 * 'statut2' => 'imagestatut2.png',
98 * ...
99 * )
100 * ```
101 *
102 * Mettre une chaine vide pour ne pas avoir d'image pour un statut particulier.
103 *
104 * Si rien n'est declaré et que le statut est dans un des cas connus habituels
105 * (prepa, prop, publie, refuse, poubelle), alors on renvoie l'image par défaut pour ce statut
106 *
107 * @param string $objet
108 * @param string $statut
109 * @return string|null
110 * null si pas capable de déterminer l'image
111 */
112 function statut_image($objet, $statut) {
113 $src = null;
114 $table = table_objet_sql($objet);
115 $desc = lister_tables_objets_sql($table);
116 if (isset($desc['statut_images'])) {
117 // si une declaration statut_images
118 // mais rien pour le statut demande, ne rien afficher
119 $src = '';
120 if (isset($desc['statut_images'][$statut])) {
121 $src = $desc['statut_images'][$statut];
122 } // sinon image par defaut ?
123 elseif (isset($desc['statut_images'][0])) {
124 $src = $desc['statut_images'][0];
125 }
126 } else {
127 switch ($statut) {
128 case 'prepa':
129 $src = 'puce-preparer-8.png';
130 break;
131 case 'prop':
132 $src = 'puce-proposer-8.png';
133 break;
134 case 'publie':
135 $src = 'puce-publier-8.png';
136 break;
137 case 'refuse':
138 $src = 'puce-refuser-8.png';
139 break;
140 case 'poubelle':
141 case 'poub':
142 $src = 'puce-supprimer-8.png';
143 break;
144 }
145 }
146
147 return $src;
148 }
149
150 /**
151 * Récupérer le titre correspondant au statut pour un objet éditorial indiqué
152 *
153 * Retrouve le titre correspondant au statut, tel qu'il a été declaré dans
154 * `declarer_tables_objets_sql` sous la forme :
155 *
156 * ```
157 * array(
158 * 'titre par defaut',
159 * 'statut1' => 'titre statut 1',
160 * 'statut2' => 'titre statut 2',
161 * ...
162 * )
163 * ```
164 *
165 * Mettre une chaine vide pour ne pas avoir de titre pour un statut particulier.
166 *
167 * Si rien n'est declaré et que le statut est dans un des cas connus habituels
168 * (prepa, prop, publie, refuse, poubelle), alors on renvoie le texte par défaut pour ce statut
169 *
170 * @param string $objet
171 * @param string $statut
172 * @return string
173 */
174 function statut_titre($objet, $statut) {
175 $titre = '';
176 $table = table_objet_sql($objet);
177 $desc = lister_tables_objets_sql($table);
178 if (isset($desc['statut_titres'])) {
179 // si une declaration statut_titres
180 // mais rien pour le statut demande, ne rien afficher
181 if (isset($desc['statut_titres'][$statut])) {
182 $titre = $desc['statut_titres'][$statut];
183 } // sinon image par defaut ?
184 elseif (isset($desc['statut_titres'][0])) {
185 $titre = $desc['statut_titres'][0];
186 }
187 } else {
188 switch ($statut) {
189 case 'prepa':
190 $titre = 'texte_statut_en_cours_redaction';
191 break;
192 case 'prop':
193 $titre = 'texte_statut_propose_evaluation';
194 break;
195 case 'publie':
196 $titre = 'texte_statut_publie';
197 break;
198 case 'refuse':
199 $titre = 'texte_statut_refuse';
200 break;
201 case 'poubelle':
202 case 'poub':
203 $titre = 'texte_statut_poubelle';
204 break;
205 }
206 }
207
208 return $titre ? _T($titre) : '';
209 }
210
211
212 /**
213 * Recuperer le texte correspondant au choix de statut, tel que declare dans
214 * declarer_tables_objets_sql
215 * sous la forme
216 * array('statut1'=>'texte statut 1','statut2'=>'texte statut 2' ...)
217 * mettre une chaine vide pour ne pas proposer un statut
218 * les statuts seront proposes dans le meme ordre que dans la declaration
219 *
220 * si rien de declare et que le statut est dans les cas connus (prepa, prop, publie, refuse, poubelle)
221 * renvoyer le texte par defaut
222 *
223 * @param string $objet
224 * @param string $statut
225 * @return string
226 */
227 function statut_texte_instituer($objet, $statut) {
228 $texte = '';
229 $table = table_objet_sql($objet);
230 $desc = lister_tables_objets_sql($table);
231 if (isset($desc['statut_textes_instituer'])) {
232 // si une declaration statut_titres
233 // mais rien pour le statut demande, ne rien afficher
234 if (isset($desc['statut_textes_instituer'][$statut])) {
235 $texte = $desc['statut_textes_instituer'][$statut];
236 }
237 } else {
238 switch ($statut) {
239 case 'prepa':
240 $texte = 'texte_statut_en_cours_redaction';
241 break;
242 case 'prop':
243 $texte = 'texte_statut_propose_evaluation';
244 break;
245 case 'publie':
246 $texte = 'texte_statut_publie';
247 break;
248 case 'refuse':
249 $texte = 'texte_statut_refuse';
250 break;
251 case 'poubelle':
252 case 'poub':
253 $texte = 'texte_statut_poubelle';
254 break;
255 }
256 }
257
258 return $texte ? _T($texte) : '';
259 }
260
261
262 /**
263 * Afficher la puce statut d'un auteur
264 *
265 * Ne semble plus servir : desactive
266 * Hack de compatibilite: les appels directs ont un $type != 'auteur'
267 * si l'auteur ne peut pas se connecter
268 *
269 * @param int $id
270 * @param string $statut
271 * @param int $id_parent
272 * @param string $type
273 * @param string $ajax
274 * @param bool $menu_rapide
275 * @return string
276 */
277 function puce_statut_auteur_dist($id, $statut, $id_parent, $type, $ajax = '', $menu_rapide = _ACTIVER_PUCE_RAPIDE) {
278 $img = statut_image('auteur', $statut);
279 if (!$img) {
280 return '';
281 }
282 $alt = statut_titre('auteur', $statut);
283
284 $fond = '';
285 $titre = '';
286
287 /*
288 if ($type != 'auteur') {
289 $img2 = chemin_image('del-16.png');
290 $titre = _T('titre_image_redacteur');
291 $fond = http_style_background($img2, 'top left no-repeat;');
292 }
293 else {
294 }
295 */
296
297 return http_img_pack($img, $alt, $fond, $alt);
298 }
299
300
301 function puce_statut_rubrique_dist($id, $statut, $id_rubrique, $type, $ajax = '', $menu_rapide = _ACTIVER_PUCE_RAPIDE) {
302 return http_img_pack('rubrique-16.png', '');
303 }
304
305 /**
306 * Retourne le contenu d'une puce avec changement de statut possible
307 * si on en a l'autorisation, sinon simplement l'image de la puce
308 *
309 * @param int $id
310 * Identifiant de l'objet
311 * @param string $statut
312 * Statut actuel de l'objet
313 * @param int $id_rubrique
314 * Identifiant du parent, une rubrique
315 * @param string $type
316 * Type d'objet
317 * @param bool $ajax
318 * Indique s'il ne faut renvoyer que le coeur du menu car on est
319 * dans une requete ajax suite à un post de changement rapide
320 * @param bool $menu_rapide
321 * Indique si l'on peut changer le statut, ou si on l'affiche simplement
322 * @return string
323 * Code HTML de l'image de puce de statut à insérer (et du menu de changement si présent)
324 **/
325 function puce_statut_changement_rapide(
326 $id,
327 $statut,
328 $id_rubrique,
329 $type = 'article',
330 $ajax = false,
331 $menu_rapide = _ACTIVER_PUCE_RAPIDE
332 ) {
333 $src = statut_image($type, $statut);
334 if (!$src) {
335 return $src;
336 }
337
338 if (!$id
339 or !_SPIP_AJAX
340 or !$menu_rapide
341 ) {
342 $ajax_node = '';
343 } else {
344 $ajax_node = " class='imgstatut$type$id'";
345 }
346
347
348 $t = statut_titre($type, $statut);
349 $inser_puce = http_img_pack($src, $t, $ajax_node, $t);
350
351 if (!$ajax_node) {
352 return $inser_puce;
353 }
354
355 $table = table_objet_sql($type);
356 $desc = lister_tables_objets_sql($table);
357 if (!isset($desc['statut_textes_instituer'])) {
358 return $inser_puce;
359 }
360
361 if (!function_exists('autoriser')) {
362 include_spip('inc/autoriser');
363 }
364
365 // cas ou l'on a un parent connu (devrait disparaitre au profit du second cas plus generique)
366 if ($id_rubrique) {
367 if (!autoriser('publierdans', 'rubrique', $id_rubrique)) {
368 return $inser_puce;
369 }
370 } // si pas d'id_rubrique fourni, tester directement instituer type avec le statut publie
371 else {
372 if (!autoriser('instituer', $type, $id, null, array('statut' => 'publie'))) {
373 return $inser_puce;
374 }
375 }
376
377 $coord = array_flip(array_keys($desc['statut_textes_instituer']));
378 if (!isset($coord[$statut])) {
379 return $inser_puce;
380 }
381
382 $unit = 8/*widh de img*/ + 4/*padding*/
383 ;
384 $margin = 4; /* marge a gauche + droite */
385 $zero = 1 /*border*/ + $margin / 2 + 2 /*padding*/
386 ;
387 $clip = $zero + ($unit * $coord[$statut]);
388
389 if ($ajax) {
390 $width = $unit * count($desc['statut_textes_instituer']) + $margin;
391 $out = "<span class='puce_objet_fixe $type'>"
392 . $inser_puce
393 . "</span>"
394 . "<span class='puce_objet_popup $type statutdecal$type$id' style='width:{$width}px;margin-left:-{$clip}px;'>";
395 $i = 0;
396 foreach ($desc['statut_textes_instituer'] as $s => $t) {
397 $out .= afficher_script_statut($id, $type, -$zero - $i++ * $unit, statut_image($type, $s), $s, _T($t));
398 }
399 $out .= "</span>";
400
401 return $out;
402 } else {
403
404 $nom = "puce_statut_";
405 $action = generer_url_ecrire('puce_statut', "", true);
406 $action = "if (!this.puce_loaded) { this.puce_loaded = true; prepare_selec_statut(this, '$nom', '$type', '$id', '$action'); }";
407 $over = " onmouseover=\"$action\"";
408
409 $lang_dir = lang_dir(lang_typo());
410
411 return "<span class='puce_objet $type' id='$nom$type$id' dir='$lang_dir'$over>"
412 . $inser_puce
413 . '</span>';
414 }
415 }
416
417
418 function afficher_script_statut($id, $type, $n, $img, $statut, $titre, $act = '') {
419 $h = generer_action_auteur("instituer_objet", "$type-$id-$statut");
420 $h = "selec_statut('$id', '$type', $n, jQuery('img',this).attr('src'), '$h');return false;";
421 $t = supprimer_tags($titre);
422
423 return "<a href=\"#\" onclick=\"$h\" title=\"$t\"$act>" . http_img_pack($img, $t) . "</a>";
424 }
425
426 // compat
427 // La couleur du statut
428
429 function puce_statut($statut, $atts = '') {
430 $src = statut_image('article', $statut);
431 if (!$src) {
432 return '';
433 }
434
435 return http_img_pack($src, statut_titre('article', $statut), $atts);
436 }