[SPIP] ~v3.0.20-->v3.0.25
[lhc/web/clavette_www.git] / www / plugins-dist / mots / action / editer_mot.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 if (!defined("_ECRIRE_INC_VERSION")) return;
14
15 include_spip('inc/filtres');
16
17 // Editer (modification) d'un mot-cle
18 // http://code.spip.net/@action_editer_mot_dist
19 function action_editer_mot_dist($arg=null)
20 {
21 if (is_null($arg)){
22 $securiser_action = charger_fonction('securiser_action', 'inc');
23 $arg = $securiser_action();
24 }
25 $id_mot = intval($arg);
26
27 $id_groupe = intval(_request('id_groupe'));
28 if (!$id_mot AND $id_groupe) {
29 $id_mot = mot_inserer($id_groupe);
30 }
31
32 // Enregistre l'envoi dans la BD
33 if ($id_mot > 0) $err = mot_modifier($id_mot);
34
35 return array($id_mot,$err);
36 }
37
38 /**
39 * Insertion d'un mot dans un groupe
40 * @param int $id_groupe
41 * @return int
42 */
43 function mot_inserer($id_groupe) {
44
45 $champs = array();
46 $row = sql_fetsel("titre", "spip_groupes_mots", "id_groupe=".intval($id_groupe));
47 if ($row) {
48 $champs['id_groupe'] = $id_groupe;
49 $champs['type'] = $row['titre'];
50 }
51 else
52 return false;
53
54
55 // Envoyer aux plugins
56 $champs = pipeline('pre_insertion',
57 array(
58 'args' => array(
59 'table' => 'spip_mots',
60 ),
61 'data' => $champs
62 )
63 );
64
65 $id_mot = sql_insertq("spip_mots", $champs);
66
67 pipeline('post_insertion',
68 array(
69 'args' => array(
70 'table' => 'spip_mots',
71 'id_objet' => $id_mot
72 ),
73 'data' => $champs
74 )
75 );
76
77 return $id_mot;
78 }
79
80 /**
81 * Modifier un mot
82 * @param int $id_mot
83 * @param array $set
84 * @return string
85 */
86 function mot_modifier($id_mot, $set=null) {
87 include_spip('inc/modifier');
88 $c = collecter_requests(
89 // white list
90 array(
91 'titre', 'descriptif', 'texte', 'id_groupe'
92 ),
93 // black list
94 array('id_groupe'),
95 // donnees eventuellement fournies
96 $set
97 );
98
99 if ($err = objet_modifier_champs('mot', $id_mot,
100 array(
101 'nonvide' => array('titre' => _T('info_sans_titre'))
102 ),
103 $c))
104 return $err;
105
106 $c = collecter_requests(array('id_groupe', 'type'),array(),$set);
107 $err = mot_instituer($id_mot, $c);
108 return $err;
109 }
110
111 /**
112 * Modifier le groupe parent d'un mot
113 * @param $id_mot
114 * @param $c
115 * @return void
116 */
117 function mot_instituer($id_mot, $c){
118 $champs = array();
119 // regler le groupe
120 if (isset($c['id_groupe']) OR isset($c['type'])) {
121 $row = sql_fetsel("titre", "spip_groupes_mots", "id_groupe=".intval($c['id_groupe']));
122 if ($row) {
123 $champs['id_groupe'] = $c['id_groupe'];
124 $champs['type'] = $row['titre'];
125 }
126 }
127
128 // Envoyer aux plugins
129 $champs = pipeline('pre_edition',
130 array(
131 'args' => array(
132 'table' => 'spip_mots',
133 'id_objet' => $id_mot,
134 'action'=>'instituer',
135 ),
136 'data' => $champs
137 )
138 );
139
140 if (!$champs) return;
141
142 sql_updateq('spip_mots', $champs, "id_mot=".intval($id_mot));
143
144 //
145 // Post-modifications
146 //
147
148 // Invalider les caches
149 include_spip('inc/invalideur');
150 suivre_invalideur("id='mot/$id_mot'");
151
152 // Pipeline
153 pipeline('post_edition',
154 array(
155 'args' => array(
156 'table' => 'spip_mots',
157 'id_objet' => $id_mot,
158 'action'=>'instituer',
159 ),
160 'data' => $champs
161 )
162 );
163
164 // Notifications
165 if ($notifications = charger_fonction('notifications', 'inc')) {
166 $notifications('instituermot', $id_mot,
167 array('id_groupe' => $champs['id_groupe'])
168 );
169 }
170
171 return ''; // pas d'erreur
172 }
173
174 /**
175 * Supprimer un mot
176 * @param int $id_mot
177 * @return void
178 */
179 function mot_supprimer($id_mot) {
180 sql_delete("spip_mots", "id_mot=".intval($id_mot));
181 mot_dissocier($id_mot, '*');
182 pipeline('trig_supprimer_objets_lies',
183 array(
184 array('type'=>'mot','id'=>$id_mot)
185 )
186 );
187 }
188
189
190
191 /**
192 * Associer un mot a des objets listes sous forme
193 * array($objet=>$id_objets,...)
194 * $id_objets peut lui meme etre un scalaire ou un tableau pour une liste d'objets du meme type
195 *
196 * on peut passer optionnellement une qualification du (des) lien(s) qui sera
197 * alors appliquee dans la foulee.
198 * En cas de lot de liens, c'est la meme qualification qui est appliquee a tous
199 *
200 * Exemples:
201 * mot_associer(3, array('auteur'=>2));
202 * mot_associer(3, array('auteur'=>2), array('vu'=>'oui)); // ne fonctionnera pas ici car pas de champ 'vu' sur spip_mots_liens
203 *
204 * @param int $id_mot
205 * @param array $objets
206 * @param array $qualif
207 * @return string
208 */
209 function mot_associer($id_mot,$objets, $qualif = null){
210
211 include_spip('action/editer_liens');
212
213 // si il s'agit d'un groupe avec 'unseul', alors supprimer d'abord les autres
214 // mots de ce groupe associe a ces objets
215 $id_groupe = sql_getfetsel('id_groupe','spip_mots','id_mot='.intval($id_mot));
216 if (un_seul_mot_dans_groupe($id_groupe)) {
217 $mots_groupe = sql_allfetsel("id_mot", "spip_mots", "id_groupe=".intval($id_groupe));
218 $mots_groupe = array_map('reset',$mots_groupe);
219 objet_dissocier(array('mot'=>$mots_groupe), $objets);
220 }
221
222 return objet_associer(array('mot'=>$id_mot), $objets, $qualif);
223 }
224
225
226
227 /**
228 * Dossocier un mot des objets listes sous forme
229 * array($objet=>$id_objets,...)
230 * $id_objets peut lui meme etre un scalaire ou un tableau pour une liste d'objets du meme type
231 *
232 * un * pour $id_mot,$objet,$id_objet permet de traiter par lot
233 *
234 * @param int $id_mot
235 * @param array $objets
236 * @return string
237 */
238 function mot_dissocier($id_mot,$objets){
239 include_spip('action/editer_liens');
240 return objet_dissocier(array('mot'=>$id_mot), $objets);
241 }
242
243 /**
244 * Qualifier le lien d'un mot avec les objets listes
245 * array($objet=>$id_objets,...)
246 * $id_objets peut lui meme etre un scalaire ou un tableau pour une liste d'objets du meme type
247 * exemple :
248 * $c = array('vu'=>'oui');
249 * un * pour $id_auteur,$objet,$id_objet permet de traiter par lot
250 *
251 * @param int $id_mot
252 * @param array $objets
253 * @param array $qualif
254 */
255 function mot_qualifier($id_mot,$objets,$qualif){
256 include_spip('action/editer_liens');
257 return objet_qualifier(array('mot'=>$id_mot), $objets, $qualif);
258 }
259
260
261
262 /**
263 * Renvoyer true si le groupe de mot ne doit etre associe qu'une fois aux objet
264 * (maximum un seul mot de ce groupe associe a chaque objet)
265 *
266 * @param int $id_groupe
267 * @return bool
268 */
269 function un_seul_mot_dans_groupe($id_groupe)
270 {
271 return sql_countsel('spip_groupes_mots', "id_groupe=$id_groupe AND unseul='oui'");
272 }
273
274
275
276 function insert_mot($id_groupe) {
277 return mot_inserer($id_groupe);
278 }
279 function mots_set($id_mot, $set=null) {
280 return mot_modifier($id_mot, $set);
281 }
282 function revision_mot($id_mot, $c=false) {
283 return mot_modifier($id_mot, $c);
284 }
285
286 ?>