fleches plus petites pour #484
[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(
d1ebc57a 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),
4f10a058 34 );
35 }
36
37 function handler_events(&$page)
38 {
39 global $globals;
40
3cabafae 41 new_group_open_page('xnetevents/index.tpl');
4f10a058 42
2b9e5fd3 43 if (Post::has('del')) {
44 if (!may_update()) {
45 return PL_NOT_ALLOWED;
46 }
47
5e2307dc 48 $eid = Post::v('del');
2b9e5fd3 49
08cce2ff 50 $res = XDB::query("SELECT asso_id, short_name FROM groupex.evenements
3cabafae 51 WHERE eid = {?} AND asso_id = {?}",
52 $eid, $globals->asso('id'));
2b9e5fd3 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 = {?})
3cabafae 100 WHERE asso_id = {?}"
101 . (is_member() || may_update() ? "" : " AND accept_nonmembre != 0 ")
102 . "GROUP BY e.eid
cb24457f 103 ORDER BY inscr_open DESC, debut DESC", S::v('uid'), $globals->asso('id'));
4f10a058 104
105 $evts = array();
d6d580ec 106
4f10a058 107 while ($e = $evenements->next()) {
3cabafae 108 $e['show_participants'] = ($e['show_participants'] && (is_member() || may_update()));
08cce2ff 109 $res = XDB::query(
4f10a058 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 = {?}",
cab08090 115 S::v('uid'), $e['eid']);
d6d580ec 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
08cce2ff 123 $query = XDB::query(
4f10a058 124 "SELECT montant
125 FROM {$globals->money->mpay_tprefix}transactions AS t
cab08090 126 WHERE ref = {?} AND uid = {?}", $e['paiement_id'], S::v('uid'));
4f10a058 127 $montants = $query->fetchColumn();
d6d580ec 128
4f10a058 129 foreach ($montants as $m) {
d6d580ec 130 $p = strtr(substr($m, 0, strpos($m, 'EUR')), ',', '.');
4f10a058 131 $e['paid'] += trim($p);
132 }
d6d580ec 133
4f10a058 134 $evts[] = $e;
135 }
136
137 $page->assign('evenements', $evts);
138 $page->assign('is_member', is_member());
139 }
140
d6d580ec 141 function handler_sub(&$page, $eid = null)
142 {
352fb101 143 require_once dirname(__FILE__).'/xnetevents/xnetevents.inc.php';
d6d580ec 144
3cabafae 145 new_group_open_page('xnetevents/subscribe.tpl');
d6d580ec 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 }
3cabafae 155 if (!$evt['accept_nonmembre'] && !is_member() && !may_update()) {
156 $page->kill('Cet événement est fermé aux non-membres du groupe');
157 }
d6d580ec 158
159 $page->assign('event', $evt);
160
161 if (!Post::has('submit')) {
162 return;
163 }
164
5e2307dc 165 $moments = Post::v('moment', array());
166 $pers = Post::v('personnes', array());
d6d580ec 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) {
08cce2ff 197 XDB::execute(
d6d580ec 198 "REPLACE INTO groupex.evenements_participants
199 VALUES ({?}, {?}, {?}, {?}, {?})",
cab08090 200 $eid, S::v('uid'), $j, $nb, $evt['paid']);
5070a22d 201 $page->assign('updated', true);
d6d580ec 202 } else {
08cce2ff 203 XDB::execute(
d6d580ec 204 "DELETE FROM groupex.evenements_participants
205 WHERE eid = {?} AND uid = {?} AND item_id = {?}",
cab08090 206 $eid, S::v("uid"), $j);
5070a22d 207 $page->assign('updated', true);
d6d580ec 208 }
209 }
210
211 $page->assign('event', get_event_detail($eid));
212 }
213
4f10a058 214 function handler_csv(&$page, $eid = null, $item_id = null)
215 {
352fb101 216 require_once dirname(__FILE__).'/xnetevents/xnetevents.inc.php';
4f10a058 217
bd46a8e4 218 if (!is_numeric($item_id)) {
219 $item_id = null;
220 }
221
4f10a058 222 $evt = get_event_detail($eid, $item_id);
223 if (!$evt) {
224 return PL_NOT_FOUND;
225 }
226
dba106f6 227 header('Content-type: text/x-csv; encoding=iso-8859-1');
4f10a058 228 header('Pragma: ');
229 header('Cache-Control: ');
230
801fcad8 231 $page->changeTpl('xnetevents/csv.tpl', NO_SKIN);
4f10a058 232
233 $admin = may_update();
234
5e2307dc 235 $tri = (Env::v('order') == 'alpha' ? 'promo, nom, prenom' : 'nom, prenom, promo');
4f10a058 236
ed21e24a 237 $page->assign('participants',
238 get_event_participants($evt, $item_id, $tri));
4f10a058 239
4f10a058 240 $page->assign('admin', $admin);
241 $page->assign('moments', $evt['moments']);
242 $page->assign('money', $evt['money']);
5e2307dc 243 $page->assign('tout', !Env::v('item_id', false));
4f10a058 244 }
bd46a8e4 245
246 function handler_edit(&$page, $eid = null)
247 {
248 global $globals;
249
5070a22d 250 // check the event is in our group
bd46a8e4 251 if (!is_null($eid)) {
08cce2ff 252 $res = XDB::query("SELECT short_name, asso_id
bd46a8e4 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
5070a22d 261 new_groupadmin_page('xnetevents/edit.tpl');
bd46a8e4 262
5070a22d 263 $moments = range(1, 4);
264 $page->assign('moments', $moments);
bd46a8e4 265
5e2307dc 266 if (Post::v('intitule')) {
5070a22d 267 require_once dirname(__FILE__).'/xnetevents/xnetevents.inc.php';
268 $short_name = event_change_shortname($page, $infos['short_name'],
5e2307dc 269 Env::v('short_name', ''));
5070a22d 270
271 $evt = array(
272 'eid' => $eid,
273 'asso_id' => $globals->asso('id'),
cab08090 274 'organisateur_uid' => S::v('uid'),
5e2307dc 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',
5070a22d 282 'short_name' => $short_name,
283 );
284
285 $trivial = array('intitule', 'descriptif', 'noinvite',
3cabafae 286 'show_participants', 'accept_nonmembre');
5070a22d 287 foreach ($trivial as $k) {
5e2307dc 288 $evt[$k] = Post::v($k);
bd46a8e4 289 }
290
5e2307dc 291 if (Post::v('deadline')) {
292 $evt['deadline_inscription'] = Post::v('inscr_Year').'-'
293 . Post::v('inscr_Month').'-'
294 . Post::v('inscr_Day');
5070a22d 295 } else {
296 $evt['deadline_inscription'] = null;
9ece1588 297 }
bd46a8e4 298
299 // Store the modifications in the database
08cce2ff 300 XDB::execute('REPLACE INTO groupex.evenements
bd46a8e4 301 SET eid={?}, asso_id={?}, organisateur_uid={?}, intitule={?},
5070a22d 302 paiement_id = {?}, descriptif = {?}, debut = {?},
303 fin = {?}, show_participants = {?}, short_name = {?},
3cabafae 304 deadline_inscription = {?}, noinvite = {?},
305 accept_nonmembre = {?}',
5070a22d 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'],
3cabafae 310 $evt['noinvite'], $evt['accept_nonmembre']);
bd46a8e4 311
312 // if new event, get its id
313 if (!$eid) {
5070a22d 314 $eid = mysql_insert_id();
bd46a8e4 315 }
316
5070a22d 317 $nb_moments = 0;
bd46a8e4 318 $money_defaut = 0;
319
320 foreach ($moments as $i) {
5e2307dc 321 if (Post::v('titre'.$i)) {
bd46a8e4 322 $nb_moments++;
5070a22d 323
5e2307dc 324 $montant = strtr(Post::v('montant'.$i), ',', '.');
5070a22d 325 $money_defaut += (float)$montant;
08cce2ff 326 XDB::execute("
bd46a8e4 327 REPLACE INTO groupex.evenements_items
328 VALUES ({?}, {?}, {?}, {?}, {?})",
5e2307dc 329 $eid, $i, Post::v('titre'.$i),
330 Post::v('details'.$i), $montant);
bd46a8e4 331 } else {
08cce2ff 332 XDB::execute("DELETE FROM groupex.evenements_items
bd46a8e4 333 WHERE eid = {?} AND item_id = {?}", $eid, $i);
334 }
335 }
336
337 // request for a new payment
5e2307dc 338 if (Post::v('paiement_id') == -1 && $money_defaut >= 0) {
bd46a8e4 339 require_once 'validations.inc.php';
cab08090 340 $p = new PayReq(S::v('uid'),
5e2307dc 341 Post::v('intitule')." - ".$globals->asso('nom'),
342 Post::v('site'), $money_defaut,
343 Post::v('confirmation'), 0, 999,
bd46a8e4 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) {
08cce2ff 350 XDB::execute("INSERT INTO groupex.evenements_items
bd46a8e4 351 VALUES ({?}, {?}, '', '', 0)", $eid, 1);
352 }
bd46a8e4 353
5070a22d 354 if (is_null($evt['eid'])) {
47e64118 355 pl_redirect('events/'.$eid);
5070a22d 356 }
bd46a8e4 357 }
358
359 // get a list of all the payment for this asso
08cce2ff 360 $res = XDB::iterator("SELECT id, text
bd46a8e4 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) {
08cce2ff 370 $res = XDB::query(
bd46a8e4 371 "SELECT eid, intitule, descriptif, debut, fin,
9ece1588 372 show_participants, paiement_id, short_name,
3cabafae 373 deadline_inscription, noinvite, accept_nonmembre
bd46a8e4 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';
08cce2ff 379 $res = XDB::query("SELECT stamp FROM requests
bd46a8e4 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
08cce2ff 389 $res = XDB::iterator(
bd46a8e4 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
352fb101 407 require_once dirname(__FILE__).'/xnetevents/xnetevents.inc.php';
bd46a8e4 408
409 $evt = get_event_detail($eid, $item_id);
bd46a8e4 410 if (!$evt) {
411 return PL_NOT_FOUND;
412 }
413
414 if ($evt['show_participants']) {
ed21e24a 415 new_group_page('xnetevents/admin.tpl');
bd46a8e4 416 } else {
ed21e24a 417 new_groupadmin_page('xnetevents/admin.tpl');
bd46a8e4 418 }
419
5e2307dc 420 if (may_update() && Post::v('adm')) {
421 $member = get_infos(Post::v('mail'));
ed21e24a 422 if (!$member) {
423 $page->trig("Membre introuvable");
bd46a8e4 424 }
bd46a8e4 425
ed21e24a 426 // change the price paid by a participant
5e2307dc 427 if (Env::v('adm') == 'prix' && $member) {
08cce2ff 428 XDB::execute("UPDATE groupex.evenements_participants
ed21e24a 429 SET paid = IF(paid + {?} > 0, paid + {?}, 0)
430 WHERE uid = {?} AND eid = {?}",
5e2307dc 431 strtr(Env::v('montant'), ',', '.'),
432 strtr(Env::v('montant'), ',', '.'),
ed21e24a 433 $member['uid'], $eid);
434 }
bd46a8e4 435
ed21e24a 436 // change the number of personns coming with a participant
5e2307dc 437 if (Env::v('adm') == 'nbs' && $member) {
08cce2ff 438 $res = XDB::query("SELECT paid
ed21e24a 439 FROM groupex.evenements_participants
440 WHERE uid = {?} AND eid = {?}",
441 $member['uid'], $eid);
442
443 $paid = intval($res->fetchOneCell());
5e2307dc 444 $nbs = Post::v('nb', array());
ed21e24a 445
446 foreach ($nbs as $id => $nb) {
5070a22d 447 $nb = max(intval($nb), 0);
ed21e24a 448
449 if ($nb) {
08cce2ff 450 XDB::execute("REPLACE INTO groupex.evenements_participants
ed21e24a 451 VALUES ({?}, {?}, {?}, {?}, {?})",
452 $eid, $member['uid'], $id, $nb, $paid);
453 } else {
08cce2ff 454 XDB::execute("DELETE FROM groupex.evenements_participants
ed21e24a 455 WHERE uid = {?} AND eid = {?} AND item_id = {?}",
456 $member['uid'], $eid, $id);
457 }
bd46a8e4 458 }
ed21e24a 459
08cce2ff 460 $res = XDB::query("SELECT uid FROM groupex.evenements_participants
ed21e24a 461 WHERE uid = {?} AND eid = {?}",
462 $member['uid'], $eid);
bd46a8e4 463 $u = $res->fetchOneCell();
ed21e24a 464 subscribe_lists_event($u, $member['uid'], $evt);
bd46a8e4 465 }
ed21e24a 466
bd46a8e4 467 $evt = get_event_detail($eid, $item_id);
468 }
469
ed21e24a 470 $page->assign('admin', may_update());
bd46a8e4 471 $page->assign('evt', $evt);
1f3362a3 472 $page->assign('tout', is_null($item_id));
bd46a8e4 473
ed21e24a 474 if (count($evt['moments'])) {
475 $page->assign('moments', $evt['moments']);
476 }
bd46a8e4 477
5e2307dc 478 $tri = (Env::v('order') == 'alpha' ? 'promo, nom, prenom' : 'nom, prenom, promo');
1f3362a3 479 $whereitemid = is_null($item_id) ? '' : "AND ep.item_id = $item_id";
08cce2ff 480 $res = XDB::iterRow(
ed21e24a 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)
bd46a8e4 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.'
ed21e24a 489 GROUP BY UPPER(SUBSTRING(IF(u.nom IS NULL,m.nom,u.nom), 1, 1))', $eid);
bd46a8e4 490
491 $alphabet = array();
492 $nb_tot = 0;
493 while (list($char, $nb) = $res->next()) {
494 $alphabet[ord($char)] = $char;
495 $nb_tot += $nb;
5e2307dc 496 if (Env::has('initiale') && $char == strtoupper(Env::v('initiale'))) {
bd46a8e4 497 $tot = $nb;
498 }
499 }
500 ksort($alphabet);
501 $page->assign('alphabet', $alphabet);
502
5e2307dc 503 $ofs = Env::i('offset');
504 $tot = Env::v('initiale') ? $tot : $nb_tot;
bd46a8e4 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
bd46a8e4 520 if ($evt['paiement_id']) {
08cce2ff 521 $res = XDB::iterator(
bd46a8e4 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']);
ed21e24a 530 $page->assign('oublis', $res->total());
531 $page->assign('oubliinscription', $res);
bd46a8e4 532 }
533
ed21e24a 534 $page->assign('participants',
535 get_event_participants($evt, $item_id, $tri,
536 "LIMIT ".($ofs*NB_PER_PAGE).", ".NB_PER_PAGE));
bd46a8e4 537 }
4f10a058 538}
539
540?>