rework XOrgDB -> XDB (shorter, easier to use).
[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_page('xnetevents/index.tpl');
42
43 if (Post::has('del')) {
44 if (!may_update()) {
45 return PL_NOT_ALLOWED;
46 }
47
48 $eid = Post::get('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 GROUP BY e.eid
102 ORDER BY debut", Session::get('uid'), $globals->asso('id'));
103
104 $evts = array();
105
106 while ($e = $evenements->next()) {
107 $res = XDB::query(
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']);
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
121 $query = 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();
126
127 foreach ($montants as $m) {
128 $p = strtr(substr($m, 0, strpos($m, 'EUR')), ',', '.');
129 $e['paid'] += trim($p);
130 }
131
132 $evts[] = $e;
133 }
134
135 $page->assign('evenements', $evts);
136 $page->assign('is_member', is_member());
137 }
138
139 function handler_sub(&$page, $eid = null)
140 {
141 global $globals;
142
143 require_once dirname(__FILE__).'/xnetevents/xnetevents.inc.php';
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 XDB::execute(
195 "REPLACE INTO groupex.evenements_participants
196 VALUES ({?}, {?}, {?}, {?}, {?})",
197 $eid, Session::getInt('uid'), $j, $nb, $evt['paid']);
198 $page->assign('updated', true);
199 } else {
200 XDB::execute(
201 "DELETE FROM groupex.evenements_participants
202 WHERE eid = {?} AND uid = {?} AND item_id = {?}",
203 $eid, Session::getInt("uid"), $j);
204 $page->assign('updated', true);
205 }
206 }
207
208 $page->assign('event', get_event_detail($eid));
209 }
210
211 function handler_csv(&$page, $eid = null, $item_id = null)
212 {
213 require_once dirname(__FILE__).'/xnetevents/xnetevents.inc.php';
214
215 if (!is_numeric($item_id)) {
216 $item_id = null;
217 }
218
219 $evt = get_event_detail($eid, $item_id);
220 if (!$evt) {
221 return PL_NOT_FOUND;
222 }
223
224 header('Content-type: text/x-csv; encoding=iso-8859-1');
225 header('Pragma: ');
226 header('Cache-Control: ');
227
228 new_nonhtml_page('xnetevents/csv.tpl');
229
230 $admin = may_update();
231
232 $tri = (Env::get('order') == 'alpha' ? 'promo, nom, prenom' : 'nom, prenom, promo');
233
234 $page->assign('participants',
235 get_event_participants($evt, $item_id, $tri));
236
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 }
242
243 function handler_edit(&$page, $eid = null)
244 {
245 global $globals;
246
247 // check the event is in our group
248 if (!is_null($eid)) {
249 $res = 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
258 new_groupadmin_page('xnetevents/edit.tpl');
259
260 $moments = range(1, 4);
261 $page->assign('moments', $moments);
262
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);
286 }
287
288 if (Post::get('deadline')) {
289 $evt['deadline_inscription'] = Post::get('inscr_Year').'-'
290 . Post::get('inscr_Month').'-'
291 . Post::get('inscr_Day');
292 } else {
293 $evt['deadline_inscription'] = null;
294 }
295
296 // Store the modifications in the database
297 XDB::execute('REPLACE INTO groupex.evenements
298 SET eid={?}, asso_id={?}, organisateur_uid={?}, intitule={?},
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']);
307
308 // if new event, get its id
309 if (!$eid) {
310 $eid = mysql_insert_id();
311 }
312
313 $nb_moments = 0;
314 $money_defaut = 0;
315
316 foreach ($moments as $i) {
317 if (Post::get('titre'.$i)) {
318 $nb_moments++;
319
320 $montant = strtr(Post::get('montant'.$i), ',', '.');
321 $money_defaut += (float)$montant;
322 XDB::execute("
323 REPLACE INTO groupex.evenements_items
324 VALUES ({?}, {?}, {?}, {?}, {?})",
325 $eid, $i, Post::get('titre'.$i),
326 Post::get('details'.$i), $montant);
327 } else {
328 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 XDB::execute("INSERT INTO groupex.evenements_items
347 VALUES ({?}, {?}, '', '', 0)", $eid, 1);
348 }
349
350 if (is_null($evt['eid'])) {
351 global $platal;
352 redirect(smarty_function_rel().'/'.$platal->path.'/'.$eid);
353 }
354 }
355
356 // get a list of all the payment for this asso
357 $res = 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 = XDB::query(
368 "SELECT eid, intitule, descriptif, debut, fin,
369 show_participants, paiement_id, short_name,
370 deadline_inscription, noinvite
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 = 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 = 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
404 require_once dirname(__FILE__).'/xnetevents/xnetevents.inc.php';
405
406 $evt = get_event_detail($eid, $item_id);
407 if (!$evt) {
408 return PL_NOT_FOUND;
409 }
410
411 if ($evt['show_participants']) {
412 new_group_page('xnetevents/admin.tpl');
413 } else {
414 new_groupadmin_page('xnetevents/admin.tpl');
415 }
416
417 if (may_update() && Post::get('adm')) {
418 $member = get_infos(Post::get('mail'));
419 if (!$member) {
420 $page->trig("Membre introuvable");
421 }
422
423 // change the price paid by a participant
424 if (Env::get('adm') == 'prix' && $member) {
425 XDB::execute("UPDATE groupex.evenements_participants
426 SET paid = IF(paid + {?} > 0, paid + {?}, 0)
427 WHERE uid = {?} AND eid = {?}",
428 strtr(Env::get('montant'), ',', '.'),
429 strtr(Env::get('montant'), ',', '.'),
430 $member['uid'], $eid);
431 }
432
433 // change the number of personns coming with a participant
434 if (Env::get('adm') == 'nbs' && $member) {
435 $res = 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) {
444 $nb = max(intval($nb), 0);
445
446 if ($nb) {
447 XDB::execute("REPLACE INTO groupex.evenements_participants
448 VALUES ({?}, {?}, {?}, {?}, {?})",
449 $eid, $member['uid'], $id, $nb, $paid);
450 } else {
451 XDB::execute("DELETE FROM groupex.evenements_participants
452 WHERE uid = {?} AND eid = {?} AND item_id = {?}",
453 $member['uid'], $eid, $id);
454 }
455 }
456
457 $res = XDB::query("SELECT uid FROM groupex.evenements_participants
458 WHERE uid = {?} AND eid = {?}",
459 $member['uid'], $eid);
460 $u = $res->fetchOneCell();
461 subscribe_lists_event($u, $member['uid'], $evt);
462 }
463
464 $evt = get_event_detail($eid, $item_id);
465 }
466
467 $page->assign('admin', may_update());
468 $page->assign('evt', $evt);
469 $page->assign('tout', is_null($item_id));
470
471 if (count($evt['moments'])) {
472 $page->assign('moments', $evt['moments']);
473 }
474
475 $tri = (Env::get('order') == 'alpha' ? 'promo, nom, prenom' : 'nom, prenom, promo');
476 $whereitemid = is_null($item_id) ? '' : "AND ep.item_id = $item_id";
477 $res = XDB::iterRow(
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)
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.'
486 GROUP BY UPPER(SUBSTRING(IF(u.nom IS NULL,m.nom,u.nom), 1, 1))', $eid);
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
517 if ($evt['paiement_id']) {
518 $res = XDB::iterator(
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']);
527 $page->assign('oublis', $res->total());
528 $page->assign('oubliinscription', $res);
529 }
530
531 $page->assign('participants',
532 get_event_participants($evt, $item_id, $tri,
533 "LIMIT ".($ofs*NB_PER_PAGE).", ".NB_PER_PAGE));
534 }
535 }
536
537 ?>