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