[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/www.git] / www / plugins-dist / breves / breves_pipelines.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2016 *
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 * Utilisations de pipelines
15 *
16 * @package SPIP\Breves\Pipelines
17 **/
18 if (!defined('_ECRIRE_INC_VERSION')) {
19 return;
20 }
21
22
23 /**
24 * Définir les meta de configuration liées aux brèves
25 *
26 * @pipeline configurer_liste_metas
27 * @param array $metas
28 * Couples nom de la méta => valeur par défaut
29 * @return array
30 * Couples nom de la méta => valeur par défaut
31 */
32 function breves_configurer_liste_metas($metas) {
33 $metas['activer_breves'] = 'non';
34
35 return $metas;
36 }
37
38 /**
39 * Ajouter les brèves à valider sur les rubriques
40 *
41 * @pipeline rubrique_encours
42 *
43 * @param array $flux Données du pipeline
44 * @return array Données du pipeline
45 **/
46 function breves_rubrique_encours($flux) {
47 if ($flux['args']['type'] == 'rubrique') {
48 $lister_objets = charger_fonction('lister_objets', 'inc');
49
50 $id_rubrique = $flux['args']['id_objet'];
51
52 //
53 // Les breves a valider
54 //
55 $flux['data'] .= $lister_objets('breves', array(
56 'titre' => _T('breves:info_breves_valider'),
57 'statut' => array('prepa', 'prop'),
58 'id_rubrique' => $id_rubrique,
59 'par' => 'date_heure'
60 ));
61 }
62
63 return $flux;
64 }
65
66
67 /**
68 * Ajouter les brèves référencées sur les vues de rubriques
69 *
70 * @pipeline affiche_enfants
71 *
72 * @param array $flux Données du pipeline
73 * @return array Données du pipeline
74 **/
75 function breves_affiche_enfants($flux) {
76 if (isset($flux['args']['exec'])
77 and $e = trouver_objet_exec($flux['args']['exec'])
78 and $e['type'] == 'rubrique'
79 and $e['edition'] == false
80 ) {
81 $id_rubrique = $flux['args']['id_rubrique'];
82
83 if ($GLOBALS['meta']['activer_breves'] == 'oui') {
84 $lister_objets = charger_fonction('lister_objets', 'inc');
85 $bouton_breves = '';
86 $id_parent = sql_getfetsel('id_parent', 'spip_rubriques', 'id_rubrique=' . $id_rubrique);
87 if (autoriser('creerbrevedans', 'rubrique', $id_rubrique, null, array('id_parent' => $id_parent))) {
88 $bouton_breves .= icone_verticale(
89 _T('breves:icone_nouvelle_breve'),
90 generer_url_ecrire('breve_edit', "id_rubrique=$id_rubrique&new=oui"),
91 'breve-24.png',
92 'new',
93 'right'
94 )
95 . "<br class='nettoyeur' />";
96 }
97
98 $flux['data'] .= $lister_objets('breves', array(
99 'titre' => _T('breves:icone_ecrire_nouvel_article'),
100 'where' => "statut != 'prop' AND statut != 'prepa'",
101 'id_rubrique' => $id_rubrique,
102 'par' => 'date_heure'
103 ));
104 $flux['data'] .= $bouton_breves;
105 }
106 }
107
108 return $flux;
109 }
110
111
112 /**
113 * Ajoute le nombre de brèves sur l'accueil privé
114 *
115 * @pipeline accueil_informations
116 *
117 * @param string $texte
118 * HTML des informations générales concernant chaque type d'objet
119 * sur la page d'accueil privée
120 * @return string
121 * HTML des informations générales concernant chaque type d'objet
122 * sur la page d'accueil privée
123 */
124 function breves_accueil_informations($texte) {
125 include_spip('base/abstract_sql');
126
127 $q = sql_select('COUNT(*) AS cnt, statut', 'spip_breves', '', 'statut', '', '', 'COUNT(*)<>0');
128
129 $cpt = array();
130 $cpt2 = array();
131 $where = false;
132 if ($GLOBALS['visiteur_session']['statut'] == '0minirezo') {
133 $where = sql_allfetsel(
134 'id_objet',
135 'spip_auteurs_liens',
136 "objet='rubrique' AND id_auteur=" . intval($GLOBALS['visiteur_session']['id_auteur'])
137 );
138 if ($where) {
139 $where = sql_in('id_rubrique', array_map('reset', $where));
140 }
141 }
142 $defaut = $where ? '0/' : '';
143 while ($row = sql_fetch($q)) {
144 $cpt[$row['statut']] = $row['cnt'];
145 $cpt2[$row['statut']] = $defaut;
146 }
147
148 if ($cpt) {
149 if ($where) {
150 $q = sql_select('COUNT(*) AS cnt, statut', 'spip_breves', $where, 'statut');
151 while ($row = sql_fetch($q)) {
152 $r = $row['statut'];
153 $cpt2[$r] = intval($row['cnt']) . '/';
154 }
155 }
156 $texte .= "<div class='accueil_informations breves liste'>";
157 $texte .= '<h4>' . afficher_plus_info(generer_url_ecrire('breves'), '', _T('breves:info_breves_02')) . '</h4>';
158 $texte .= "<ul class='liste-items'>";
159 if (isset($cpt['prop'])) {
160 $texte .= "<li class='item'>" . _T('texte_statut_attente_validation') . ': ' . $cpt2['prop'] . $cpt['prop'] . '</li>';
161 }
162 if (isset($cpt['publie'])) {
163 $texte .= "<li class='item on'>" . _T('texte_statut_publies') . ': ' . $cpt2['publie'] . $cpt['publie'] . '</li>';
164 }
165 $texte .= '</ul>';
166 $texte .= '</div>';
167 }
168
169 return $texte;
170 }
171
172
173 /**
174 * Compter les brèves dans une rubrique
175 *
176 * @pipeline objet_compte_enfants
177 *
178 * @param array $flux Données du pipeline
179 * @return array Données du pipeline
180 */
181 function breves_objet_compte_enfants($flux) {
182 if ($flux['args']['objet'] == 'rubrique'
183 and $id_rubrique = intval($flux['args']['id_objet'])
184 ) {
185 // juste les publies ?
186 if (array_key_exists('statut', $flux['args']) and ($flux['args']['statut'] == 'publie')) {
187 $flux['data']['breve'] = sql_countsel(
188 'spip_breves',
189 'id_rubrique=' . intval($id_rubrique) . " AND (statut='publie')"
190 );
191 } else {
192 $flux['data']['breve'] = sql_countsel(
193 'spip_breves',
194 'id_rubrique=' . intval($id_rubrique) . " AND (statut='publie' OR statut='prop')"
195 );
196 }
197 }
198
199 return $flux;
200 }
201
202
203 /**
204 * Changer la langue des brèves si la rubrique change
205 *
206 * @pipeline trig_calculer_langues_rubriques
207 *
208 * @param array $flux Données du pipeline
209 * @return array Données du pipeline
210 */
211 function breves_trig_calculer_langues_rubriques($flux) {
212 $s = sql_select(
213 'A.id_breve AS id_breve, R.lang AS lang',
214 'spip_breves AS A, spip_rubriques AS R',
215 "A.id_rubrique = R.id_rubrique AND A.langue_choisie != 'oui' AND (A.lang='' OR R.lang<>'') AND R.lang<>A.lang"
216 );
217 while ($row = sql_fetch($s)) {
218 $id_breve = $row['id_breve'];
219 sql_updateq(
220 'spip_breves',
221 array('lang' => $row['lang'], 'langue_choisie' => 'non'),
222 'id_breve='.intval($id_breve)
223 );
224 }
225
226 return $flux;
227 }
228
229
230 /**
231 * Publier et dater les rubriques qui ont une brève publiée
232 *
233 * @pipeline calculer_rubriques
234 *
235 * @param array $flux Données du pipeline
236 * @return array Données du pipeline
237 */
238 function breves_calculer_rubriques($flux) {
239 $r = sql_select(
240 'R.id_rubrique AS id, max(A.date_heure) AS date_h',
241 'spip_rubriques AS R JOIN spip_breves AS A ON R.id_rubrique = A.id_rubrique',
242 "A.date_heure>R.date_tmp AND A.statut='publie' ",
243 'R.id_rubrique'
244 );
245 while ($row = sql_fetch($r)) {
246 sql_updateq(
247 'spip_rubriques',
248 array('statut_tmp' => 'publie', 'date_tmp' => $row['date_h']),
249 'id_rubrique=' . intval($row['id'])
250 );
251 }
252
253 return $flux;
254 }
255
256
257 /**
258 * Ajouter les brèves à valider sur la page d'accueil
259 *
260 * @pipeline accueil_encours
261 *
262 * @param string $flux HTML du bloc encours sur la page d'accueil privée
263 * @return string HTML du bloc encours sur la page d'accueil privée
264 **/
265 function breves_accueil_encours($flux) {
266 $lister_objets = charger_fonction('lister_objets', 'inc');
267
268 $flux .= $lister_objets('breves', array(
269 'titre' => afficher_plus_info(generer_url_ecrire('breves')) . _T('breves:info_breves_valider'),
270 'statut' => array('prepa', 'prop'),
271 'par' => 'date_heure'
272 ));
273
274 return $flux;
275 }
276
277
278 /**
279 * Optimiser la base de données en supprimant les liens de brèves orphelins
280 *
281 * @pipeline optimiser_base_disparus
282 *
283 * @param array $flux Données du pipeline
284 * @return array Données du pipeline
285 */
286 function breves_optimiser_base_disparus($flux) {
287 $n = &$flux['data'];
288 $mydate = $flux['args']['date'];
289
290
291 # les breves qui sont dans une id_rubrique inexistante
292 $res = sql_select(
293 'B.id_breve AS id',
294 'spip_breves AS B
295 LEFT JOIN spip_rubriques AS R
296 ON B.id_rubrique=R.id_rubrique',
297 'R.id_rubrique IS NULL
298 AND B.maj < ' . sql_quote($mydate)
299 );
300
301 $n += optimiser_sansref('spip_breves', 'id_breve', $res);
302
303 //
304 // Breves
305 //
306 sql_delete('spip_breves', "statut='refuse' AND maj < $mydate");
307
308 return $flux;
309 }
310
311 /**
312 * Afficher le nombre de brèves dans chaque rubrique
313 *
314 * @pipeline boite_infos
315 *
316 * @param array $flux Données du pipeline
317 * @return array Données du pipeline
318 */
319 function breves_boite_infos($flux) {
320 if (
321 isset($flux['args']['type']) and $flux['args']['type'] == 'rubrique'
322 and isset($flux['args']['id']) and $id_rubrique = $flux['args']['id']
323 ) {
324 if ($nb = sql_countsel('spip_breves', "statut='publie' AND id_rubrique=" . intval($id_rubrique))) {
325 $nb = '<div>' . singulier_ou_pluriel($nb, 'breves:info_1_breve', 'breves:info_nb_breves') . '</div>';
326 if ($p = strpos($flux['data'], '<!--nb_elements-->')) {
327 $flux['data'] = substr_replace($flux['data'], $nb, $p, 0);
328 }
329 }
330 }
331
332 return $flux;
333 }
334
335 /**
336 * Ajoute le formulaire de configuration des brèves sur la page de
337 * configuration des contenus
338 *
339 * @pipeline affiche_milieu
340 *
341 * @param array $flux Données du pipeline
342 * @return array Données du pipeline
343 */
344 function breves_affiche_milieu($flux) {
345 if ($flux['args']['exec'] == 'configurer_contenu') {
346 $flux['data'] .= recuperer_fond('prive/squelettes/inclure/configurer', array('configurer' => 'configurer_breves'));
347 }
348
349 return $flux;
350 }