Fix archive events.
[platal.git] / modules / xnetevents.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2014 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 define('NB_PER_PAGE', 25);
23
24 class XnetEventsModule extends PLModule
25 {
26 function handlers()
27 {
28 return array(
29 '%grp/events' => $this->make_hook('events', AUTH_PASSWD, 'groups'),
30 '%grp/events/sub' => $this->make_hook('sub', AUTH_PASSWD, 'groups'),
31 '%grp/events/csv' => $this->make_hook('csv', AUTH_PASSWD, 'groups', NO_HTTPS),
32 '%grp/events/ical' => $this->make_hook('ical', AUTH_PASSWD, 'groups', NO_HTTPS),
33 '%grp/events/edit' => $this->make_hook('edit', AUTH_PASSWD, 'groupadmin'),
34 '%grp/events/admin' => $this->make_hook('admin', AUTH_PASSWD, 'groupmember'),
35 );
36 }
37
38 function handler_events($page, $archive = null)
39 {
40 global $globals;
41
42 $page->changeTpl('xnetevents/index.tpl');
43 $this->load('xnetevents.inc.php');
44
45 $action = null;
46 $archive = ($archive == 'archive' && may_update());
47
48 if (Post::has('del')) {
49 $action = 'del';
50 $eid = Post::v('del');
51 } elseif (Post::has('archive')) {
52 $action = 'archive';
53 $eid = Post::v('archive');
54 } elseif (Post::has('unarchive')) {
55 $action = 'unarchive';
56 $eid = Post::v('unarchive');
57 }
58
59 if (!is_null($action)) {
60 if (!may_update()) {
61 return PL_FORBIDDEN;
62 }
63 S::assert_xsrf_token();
64
65 $res = XDB::query("SELECT asso_id, short_name FROM group_events
66 WHERE eid = {?} AND asso_id = {?}",
67 $eid, $globals->asso('id'));
68
69 $tmp = $res->fetchOneRow();
70 if (!$tmp) {
71 return PL_FORBIDDEN;
72 }
73 }
74
75 if ($action == 'del') {
76 // deletes the event mailing aliases
77 if ($tmp[1]) {
78 require_once 'emails.inc.php';
79 foreach (explode(',', $globals->xnet->event_lists) as $suffix) {
80 delete_list_alias($tmp[1] . $suffix, $globals->xnet->evts_domain, 'event');
81 }
82 }
83
84 // archive le paiement associé si il existe
85 $pay_id = XDB::fetchOneCell("SELECT paiement_id
86 FROM group_events
87 WHERE eid = {?} AND asso_id = {?}",
88 $eid, $globals->asso('id'));
89 if (!$pay_id=='') {
90 XDB::execute("UPDATE payments
91 SET flags = 'old'
92 WHERE id = {?}",
93 $pay_id);
94 }
95 // deletes the event items
96 XDB::execute('DELETE FROM group_event_items
97 WHERE eid = {?}', $eid);
98
99 // deletes the event participants
100 XDB::execute('DELETE FROM group_event_participants
101 WHERE eid = {?}', $eid);
102
103 // deletes the event
104 XDB::execute('DELETE FROM group_events
105 WHERE eid = {?} AND asso_id = {?}',
106 $eid, $globals->asso('id'));
107
108 // delete the requests for payments
109 XDB::execute("DELETE FROM requests
110 WHERE type = 'paiements' AND data LIKE {?}",
111 PayReq::same_event($eid, $globals->asso('id')));
112 $globals->updateNbValid();
113 }
114
115 if ($action == 'archive') {
116 $pay_id = XDB::fetchOneCell("SELECT paiement_id
117 FROM group_events
118 WHERE eid = {?} AND asso_id = {?}",
119 $eid, $globals->asso('id'));
120 if (!$pay_id=='') {
121 XDB::execute("UPDATE payments
122 SET flags = 'old'
123 WHERE id = {?}",
124 $pay_id);
125 }
126 XDB::execute("UPDATE group_events
127 SET archive = 1
128 WHERE eid = {?} AND asso_id = {?}",
129 $eid, $globals->asso('id'));
130 }
131
132 if ($action == 'unarchive') {
133 $pay_id = XDB::fetchOneCell("SELECT paiement_id FROM group_events
134 WHERE eid = {?} AND asso_id = {?}",
135 $eid, $globals->asso('id'));
136 if (!$pay_id=='') {
137 XDB::execute("UPDATE payments
138 SET flags = ''
139 WHERE id = {?}",
140 $pay_id);
141 }
142 XDB::execute("UPDATE group_events
143 SET archive = 0
144 WHERE eid = {?} AND asso_id = {?}",
145 $eid, $globals->asso('id'));
146 }
147
148 $page->assign('archive', $archive);
149
150 if (Post::has('order')) {
151 $order = Post::v('order');
152 XDB::execute("UPDATE groups
153 SET event_order = {?}
154 WHERE id = {?}",
155 $order, $globals->asso('id'));
156 }
157 $order = get_event_order($globals->asso('id'));
158 $evts = get_events($globals->asso('id'), $order, $archive);
159 $page->assign('order', $order);
160
161 $undisplayed_events = 0;
162 foreach ($evts as $eid => &$e) {
163 if (!is_member() && !may_update() && !$e['accept_nonmembre']) {
164 $undisplayed_events ++;
165 continue;
166 }
167
168 $e['show_participants'] = ($e['show_participants'] && (is_member() || may_update()));
169 $e['items'] = get_event_items($eid);
170 $e['topay'] = 0;
171 $e['paid'] = 0;
172 $sub = get_event_subscription($eid, S::i('uid'));
173 if (empty($sub)) {
174 $e['inscrit'] = false;
175 } else {
176 $e['inscrit'] = true;
177 foreach ($e['items'] as $item_id => $m) {
178 if (isset($sub[$item_id])) {
179 $e['topay'] += $sub[$item_id]['nb'] * $m['montant'];
180 $e['paid'] += $sub[$item_id]['paid'];
181 }
182 }
183 }
184 $e['sub'] = $sub;
185
186 $telepaid = get_event_telepaid($eid, S::i('uid'));
187 $e['paid'] += $telepaid;
188
189 $e['date'] = make_event_date($e['debut'], $e['fin']);
190 if ($e['deadline_inscription'] == null || strtotime($e['deadline_inscription']) >= time()) {
191 $e['inscr_open'] = true;
192 } else {
193 $e['inscr_open'] = false;
194 }
195
196 if (Env::has('updated') && $e['eid'] == Env::i('updated')) {
197 $page->assign('updated', $e);
198 }
199 }
200
201 $page->assign('evenements', $evts);
202 $page->assign('undisplayed_events', $undisplayed_events);
203 }
204
205 function handler_sub($page, $eid = null)
206 {
207 $this->load('xnetevents.inc.php');
208 $page->changeTpl('xnetevents/subscribe.tpl');
209
210 $evt = get_event($eid);
211 if (is_null($evt)) {
212 return PL_NOT_FOUND;
213 }
214
215 global $globals;
216
217 if (!$evt['inscr_open']) {
218 $page->kill('Les inscriptions pour cet événement sont closes');
219 }
220 if (!$evt['accept_nonmembre'] && !is_member() && !may_update()) {
221 $url = $globals->asso('sub_url');
222 if (empty($url)) {
223 $url = $platal->ns . 'subscribe';
224 }
225 $page->kill('Cet événement est reservé aux membres du groupe ' . $globals->asso('nom') .
226 '. Pour devenir membre, rends-toi sur la page de <a href="' . $url . '">demande d\'inscripton</a>.');
227 }
228
229 $res = XDB::query("SELECT stamp
230 FROM requests
231 WHERE type = 'paiements' AND data LIKE {?}",
232 PayReq::same_event($eid, $globals->asso('id')));
233 $page->assign('validation', $res->numRows());
234
235 $page->assign('eid', $eid);
236 $page->assign('event', $evt);
237
238 $items = get_event_items($eid);
239 $subs = get_event_subscription($eid, S::v('uid'));
240
241 if (Post::has('submit')) {
242 S::assert_xsrf_token();
243 $moments = Post::v('moment', array());
244 $pers = Post::v('personnes', array());
245 $old_subs = $subs;
246 $subs = array();
247
248 foreach ($moments as $j => $v) {
249 $subs[$j] = intval($v);
250
251 // retrieve other field when more than one person
252 if ($subs[$j] == 2) {
253 if (!isset($pers[$j]) || !is_numeric($pers[$j]) || $pers[$j] < 0) {
254 $page->trigError("Tu dois choisir un nombre d'invités correct&nbsp;!");
255 return;
256 }
257 $subs[$j] = $pers[$j];
258 }
259 }
260
261 // count what the user must pay, and what he manually paid
262 $manual_paid = 0;
263 foreach ($items as $item_id => $item) {
264 if (array_key_exists($item_id, $old_subs)) {
265 $manual_paid += $old_subs[$item_id]['paid'];
266 }
267 }
268 // impossible to unsubscribe if you already paid sthing
269 if (!array_sum($subs) && $manual_paid != 0) {
270 $page->trigError("Impossible de te désinscrire complètement " .
271 "parce que tu as fait un paiement par " .
272 "chèque ou par liquide. Contacte un " .
273 "administrateur du groupe si tu es sûr de " .
274 "ne pas venir.");
275 $updated = false;
276 } else {
277 // update actual inscriptions
278 $updated = subscribe(S::v('uid'), $eid, $subs);
279 }
280 if ($updated) {
281 $evt = get_event_detail($eid);
282 if ($evt['topay'] > 0) {
283 $page->trigSuccess('Ton inscription à l\'événement a été mise à jour avec succès, tu peux payer ta participation en cliquant ci-dessous');
284 } else {
285 $page->trigSuccess('Ton inscription à l\'événement a été mise à jour avec succès.');
286 }
287
288 if ($evt['subscription_notification'] != 'nobody') {
289 $mailer = new PlMailer('xnetevents/subscription-notif.mail.tpl');
290 if ($evt['subscription_notification'] != 'creator') {
291 $admins = $globals->asso()->iterAdmins();
292 while ($admin = $admins->next()) {
293 $mailer->addTo($admin);
294 }
295 }
296 if ($evt['subscription_notification'] != 'animator') {
297 $mailer->addTo($evt['organizer']);
298 }
299 $mailer->assign('group', $globals->asso('nom'));
300 $mailer->assign('event', $evt['intitule']);
301 $mailer->assign('subs', $subs);
302 $mailer->assign('moments', $evt['moments']);
303 $mailer->assign('name', S::user()->fullName('promo'));
304 $mailer->send();
305 }
306 }
307 }
308 $subs = get_event_subscription($eid, S::v('uid'));
309 // count what the user must pay
310 $topay = 0;
311 $manually_paid = 0;
312 foreach ($items as $item_id => $item) {
313 if (array_key_exists($item_id, $subs)) {
314 $topay += $item['montant']*$subs[$item_id]['nb'];
315 $manually_paid += $subs[$item_id]['paid'];
316 }
317 }
318 $paid = $manually_paid + get_event_telepaid($eid, S::v('uid'));
319 $page->assign('moments', $items);
320 $page->assign('subs', $subs);
321 $page->assign('topay', $topay);
322 $page->assign('paid', $paid);
323 }
324
325 function handler_csv($page, $eid = null, $item_id = null)
326 {
327 $this->load('xnetevents.inc.php');
328
329 if (!is_numeric($item_id)) {
330 $item_id = null;
331 }
332
333 $evt = get_event_detail($eid, $item_id);
334 if (!$evt) {
335 return PL_NOT_FOUND;
336 }
337
338 pl_cached_content_headers('text/x-csv', 'iso-8859-1', 1);
339 $page->changeTpl('xnetevents/csv.tpl', NO_SKIN);
340
341 $admin = may_update();
342 $tri = (Env::v('order') == 'alpha' ? UserFilter::sortByPromo() : UserFilter::sortByName());
343 $all = !Env::v('item_id', false);
344
345 $participants = get_event_participants($evt, $item_id, $tri);
346 $title = 'Nom;Prénom;Promotion;Email';
347 if ($admin) {
348 $title .=';Société;Poste';
349 }
350 if ($all) {
351 foreach ($evt['moments'] as $moment) {
352 $title .= ';' . $moment['titre'];
353 }
354 }
355 if ($admin && $evt['money']) {
356 $title .= ';À payer;';
357 if ($evt['paiement_id']) {
358 $title .= 'Télépaiement;Liquide/Chèque;';
359 }
360 $title .= 'Payé';
361 } else {
362 $title .= ';Nombre';
363 }
364 echo utf8_decode($title) . "\n";
365
366 if ($participants) {
367 foreach ($participants as $participant) {
368 $user = $participant['user'];
369 $line = $user->lastName() . ';' . $user->firstName() . ';' . $user->promo() . ';' . $user->bestEmail();
370 if ($admin && $user->hasProfile()) {
371 $line .= ';' . $user->profile()->getMainJob()->company->name . ';' . $user->profile()->getMainJob()->description;
372 } else {
373 $line .= ';;';
374 }
375 if ($all) {
376 foreach ($evt['moments'] as $moment) {
377 $line .= ';' . $participant[$moment['item_id']];
378 }
379 }
380 if ($admin && $evt['money']) {
381 $line .= ';' . $participant['montant'] . ';';
382 if ($evt['paiement_id']) {
383 $line .= $participant['telepayment'] . ';' . $participant['adminpaid'] . ';';
384 }
385 $line .= $participant['paid'];
386 } else {
387 $line .= ';' . $participant['nb'];
388 }
389
390 echo utf8_decode($line) . "\n";
391 }
392 }
393 exit();
394 }
395
396 function handler_ical($page, $eid = null)
397 {
398 global $globals;
399
400 $this->load('xnetevents.inc.php');
401 $evt = get_event_detail($eid);
402 if (!$evt) {
403 return PL_FORBIDDEN;
404 }
405 $evt['debut'] = preg_replace('/(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/', "\\1\\2\\3T\\4\\5\\6", $evt['debut']);
406 $evt['fin'] = preg_replace('/(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/', "\\1\\2\\3T\\4\\5\\6", $evt['fin']);
407
408 foreach ($evt['moments'] as $m) {
409 $evt['descriptif'] .= "\n\n** " . $m['titre'] . " **\n" . $m['details'];
410 }
411
412 $page->changeTpl('xnetevents/calendar.tpl', NO_SKIN);
413
414 require_once('ical.inc.php');
415 $page->assign('asso', $globals->asso());
416 $page->assign('timestamp', time());
417 $page->assign('admin', may_update());
418
419 if (may_update()) {
420 $page->assign('participants', get_event_participants($evt, null, UserFilter::sortByPromo()));
421 }
422 $page->register_function('display_ical', 'display_ical');
423 $page->assign_by_ref('e', $evt);
424
425 pl_content_headers("text/calendar");
426 }
427
428 function handler_edit($page, $eid = null)
429 {
430 global $globals;
431
432 // get eid if the the given one is a short name
433 if (!is_null($eid) && !is_numeric($eid)) {
434 $res = XDB::query("SELECT eid
435 FROM group_events
436 WHERE asso_id = {?} AND short_name = {?}",
437 $globals->asso('id'), $eid);
438 if ($res->numRows()) {
439 $eid = (int)$res->fetchOneCell();
440 }
441 }
442
443 // check the event is in our group
444 if (!is_null($eid)) {
445 $res = XDB::query("SELECT short_name
446 FROM group_events
447 WHERE eid = {?} AND asso_id = {?}",
448 $eid, $globals->asso('id'));
449 if ($res->numRows()) {
450 $infos = $res->fetchOneAssoc();
451 } else {
452 return PL_FORBIDDEN;
453 }
454 }
455
456 $page->changeTpl('xnetevents/edit.tpl');
457
458 $moments = range(1, 4);
459 $error = false;
460 $page->assign('moments', $moments);
461
462 if (Post::v('intitule')) {
463 S::assert_xsrf_token();
464
465 $this->load('xnetevents.inc.php');
466 $short_name = event_change_shortname($page, $eid,
467 $infos['short_name'],
468 Env::v('short_name', ''));
469 if ($short_name != Env::v('short_name')) {
470 $error = true;
471 }
472 $evt = array(
473 'eid' => $eid,
474 'asso_id' => $globals->asso('id'),
475 'paiement_id' => Post::v('paiement_id') > 0 ? Post::v('paiement_id') : null,
476 'debut' => Post::v('deb_Year').'-'.Post::v('deb_Month')
477 .'-'.Post::v('deb_Day').' '.Post::v('deb_Hour')
478 .':'.Post::v('deb_Minute').':00',
479 'fin' => Post::v('fin_Year').'-'.Post::v('fin_Month')
480 .'-'.Post::v('fin_Day').' '.Post::v('fin_Hour')
481 .':'.Post::v('fin_Minute').':00',
482 'short_name' => $short_name,
483 );
484
485 $trivial = array('intitule', 'descriptif', 'noinvite', 'subscription_notification',
486 'show_participants', 'accept_nonmembre', 'uid');
487 foreach ($trivial as $k) {
488 $evt[$k] = Post::v($k);
489 }
490 if (!$eid) {
491 $evt['uid'] = S::v('uid');
492 }
493
494 if (Post::v('deadline')) {
495 $evt['deadline_inscription'] = Post::v('inscr_Year').'-'
496 . Post::v('inscr_Month').'-'
497 . Post::v('inscr_Day');
498 } else {
499 $evt['deadline_inscription'] = null;
500 }
501
502 // Store the modifications in the database
503 XDB::execute('INSERT INTO group_events (eid, asso_id, uid, intitule, paiement_id,
504 descriptif, debut, fin, show_participants,
505 short_name, deadline_inscription, noinvite,
506 accept_nonmembre, subscription_notification)
507 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})
508 ON DUPLICATE KEY UPDATE asso_id = VALUES(asso_id), uid = VALUES(uid), intitule = VALUES(intitule),
509 paiement_id = VALUES(paiement_id), descriptif = VALUES(descriptif), debut = VALUES(debut),
510 fin = VALUES(fin), show_participants = VALUES(show_participants), short_name = VALUES(short_name),
511 deadline_inscription = VALUES(deadline_inscription), noinvite = VALUES(noinvite),
512 accept_nonmembre = VALUES(accept_nonmembre), subscription_notification = VALUES(subscription_notification)',
513 $evt['eid'], $evt['asso_id'], $evt['uid'],
514 $evt['intitule'], $evt['paiement_id'], $evt['descriptif'],
515 $evt['debut'], $evt['fin'], $evt['show_participants'],
516 $evt['short_name'], $evt['deadline_inscription'],
517 $evt['noinvite'], $evt['accept_nonmembre'], $evt['subscription_notification']);
518
519 // if new event, get its id
520 if (!$eid) {
521 $eid = XDB::insertId();
522 }
523
524 foreach ($moments as $i) {
525 if (Post::v('titre' . $i)) {
526 $nb_moments++;
527
528 $montant = strtr(Post::v('montant' . $i), ',', '.');
529 $money_defaut += (float)$montant;
530 XDB::execute('INSERT INTO group_event_items (eid, item_id, titre, details, montant)
531 VALUES ({?}, {?}, {?}, {?}, {?})
532 ON DUPLICATE KEY UPDATE titre = VALUES(titre), details = VALUES(details), montant = VALUES(montant)',
533 $eid, $i, Post::v('titre' . $i), Post::v('details' . $i), $montant);
534 } else {
535 XDB::execute('DELETE FROM group_event_items
536 WHERE eid = {?} AND item_id = {?}', $eid, $i);
537 }
538 }
539 // request for a new payment
540 if (Post::v('paiement_id') == -1 && $money_defaut >= 0) {
541 $p = new PayReq(S::user(),
542 $globals->asso('nom')." - ".Post::v('intitule'),
543 Post::v('site'), $money_defaut,
544 Post::v('confirmation'), 0, 999,
545 $globals->asso('id'), $eid, Post::v('payment_public') == 'yes');
546 if ($p->accept()) {
547 $p->submit();
548 } else {
549 $page->assign('payment_message', Post::v('confirmation'));
550 $page->assign('payment_site', Post::v('site'));
551 $page->assign('payment_public', Post::v('payment_public') == 'yes');
552 $page->assign('error', true);
553 $error = true;
554 }
555 }
556
557 // events with no sub-event: add a sub-event with default name
558 if ($nb_moments == 0) {
559 XDB::execute("INSERT INTO group_event_items
560 VALUES ({?}, {?}, 'Événement', '', 0)", $eid, 1);
561 }
562
563 if (!$error) {
564 pl_redirect('events');
565 }
566 }
567
568 // get a list of all the payment for this asso
569 $res = XDB::iterator("SELECT id, text
570 FROM payments
571 WHERE asso_id = {?} AND NOT FIND_IN_SET('old', flags)",
572 $globals->asso('id'));
573 $paiements = array();
574 while ($a = $res->next()) $paiements[$a['id']] = $a['text']; {
575 $page->assign('paiements', $paiements);
576 }
577
578 // when modifying an old event retreive the old datas
579 if ($eid) {
580 $res = XDB::query(
581 "SELECT eid, intitule, descriptif, debut, fin, uid,
582 show_participants, paiement_id, short_name,
583 deadline_inscription, noinvite, accept_nonmembre, subscription_notification
584 FROM group_events
585 WHERE eid = {?}", $eid);
586 $evt = $res->fetchOneAssoc();
587 // find out if there is already a request for a payment for this event
588 $res = XDB::query("SELECT stamp
589 FROM requests
590 WHERE type = 'paiements' AND data LIKE {?}",
591 PayReq::same_event($eid, $globals->asso('id')));
592 $stamp = $res->fetchOneCell();
593 if ($stamp) {
594 $evt['paiement_id'] = -2;
595 $evt['paiement_req'] = $stamp;
596 }
597 $page->assign('evt', $evt);
598 // get all the different moments infos
599 $res = XDB::iterator(
600 "SELECT item_id, titre, details, montant
601 FROM group_event_items AS ei
602 INNER JOIN group_events AS e ON(e.eid = ei.eid)
603 WHERE e.eid = {?}
604 ORDER BY item_id", $eid);
605 $items = array();
606 while ($item = $res->next()) {
607 $items[$item['item_id']] = $item;
608 }
609 $page->assign('items', $items);
610 }
611 $page->assign('url_ref', $eid);
612 }
613
614 function handler_admin($page, $eid = null, $item_id = null)
615 {
616 global $globals;
617
618 $this->load('xnetevents.inc.php');
619
620 $evt = get_event_detail($eid, $item_id);
621 if (!$evt) {
622 return PL_NOT_FOUND;
623 }
624
625 $page->changeTpl('xnetevents/admin.tpl');
626 if (!$evt['show_participants'] && !may_update()) {
627 return PL_FORBIDDEN;
628 }
629
630 if (may_update() && Post::v('adm')) {
631 S::assert_xsrf_token();
632
633 $member = User::getSilent(Post::v('mail'));
634 if (!$member) {
635 $page->trigError("Membre introuvable");
636 }
637
638 // change the price paid by a participant
639 if (Env::v('adm') == 'prix' && $member) {
640 $amount = strtr(Env::v('montant'), ',', '.');
641 XDB::execute("UPDATE group_event_participants
642 SET paid = paid + {?}
643 WHERE uid = {?} AND eid = {?} AND nb > 0
644 ORDER BY item_id ASC
645 LIMIT 1",
646 $amount, $member->uid, $evt['eid']);
647 subscribe_lists_event($member->uid, $evt['short_name'], 1, $amount);
648 }
649
650 // change the number of personns coming with a participant
651 if (Env::v('adm') == 'nbs' && $member) {
652 $res = XDB::query("SELECT SUM(paid)
653 FROM group_event_participants
654 WHERE uid = {?} AND eid = {?}",
655 $member->uid, $evt['eid']);
656
657 $paid = $res->fetchOneCell();
658
659 // Ensure we have an integer
660 if ($paid == null) {
661 $paid = 0;
662 }
663
664 $nbs = Post::v('nb', array());
665
666 $paid_inserted = false;
667 foreach ($nbs as $id => $nb) {
668 $nb = max(intval($nb), 0);
669 if (!$paid_inserted && $nb > 0) {
670 $item_paid = $paid;
671 $paid_inserted = true;
672 } else {
673 $item_paid = 0;
674 }
675 XDB::execute('INSERT INTO group_event_participants (eid, uid, item_id, nb, flags, paid)
676 VALUES ({?}, {?}, {?}, {?}, {?}, {?})
677 ON DUPLICATE KEY UPDATE nb = VALUES(nb), flags = VALUES(flags), paid = VALUES(paid)',
678 $evt['eid'], $member->uid, $id, $nb, '', $item_paid);
679 }
680
681 $res = XDB::query('SELECT COUNT(uid) AS cnt, SUM(nb) AS nb
682 FROM group_event_participants
683 WHERE uid = {?} AND eid = {?}
684 GROUP BY uid',
685 $member->uid, $evt['eid']);
686 $u = $res->fetchOneAssoc();
687 if ($paid == 0 && Post::v('cancel')) {
688 XDB::execute("DELETE FROM group_event_participants
689 WHERE uid = {?} AND eid = {?}",
690 $member->uid, $evt['eid']);
691 $u = 0;
692 subscribe_lists_event($member->uid, $evt['short_name'], -1, $paid);
693 } else {
694 $u = $u['cnt'] ? $u['nb'] : null;
695 subscribe_lists_event($member->uid, $evt['short_name'], ($u > 0 ? 1 : 0), $paid);
696 }
697 }
698
699 $evt = get_event_detail($eid, $item_id);
700 }
701
702 $page->assign_by_ref('evt', $evt);
703 $page->assign('tout', is_null($item_id));
704
705 if (count($evt['moments'])) {
706 $page->assign('moments', $evt['moments']);
707 }
708
709 if ($evt['paiement_id']) {
710 $infos = User::getBulkUsersWithUIDs(
711 XDB::fetchAllAssoc('SELECT t.uid, t.amount
712 FROM payment_transactions AS t
713 LEFT JOIN group_event_participants AS ep ON(ep.uid = t.uid AND ep.eid = {?})
714 WHERE t.ref = {?} AND ep.uid IS NULL',
715 $evt['eid'], $evt['paiement_id']),
716 'uid', 'user');
717 $page->assign('oublis', count($infos));
718 $page->assign('oubliinscription', $infos);
719 }
720
721 $absents = User::getBulkUsersFromDB('SELECT p.uid
722 FROM group_event_participants AS p
723 LEFT JOIN group_event_participants AS p2 ON (p2.uid = p.uid
724 AND p2.eid = p.eid
725 AND p2.nb != 0)
726 WHERE p.eid = {?} AND p2.eid IS NULL
727 GROUP BY p.uid', $evt['eid']);
728
729 $ofs = Env::i('offset');
730 $part = get_event_participants($evt, $item_id, UserFilter::sortByName(),
731 NB_PER_PAGE, $ofs * NB_PER_PAGE);
732
733 $nbp = ceil($evt['user_count'] / NB_PER_PAGE);
734 if ($nbp > 1) {
735 $links = array();
736 if ($ofs) {
737 $links['précédent'] = $ofs - 1;
738 }
739 for ($i = 1 ; $i <= $nbp; $i++) {
740 $links[(string)$i] = $i - 1;
741 }
742 if ($ofs < $nbp - 1) {
743 $links['suivant'] = $ofs+1;
744 }
745 $page->assign('links', $links);
746 }
747
748 $page->assign('absents', $absents);
749 $page->assign('participants', $part);
750 }
751 }
752
753 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
754 ?>