Fixes conditions build for the advanced search.
[platal.git] / modules / xnetevents / xnetevents.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
8d84c630 3 * Copyright (C) 2003-2009 Polytechnique.org *
0337d704 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// {{{ function get_event_detail()
d6d580ec 23
2ac0bcee 24function get_event_detail($eid, $item_id = false, $asso_id = null)
d6d580ec 25{
0337d704 26 global $globals;
2ac0bcee
FB
27 if (is_null($asso_id)) {
28 $asso_id = $globals->asso('id');
29 }
08cce2ff 30 $res = XDB::query(
9ff5b337
SJ
31 "SELECT SUM(nb) AS nb_tot, COUNT(DISTINCT ep.uid) AS nb, e.*,
32 IF(e.deadline_inscription, e.deadline_inscription >= LEFT(NOW(), 10), 1) AS inscr_open,
33 LEFT(10, e.debut) AS first_day, LEFT(10, e.fin) AS last_day,
34 LEFT(NOW(), 10) AS now, ei.titre,
35 al.vid AS absent_list, pl.vid AS participant_list,
36 bl.vid AS payed_list, ul.vid AS booked_unpayed_list,
37 a.nom, a.prenom, a.promo, aa.alias
00112b2e
VZ
38 FROM #groupex#.evenements AS e
39 INNER JOIN #x4dat#.auth_user_md5 AS a ON a.user_id = e.organisateur_uid
40 INNER JOIN #x4dat#.aliases AS aa ON (aa.type = 'a_vie' AND aa.id = a.user_id)
41 INNER JOIN #groupex#.evenements_items AS ei ON (e.eid = ei.eid)
42 LEFT JOIN #groupex#.evenements_participants AS ep ON (e.eid = ep.eid AND ei.item_id = ep.item_id)
43 LEFT JOIN #x4dat#.virtual AS al ON (al.type = 'evt' AND al.alias = CONCAT(short_name, {?}))
44 LEFT JOIN #x4dat#.virtual AS pl ON (pl.type = 'evt' AND pl.alias = CONCAT(short_name, {?}))
45 LEFT JOIN #x4dat#.virtual AS bl ON (bl.type = 'evt' AND bl.alias = CONCAT(short_name, {?}))
46 LEFT JOIN #x4dat#.virtual AS ul ON (ul.type = 'evt' AND ul.alias = CONCAT(short_name, {?}))
9ff5b337
SJ
47 WHERE (e.eid = {?} OR e.short_name = {?}) AND ei.item_id = {?} AND e.asso_id = {?}
48 GROUP BY ei.item_id",
49 '-absents@' . $globals->xnet->evts_domain,
50 '-participants@' . $globals->xnet->evts_domain,
51 '-paye@' . $globals->xnet->evts_domain,
52 '-participants-non-paye@' . $globals->xnet->evts_domain,
53 $eid, $eid, $item_id ? $item_id : 1, $asso_id);
d6d580ec 54
0337d704 55 $evt = $res->fetchOneAssoc();
d6d580ec 56
df1cf596 57 if (!$evt) {
d6d580ec 58 return null;
59 }
df1cf596
FB
60 if ($GLOBALS['IS_XNET_SITE'] && $evt['accept_nonmembre'] == 0 && !is_member() && !may_update()) {
61 return false;
62 }
0337d704 63
64 // smart calculation of the total number
65 if (!$item_id) {
08cce2ff 66 $res = XDB::query(
0337d704 67 "SELECT MAX(nb)
00112b2e
VZ
68 FROM #groupex#.evenements AS e
69 INNER JOIN #groupex#.evenements_items AS ei ON (e.eid = ei.eid)
70 LEFT JOIN #groupex#.evenements_participants AS ep
d6d580ec 71 ON (e.eid = ep.eid AND ei.item_id = ep.item_id)
0337d704 72 WHERE e.eid = {?}
f6bdd218 73 GROUP BY ep.uid", $evt['eid']);
0337d704 74 $evt['nb_tot'] = array_sum($res->fetchColumn());
75 $evt['titre'] = '';
76 $evt['item_id'] = 0;
77 }
d6d580ec 78
08cce2ff 79 $res = XDB::query(
2ac0bcee 80 "SELECT titre, details, montant, ei.item_id, nb, ep.paid, FIND_IN_SET('notify_payment', ep.flags) AS notify_payment
00112b2e
VZ
81 FROM #groupex#.evenements_items AS ei
82 LEFT JOIN #groupex#.evenements_participants AS ep
d6d580ec 83 ON (ep.eid = ei.eid AND ep.item_id = ei.item_id AND uid = {?})
84 WHERE ei.eid = {?}",
cab08090 85 S::v('uid'), $evt['eid']);
d6d580ec 86 $evt['moments'] = $res->fetchAllAssoc();
87
88 $evt['topay'] = 0;
98a7e9dc 89 $evt['paid'] = 0;
2ac0bcee 90 $evt['notify_payment'] = false;
d6d580ec 91 foreach ($evt['moments'] as $m) {
92 $evt['topay'] += $m['nb'] * $m['montant'];
98a7e9dc 93 if ($m['montant']) {
54ac7230 94 $evt['money'] = true;
98a7e9dc 95 }
96 $evt['paid'] = $m['paid'];
2ac0bcee 97 $evt['notify_payment'] = $evt['notify_payment'] || $m['notify_payment'];
d6d580ec 98 }
99
08cce2ff 100 $req = XDB::query(
d6d580ec 101 "SELECT montant
102 FROM {$globals->money->mpay_tprefix}transactions AS t
cab08090 103 WHERE ref = {?} AND uid = {?}", $evt['paiement_id'], S::v('uid'));
d6d580ec 104 $montants = $req->fetchColumn();
105
8bac35d8 106 $evt['telepaid'] = 0;
d6d580ec 107 foreach ($montants as $m) {
108 $p = strtr(substr($m, 0, strpos($m, 'EUR')), ',', '.');
109 $evt['paid'] += trim($p);
8bac35d8 110 $evt['telepaid'] += trim($p);
0337d704 111 }
d6d580ec 112
20c5c7e6
SJ
113 make_event_date($evt);
114
0337d704 115 return $evt;
116}
d6d580ec 117
0337d704 118// }}}
119
120// {{{ function get_event_participants()
e01ebe65 121function get_event_participants(&$evt, $item_id, $tri, $limit = '') {
0337d704 122 global $globals;
ed21e24a 123
124 if (Env::has('initiale')) {
125 $where = 'AND IF(u.nom IS NULL, m.nom,
126 IF(u.nom_usage<>"", u.nom_usage, u.nom))
5e2307dc 127 LIKE "'.addslashes(Env::v('initiale')).'%"';
ed21e24a 128 } else {
129 $where = '';
130 }
131
132 $eid = $evt['eid'];
2ac0bcee 133 $money = $evt['money'] && (function_exists('may_update')) && may_update();
ed21e24a 134 $pay_id = $evt['paiement_id'];
135
0337d704 136 $query =
dc2073c3 137 "SELECT IF(m.origine != 'X',m.nom,IF(u.nom_usage<>'', u.nom_usage, u.nom)) AS nom,
138 IF(m.origine != 'X',m.prenom,u.prenom) AS prenom,
a7de4ef7 139 IF(m.origine != 'X','extérieur',u.promo) AS promo,
dc2073c3 140 IF(m.origine != 'X' OR u.perms = 'pending',m.email,a.alias) AS email,
141 IF(m.origine != 'X',m.sexe,FIND_IN_SET('femme', u.flags)) AS femme,
0337d704 142 m.perms='admin' AS admin,
346dddd7 143 (m.origine = 'X' OR m.origine IS NULL) AS x,
976c685d 144 ep.uid, SUM(ep.paid) AS paid, SUM(ep.nb) AS nb,
2ac0bcee 145 FIND_IN_SET('notify_payment', ep.flags) AS notify_payment
00112b2e
VZ
146 FROM #groupex#.evenements_participants AS ep
147 INNER JOIN #groupex#.evenements AS e ON (ep.eid = e.eid)
148 LEFT JOIN #groupex#.membres AS m ON ( ep.uid = m.uid AND e.asso_id = m.asso_id)
149 LEFT JOIN #x4dat#.auth_user_md5 AS u ON ( u.user_id = ep.uid )
150 LEFT JOIN #x4dat#.aliases AS a ON ( a.id = ep.uid AND a.type='a_vie' )
976c685d 151 WHERE ep.eid = {?}
0337d704 152 ".(($item_id)?" AND item_id = $item_id":"")."
153 $where
dc2073c3 154 GROUP BY ep.uid
155 ORDER BY $tri $limit";
ed21e24a 156
0337d704 157 if ($item_id) {
08cce2ff 158 $res = XDB::query($query, $eid);
0337d704 159 return $res->fetchAllAssoc();
160 }
ed21e24a 161
08cce2ff 162 $res = XDB::iterator($query, $eid);
0337d704 163 $tab = array();
164 $user = 0;
ed21e24a 165
e01ebe65
FB
166 $evt['adminpaid'] = 0;
167 $evt['telepaid'] = 0;
168 $evt['topay'] = 0;
169 $evt['paid'] = 0;
0337d704 170 while ($u = $res->next()) {
976c685d
FB
171 if ($u['nb'] == 0) {
172 continue;
173 }
58591700 174 $u['adminpaid'] = $u['paid'];
0337d704 175 $u['montant'] = 0;
2ac0bcee 176 if ($money && $pay_id) {
08cce2ff 177 $res_ = XDB::query(
0337d704 178 "SELECT montant
179 FROM {$globals->money->mpay_tprefix}transactions AS t
180 WHERE ref = {?} AND uid = {?}",
181 $pay_id, $u['uid']);
182 $montants = $res_->fetchColumn();
183 foreach ($montants as $m) {
e01ebe65
FB
184 $p = strtr(substr($m, 0, strpos($m, "EUR")), ",", ".");
185 $u['paid'] += trim($p);
0337d704 186 }
2ac0bcee 187 }
58591700 188 $u['telepayment'] = $u['paid'] - $u['adminpaid'];
08cce2ff 189 $res_ = XDB::iterator(
2ac0bcee 190 "SELECT ep.nb, ep.item_id, ei.montant
00112b2e
VZ
191 FROM #groupex#.evenements_participants AS ep
192 INNER JOIN #groupex#.evenements_items AS ei ON (ei.eid = ep.eid AND ei.item_id = ep.item_id)
2ac0bcee 193 WHERE ep.eid = {?} AND ep.uid = {?}",
0337d704 194 $eid, $u['uid']);
195 while ($i = $res_->next()) {
196 $u[$i['item_id']] = $i['nb'];
197 $u['montant'] += $i['montant']*$i['nb'];
198 }
2ac0bcee 199 $tab[] = $u;
e01ebe65
FB
200 $evt['telepaid'] += $u['telepayment'];
201 $evt['adminpaid'] += $u['adminpaid'];
202 $evt['paid'] += $u['paid'];
203 $evt['topay'] += $u['montant'];
0337d704 204 }
205 return $tab;
206}
207// }}}
208
209// {{{ function subscribe_lists_event()
9ff5b337 210function subscribe_lists_event($participate, $uid, $evt, $paid, $payment = null)
9193e8f7 211{
d7610c35
FB
212 global $globals;
213 $page =& Platal::page();
ed21e24a 214
9ff5b337
SJ
215 $participant_list = $evt['participant_list'];
216 $absent_list = $evt['absent_list'];
217 $unpayed_list = $evt['booked_unpayed_list'];
218 $payed_list = $evt['payed_list'];
ed21e24a 219
4514bea3
VZ
220 $user = User::getSilent($uid);
221 if ($user) {
222 $email = $user->forlifeEmail();
0337d704 223 } else {
9ff5b337 224 $res = XDB::query("SELECT email
00112b2e 225 FROM #groupex#.membres
9ff5b337 226 WHERE uid = {?} AND asso_id = {?}",
da146d01 227 $uid, $globals->asso('id'));
0337d704 228 $email = $res->fetchOneCell();
229 }
230
9193e8f7 231 function subscribe($list, $email)
232 {
233 if ($list && $email) {
9ff5b337
SJ
234 XDB::execute("REPLACE INTO virtual_redirect
235 VALUES ({?},{?})",
9193e8f7 236 $list, $email);
237 }
0337d704 238 }
239
9193e8f7 240 function unsubscribe($list, $email)
241 {
242 if ($list && $email) {
9ff5b337
SJ
243 XDB::execute("DELETE FROM virtual_redirect
244 WHERE vid = {?} AND redirect = {?}",
9193e8f7 245 $list, $email);
246 }
0337d704 247 }
248
9ff5b337
SJ
249 if (is_null($payment)) {
250 if (is_null($participate)) {
251 unsubscribe($participant_list, $email);
252 subscribe($absent_list, $email);
253 } elseif ($participate) {
254 subscribe($participant_list, $email);
255 unsubscribe($absent_list, $email);
256 } else {
257 unsubscribe($participant_list, $email);
258 unsubscribe($absent_list, $email);
259 }
260 }
261 if ($paid > 0) {
262 unsubscribe($unpayed_list, $email);
263 subscribe($payed_list, $email);
9193e8f7 264 } else {
9ff5b337
SJ
265 unsubscribe($payed_list, $email);
266 subscribe($unpayed_list, $email);
9193e8f7 267 }
0337d704 268}
269// }}}
270
20c5c7e6 271// {{{ function event_change_shortname()
2847640f 272function event_change_shortname(&$page, $eid, $old, $new)
5070a22d 273{
274 global $globals;
275
f56e5e53 276 if (is_null($old)) {
277 $old = '';
278 }
a7de4ef7 279 // Quelques vérifications sur l'alias (caractères spéciaux)
2179ffa2 280 if ($new && !preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $new)) {
a7d35093 281 $page->trigError("Le raccourci demandé n'est pas valide.
a7de4ef7 282 Vérifie qu'il comporte entre 3 et 20 caractères
283 et qu'il ne contient que des lettres non accentuées,
284 des chiffres ou les caractères - et .");
5070a22d 285 return $old;
940ae3a0
SJ
286 } elseif ($new && ctype_digit($new)) {
287 $page->trigError("Le raccourci demandé ne peut être accepté car il
288 ne contient que des chiffres. Rajoute-lui par exemple
289 une lettre.");
290 return $old;
5070a22d 291 }
292
a7de4ef7 293 //vérifier que l'alias n'est pas déja pris
5070a22d 294 if ($new && $old != $new) {
9193e8f7 295 $res = XDB::query('SELECT COUNT(*)
00112b2e 296 FROM #groupex#.evenements
9193e8f7 297 WHERE short_name = {?}',
298 $new);
5070a22d 299 if ($res->fetchOneCell() > 0) {
a7d35093 300 $page->trigError("Le raccourci demandé est déjà utilisé. Choisis en un autre.");
5070a22d 301 return $old;
302 }
303 }
304
305 if ($old == $new) {
306 return $new;
307 }
308
309 if ($old && $new) {
310 // if had a previous shortname change the old lists
9ff5b337 311 foreach (array('-absents@', '-participants@', '-paye@', '-participants-non-paye@') as $v) {
5070a22d 312 $v .= $globals->xnet->evts_domain;
9ff5b337
SJ
313 XDB::execute("UPDATE virtual
314 SET alias = {?}
315 WHERE type = 'evt' AND alias = {?}",
316 $new . $v, $old . $v);
5070a22d 317 }
5070a22d 318 return $new;
319 }
320
321 if (!$old && $new) {
322 // if we have a first new short_name create the lists
9ff5b337
SJ
323 $lastid = array();
324 $where = array(
325 '-participants@' => 'ep.nb > 0',
326 '-paye@' => 'ep.paid > 0',
327 '-participants-non-paye@' => 'ep.nb > 0 AND ep.paid = 0'
328 );
329
330 foreach (array('-absents@', '-participants@', '-paye@', '-participants-non-paye@') as $v) {
331 XDB::execute("INSERT INTO virtual
332 SET type = 'evt', alias = {?}",
333 $new . $v . $globals->xnet->evts_domain);
334
335 $lastid[$v] = XDB::insertId();
336 }
5070a22d 337
9ff5b337
SJ
338 foreach (array('-participants@', '-paye@', '-participants-non-paye@') as $v) {
339 XDB::execute("INSERT IGNORE INTO virtual_redirect (
340 SELECT {?} AS vid, IF(a.alias IS NULL, m.email, CONCAT(a.alias, {?})) AS redirect
00112b2e
VZ
341 FROM #groupex#.evenements_participants AS ep
342 LEFT JOIN #groupex#.membres AS m ON (ep.uid = m.uid)
343 LEFT JOIN #x4dat#.auth_user_md5 AS u ON (u.user_id = ep.uid)
344 LEFT JOIN #x4dat#.aliases AS a ON (a.id = ep.uid AND a.type = 'a_vie')
9ff5b337
SJ
345 WHERE ep.eid = {?} AND " . $where[$v] . "
346 GROUP BY ep.uid)",
347 $lastid[$v], '@' . $globals->mail->domain, $eid);
348 }
54f5ccd5 349 XDB::execute("INSERT IGNORE INTO virtual_redirect (
9ff5b337 350 SELECT {?} AS vid, IF(a.alias IS NULL, m.email, CONCAT(a.alias, {?})) AS redirect
00112b2e
VZ
351 FROM #groupex#.membres AS m
352 LEFT JOIN #groupex#.evenements_participants AS ep ON (ep.uid = m.uid AND ep.eid = {?})
353 LEFT JOIN #x4dat#.auth_user_md5 AS u ON (u.user_id = m.uid)
354 LEFT JOIN #x4dat#.aliases AS a ON (a.id = m.uid AND a.type = 'a_vie')
9ff5b337
SJ
355 WHERE m.asso_id = {?} AND ep.uid IS NULL
356 GROUP BY m.uid)",
357 $lastid['-absents@'], '@' . $globals->mail->domain, $eid, $globals->asso('id'));
5070a22d 358
359 return $new;
360 }
361
362 if ($old && !$new) {
363 // if we delete the old short name, delete the lists
9ff5b337 364 foreach (array('-absents@', '-participants@', '-paye@', '-participants-non-paye@') as $v) {
5070a22d 365 $v .= $globals->xnet->evts_domain;
9ff5b337
SJ
366 XDB::execute("DELETE virtual, virtual_redirect
367 FROM virtual
368 LEFT JOIN virtual_redirect USING(vid)
369 WHERE virtual.alias = {?}",
370 $infos['short_name'] . $v);
5070a22d 371 }
372 return $new;
373 }
374
375 // cannot happen
376 return $old;
377}
20c5c7e6
SJ
378// }}}
379
380// {{{ function make_event_date()
381function make_event_date(&$e)
382{
383 $start = strtotime($e['debut']);
384 $end = strtotime($e['fin']);
385 $first_day = strtotime($e['first_day']);
386 $last_day = strtotime($e['last_day']);
387 unset($e['debut'], $e['fin'], $e['first_day'], $e['last_day']);
388
389 $date = "";
390 if ($start && $end != $start) {
391 if ($first_day == $last_day) {
392 $date .= "le " . strftime("%d %B %Y", $start) . " de "
393 . strftime("%H:%M", $start) . " à " . strftime("%H:%M", $end);
394 } else {
395 $date .= "du " . strftime("%d %B %Y à %H:%M", $start)
396 . "\nau " . strftime("%d %B %Y à %H:%M", $end);
397 }
398 } else {
399 $date .= "le " . strftime("%d %B %Y à %H:%M", $start);
400 }
401 $e['date'] = $date;
402}
403// }}}
5070a22d 404
a7de4ef7 405// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 406?>