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