1 | <?php |
---|
2 | /** |
---|
3 | * Plugin Agenda 4 pour Spip 3.0 |
---|
4 | * Licence GPL 3 |
---|
5 | * |
---|
6 | * 2006-2011 |
---|
7 | * Auteurs : cf paquet.xml |
---|
8 | */ |
---|
9 | |
---|
10 | if (!defined("_ECRIRE_INC_VERSION")) return; |
---|
11 | |
---|
12 | /** |
---|
13 | * Edition d'un evenement |
---|
14 | * @param string $arg |
---|
15 | * @return array |
---|
16 | */ |
---|
17 | function action_editer_evenement_dist($arg=null){ |
---|
18 | |
---|
19 | if (is_null($arg)){ |
---|
20 | $securiser_action = charger_fonction('securiser_action', 'inc'); |
---|
21 | $arg = $securiser_action(); |
---|
22 | } |
---|
23 | |
---|
24 | // si id_evenement n'est pas un nombre, c'est une creation |
---|
25 | // mais on verifie qu'on a toutes les donnees qu'il faut. |
---|
26 | if (!$id_evenement = intval($arg)) { |
---|
27 | $id_parent = _request('id_parent'); |
---|
28 | if (!$id_evenement = agenda_action_insert_evenement($id_parent)) |
---|
29 | return array(false,_L('echec')); |
---|
30 | } |
---|
31 | |
---|
32 | $err = action_evenement_set($id_evenement); |
---|
33 | return array($id_evenement,$err); |
---|
34 | } |
---|
35 | |
---|
36 | /** |
---|
37 | * Creer un nouvel evenement |
---|
38 | * |
---|
39 | * @param int $id_article |
---|
40 | * @param int $id_evenement_source |
---|
41 | * @return int |
---|
42 | */ |
---|
43 | function evenement_inserer($id_article,$id_evenement_source = 0){ |
---|
44 | include_spip('inc/autoriser'); |
---|
45 | if (!autoriser('creerevenementdans','article',$id_article)){ |
---|
46 | spip_log("agenda action formulaire article : auteur ".$GLOBALS['visiteur_session']['id_auteur']." n'a pas le droit de creer un evenement dans article $id_article",'agenda'); |
---|
47 | return false; |
---|
48 | } |
---|
49 | |
---|
50 | $champs = array( |
---|
51 | "id_evenement_source"=>intval($id_evenement_source), |
---|
52 | 'id_article'=>intval($id_article), |
---|
53 | 'statut' => 'prop', |
---|
54 | ); |
---|
55 | |
---|
56 | // Envoyer aux plugins |
---|
57 | $champs = pipeline('pre_insertion', |
---|
58 | array( |
---|
59 | 'args' => array( |
---|
60 | 'table' => 'spip_evenements', |
---|
61 | ), |
---|
62 | 'data' => $champs |
---|
63 | ) |
---|
64 | ); |
---|
65 | |
---|
66 | // nouvel evenement |
---|
67 | $id_evenement = sql_insertq("spip_evenements", $champs); |
---|
68 | |
---|
69 | pipeline('post_insertion', |
---|
70 | array( |
---|
71 | 'args' => array( |
---|
72 | 'table' => 'spip_evenements', |
---|
73 | 'id_objet' => $id_evenement |
---|
74 | ), |
---|
75 | 'data' => $champs |
---|
76 | ) |
---|
77 | ); |
---|
78 | |
---|
79 | if (!$id_evenement){ |
---|
80 | spip_log("agenda action formulaire evenement : impossible d'ajouter un evenement",'agenda'); |
---|
81 | return false; |
---|
82 | } |
---|
83 | return $id_evenement; |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Modifier un evenement existant |
---|
88 | * |
---|
89 | * @param int $id_evenement |
---|
90 | * @param array $set |
---|
91 | * @return bool|string |
---|
92 | */ |
---|
93 | function evenement_modifier($id_evenement, $set=null){ |
---|
94 | |
---|
95 | include_spip('inc/modifier'); |
---|
96 | include_spip('inc/filtres'); |
---|
97 | $c = collecter_requests( |
---|
98 | // white list |
---|
99 | objet_info('evenement','champs_editables'), |
---|
100 | // black list |
---|
101 | array('statut','id_article'), |
---|
102 | // donnees eventuellement fournies |
---|
103 | $set |
---|
104 | ); |
---|
105 | |
---|
106 | // Si l'evenement est publie, invalider les caches et demander sa reindexation |
---|
107 | $t = sql_getfetsel("statut", "spip_evenements", "id_evenement=".intval($id_evenement)); |
---|
108 | $invalideur = $indexation = false; |
---|
109 | if ($t == 'publie') { |
---|
110 | $invalideur = "id='evenement/$id_evenement'"; |
---|
111 | $indexation = true; |
---|
112 | } |
---|
113 | |
---|
114 | if ($err = objet_modifier_champs('evenement', $id_evenement, |
---|
115 | array( |
---|
116 | 'nonvide' => array('titre' => _T('info_nouvel_evenement')." "._T('info_numero_abbreviation').$id_evenement), |
---|
117 | 'invalideur' => $invalideur, |
---|
118 | 'indexation' => $indexation, |
---|
119 | ), |
---|
120 | $c)) |
---|
121 | return $err; |
---|
122 | |
---|
123 | if (!is_null($repetitions = _request('repetitions', $set))) |
---|
124 | agenda_action_revision_evenement_repetitions($id_evenement, $repetitions); |
---|
125 | |
---|
126 | // Modification de statut, changement de parent ? |
---|
127 | $c = collecter_requests(array('statut', 'id_parent'), array(), $set); |
---|
128 | $err = evenement_instituer($id_evenement, $c); |
---|
129 | |
---|
130 | return $err; |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | function agenda_action_revision_evenement_repetitions($id_evenement, $repetitions=""){ |
---|
135 | include_spip('inc/filtres'); |
---|
136 | $repetitions = preg_split(",[^0-9\-\/],",$repetitions); |
---|
137 | // gestion des repetitions |
---|
138 | $rep = array(); |
---|
139 | foreach($repetitions as $date){ |
---|
140 | if (strlen($date)){ |
---|
141 | $date = recup_date($date); |
---|
142 | if ($date=mktime(0,0,0,$date[1],$date[2],$date[0])) |
---|
143 | $rep[] = $date; |
---|
144 | } |
---|
145 | } |
---|
146 | agenda_action_update_repetitions($id_evenement, $rep); |
---|
147 | } |
---|
148 | |
---|
149 | function agenda_action_update_repetitions($id_evenement, $repetitions){ |
---|
150 | // evenement source |
---|
151 | if ($row = sql_fetsel("*", "spip_evenements","id_evenement=".intval($id_evenement))){ |
---|
152 | // Si ce n'est pas un événement source, on n'a rien à faire ici |
---|
153 | if ($row['id_evenement_source'] != 0){ return; } |
---|
154 | |
---|
155 | // On ne garde que les données correctes pour une modification |
---|
156 | $c = collecter_requests( |
---|
157 | // white list |
---|
158 | objet_info('evenement','champs_editables'), |
---|
159 | // black list |
---|
160 | array('id_evenement', 'id_evenement_source'), |
---|
161 | // donnees fournies |
---|
162 | $row |
---|
163 | ); |
---|
164 | |
---|
165 | // Savoir si la source était publiée ou pas |
---|
166 | $publie = ($row['statut'] == 'publie'); |
---|
167 | |
---|
168 | // On calcule la durée en secondes de l'événement source pour la reporter telle quelle aux répétitions |
---|
169 | $date_debut = strtotime($row['date_debut']); |
---|
170 | $date_fin = strtotime($row['date_fin']); |
---|
171 | $duree = $date_fin - $date_debut; |
---|
172 | |
---|
173 | $repetitions_updated = array(); |
---|
174 | // mettre a jour toutes les repetitions deja existantes ou les supprimer si plus lieu |
---|
175 | $res = sql_select("id_evenement,date_debut","spip_evenements","id_evenement_source=".intval($id_evenement)); |
---|
176 | while ($row = sql_fetch($res)){ |
---|
177 | $date = strtotime(date('Y-m-d', strtotime($row['date_debut']))); |
---|
178 | if (in_array($date, $repetitions)){ |
---|
179 | // Cette répétition existe déjà, on la met à jour |
---|
180 | $repetitions_updated[] = $date; |
---|
181 | |
---|
182 | // On calcule les nouvelles dates/heures en reportant la durée de la source |
---|
183 | $update_date_debut = date('Y-m-d', $date).' '.date('H:i:s', $date_debut); |
---|
184 | $update_date_fin = date('Y-m-d H:i:s', strtotime($update_date_debut)+$duree); |
---|
185 | |
---|
186 | // Seules les dates sont changées dans les champs de la source |
---|
187 | // TODO : prendre en charge la mise a jour uniquement si conforme a l'original |
---|
188 | $c['date_debut'] = $update_date_debut; |
---|
189 | $c['date_fin'] = $update_date_fin; |
---|
190 | |
---|
191 | // mettre a jour l'evenement |
---|
192 | sql_updateq( |
---|
193 | 'spip_evenements', |
---|
194 | $c, |
---|
195 | 'id_evenement = '.$row['id_evenement'] |
---|
196 | ); |
---|
197 | } |
---|
198 | else { |
---|
199 | // il est supprime |
---|
200 | sql_delete("spip_evenements","id_evenement=".$row['id_evenement']); |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | // regarder les repetitions a ajouter |
---|
205 | foreach($repetitions as $date){ |
---|
206 | if (!in_array($date, $repetitions_updated)){ |
---|
207 | // On calcule les dates/heures en reportant la durée de la source |
---|
208 | $update_date_debut = date('Y-m-d', $date)." ".date('H:i:s', $date_debut); |
---|
209 | $update_date_fin = date('Y-m-d H:i:s', strtotime($update_date_debut)+$duree); |
---|
210 | |
---|
211 | // Seules les dates sont changées dans les champs de la source |
---|
212 | $c['date_debut'] = $update_date_debut; |
---|
213 | $c['date_fin'] = $update_date_fin; |
---|
214 | |
---|
215 | // On crée la nouvelle répétition |
---|
216 | if ($id_evenement_new = agenda_action_insert_evenement($c['id_article'], $id_evenement)) { |
---|
217 | // Si c'est bon, on ajoute tous les champs |
---|
218 | sql_updateq( |
---|
219 | 'spip_evenements', |
---|
220 | $c, |
---|
221 | 'id_evenement = '.$id_evenement_new |
---|
222 | ); |
---|
223 | |
---|
224 | // Pour les créations il ne faut pas oublier de dupliquer les liens |
---|
225 | // En effet, sinon les documents insérés avant la création (0-id_auteur) ne seront pas dupliqués |
---|
226 | include_spip('action/editer_liens'); |
---|
227 | objet_dupliquer_liens('evenement', $id_evenement, $id_evenement_new); |
---|
228 | } |
---|
229 | } |
---|
230 | } |
---|
231 | } |
---|
232 | } |
---|
233 | |
---|
234 | /** |
---|
235 | * Instituer un evenement |
---|
236 | * |
---|
237 | * @param int $id_evenement |
---|
238 | * @param array $c |
---|
239 | * @return bool|string |
---|
240 | */ |
---|
241 | function evenement_instituer($id_evenement, $c) { |
---|
242 | |
---|
243 | include_spip('inc/autoriser'); |
---|
244 | include_spip('inc/modifier'); |
---|
245 | |
---|
246 | $row = sql_fetsel("id_article, statut", "spip_evenements", "id_evenement=".intval($id_evenement)); |
---|
247 | $id_parent = $id_parent_ancien = $row['id_article']; |
---|
248 | $statut = $statut_ancien = $row['statut']; |
---|
249 | |
---|
250 | $champs = array(); |
---|
251 | |
---|
252 | if (!autoriser('modifier', 'article', $id_parent) |
---|
253 | OR (isset($c['id_parent']) AND !autoriser('modifier', 'article', $c['id_parent']))){ |
---|
254 | spip_log("editer_evenement $id_evenement refus " . join(' ', $c)); |
---|
255 | return false; |
---|
256 | } |
---|
257 | |
---|
258 | // Verifier que l'article demande existe et est different |
---|
259 | // de l'article actuel |
---|
260 | if ($c['id_parent'] |
---|
261 | AND $c['id_parent'] != $id_parent |
---|
262 | AND (sql_countsel("spip_articles", "id_article=".intval($c['id_parent'])))) { |
---|
263 | $id_parent = $champs['id_article'] = $c['id_parent']; |
---|
264 | } |
---|
265 | |
---|
266 | $sa = sql_getfetsel('statut','spip_articles','id_article='.intval($id_parent)); |
---|
267 | if ($id_parent |
---|
268 | AND ( |
---|
269 | $id_parent!==$id_parent_ancien OR $statut=='0' |
---|
270 | )){ |
---|
271 | switch($sa){ |
---|
272 | case 'publie': |
---|
273 | // statut par defaut si besoin |
---|
274 | if ($statut=='0') |
---|
275 | $champs['statut'] = $statut = 'publie'; |
---|
276 | break; |
---|
277 | case 'poubelle': |
---|
278 | // si article a la poubelle, evenement aussi |
---|
279 | $champs['statut'] = $statut = 'poubelle'; |
---|
280 | break; |
---|
281 | default: |
---|
282 | // pas de publie ni 0 si article pas publie |
---|
283 | if (in_array($statut,array('publie','0'))) |
---|
284 | $champs['statut'] = $statut = 'prop'; |
---|
285 | break; |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | // si pas d'article lie, et statut par defaut |
---|
290 | // on met en propose |
---|
291 | if ($statut=='0') |
---|
292 | $champs['statut'] = $statut = 'prop'; |
---|
293 | |
---|
294 | if (isset($c['statut']) |
---|
295 | AND $s = $c['statut'] |
---|
296 | AND $s != $statut) { |
---|
297 | // pour instituer un evenement il faut avoir le droit d'instituer l'article associe avec le meme statut |
---|
298 | if (autoriser('instituer', 'article', $id_parent, null, array('statut'=>$s)) |
---|
299 | AND ($sa=='publie' OR $s!=='publie')) |
---|
300 | $champs['statut'] = $statut = $s; |
---|
301 | else |
---|
302 | spip_log("editer_evenement $id_evenement refus " . join(' ', $c)); |
---|
303 | } |
---|
304 | |
---|
305 | // Envoyer aux plugins |
---|
306 | $champs = pipeline('pre_edition', |
---|
307 | array( |
---|
308 | 'args' => array( |
---|
309 | 'table' => 'spip_evenements', |
---|
310 | 'action'=>'instituer', |
---|
311 | 'id_objet' => $id_evenement, |
---|
312 | 'id_parent_ancien' => $id_parent_ancien, |
---|
313 | 'statut_ancien' => $statut_ancien, |
---|
314 | ), |
---|
315 | 'data' => $champs |
---|
316 | ) |
---|
317 | ); |
---|
318 | |
---|
319 | if (!count($champs)) return; |
---|
320 | |
---|
321 | // Envoyer les modifs sur l'evenement et toutes ses repetitons |
---|
322 | $ids = array_map('reset', sql_allfetsel('id_evenement', 'spip_evenements', 'id_evenement_source='.intval($id_evenement))); |
---|
323 | $ids[] = intval($id_evenement); |
---|
324 | sql_updateq('spip_evenements', $champs, sql_in('id_evenement', $ids)); |
---|
325 | |
---|
326 | // Invalider les caches |
---|
327 | include_spip('inc/invalideur'); |
---|
328 | suivre_invalideur("id='id_article/$id_parent_ancien'"); |
---|
329 | suivre_invalideur("id='id_article/$id_parent'"); |
---|
330 | |
---|
331 | // Pipeline |
---|
332 | pipeline('post_edition', |
---|
333 | array( |
---|
334 | 'args' => array( |
---|
335 | 'table' => 'spip_evenements', |
---|
336 | 'action'=>'instituer', |
---|
337 | 'id_objet' => $id_evenement, |
---|
338 | 'id_parent_ancien' => $id_parent_ancien, |
---|
339 | 'statut_ancien' => $statut_ancien, |
---|
340 | ), |
---|
341 | 'data' => $champs |
---|
342 | ) |
---|
343 | ); |
---|
344 | |
---|
345 | // Notifications |
---|
346 | if ($notifications = charger_fonction('notifications', 'inc')) { |
---|
347 | $notifications('instituerevenement', $id_evenement, |
---|
348 | array('id_parent' => $id_parent, 'statut' => $statut, 'id_parent_ancien' => $id_parent, 'statut_ancien' => $statut_ancien) |
---|
349 | ); |
---|
350 | } |
---|
351 | |
---|
352 | return ''; // pas d'erreur |
---|
353 | } |
---|
354 | |
---|
355 | /* |
---|
356 | function agenda_action_supprime_repetitions($supp_evenement){ |
---|
357 | $res = sql_select("id_evenement", "spip_evenements", "id_evenement_source=".intval($supp_evenement)); |
---|
358 | while ($row = sql_fetch($res)){ |
---|
359 | $id_evenement = $row['id_evenement']; |
---|
360 | sql_delete("spip_evenements", "id_evenement=".intval($id_evenement)); |
---|
361 | } |
---|
362 | } |
---|
363 | */ |
---|
364 | /* |
---|
365 | function agenda_action_supprime_evenement($id_article,$supp_evenement){ |
---|
366 | $id_evenement = sql_getfetsel("id_evenement", "spip_evenements", array( |
---|
367 | "id_article=" . intval($id_article), |
---|
368 | "id_evenement=" . intval($supp_evenement))); |
---|
369 | if (intval($id_evenement) AND $id_evenement == $supp_evenement){ |
---|
370 | sql_delete("spip_evenements", "id_evenement=".intval($id_evenement)); |
---|
371 | agenda_action_supprime_repetitions($id_evenement); |
---|
372 | } |
---|
373 | include_spip('inc/invalideur'); |
---|
374 | suivre_invalideur("article/$id_article"); |
---|
375 | $id_evenement = 0; |
---|
376 | return $id_evenement; |
---|
377 | }*/ |
---|
378 | |
---|
379 | |
---|
380 | function agenda_action_insert_evenement($id_article,$id_evenement_source = 0){return evenement_inserer($id_article,$id_evenement_source);} |
---|
381 | function action_evenement_set($id_evenement, $set=null){return evenement_modifier($id_evenement, $set);} |
---|
382 | function agenda_action_instituer_evenement($id_evenement, $c) {return evenement_instituer($id_evenement,$c);} |
---|
383 | ?> |
---|