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