[SPIP] +2.1.12
[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-2011 *
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 $ok &= autoriser('editermots', $objet, $id_objet, null,
256 array('id_groupe'=>$id_groupe,'groupe_champs'=>$row));
257 if ($ok)
258 $res .= "<div style='text-align: right' class='arial1'>"
259 . 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")
260 . "</div><br />\n";
261 }
262
263 // formulaire groupe par groupe
264 $ajouter ='';
265 $cond_mots_vus = $les_mots
266 ? " AND " . sql_in('id_mot', $les_mots, 'NOT')
267 : '';
268
269 define('_TRI_GROUPES_MOTS', 'titre');
270 foreach(sql_allfetsel('*,' . sql_multi ("titre", $spip_lang), 'spip_groupes_mots', '', '', _TRI_GROUPES_MOTS) as $row) {
271 $id_groupe = $row['id_groupe'];
272 if (autoriser('editermots', $objet, $id_objet, null,
273 array('id_groupe'=>$id_groupe,'groupe_champs'=>$row))
274 AND $menu = menu_mots($row, $id_groupes_vus, $cond_mots_vus)) {
275 list($corps, $clic) = $menu;
276 $ajouter .= ajax_action_post('editer_mots',
277 "$id_objet,,$table,$table_id,$objet",
278 $url_base,
279 "$table_id=$id_objet",
280 $corps,
281 $clic,
282 " class='visible_au_chargement spip_xx-small' id='valider_groupe_$id_groupe'", "",
283 "&id_objet=$id_objet&objet=$objet&select_groupe=$id_groupe");
284
285 // forcer la visibilite si au moins un mot obligatoire absent
286 // attention true <> 1 pour bouton_block_depliable
287 if ($row['obligatoire'] == 'oui'
288 AND !in_array($id_groupe, $id_groupes_vus))
289 $visible = true;
290 }
291 }
292
293 if ($ajouter) {
294 $res .= "<div style='float:$spip_lang_right; width:280px;position:relative;display:inline;'>"
295 . $ajouter
296 ."</div>\n"
297 . "<span class='verdana1'><b>"
298 ._T('titre_ajouter_mot_cle')
299 ."</b></span><br />\n";
300 }
301
302 return array($visible, $res);
303 }
304
305 function creer_mot($table, $id_objet, $table_id, $url_base, $mot='', $id_groupe=0)
306 {
307 static $titres = array(
308 'articles'=>'icone_creer_mot_cle',
309 'breves'=>'icone_creer_mot_cle_breve',
310 'rubriques'=>'icone_creer_mot_cle_rubrique',
311 'sites'=>'icone_creer_mot_cle_site'
312 );
313
314 if (!($id_groupe ?
315 autoriser('modifier','groupemots', $id_groupe) :
316 autoriser('modifier','groupemots'))
317 )
318 return '';
319
320 $legende = isset($titres[$table])
321 ? _T($titres[$table])
322 : _T('icone_creer_mot_cle');
323
324 $args = "new=oui&ajouter_id_article=$id_objet&table=$table&table_id=$table_id"
325 . (!$mot ? '' : ("&titre=".rawurlencode($mot)))
326 . (!$id_groupe ? '' : ("&id_groupe=".intval($id_groupe)))
327 . "&redirect=" . generer_url_retour($url_base, "$table_id=$id_objet");
328
329 return icone_horizontale_display($legende, generer_url_ecrire("mots_edit", $args), "mot-cle-24.gif", "creer.gif", false);
330 }
331
332 // http://doc.spip.org/@menu_mots
333 function menu_mots($row, $id_groupes_vus, $cond_mots_vus)
334 {
335 $id_groupe = $row['id_groupe'];
336
337 // nombre de mots dans le groupe ?
338 $n = sql_countsel("spip_mots", "id_groupe=$id_groupe" . $cond_mots_vus);
339 if (!$n) return '';
340
341 // mot seul, si deja present on se casse
342 $unseul = ($row['unseul'] == 'oui');
343 if ($unseul
344 AND in_array($id_groupe, $id_groupes_vus))
345 return '';
346
347 $titre = textebrut(typo($row['titre']));
348 $obligatoire = ($row['obligatoire']=='oui'
349 AND !in_array($id_groupe, $id_groupes_vus));
350
351 // forcer le recalcul du noeud car on est en Ajax
352 $rand = rand(0,10000); # pour antifocus & ajax
353 $ancre = "valider_groupe_$id_groupe";
354 $jscript1 = "findObj_forcer('$ancre').style.visibility='visible';";
355 $jscript2 = "if(!antifocus_mots['$rand-$id_groupe']){this.value='';antifocus_mots['$rand-$id_groupe']=true;}";
356
357 if (!defined('_MAX_MOTS_LISTE')) define('_MAX_MOTS_LISTE', '50');
358 if ($n > _MAX_MOTS_LISTE) {
359 $jscript = "onfocus=\"$jscript1 $jscript2\"";
360
361 if ($obligatoire)
362 $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 />";
363 else if ($unseul) {
364 $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 />";
365 } else
366 $res = "<input type='text' name='cherche_mot' id='cherche_mot$id_groupe' style='width: 180px; ' value=\"".entites_html($titre)."\" size='20' $jscript />";
367
368 $res .= "<input type='hidden' name='select_groupe' value='$id_groupe' />&nbsp;";
369 return array($res, _T('bouton_chercher'));
370 } else {
371 if ($obligatoire)
372 $style = 'width: 180px; background-color:#E86519;';
373 else if ($unseul)
374 $style = 'width: 180px; background-color:#cccccc;';
375 else
376 $style = 'width: 180px;';
377
378 $q = sql_allfetsel("id_mot, type, titre", "spip_mots", "id_groupe =$id_groupe " . $cond_mots_vus, "", "titre");
379
380 foreach($q as $k => $r) {
381 $q[$k] = "<option value='" .$r['id_mot'] .
382 "'>&nbsp;&nbsp;&nbsp;" .
383 textebrut(typo($r['titre'])) .
384 "</option>";
385 }
386 $res = "<select name='nouv_mot' id='nouv_mot$id_groupe' size='1' style='$style' onchange=\"$jscript1\">"
387 . "\n<option value='x' style='font-variant: small-caps;'>"
388 . $titre
389 . "</option>\n"
390 . join("\n", $q)
391 . "</select>&nbsp;";
392
393 return array($res, _T('bouton_choisir'));
394 }
395 }
396
397 ?>