[SPIP] ~2.1.12 -->2.1.25
[velocampus/web/www.git] / www / ecrire / inc / editer_mots.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2014 *
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 if (!defined('_ECRIRE_INC_VERSION')) return;
14 include_spip('inc/actions');
15 include_spip('inc/mots');
16
17 // $flag indique si on est autorise a modifier l'article
18 // http://doc.spip.org/@inc_editer_mots_dist
19 function inc_editer_mots_dist($objet, $id_objet, $cherche_mot, $select_groupe, $flag, $visible = false, $url_base='') {
20 if ($GLOBALS['meta']["articles_mots"] == 'non') return '';
21 if (!preg_match('/^[0-9, ]*$/', $select_groupe)) return '';
22 $trouver_table = charger_fonction('trouver_table', 'base');
23 $nom = table_objet($objet);
24 $desc = $trouver_table($nom);
25 $table_id = @$desc['key']["PRIMARY KEY"];
26
27 $reponse = ($flag AND $cherche_mot)
28 ? chercher_inserer_mot($cherche_mot, $select_groupe, $objet, $id_objet, $nom, $table_id, $url_base)
29 : '';
30
31 list($liste, $mots) = afficher_mots_cles($flag, $objet, $id_objet, $nom, $table_id, $url_base);
32
33 $aff =false;
34 $bouton = _T('titre_mots_cles').aide ("artmots");
35
36 if ($liste)
37 $aff = true;
38
39 if ($flag) { // si droit de modif donner le formulaire
40 $visible = ($visible OR $cherche_mot OR ($flag === 'ajax'));
41 list($visible, $res) = formulaire_mots_cles($id_objet, $mots, $nom, $table_id, $url_base, $visible, $objet);
42
43 if ($res) {
44 $liste .= debut_block_depliable($visible,"lesmots")
45 . $res
46 . creer_mot($nom, $id_objet, $table_id, $url_base, $cherche_mot, $select_groupe)
47 . fin_block();
48 $bouton = bouton_block_depliable($bouton, $visible,"lesmots");
49 $aff = true;
50 }
51 }
52
53 if (!$aff)
54 return '';
55
56 $res = debut_cadre_enfonce("mot-cle-24.gif", true, "", $bouton)
57 . $reponse
58 . $liste
59 . fin_cadre_enfonce(true);
60
61 return ajax_action_greffe("editer_mots", $id_objet, $res);
62 }
63
64 function chercher_inserer_mot($cherche_mot, $select_groupe, $objet, $id_objet, $nom, $table_id, $url_base)
65 {
66 $modifier = false;
67
68 list($reponse, $nouveaux_mots) = recherche_mot_cle($cherche_mot, $select_groupe, $objet, $id_objet, $nom, $table_id, $url_base);
69 foreach($nouveaux_mots as $nouv_mot) {
70 if ($nouv_mot!='x') {
71 $modifier |= inserer_mot("spip_mots_$nom", $table_id, $id_objet, $nouv_mot);
72 }
73 }
74 if ($modifier) {
75 pipeline('post_edition',
76 array(
77 'args' => array(
78 'operation' => 'editer_mots',
79 'table' => table_objet_sql($objet),
80 'id_objet' => $id_objet
81 ),
82 'data' => null
83 )
84 );
85 }
86 return $reponse;
87 }
88 // http://doc.spip.org/@inserer_mot
89 function inserer_mot($table, $table_id, $id_objet, $id_mot)
90 {
91 $r = sql_countsel($table, "id_mot=$id_mot AND $table_id=$id_objet");
92 if (!$r) {
93 sql_insertq($table, array('id_mot' =>$id_mot, $table_id => $id_objet));
94 return true;
95 }
96 }
97
98
99 // http://doc.spip.org/@recherche_mot_cle
100 function recherche_mot_cle($cherche_mots, $id_groupe, $objet, $id_objet, $table, $table_id, $url_base)
101 {
102 $ou = _T('info_mot_cle_ajoute') . ' ';
103 if ($table == 'articles') $ou .= _T('info_l_article');
104 else if ($table == 'breves') $ou .= _T('info_la_breve');
105 else if ($table == 'rubriques') $ou .= _T('info_la_rubrique');
106
107 $result = sql_select("id_mot, titre", "spip_mots", (!$id_groupe ? '' : sql_in('id_groupe', $id_groupe)));
108
109 $table_mots = array();
110 $table_ids = array();
111 while ($row = sql_fetch($result)) {
112 $table_ids[] = $row['id_mot'];
113 $table_mots[] = $row['titre'];
114 }
115
116 $nouveaux_mots = array();
117 $res = '';
118
119 foreach (preg_split("/ *[,;] */", $cherche_mots) as $cherche_mot) {
120 if ($cherche_mot) {
121 $resultat = mots_ressemblants($cherche_mot, $table_mots, $table_ids);
122 $res .= "<br />" . debut_boite_info(true);
123 if (!$resultat) {
124 $res .= "<b>"._T('info_non_resultat', array('cherche_mot' => htmlspecialchars($cherche_mot)))."</b><br />";
125 }
126 else if (count($resultat) == 1) {
127 $n = $resultat[0];
128 $nouveaux_mots[] = $n;
129 $t = sql_getfetsel("titre", "spip_mots", "id_mot=$n");
130 $res .= "<b>"
131 . $ou
132 . ": </b><br />\n<ul><li><span class='verdana1 spip_small'><b><span class='spip_medium'>"
133 . typo($t)
134 . "</span></b></span></li></ul>";
135 }
136 else $res .= affiche_mots_ressemblant($cherche_mot, $objet, $id_objet, $resultat, $table, $table_id, $url_base);
137
138 $res .= fin_boite_info(true) . "<br />";
139 }
140 }
141 return array($res, $nouveaux_mots);
142 }
143
144 // http://doc.spip.org/@afficher_mots_cles
145 function afficher_mots_cles($flag, $objet, $id_objet, $table, $table_id, $url)
146 {
147 $q = array('SELECT' => "M.id_mot, M.titre, M.id_groupe", 'FROM' => "spip_mots AS M LEFT JOIN spip_mots_$table AS L ON M.id_mot=L.id_mot", 'WHERE' => "L.$table_id=$id_objet", 'ORDER BY' => "M.type, M.titre");
148
149 $ret = generer_url_retour($url, "$table_id=$id_objet#editer_mots-$id_objet");
150 $styles = array(array('arial11',25), array('arial2'), array('arial2'), array('arial1'));
151
152 $presenter_liste = charger_fonction('presenter_liste', 'inc');
153
154 // cette variable est passee par reference
155 // pour recevoir les valeurs du champ indique
156 $mots = 'id_mot';
157 $a = array($flag,$id_objet, $objet, $ret, $table, $table_id, $url);
158 $res = $presenter_liste($q, 'editer_mots_un', $mots, $a, false, $styles);
159
160 return array($res, $mots);
161 }
162
163 // http://doc.spip.org/@editer_mots_un
164 function editer_mots_un($row, $own)
165 {
166 $puce_statut = charger_fonction('puce_statut', 'inc');
167
168 list ($flag_editable, $id_objet, $objet, $ret, $table, $table_id, $url_base) = $own;
169
170 $id_mot = $row['id_mot'];
171 $titre_mot = $row['titre'];
172 $id_groupe = $row['id_groupe'];
173
174 $url = generer_url_ecrire('mots_edit', "id_mot=$id_mot&redirect=$ret");
175
176 $groupe_champs = sql_fetsel("*", "spip_groupes_mots", "id_groupe = $id_groupe");
177 $groupe = typo($groupe_champs['titre']);
178
179 if (autoriser('modifier', 'groupemots', $id_groupe))
180 $groupe = "<a href='" . generer_url_ecrire("mots_type","id_groupe=$id_groupe") . "'>$groupe</a>";
181
182 $mot = "<a href='$url'>".typo($titre_mot)."</a>";
183
184 $retire = '';
185 if ($flag_editable
186 AND autoriser('editermots', $objet, $id_objet, null, array('id_groupe'=>$id_groupe,'groupe_champs'=>$groupe_champs))
187 ) {
188 $r = _T('info_retirer_mot')
189 . "&nbsp;"
190 . http_img_pack('croix-rouge.gif', "X", " class='puce' style='vertical-align: bottom;'");
191
192 $retire = ajax_action_auteur('editer_mots', "$id_objet,$id_mot,$table,$table_id,$objet", $url_base, "$table_id=$id_objet", array($r,''),"&id_objet=$id_objet&objet=$objet");
193
194 // Changer ; si unseul, poser un petit menu
195 if (sql_getfetsel('unseul', 'spip_groupes_mots', 'id_groupe='.$id_groupe)
196 == 'oui')
197 $mot = formulaire_mot_remplace($id_groupe, $id_mot, $url_base, $table, $table_id, $objet, $id_objet);
198 }
199
200 $cle = $puce_statut($id_mot, 'publie', $id_groupe, 'mot');
201
202 return array("<a href='$url'>$cle</a>", $mot, $groupe, $retire);
203 }
204
205 // http://doc.spip.org/@formulaire_mot_remplace
206 function formulaire_mot_remplace($id_groupe, $id_mot, $url_base, $table, $table_id, $objet, $id_objet)
207 {
208 $res = sql_allfetsel("id_mot, titre", "spip_mots", "id_groupe = $id_groupe", "", "titre");
209
210 foreach($res as $k => $row) {
211 $id = $row['id_mot'];
212 $titre = supprimer_tags(typo($row['titre']));
213 $selected = ($id == $id_mot) ? " selected='selected'" : "";
214 $res[$k]= "<option value='$id'$selected> $titre</option>";
215 }
216
217 $ancre = "valider_groupe_$id_groupe";
218 // forcer le recalcul du noeud car on est en Ajax
219 $jscript1 = "findObj_forcer('$ancre').style.visibility='visible';";
220
221 $corps = "\n<select name='nouv_mot' id='nouv_mot$id_groupe' onchange=\"$jscript1\""
222 . " class='spip_xx-small' style='width:90px;'>"
223 . join("\n", $res)
224 . "</select>\n&nbsp;" ;
225
226 $t = _T('bouton_changer');
227
228 return ajax_action_post('editer_mots', "$id_objet,$id_mot,$table,$table_id,$objet", $url_base, "$table_id=$id_objet",$corps, $t, " class='visible_au_chargement' id='$ancre'", "", "&id_objet=$id_objet&objet=$objet");
229 }
230
231 // int $id_objet : id_article
232 // array $les_mots : les mots deja apposes
233 // string $table : 'articles'
234 // string $table_id : 'id_article'
235 // string $url_base : 'articles' (?)
236 // boolean $visible : determiner si le formulaire est deplie
237 // string $objet : 'article'
238 // http://doc.spip.org/@formulaire_mots_cles
239 function formulaire_mots_cles($id_objet, $les_mots, $table, $table_id, $url_base, $visible, $objet) {
240 global $spip_lang, $spip_lang_right;
241
242 $res = '';
243
244 // liste des groupes de mots contenant au moins un mot deja appose a l'objet
245 $id_groupes_vus = array_map('array_pop',
246 sql_allfetsel('DISTINCT(id_groupe)', 'spip_mots',
247 sql_in('id_mot', $les_mots)));
248
249 // supprimer tous les mots ?
250 // a partir de 3 mots on regarde si l'ensemble des mots sont supprimables
251 // si oui on propose ce lien
252 if (count($les_mots)>= 3) {
253 $ok = true;
254 foreach ($id_groupes_vus as $id_groupe)
255 if (!autoriser('editermots', $objet, $id_objet, null,
256 array('id_groupe'=>$id_groupe))) {
257 $ok = false;
258 break;
259 }
260 if ($ok)
261 $res .= "<div style='text-align: right' class='arial1'>"
262 . ajax_action_auteur('editer_mots', "$id_objet,-1,$table,$table_id,$objet", $url_base, "$table_id=$id_objet", array(_T('info_retirer_mots'),''),"&id_objet=$id_objet&objet=$objet")
263 . "</div><br />\n";
264 }
265
266 // formulaire groupe par groupe
267 $ajouter ='';
268 $cond_mots_vus = $les_mots
269 ? " AND " . sql_in('id_mot', $les_mots, 'NOT')
270 : '';
271
272 define('_TRI_GROUPES_MOTS', 'titre');
273 foreach(sql_allfetsel('*,' . sql_multi ("titre", $spip_lang), 'spip_groupes_mots', '', '', _TRI_GROUPES_MOTS) as $row) {
274 $id_groupe = $row['id_groupe'];
275 if (autoriser('editermots', $objet, $id_objet, null,
276 array('id_groupe'=>$id_groupe,'groupe_champs'=>$row))
277 AND $menu = menu_mots($row, $id_groupes_vus, $cond_mots_vus)) {
278 list($corps, $clic) = $menu;
279 $ajouter .= ajax_action_post('editer_mots',
280 "$id_objet,,$table,$table_id,$objet",
281 $url_base,
282 "$table_id=$id_objet",
283 $corps,
284 $clic,
285 " class='visible_au_chargement spip_xx-small' id='valider_groupe_$id_groupe'", "",
286 "&id_objet=$id_objet&objet=$objet&select_groupe=$id_groupe");
287
288 // forcer la visibilite si au moins un mot obligatoire absent
289 // attention true <> 1 pour bouton_block_depliable
290 if ($row['obligatoire'] == 'oui'
291 AND !in_array($id_groupe, $id_groupes_vus))
292 $visible = true;
293 }
294 }
295
296 if ($ajouter) {
297 $res .= "<div style='float:$spip_lang_right; width:280px;position:relative;display:inline;'>"
298 . $ajouter
299 ."</div>\n"
300 . "<span class='verdana1'><b>"
301 ._T('titre_ajouter_mot_cle')
302 ."</b></span><br />\n";
303 }
304
305 return array($visible, $res);
306 }
307
308 function creer_mot($table, $id_objet, $table_id, $url_base, $mot='', $id_groupe=0)
309 {
310 static $titres = array(
311 'articles'=>'icone_creer_mot_cle',
312 'breves'=>'icone_creer_mot_cle_breve',
313 'rubriques'=>'icone_creer_mot_cle_rubrique',
314 'sites'=>'icone_creer_mot_cle_site'
315 );
316
317 if (!($id_groupe ?
318 autoriser('modifier','groupemots', $id_groupe) :
319 autoriser('modifier','groupemots'))
320 )
321 return '';
322
323 $legende = isset($titres[$table])
324 ? _T($titres[$table])
325 : _T('icone_creer_mot_cle');
326
327 $args = "new=oui&ajouter_id_article=$id_objet&table=$table&table_id=$table_id"
328 . (!$mot ? '' : ("&titre=".rawurlencode($mot)))
329 . (!$id_groupe ? '' : ("&id_groupe=".intval($id_groupe)))
330 . "&redirect=" . generer_url_retour($url_base, "$table_id=$id_objet");
331
332 return icone_horizontale_display($legende, generer_url_ecrire("mots_edit", $args), "mot-cle-24.gif", "creer.gif", false);
333 }
334
335 // http://doc.spip.org/@menu_mots
336 function menu_mots($row, $id_groupes_vus, $cond_mots_vus)
337 {
338 $id_groupe = $row['id_groupe'];
339
340 // nombre de mots dans le groupe ?
341 $n = sql_countsel("spip_mots", "id_groupe=$id_groupe" . $cond_mots_vus);
342 if (!$n) return '';
343
344 // mot seul, si deja present on se casse
345 $unseul = ($row['unseul'] == 'oui');
346 if ($unseul
347 AND in_array($id_groupe, $id_groupes_vus))
348 return '';
349
350 $titre = textebrut(typo($row['titre']));
351 $obligatoire = ($row['obligatoire']=='oui'
352 AND !in_array($id_groupe, $id_groupes_vus));
353
354 // forcer le recalcul du noeud car on est en Ajax
355 $rand = rand(0,10000); # pour antifocus & ajax
356 $ancre = "valider_groupe_$id_groupe";
357 $jscript1 = "findObj_forcer('$ancre').style.visibility='visible';";
358 $jscript2 = "if(!antifocus_mots['$rand-$id_groupe']){this.value='';antifocus_mots['$rand-$id_groupe']=true;}";
359
360 if (!defined('_MAX_MOTS_LISTE')) define('_MAX_MOTS_LISTE', '50');
361 if ($n > _MAX_MOTS_LISTE) {
362 $jscript = "onfocus=\"$jscript1 $jscript2\"";
363
364 if ($obligatoire)
365 $res = "<input type='text' name='cherche_mot' id='cherche_mot$id_groupe' style='width: 180px; background-color:#E86519;' value=\"".entites_html($titre)."\" size='20' $jscript />";
366 else if ($unseul) {
367 $res = "<input type='text' name='cherche_mot' id='cherche_mot$id_groupe' style='width: 180px; background-color:#cccccc;' value=\"".entites_html($titre)."\" size='20' $jscript />";
368 } else
369 $res = "<input type='text' name='cherche_mot' id='cherche_mot$id_groupe' style='width: 180px; ' value=\"".entites_html($titre)."\" size='20' $jscript />";
370
371 $res .= "<input type='hidden' name='select_groupe' value='$id_groupe' />&nbsp;";
372 return array($res, _T('bouton_chercher'));
373 } else {
374 if ($obligatoire)
375 $style = 'width: 180px; background-color:#E86519;';
376 else if ($unseul)
377 $style = 'width: 180px; background-color:#cccccc;';
378 else
379 $style = 'width: 180px;';
380
381 $q = sql_allfetsel("id_mot, type, titre", "spip_mots", "id_groupe =$id_groupe " . $cond_mots_vus, "", "titre");
382
383 foreach($q as $k => $r) {
384 $q[$k] = "<option value='" .$r['id_mot'] .
385 "'>&nbsp;&nbsp;&nbsp;" .
386 textebrut(typo($r['titre'])) .
387 "</option>";
388 }
389 $res = "<select name='nouv_mot' id='nouv_mot$id_groupe' size='1' style='$style' onchange=\"$jscript1\">"
390 . "\n<option value='x' style='font-variant: small-caps;'>"
391 . $titre
392 . "</option>\n"
393 . join("\n", $q)
394 . "</select>&nbsp;";
395
396 return array($res, _T('bouton_choisir'));
397 }
398 }
399
400 ?>