closes wish #517, url catcher
[platal.git] / modules / xnetevents.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 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_MDP),
30 '%grp/events/sub' => $this->make_hook('sub', AUTH_MDP),
31 '%grp/events/csv' => $this->make_hook('csv', AUTH_MDP),
32 '%grp/events/edit' => $this->make_hook('edit', AUTH_MDP),
33 '%grp/events/admin' => $this->make_hook('admin', AUTH_MDP),
34 );
35 }
36
37 function handler_events(&$page)
38 {
39 global $globals;
40
41 new_group_open_page('xnetevents/index.tpl');
42
43 if (Post::has('del')) {
44 if (!may_update()) {
45 return PL_NOT_ALLOWED;
46 }
47
48 $eid = Post::v('del');
49
50 $res = XDB::query("SELECT asso_id, short_name FROM groupex.evenements
51 WHERE eid = {?} AND asso_id = {?}",
52 $eid, $globals->asso('id'));
53
54 $tmp = $res->fetchOneRow();
55 if (!$tmp) {
56 return PL_NOT_ALLOWED;
57 }
58
59 // deletes the event mailing aliases
60 if ($tmp[1]) {
61 XDB::execute(
62 "DELETE FROM virtual WHERE type = 'evt' AND alias LIKE {?}",
63 $tmp[1].'-absents@%');
64 XDB::execute(
65 "DELETE FROM virtual WHERE type = 'evt' AND alias LIKE {?}",
66 $tmp[1].'-participants@%');
67 }
68
69 // deletes the event items
70 XDB::execute("DELETE FROM groupex.evenements_items WHERE eid = {?}", $eid);
71
72 // deletes the event participants
73 XDB::execute("DELETE FROM groupex.evenements_participants
74 WHERE eid = {?}", $eid);
75
76 // deletes the event
77 XDB::execute("DELETE FROM groupex.evenements
78 WHERE eid = {?} AND asso_id = {?}",
79 $eid, $globals->asso('id'));
80
81 // delete the requests for payments
82 require_once 'validations.inc.php';
83 XDB::execute("DELETE FROM requests
84 WHERE type = 'paiements' AND data LIKE {?}",
85 PayReq::same_event($eid, $globals->asso('id')));
86 }
87
88 $page->assign('admin', may_update());
89
90 $evenements = XDB::iterator(
91 "SELECT e.*, LEFT(10, e.debut) AS debut_day, LEFT(10, e.fin) AS fin_day,
92 IF(e.deadline_inscription, e.deadline_inscription >= LEFT(NOW(), 10),
93 1) AS inscr_open, e.deadline_inscription,
94 u.nom, u.prenom, u.promo, a.alias,
95 MAX(ep.nb) AS inscrit, MAX(ep.paid) AS paid
96 FROM groupex.evenements AS e
97 INNER JOIN x4dat.auth_user_md5 AS u ON u.user_id = e.organisateur_uid
98 INNER JOIN x4dat.aliases AS a ON (a.type = 'a_vie' AND a.id = u.user_id)
99 LEFT JOIN groupex.evenements_participants AS ep ON (ep.eid = e.eid AND ep.uid = {?})
100 WHERE asso_id = {?}"
101 . (is_member() || may_update() ? "" : " AND accept_nonmembre != 0 ")
102 . "GROUP BY e.eid
103 ORDER BY inscr_open DESC, debut DESC", S::v('uid'), $globals->asso('id'));
104
105 $evts = array();
106
107 while ($e = $evenements->next()) {
108 $e['show_participants'] = ($e['show_participants'] && (is_member() || may_update()));
109 $res = XDB::query(
110 "SELECT titre, details, montant, ei.item_id, nb
111 FROM groupex.evenements_items AS ei
112 LEFT JOIN groupex.evenements_participants AS ep
113 ON (ep.eid = ei.eid AND ep.item_id = ei.item_id AND uid = {?})
114 WHERE ei.eid = {?}",
115 S::v('uid'), $e['eid']);
116 $e['moments'] = $res->fetchAllAssoc();
117
118 $e['topay'] = 0;
119 foreach ($e['moments'] as $m) {
120 $e['topay'] += $m['nb'] * $m['montant'];
121 }
122
123 $query = XDB::query(
124 "SELECT montant
125 FROM {$globals->money->mpay_tprefix}transactions AS t
126 WHERE ref = {?} AND uid = {?}", $e['paiement_id'], S::v('uid'));
127 $montants = $query->fetchColumn();
128
129 foreach ($montants as $m) {
130 $p = strtr(substr($m, 0, strpos($m, 'EUR')), ',', '.');
131 $e['paid'] += trim($p);
132 }
133
134 $evts[] = $e;
135 }
136
137 $page->assign('evenements', $evts);
138 $page->assign('is_member', is_member());
139 }
140
141 function handler_sub(&$page, $eid = null)
142 {
143 require_once dirname(__FILE__).'/xnetevents/xnetevents.inc.php';
144
145 new_group_open_page('xnetevents/subscribe.tpl');
146
147 $evt = get_event_detail($eid);
148 if (!$evt) {
149 return PL_NOT_FOUND;
150 }
151
152 if (!$evt['inscr_open']) {
153 $page->kill('Les inscriptions pour cet événement sont closes');
154 }
155 if (!$evt['accept_nonmembre'] && !is_member() && !may_update()) {
156 $page->kill('Cet événement est fermé aux non-membres du groupe');
157 }
158
159 $page->assign('event', $evt);
160
161 if (!Post::has('submit')) {
162 return;
163 }
164
165 $moments = Post::v('moment', array());
166 $pers = Post::v('personnes', array());
167 $subs = array();
168
169 foreach ($moments as $j => $v) {
170 $subs[$j] = intval($v);
171
172 // retreive ohter field when more than one person
173 if ($subs[$j] == 2) {
174 if (!isset($pers[$j]) || !is_numeric($pers[$j])
175 || $pers[$j] < 0)
176 {
177 $page->trig('Tu dois choisir un nombre d\'invités correct !');
178 return;
179 }
180 $subs[$j] = 1 + $pers[$j];
181 }
182 }
183
184 // impossible to unsubscribe if you already paid sthing
185 if (array_sum($subs) && $evt['paid'] != 0) {
186 $page->trig("Impossible de te désinscrire complètement ".
187 "parce que tu as fait un paiement par ".
188 "chèque ou par liquide. Contacte un ".
189 "administrateur du groupe si tu es sûr de ".
190 "ne pas venir");
191 return;
192 }
193
194 // update actual inscriptions
195 foreach ($subs as $j => $nb) {
196 if ($nb > 0) {
197 XDB::execute(
198 "REPLACE INTO groupex.evenements_participants
199 VALUES ({?}, {?}, {?}, {?}, {?})",
200 $eid, S::v('uid'), $j, $nb, $evt['paid']);
201 $page->assign('updated', true);
202 } else {
203 XDB::execute(
204 "DELETE FROM groupex.evenements_participants
205 WHERE eid = {?} AND uid = {?} AND item_id = {?}",
206 $eid, S::v("uid"), $j);
207 $page->assign('updated', true);
208 }
209 }
210
211 $page->assign('event', get_event_detail($eid));
212 }
213
214 function handler_csv(&$page, $eid = null, $item_id = null)
215 {
216 require_once dirname(__FILE__).'/xnetevents/xnetevents.inc.php';
217
218 if (!is_numeric($item_id)) {
219 $item_id = null;
220 }
221
222 $evt = get_event_detail($eid, $item_id);
223 if (!$evt) {
224 return PL_NOT_FOUND;
225 }
226
227 header('Content-type: text/x-csv; encoding=iso-8859-1');
228 header('Pragma: ');
229 header('Cache-Control: ');
230
231 $page->changeTpl('xnetevents/csv.tpl', NO_SKIN);
232
233 $admin = may_update();
234
235 $tri = (Env::v('order') == 'alpha' ? 'promo, nom, prenom' : 'nom, prenom, promo');
236
237 $page->assign('participants',
238 get_event_participants($evt, $item_id, $tri));
239
240 $page->assign('admin', $admin);
241 $page->assign('moments', $evt['moments']);
242 $page->assign('money', $evt['money']);
243 $page->assign('tout', !Env::v('item_id', false));
244 }
245
246 function handler_edit(&$page, $eid = null)
247 {
248 global $globals;
249
250 // check the event is in our group
251 if (!is_null($eid)) {
252 $res = XDB::query("SELECT short_name, asso_id
253 FROM groupex.evenements
254 WHERE eid = {?}", $eid);
255 $infos = $res->fetchOneAssoc();
256 if ($infos['asso_id'] != $globals->asso('id')) {
257 return PL_NOT_ALLOWED;
258 }
259 }
260
261 new_groupadmin_page('xnetevents/edit.tpl');
262
263 $moments = range(1, 4);
264 $page->assign('moments', $moments);
265
266 if (Post::v('intitule')) {
267 require_once dirname(__FILE__).'/xnetevents/xnetevents.inc.php';
268 $short_name = event_change_shortname($page, $infos['short_name'],
269 Env::v('short_name', ''));
270
271 $evt = array(
272 'eid' => $eid,
273 'asso_id' => $globals->asso('id'),
274 'organisateur_uid' => S::v('uid'),
275 'paiement_id' => Post::v('paiement_id') > 0 ? Post::v('paiement_id') : null,
276 'debut' => Post::v('deb_Year').'-'.Post::v('deb_Month')
277 .'-'.Post::v('deb_Day').' '.Post::v('deb_Hour')
278 .':'.Post::v('deb_Minute').':00',
279 'fin' => Post::v('fin_Year').'-'.Post::v('fin_Month')
280 .'-'.Post::v('fin_Day').' '.Post::v('fin_Hour')
281 .':'.Post::v('fin_Minute').':00',
282 'short_name' => $short_name,
283 );
284
285 $trivial = array('intitule', 'descriptif', 'noinvite',
286 'show_participants', 'accept_nonmembre');
287 foreach ($trivial as $k) {
288 $evt[$k] = Post::v($k);
289 }
290
291 if (Post::v('deadline')) {
292 $evt['deadline_inscription'] = Post::v('inscr_Year').'-'
293 . Post::v('inscr_Month').'-'
294 . Post::v('inscr_Day');
295 } else {
296 $evt['deadline_inscription'] = null;
297 }
298
299 // Store the modifications in the database
300 XDB::execute('REPLACE INTO groupex.evenements
301 SET eid={?}, asso_id={?}, organisateur_uid={?}, intitule={?},
302 paiement_id = {?}, descriptif = {?}, debut = {?},
303 fin = {?}, show_participants = {?}, short_name = {?},
304 deadline_inscription = {?}, noinvite = {?},
305 accept_nonmembre = {?}',
306 $evt['eid'], $evt['asso_id'], $evt['organisateur_uid'],
307 $evt['intitule'], $evt['paiement_id'], $evt['descriptif'],
308 $evt['debut'], $evt['fin'], $evt['show_participants'],
309 $evt['short_name'], $evt['deadline_inscription'],
310 $evt['noinvite'], $evt['accept_nonmembre']);
311
312 // if new event, get its id
313 if (!$eid) {
314 $eid = mysql_insert_id();
315 }
316
317 $nb_moments = 0;
318 $money_defaut = 0;
319
320 foreach ($moments as $i) {
321 if (Post::v('titre'.$i)) {
322 $nb_moments++;
323
324 $montant = strtr(Post::v('montant'.$i), ',', '.');
325 $money_defaut += (float)$montant;
326 XDB::execute("
327 REPLACE INTO groupex.evenements_items
328 VALUES ({?}, {?}, {?}, {?}, {?})",
329 $eid, $i, Post::v('titre'.$i),
330 Post::v('details'.$i), $montant);
331 } else {
332 XDB::execute("DELETE FROM groupex.evenements_items
333 WHERE eid = {?} AND item_id = {?}", $eid, $i);
334 }
335 }
336
337 // request for a new payment
338 if (Post::v('paiement_id') == -1 && $money_defaut >= 0) {
339 require_once 'validations.inc.php';
340 $p = new PayReq(S::v('uid'),
341 Post::v('intitule')." - ".$globals->asso('nom'),
342 Post::v('site'), $money_defaut,
343 Post::v('confirmation'), 0, 999,
344 $globals->asso('id'), $eid);
345 $p->submit();
346 }
347
348 // events with no sub-event: add a sub-event with no name
349 if ($nb_moments == 0) {
350 XDB::execute("INSERT INTO groupex.evenements_items
351 VALUES ({?}, {?}, '', '', 0)", $eid, 1);
352 }
353
354 if (is_null($evt['eid'])) {
355 pl_redirect('events/'.$eid);
356 }
357 }
358
359 // get a list of all the payment for this asso
360 $res = XDB::iterator("SELECT id, text
361 FROM {$globals->money->mpay_tprefix}paiements
362 WHERE asso_id = {?}", $globals->asso('id'));
363 $paiements = array();
364 while ($a = $res->next()) $paiements[$a['id']] = $a['text']; {
365 $page->assign('paiements', $paiements);
366 }
367
368 // when modifying an old event retreive the old datas
369 if ($eid) {
370 $res = XDB::query(
371 "SELECT eid, intitule, descriptif, debut, fin,
372 show_participants, paiement_id, short_name,
373 deadline_inscription, noinvite, accept_nonmembre
374 FROM groupex.evenements
375 WHERE eid = {?}", $eid);
376 $evt = $res->fetchOneAssoc();
377 // find out if there is already a request for a payment for this event
378 require_once 'validations.inc.php';
379 $res = XDB::query("SELECT stamp FROM requests
380 WHERE type = 'paiements' AND data LIKE {?}",
381 PayReq::same_event($eid, $globals->asso('id')));
382 $stamp = $res->fetchOneCell();
383 if ($stamp) {
384 $evt['paiement_id'] = -2;
385 $evt['paiement_req'] = $stamp;
386 }
387 $page->assign('evt', $evt);
388 // get all the different moments infos
389 $res = XDB::iterator(
390 "SELECT item_id, titre, details, montant
391 FROM groupex.evenements_items AS ei
392 INNER JOIN groupex.evenements AS e ON(e.eid = ei.eid)
393 WHERE e.eid = {?}
394 ORDER BY item_id", $eid);
395 $items = array();
396 while ($item = $res->next()) {
397 $items[$item['item_id']] = $item;
398 }
399 $page->assign('items', $items);
400 }
401 }
402
403 function handler_admin(&$page, $eid = null, $item_id = null)
404 {
405 global $globals;
406
407 require_once dirname(__FILE__).'/xnetevents/xnetevents.inc.php';
408
409 $evt = get_event_detail($eid, $item_id);
410 if (!$evt) {
411 return PL_NOT_FOUND;
412 }
413
414 if ($evt['show_participants']) {
415 new_group_page('xnetevents/admin.tpl');
416 } else {
417 new_groupadmin_page('xnetevents/admin.tpl');
418 }
419
420 if (may_update() && Post::v('adm')) {
421 $member = get_infos(Post::v('mail'));
422 if (!$member) {
423 $page->trig("Membre introuvable");
424 }
425
426 // change the price paid by a participant
427 if (Env::v('adm') == 'prix' && $member) {
428 XDB::execute("UPDATE groupex.evenements_participants
429 SET paid = IF(paid + {?} > 0, paid + {?}, 0)
430 WHERE uid = {?} AND eid = {?}",
431 strtr(Env::v('montant'), ',', '.'),
432 strtr(Env::v('montant'), ',', '.'),
433 $member['uid'], $eid);
434 }
435
436 // change the number of personns coming with a participant
437 if (Env::v('adm') == 'nbs' && $member) {
438 $res = XDB::query("SELECT paid
439 FROM groupex.evenements_participants
440 WHERE uid = {?} AND eid = {?}",
441 $member['uid'], $eid);
442
443 $paid = intval($res->fetchOneCell());
444 $nbs = Post::v('nb', array());
445
446 foreach ($nbs as $id => $nb) {
447 $nb = max(intval($nb), 0);
448
449 if ($nb) {
450 XDB::execute("REPLACE INTO groupex.evenements_participants
451 VALUES ({?}, {?}, {?}, {?}, {?})",
452 $eid, $member['uid'], $id, $nb, $paid);
453 } else {
454 XDB::execute("DELETE FROM groupex.evenements_participants
455 WHERE uid = {?} AND eid = {?} AND item_id = {?}",
456 $member['uid'], $eid, $id);
457 }
458 }
459
460 $res = XDB::query("SELECT uid FROM groupex.evenements_participants
461 WHERE uid = {?} AND eid = {?}",
462 $member['uid'], $eid);
463 $u = $res->fetchOneCell();
464 subscribe_lists_event($u, $member['uid'], $evt);
465 }
466
467 $evt = get_event_detail($eid, $item_id);
468 }
469
470 $page->assign('admin', may_update());
471 $page->assign('evt', $evt);
472 $page->assign('tout', is_null($item_id));
473
474 if (count($evt['moments'])) {
475 $page->assign('moments', $evt['moments']);
476 }
477
478 $tri = (Env::v('order') == 'alpha' ? 'promo, nom, prenom' : 'nom, prenom, promo');
479 $whereitemid = is_null($item_id) ? '' : "AND ep.item_id = $item_id";
480 $res = XDB::iterRow(
481 'SELECT UPPER(SUBSTRING(IF(u.nom IS NULL, m.nom,
482 IF(u.nom_usage<>"", u.nom_usage, u.nom)), 1, 1)),
483 COUNT(DISTINCT ep.uid)
484 FROM groupex.evenements_participants AS ep
485 INNER JOIN groupex.evenements AS e ON (ep.eid = e.eid)
486 LEFT JOIN groupex.membres AS m ON ( ep.uid = m.uid AND e.asso_id = m.asso_id)
487 LEFT JOIN auth_user_md5 AS u ON ( u.user_id = ep.uid )
488 WHERE ep.eid = {?} '.$whereitemid.'
489 GROUP BY UPPER(SUBSTRING(IF(u.nom IS NULL,m.nom,u.nom), 1, 1))', $eid);
490
491 $alphabet = array();
492 $nb_tot = 0;
493 while (list($char, $nb) = $res->next()) {
494 $alphabet[ord($char)] = $char;
495 $nb_tot += $nb;
496 if (Env::has('initiale') && $char == strtoupper(Env::v('initiale'))) {
497 $tot = $nb;
498 }
499 }
500 ksort($alphabet);
501 $page->assign('alphabet', $alphabet);
502
503 $ofs = Env::i('offset');
504 $tot = Env::v('initiale') ? $tot : $nb_tot;
505 $nbp = intval(($tot-1)/NB_PER_PAGE);
506 $links = array();
507 if ($ofs) {
508 $links['précédent'] = $ofs-1;
509 }
510 for ($i = 0; $i <= $nbp; $i++) {
511 $links[(string)($i+1)] = $i;
512 }
513 if ($ofs < $nbp) {
514 $links['suivant'] = $ofs+1;
515 }
516 if (count($links)>1) {
517 $page->assign('links', $links);
518 }
519
520 if ($evt['paiement_id']) {
521 $res = XDB::iterator(
522 "SELECT IF(u.nom_usage<>'', u.nom_usage, u.nom) AS nom, u.prenom,
523 u.promo, a.alias AS email, t.montant
524 FROM {$globals->money->mpay_tprefix}transactions AS t
525 INNER JOIN auth_user_md5 AS u ON(t.uid = u.user_id)
526 INNER JOIN aliases AS a ON (a.id = t.uid AND a.type='a_vie' )
527 LEFT JOIN groupex.evenements_participants AS ep ON(ep.uid = t.uid AND ep.eid = {?})
528 WHERE t.ref = {?} AND ep.uid IS NULL",
529 $evt['eid'], $evt['paiement_id']);
530 $page->assign('oublis', $res->total());
531 $page->assign('oubliinscription', $res);
532 }
533
534 $page->assign('participants',
535 get_event_participants($evt, $item_id, $tri,
536 "LIMIT ".($ofs*NB_PER_PAGE).", ".NB_PER_PAGE));
537 }
538 }
539
540 ?>