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