typo
[platal.git] / modules / xnetevents / xnetevents.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
5ddeb07c 3 * Copyright (C) 2003-2007 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
2ac0bcee 54 if (!$evt || ($GLOBALS['IS_XNET_SITE'] && $evt['accept_nonmembre'] == 0 && !is_member() && !may_update())) {
d6d580ec 55 return null;
56 }
0337d704 57
58 // smart calculation of the total number
59 if (!$item_id) {
08cce2ff 60 $res = XDB::query(
0337d704 61 "SELECT MAX(nb)
d6d580ec 62 FROM groupex.evenements AS e
63 INNER JOIN groupex.evenements_items AS ei ON (e.eid = ei.eid)
64 LEFT JOIN groupex.evenements_participants AS ep
65 ON (e.eid = ep.eid AND ei.item_id = ep.item_id)
0337d704 66 WHERE e.eid = {?}
f6bdd218 67 GROUP BY ep.uid", $evt['eid']);
0337d704 68 $evt['nb_tot'] = array_sum($res->fetchColumn());
69 $evt['titre'] = '';
70 $evt['item_id'] = 0;
71 }
d6d580ec 72
08cce2ff 73 $res = XDB::query(
2ac0bcee 74 "SELECT titre, details, montant, ei.item_id, nb, ep.paid, FIND_IN_SET('notify_payment', ep.flags) AS notify_payment
d6d580ec 75 FROM groupex.evenements_items AS ei
76 LEFT JOIN groupex.evenements_participants AS ep
77 ON (ep.eid = ei.eid AND ep.item_id = ei.item_id AND uid = {?})
78 WHERE ei.eid = {?}",
cab08090 79 S::v('uid'), $evt['eid']);
d6d580ec 80 $evt['moments'] = $res->fetchAllAssoc();
81
82 $evt['topay'] = 0;
98a7e9dc 83 $evt['paid'] = 0;
2ac0bcee 84 $evt['notify_payment'] = false;
d6d580ec 85 foreach ($evt['moments'] as $m) {
86 $evt['topay'] += $m['nb'] * $m['montant'];
98a7e9dc 87 if ($m['montant']) {
54ac7230 88 $evt['money'] = true;
98a7e9dc 89 }
90 $evt['paid'] = $m['paid'];
2ac0bcee 91 $evt['notify_payment'] = $evt['notify_payment'] || $m['notify_payment'];
d6d580ec 92 }
93
08cce2ff 94 $req = XDB::query(
d6d580ec 95 "SELECT montant
96 FROM {$globals->money->mpay_tprefix}transactions AS t
cab08090 97 WHERE ref = {?} AND uid = {?}", $evt['paiement_id'], S::v('uid'));
d6d580ec 98 $montants = $req->fetchColumn();
99
8bac35d8 100 $evt['telepaid'] = 0;
d6d580ec 101 foreach ($montants as $m) {
102 $p = strtr(substr($m, 0, strpos($m, 'EUR')), ',', '.');
103 $evt['paid'] += trim($p);
8bac35d8 104 $evt['telepaid'] += trim($p);
0337d704 105 }
d6d580ec 106
0337d704 107 return $evt;
108}
d6d580ec 109
0337d704 110// }}}
111
112// {{{ function get_event_participants()
ed21e24a 113function get_event_participants($evt, $item_id, $tri, $limit = '') {
0337d704 114 global $globals;
ed21e24a 115
116 if (Env::has('initiale')) {
117 $where = 'AND IF(u.nom IS NULL, m.nom,
118 IF(u.nom_usage<>"", u.nom_usage, u.nom))
5e2307dc 119 LIKE "'.addslashes(Env::v('initiale')).'%"';
ed21e24a 120 } else {
121 $where = '';
122 }
123
124 $eid = $evt['eid'];
2ac0bcee 125 $money = $evt['money'] && (function_exists('may_update')) && may_update();
ed21e24a 126 $pay_id = $evt['paiement_id'];
127
0337d704 128 $query =
dc2073c3 129 "SELECT IF(m.origine != 'X',m.nom,IF(u.nom_usage<>'', u.nom_usage, u.nom)) AS nom,
130 IF(m.origine != 'X',m.prenom,u.prenom) AS prenom,
a7de4ef7 131 IF(m.origine != 'X','extérieur',u.promo) AS promo,
dc2073c3 132 IF(m.origine != 'X' OR u.perms = 'pending',m.email,a.alias) AS email,
133 IF(m.origine != 'X',m.sexe,FIND_IN_SET('femme', u.flags)) AS femme,
0337d704 134 m.perms='admin' AS admin,
346dddd7 135 (m.origine = 'X' OR m.origine IS NULL) AS x,
2ac0bcee
FB
136 ep.uid, SUM(ep.paid) AS paid, SUM(nb) AS nb,
137 FIND_IN_SET('notify_payment', ep.flags) AS notify_payment
dc2073c3 138 FROM groupex.evenements_participants AS ep
139 INNER JOIN groupex.evenements AS e ON (ep.eid = e.eid)
2ac0bcee 140 LEFT JOIN groupex.membres AS m ON ( ep.uid = m.uid AND e.asso_id = m.asso_id)
dc2073c3 141 LEFT JOIN auth_user_md5 AS u ON ( u.user_id = ep.uid )
142 LEFT JOIN aliases AS a ON ( a.id = ep.uid AND a.type='a_vie' )
61664f8b 143 WHERE ep.eid = {?} AND ep.nb > 0
0337d704 144 ".(($item_id)?" AND item_id = $item_id":"")."
145 $where
dc2073c3 146 GROUP BY ep.uid
147 ORDER BY $tri $limit";
ed21e24a 148
0337d704 149 if ($item_id) {
08cce2ff 150 $res = XDB::query($query, $eid);
0337d704 151 return $res->fetchAllAssoc();
152 }
ed21e24a 153
08cce2ff 154 $res = XDB::iterator($query, $eid);
0337d704 155 $tab = array();
156 $user = 0;
ed21e24a 157
0337d704 158 while ($u = $res->next()) {
58591700 159 $u['adminpaid'] = $u['paid'];
0337d704 160 $u['montant'] = 0;
2ac0bcee 161 if ($money && $pay_id) {
08cce2ff 162 $res_ = XDB::query(
0337d704 163 "SELECT montant
164 FROM {$globals->money->mpay_tprefix}transactions AS t
165 WHERE ref = {?} AND uid = {?}",
166 $pay_id, $u['uid']);
167 $montants = $res_->fetchColumn();
168 foreach ($montants as $m) {
169 $p = strtr(substr($m, 0, strpos($m, "EUR")), ",", ".");
170 $u['paid'] += trim($p);
171 }
2ac0bcee 172 }
58591700 173 $u['telepayment'] = $u['paid'] - $u['adminpaid'];
08cce2ff 174 $res_ = XDB::iterator(
2ac0bcee
FB
175 "SELECT ep.nb, ep.item_id, ei.montant
176 FROM groupex.evenements_participants AS ep
177 INNER JOIN groupex.evenements_items AS ei ON (ei.eid = ep.eid AND ei.item_id = ep.item_id)
178 WHERE ep.eid = {?} AND ep.uid = {?}",
0337d704 179 $eid, $u['uid']);
180 while ($i = $res_->next()) {
181 $u[$i['item_id']] = $i['nb'];
182 $u['montant'] += $i['montant']*$i['nb'];
183 }
2ac0bcee 184 $tab[] = $u;
0337d704 185 }
186 return $tab;
187}
188// }}}
189
190// {{{ function subscribe_lists_event()
9193e8f7 191function subscribe_lists_event($participate, $uid, $evt)
192{
193 require_once('user.func.inc.php');
0337d704 194 global $globals,$page;
ed21e24a 195
196 $participant_list = $evt['participant_list'];
197 $absent_list = $evt['absent_list'];
198
9193e8f7 199 $email = get_user_forlife($uid);
0337d704 200
201 if ($email) {
202 $email .= '@'.$globals->mail->domain;
203 } else {
9193e8f7 204 $res = XDB::query("SELECT email
205 FROM groupex.membres
206 WHERE uid = {?} AND asso_id = {?}",
207 S::v('uid'), $globals->asso('id'));
0337d704 208 $email = $res->fetchOneCell();
209 }
210
9193e8f7 211 function subscribe($list, $email)
212 {
213 if ($list && $email) {
214 XDB::execute("REPLACE INTO virtual_redirect
215 VALUES ({?},{?})",
216 $list, $email);
217 }
0337d704 218 }
219
9193e8f7 220 function unsubscribe($list, $email)
221 {
222 if ($list && $email) {
223 XDB::execute("DELETE FROM virtual_redirect
224 WHERE vid = {?} AND redirect = {?}",
225 $list, $email);
226 }
0337d704 227 }
228
9193e8f7 229 if (is_null($participate)) {
230 unsubscribe($participant_list, $email);
231 subscribe($absent_list, $email);
232 } elseif ($participate) {
233 subscribe($participant_list, $email);
234 unsubscribe($absent_list, $email);
235 } else {
236 unsubscribe($participant_list, $email);
237 unsubscribe($absent_list, $email);
238 }
0337d704 239}
240// }}}
241
5070a22d 242function event_change_shortname(&$page, $old, $new)
243{
244 global $globals;
245
f56e5e53 246 if (is_null($old)) {
247 $old = '';
248 }
a7de4ef7 249 // Quelques vérifications sur l'alias (caractères spéciaux)
2179ffa2 250 if ($new && !preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $new)) {
a7de4ef7 251 $page->trig("Le raccourci demandé n'est pas valide.
252 Vérifie qu'il comporte entre 3 et 20 caractères
253 et qu'il ne contient que des lettres non accentuées,
254 des chiffres ou les caractères - et .");
5070a22d 255 return $old;
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(*)
261 FROM groupex.evenements
262 WHERE short_name = {?}',
263 $new);
5070a22d 264 if ($res->fetchOneCell() > 0) {
a7de4ef7 265 $page->trig("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
276 foreach (array('-absents@', '-participants@') as $v) {
277 $v .= $globals->xnet->evts_domain;
08cce2ff 278 XDB::execute("UPDATE virtual SET alias = {?}
9193e8f7 279 WHERE type = 'evt' AND alias = {?}",
280 $new.$v, $old.$v);
5070a22d 281 }
5070a22d 282 return $new;
283 }
284
285 if (!$old && $new) {
286 // if we have a first new short_name create the lists
287
08cce2ff 288 XDB::execute("INSERT INTO virtual SET type = 'evt', alias = {?}",
5070a22d 289 $new.'-participants@'.$globals->xnet->evts_domain);
290
8b83a166 291 $lastid = XDB::insertId();
08cce2ff 292 XDB::execute(
5070a22d 293 "INSERT INTO virtual_redirect (
294 SELECT {?} AS vid, IF(u.nom IS NULL, m.email, CONCAT(a.alias, {?})) AS redirect
295 FROM groupex.evenements_participants AS ep
296 LEFT JOIN groupex.membres AS m ON (ep.uid = m.uid)
297 LEFT JOIN auth_user_md5 AS u ON (u.user_id = ep.uid)
298 LEFT JOIN aliases AS a ON (a.id = ep.uid AND a.type = 'a_vie')
299 WHERE ep.eid = {?}
300 GROUP BY ep.uid)",
301 $lastid, '@'.$globals->mail->domain, $eid);
302
08cce2ff 303 XDB::execute("INSERT INTO virtual SET type = 'evt', alias = {?}",
5070a22d 304 $new.'-absents@'.$globals->xnet->evts_domain);
305
8b83a166 306 $lastid = XDB::insertId();
08cce2ff 307 XDB::execute("INSERT INTO virtual_redirect (
5070a22d 308 SELECT {?} AS vid, IF(u.nom IS NULL, m.email, CONCAT(a.alias, {?})) AS redirect
309 FROM groupex.membres AS m
310 LEFT JOIN groupex.evenements_participants AS ep ON (ep.uid = m.uid)
311 LEFT JOIN auth_user_md5 AS u ON (u.user_id = m.uid)
312 LEFT JOIN aliases AS a ON (a.id = m.uid AND a.type = 'a_vie')
313 WHERE m.asso_id = {?} AND ep.uid IS NULL
314 GROUP BY m.uid)",
315 $lastid, "@".$globals->mail->domain, $globals->asso('id'));
316
317 return $new;
318 }
319
320 if ($old && !$new) {
321 // if we delete the old short name, delete the lists
322 foreach (array('-absents@', '-participants@') as $v) {
323 $v .= $globals->xnet->evts_domain;
08cce2ff 324 XDB::execute("DELETE virtual, virtual_redirect FROM virtual
5070a22d 325 LEFT JOIN virtual_redirect USING(vid)
326 WHERE virtual.alias = {?}",
327 $infos['short_name'].$v);
328 }
329 return $new;
330 }
331
332 // cannot happen
333 return $old;
334}
335
a7de4ef7 336// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 337?>