Revert "Rewrites xnetevents subscription page."
[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);
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_detail($eid);
211 if (is_null($evt)) {
212 return PL_NOT_FOUND;
213 }
214 if ($evt === false) {
215 global $globals, $platal;
216 $url = $globals->asso('sub_url');
217 if (empty($url)) {
218 $url = $platal->ns . 'subscribe';
219 }
220 $page->kill('Cet événement est reservé aux membres du groupe ' . $globals->asso('nom') .
221 '. Pour devenir membre, rends-toi sur la page de <a href="' . $url . '">demande d\'inscripton</a>.');
222 }
223
224 if (!$evt['inscr_open']) {
225 $page->kill('Les inscriptions pour cet événement sont closes');
226 }
227 if (!$evt['accept_nonmembre'] && !is_member() && !may_update()) {
228 $page->kill('Cet événement est fermé aux non-membres du groupe');
229 }
230
231 global $globals;
232 $res = XDB::query("SELECT stamp
233 FROM requests
234 WHERE type = 'paiements' AND data LIKE {?}",
235 PayReq::same_event($evt['eid'], $globals->asso('id')));
236 $page->assign('validation', $res->numRows());
237 $page->assign('event', $evt);
238
239 if (!Post::has('submit')) {
240 return;
241 } else {
242 S::assert_xsrf_token();
243 }
244
245 $moments = Post::v('moment', array());
246 $pers = Post::v('personnes', array());
247 $subs = array();
248
249 foreach ($moments as $j => $v) {
250 $subs[$j] = intval($v);
251
252 // retrieve other field when more than one person
253 if ($subs[$j] == 2) {
254 if (!isset($pers[$j]) || !is_numeric($pers[$j]) || $pers[$j] < 0) {
255 $page->trigError("Tu dois choisir un nombre d'invités correct&nbsp;!");
256 return;
257 }
258 $subs[$j] = $pers[$j];
259 }
260 }
261
262 // impossible to unsubscribe if you already paid sthing
263 if (!array_sum($subs) && $evt['paid'] != 0) {
264 $page->trigError("Impossible de te désinscrire complètement " .
265 "parce que tu as fait un paiement par " .
266 "chèque ou par liquide. Contacte un " .
267 "administrateur du groupe si tu es sûr de " .
268 "ne pas venir.");
269 return;
270 }
271
272 // update actual inscriptions
273 $updated = false;
274 $total = 0;
275 $paid = $evt['paid'] ? $evt['paid'] : 0;
276 $telepaid = $evt['telepaid'] ? $evt['telepaid'] : 0;
277 $paid_inserted = false;
278 foreach ($subs as $j => $nb) {
279 if ($nb >= 0) {
280 XDB::execute('INSERT INTO group_event_participants (eid, uid, item_id, nb, flags, paid)
281 VALUES ({?}, {?}, {?}, {?}, {?}, {?})
282 ON DUPLICATE KEY UPDATE nb = VALUES(nb), flags = VALUES(flags), paid = VALUES(paid)',
283 $eid, S::v('uid'), $j, $nb, (Env::has('notify_payment') ? 'notify_payment' : ''),
284 ((!$paid_inserted) ? $paid - $telepaid : 0));
285 $updated = $eid;
286 $paid_inserted = true;
287 } else {
288 XDB::execute(
289 "DELETE FROM group_event_participants
290 WHERE eid = {?} AND uid = {?} AND item_id = {?}",
291 $eid, S::v("uid"), $j);
292 $updated = $eid;
293 }
294 $total += $nb;
295 }
296 if ($updated !== false) {
297 $evt = get_event_detail($eid);
298 if ($evt['topay'] > 0) {
299 $page->trigSuccess('Ton inscription à l\'événement a été mise à jour avec succès, tu peux payer ta participation en cliquant ci-dessous');
300 } else {
301 $page->trigSuccess('Ton inscription à l\'événement a été mise à jour avec succès.');
302 }
303 subscribe_lists_event(S::i('uid'), $evt['short_name'], ($total > 0 ? 1 : 0), 0);
304
305 if ($evt['subscription_notification'] != 'nobody') {
306 $mailer = new PlMailer('xnetevents/subscription-notif.mail.tpl');
307 if ($evt['subscription_notification'] != 'creator') {
308 $admins = $globals->asso()->iterAdmins();
309 while ($admin = $admins->next()) {
310 $mailer->addTo($admin);
311 }
312 }
313 if ($evt['subscription_notification'] != 'animator') {
314 $mailer->addTo($evt['organizer']);
315 }
316 $mailer->assign('group', $globals->asso('nom'));
317 $mailer->assign('event', $evt['intitule']);
318 $mailer->assign('subs', $subs);
319 $mailer->assign('moments', $evt['moments']);
320 $mailer->assign('name', S::user()->fullName('promo'));
321 $mailer->send();
322 }
323 }
324 $page->assign('event', get_event_detail($eid));
325 }
326
327 function handler_csv($page, $eid = null, $item_id = null)
328 {
329 $this->load('xnetevents.inc.php');
330
331 if (!is_numeric($item_id)) {
332 $item_id = null;
333 }
334
335 $evt = get_event_detail($eid, $item_id);
336 if (!$evt) {
337 return PL_NOT_FOUND;
338 }
339
340 pl_cached_content_headers('text/x-csv', 'iso-8859-1', 1);
341 $page->changeTpl('xnetevents/csv.tpl', NO_SKIN);
342
343 $admin = may_update();
344 $tri = (Env::v('order') == 'alpha' ? UserFilter::sortByPromo() : UserFilter::sortByName());
345 $all = !Env::v('item_id', false);
346
347 $participants = get_event_participants($evt, $item_id, $tri);
348 $title = 'Nom;Prénom;Promotion;Email';
349 if ($admin) {
350 $title .=';Société;Poste';
351 }
352 if ($all) {
353 foreach ($evt['moments'] as $moment) {
354 $title .= ';' . $moment['titre'];
355 }
356 }
357 if ($admin && $evt['money']) {
358 $title .= ';À payer;';
359 if ($evt['paiement_id']) {
360 $title .= 'Télépaiement;Liquide/Chèque;';
361 }
362 $title .= 'Payé';
363 } else {
364 $title .= ';Nombre';
365 }
366 echo utf8_decode($title) . "\n";
367
368 if ($participants) {
369 foreach ($participants as $participant) {
370 $user = $participant['user'];
371 $line = $user->lastName() . ';' . $user->firstName() . ';' . $user->promo() . ';' . $user->bestEmail();
372 if ($admin && $user->hasProfile()) {
373 $line .= ';' . $user->profile()->getMainJob()->company->name . ';' . $user->profile()->getMainJob()->description;
374 } else {
375 $line .= ';;';
376 }
377 if ($all) {
378 foreach ($evt['moments'] as $moment) {
379 $line .= ';' . $participant[$moment['item_id']];
380 }
381 }
382 if ($admin && $evt['money']) {
383 $line .= ';' . $participant['montant'] . ';';
384 if ($evt['paiement_id']) {
385 $line .= $participant['telepayment'] . ';' . $participant['adminpaid'] . ';';
386 }
387 $line .= $participant['paid'];
388 } else {
389 $line .= ';' . $participant['nb'];
390 }
391
392 echo utf8_decode($line) . "\n";
393 }
394 }
395 exit();
396 }
397
398 function handler_ical($page, $eid = null)
399 {
400 global $globals;
401
402 $this->load('xnetevents.inc.php');
403 $evt = get_event_detail($eid);
404 if (!$evt) {
405 return PL_FORBIDDEN;
406 }
407 $evt['debut'] = preg_replace('/(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/', "\\1\\2\\3T\\4\\5\\6", $evt['debut']);
408 $evt['fin'] = preg_replace('/(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/', "\\1\\2\\3T\\4\\5\\6", $evt['fin']);
409
410 foreach ($evt['moments'] as $m) {
411 $evt['descriptif'] .= "\n\n** " . $m['titre'] . " **\n" . $m['details'];
412 }
413
414 $page->changeTpl('xnetevents/calendar.tpl', NO_SKIN);
415
416 require_once('ical.inc.php');
417 $page->assign('asso', $globals->asso());
418 $page->assign('timestamp', time());
419 $page->assign('admin', may_update());
420
421 if (may_update()) {
422 $page->assign('participants', get_event_participants($evt, null, UserFilter::sortByPromo()));
423 }
424 $page->register_function('display_ical', 'display_ical');
425 $page->assign_by_ref('e', $evt);
426
427 pl_content_headers("text/calendar");
428 }
429
430 function handler_edit($page, $eid = null)
431 {
432 global $globals;
433
434 // get eid if the the given one is a short name
435 if (!is_null($eid) && !is_numeric($eid)) {
436 $res = XDB::query("SELECT eid
437 FROM group_events
438 WHERE asso_id = {?} AND short_name = {?}",
439 $globals->asso('id'), $eid);
440 if ($res->numRows()) {
441 $eid = (int)$res->fetchOneCell();
442 }
443 }
444
445 // check the event is in our group
446 if (!is_null($eid)) {
447 $res = XDB::query("SELECT short_name
448 FROM group_events
449 WHERE eid = {?} AND asso_id = {?}",
450 $eid, $globals->asso('id'));
451 if ($res->numRows()) {
452 $infos = $res->fetchOneAssoc();
453 } else {
454 return PL_FORBIDDEN;
455 }
456 }
457
458 $page->changeTpl('xnetevents/edit.tpl');
459
460 $moments = range(1, 4);
461 $error = false;
462 $page->assign('moments', $moments);
463
464 if (Post::v('intitule')) {
465 S::assert_xsrf_token();
466
467 $this->load('xnetevents.inc.php');
468 $short_name = event_change_shortname($page, $eid,
469 $infos['short_name'],
470 Env::v('short_name', ''));
471 if ($short_name != Env::v('short_name')) {
472 $error = true;
473 }
474 $evt = array(
475 'eid' => $eid,
476 'asso_id' => $globals->asso('id'),
477 'paiement_id' => Post::v('paiement_id') > 0 ? Post::v('paiement_id') : null,
478 'debut' => Post::v('deb_Year').'-'.Post::v('deb_Month')
479 .'-'.Post::v('deb_Day').' '.Post::v('deb_Hour')
480 .':'.Post::v('deb_Minute').':00',
481 'fin' => Post::v('fin_Year').'-'.Post::v('fin_Month')
482 .'-'.Post::v('fin_Day').' '.Post::v('fin_Hour')
483 .':'.Post::v('fin_Minute').':00',
484 'short_name' => $short_name,
485 );
486
487 $trivial = array('intitule', 'descriptif', 'noinvite', 'subscription_notification',
488 'show_participants', 'accept_nonmembre', 'uid');
489 foreach ($trivial as $k) {
490 $evt[$k] = Post::v($k);
491 }
492 if (!$eid) {
493 $evt['uid'] = S::v('uid');
494 }
495
496 if (Post::v('deadline')) {
497 $evt['deadline_inscription'] = Post::v('inscr_Year').'-'
498 . Post::v('inscr_Month').'-'
499 . Post::v('inscr_Day');
500 } else {
501 $evt['deadline_inscription'] = null;
502 }
503
504 // Store the modifications in the database
505 XDB::execute('INSERT INTO group_events (eid, asso_id, uid, intitule, paiement_id,
506 descriptif, debut, fin, show_participants,
507 short_name, deadline_inscription, noinvite,
508 accept_nonmembre, subscription_notification)
509 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})
510 ON DUPLICATE KEY UPDATE asso_id = VALUES(asso_id), uid = VALUES(uid), intitule = VALUES(intitule),
511 paiement_id = VALUES(paiement_id), descriptif = VALUES(descriptif), debut = VALUES(debut),
512 fin = VALUES(fin), show_participants = VALUES(show_participants), short_name = VALUES(short_name),
513 deadline_inscription = VALUES(deadline_inscription), noinvite = VALUES(noinvite),
514 accept_nonmembre = VALUES(accept_nonmembre), subscription_notification = VALUES(subscription_notification)',
515 $evt['eid'], $evt['asso_id'], $evt['uid'],
516 $evt['intitule'], $evt['paiement_id'], $evt['descriptif'],
517 $evt['debut'], $evt['fin'], $evt['show_participants'],
518 $evt['short_name'], $evt['deadline_inscription'],
519 $evt['noinvite'], $evt['accept_nonmembre'], $evt['subscription_notification']);
520
521 // if new event, get its id
522 if (!$eid) {
523 $eid = XDB::insertId();
524 }
525
526 foreach ($moments as $i) {
527 if (Post::v('titre' . $i)) {
528 $nb_moments++;
529
530 $montant = strtr(Post::v('montant' . $i), ',', '.');
531 $money_defaut += (float)$montant;
532 XDB::execute('INSERT INTO group_event_items (eid, item_id, titre, details, montant)
533 VALUES ({?}, {?}, {?}, {?}, {?})
534 ON DUPLICATE KEY UPDATE titre = VALUES(titre), details = VALUES(details), montant = VALUES(montant)',
535 $eid, $i, Post::v('titre' . $i), Post::v('details' . $i), $montant);
536 } else {
537 XDB::execute('DELETE FROM group_event_items
538 WHERE eid = {?} AND item_id = {?}', $eid, $i);
539 }
540 }
541 // request for a new payment
542 if (Post::v('paiement_id') == -1 && $money_defaut >= 0) {
543 $p = new PayReq(S::user(),
544 $globals->asso('nom')." - ".Post::v('intitule'),
545 Post::v('site'), $money_defaut,
546 Post::v('confirmation'), 0, 999,
547 $globals->asso('id'), $eid, Post::v('payment_public') == 'yes');
548 if ($p->accept()) {
549 $p->submit();
550 } else {
551 $page->assign('payment_message', Post::v('confirmation'));
552 $page->assign('payment_site', Post::v('site'));
553 $page->assign('payment_public', Post::v('payment_public') == 'yes');
554 $page->assign('error', true);
555 $error = true;
556 }
557 }
558
559 // events with no sub-event: add a sub-event with default name
560 if ($nb_moments == 0) {
561 XDB::execute("INSERT INTO group_event_items
562 VALUES ({?}, {?}, 'Événement', '', 0)", $eid, 1);
563 }
564
565 if (!$error) {
566 pl_redirect('events');
567 }
568 }
569
570 // get a list of all the payment for this asso
571 $res = XDB::iterator("SELECT id, text
572 FROM payments
573 WHERE asso_id = {?} AND NOT FIND_IN_SET('old', flags)",
574 $globals->asso('id'));
575 $paiements = array();
576 while ($a = $res->next()) $paiements[$a['id']] = $a['text']; {
577 $page->assign('paiements', $paiements);
578 }
579
580 // when modifying an old event retreive the old datas
581 if ($eid) {
582 $res = XDB::query(
583 "SELECT eid, intitule, descriptif, debut, fin, uid,
584 show_participants, paiement_id, short_name,
585 deadline_inscription, noinvite, accept_nonmembre, subscription_notification
586 FROM group_events
587 WHERE eid = {?}", $eid);
588 $evt = $res->fetchOneAssoc();
589 // find out if there is already a request for a payment for this event
590 $res = XDB::query("SELECT stamp
591 FROM requests
592 WHERE type = 'paiements' AND data LIKE {?}",
593 PayReq::same_event($eid, $globals->asso('id')));
594 $stamp = $res->fetchOneCell();
595 if ($stamp) {
596 $evt['paiement_id'] = -2;
597 $evt['paiement_req'] = $stamp;
598 }
599 $page->assign('evt', $evt);
600 // get all the different moments infos
601 $res = XDB::iterator(
602 "SELECT item_id, titre, details, montant
603 FROM group_event_items AS ei
604 INNER JOIN group_events AS e ON(e.eid = ei.eid)
605 WHERE e.eid = {?}
606 ORDER BY item_id", $eid);
607 $items = array();
608 while ($item = $res->next()) {
609 $items[$item['item_id']] = $item;
610 }
611 $page->assign('items', $items);
612 }
613 $page->assign('url_ref', $eid);
614 }
615
616 function handler_admin($page, $eid = null, $item_id = null)
617 {
618 global $globals;
619
620 $this->load('xnetevents.inc.php');
621
622 $evt = get_event_detail($eid, $item_id);
623 if (!$evt) {
624 return PL_NOT_FOUND;
625 }
626
627 $page->changeTpl('xnetevents/admin.tpl');
628 if (!$evt['show_participants'] && !may_update()) {
629 return PL_FORBIDDEN;
630 }
631
632 if (may_update() && Post::v('adm')) {
633 S::assert_xsrf_token();
634
635 $member = User::getSilent(Post::v('mail'));
636 if (!$member) {
637 $page->trigError("Membre introuvable");
638 }
639
640 // change the price paid by a participant
641 if (Env::v('adm') == 'prix' && $member) {
642 $amount = strtr(Env::v('montant'), ',', '.');
643 XDB::execute("UPDATE group_event_participants
644 SET paid = paid + {?}
645 WHERE uid = {?} AND eid = {?} AND nb > 0
646 ORDER BY item_id ASC
647 LIMIT 1",
648 $amount, $member->uid, $evt['eid']);
649 subscribe_lists_event($member->uid, $evt['short_name'], 1, $amount);
650 }
651
652 // change the number of personns coming with a participant
653 if (Env::v('adm') == 'nbs' && $member) {
654 $res = XDB::query("SELECT SUM(paid)
655 FROM group_event_participants
656 WHERE uid = {?} AND eid = {?}",
657 $member->uid, $evt['eid']);
658
659 $paid = $res->fetchOneCell();
660
661 // Ensure we have an integer
662 if ($paid == null) {
663 $paid = 0;
664 }
665
666 $nbs = Post::v('nb', array());
667
668 $paid_inserted = false;
669 foreach ($nbs as $id => $nb) {
670 $nb = max(intval($nb), 0);
671 if (!$paid_inserted && $nb > 0) {
672 $item_paid = $paid;
673 $paid_inserted = true;
674 } else {
675 $item_paid = 0;
676 }
677 XDB::execute('INSERT INTO group_event_participants (eid, uid, item_id, nb, flags, paid)
678 VALUES ({?}, {?}, {?}, {?}, {?}, {?})
679 ON DUPLICATE KEY UPDATE nb = VALUES(nb), flags = VALUES(flags), paid = VALUES(paid)',
680 $evt['eid'], $member->uid, $id, $nb, '', $item_paid);
681 }
682
683 $res = XDB::query('SELECT COUNT(uid) AS cnt, SUM(nb) AS nb
684 FROM group_event_participants
685 WHERE uid = {?} AND eid = {?}
686 GROUP BY uid',
687 $member->uid, $evt['eid']);
688 $u = $res->fetchOneAssoc();
689 if ($paid == 0 && Post::v('cancel')) {
690 XDB::execute("DELETE FROM group_event_participants
691 WHERE uid = {?} AND eid = {?}",
692 $member->uid, $evt['eid']);
693 $u = 0;
694 subscribe_lists_event($member->uid, $evt['short_name'], -1, $paid);
695 } else {
696 $u = $u['cnt'] ? $u['nb'] : null;
697 subscribe_lists_event($member->uid, $evt['short_name'], ($u > 0 ? 1 : 0), $paid);
698 }
699 }
700
701 $evt = get_event_detail($eid, $item_id);
702 }
703
704 $page->assign_by_ref('evt', $evt);
705 $page->assign('tout', is_null($item_id));
706
707 if (count($evt['moments'])) {
708 $page->assign('moments', $evt['moments']);
709 }
710
711 if ($evt['paiement_id']) {
712 $infos = User::getBulkUsersWithUIDs(
713 XDB::fetchAllAssoc('SELECT t.uid, t.amount
714 FROM payment_transactions AS t
715 LEFT JOIN group_event_participants AS ep ON(ep.uid = t.uid AND ep.eid = {?})
716 WHERE t.ref = {?} AND ep.uid IS NULL',
717 $evt['eid'], $evt['paiement_id']),
718 'uid', 'user');
719 $page->assign('oublis', count($infos));
720 $page->assign('oubliinscription', $infos);
721 }
722
723 $absents = User::getBulkUsersFromDB('SELECT p.uid
724 FROM group_event_participants AS p
725 LEFT JOIN group_event_participants AS p2 ON (p2.uid = p.uid
726 AND p2.eid = p.eid
727 AND p2.nb != 0)
728 WHERE p.eid = {?} AND p2.eid IS NULL
729 GROUP BY p.uid', $evt['eid']);
730
731 $ofs = Env::i('offset');
732 $part = get_event_participants($evt, $item_id, UserFilter::sortByName(),
733 NB_PER_PAGE, $ofs * NB_PER_PAGE);
734
735 $nbp = ceil($evt['user_count'] / NB_PER_PAGE);
736 if ($nbp > 1) {
737 $links = array();
738 if ($ofs) {
739 $links['précédent'] = $ofs - 1;
740 }
741 for ($i = 1 ; $i <= $nbp; $i++) {
742 $links[(string)$i] = $i - 1;
743 }
744 if ($ofs < $nbp - 1) {
745 $links['suivant'] = $ofs+1;
746 }
747 $page->assign('links', $links);
748 }
749
750 $page->assign('absents', $absents);
751 $page->assign('participants', $part);
752 }
753 }
754
755 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
756 ?>