b0fab1299733891d8c21305ed61a5bc1b88b2db8
[platal.git] / modules / xnetevents / xnetevents.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 // {{{ function get_event_detail()
23
24 function get_event_detail($eid, $item_id = false, $asso_id = null)
25 {
26 global $globals;
27 if (is_null($asso_id)) {
28 $asso_id = $globals->asso('id');
29 }
30 $res = XDB::query(
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),
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,
37 al.vid AS absent_list, pl.vid AS participant_list,
38 a.nom, a.prenom, a.promo, aa.alias
39 FROM groupex.evenements AS e
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)
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)
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, {?}))
46 WHERE (e.eid = {?} OR e.short_name = {?}) AND ei.item_id = {?} AND e.asso_id = {?}
47 GROUP BY ei.item_id",
48 '-absents@'.$globals->xnet->evts_domain,
49 '-participants@'.$globals->xnet->evts_domain,
50 $eid, $eid, $item_id ? $item_id : 1, $asso_id);
51
52 $evt = $res->fetchOneAssoc();
53
54 if (!$evt) {
55 return null;
56 }
57 if ($GLOBALS['IS_XNET_SITE'] && $evt['accept_nonmembre'] == 0 && !is_member() && !may_update()) {
58 return false;
59 }
60
61 // smart calculation of the total number
62 if (!$item_id) {
63 $res = XDB::query(
64 "SELECT MAX(nb)
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)
69 WHERE e.eid = {?}
70 GROUP BY ep.uid", $evt['eid']);
71 $evt['nb_tot'] = array_sum($res->fetchColumn());
72 $evt['titre'] = '';
73 $evt['item_id'] = 0;
74 }
75
76 $res = XDB::query(
77 "SELECT titre, details, montant, ei.item_id, nb, ep.paid, FIND_IN_SET('notify_payment', ep.flags) AS notify_payment
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 = {?}",
82 S::v('uid'), $evt['eid']);
83 $evt['moments'] = $res->fetchAllAssoc();
84
85 $evt['topay'] = 0;
86 $evt['paid'] = 0;
87 $evt['notify_payment'] = false;
88 foreach ($evt['moments'] as $m) {
89 $evt['topay'] += $m['nb'] * $m['montant'];
90 if ($m['montant']) {
91 $evt['money'] = true;
92 }
93 $evt['paid'] = $m['paid'];
94 $evt['notify_payment'] = $evt['notify_payment'] || $m['notify_payment'];
95 }
96
97 $req = XDB::query(
98 "SELECT montant
99 FROM {$globals->money->mpay_tprefix}transactions AS t
100 WHERE ref = {?} AND uid = {?}", $evt['paiement_id'], S::v('uid'));
101 $montants = $req->fetchColumn();
102
103 $evt['telepaid'] = 0;
104 foreach ($montants as $m) {
105 $p = strtr(substr($m, 0, strpos($m, 'EUR')), ',', '.');
106 $evt['paid'] += trim($p);
107 $evt['telepaid'] += trim($p);
108 }
109
110 return $evt;
111 }
112
113 // }}}
114
115 // {{{ function get_event_participants()
116 function get_event_participants(&$evt, $item_id, $tri, $limit = '') {
117 global $globals;
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))
122 LIKE "'.addslashes(Env::v('initiale')).'%"';
123 } else {
124 $where = '';
125 }
126
127 $eid = $evt['eid'];
128 $money = $evt['money'] && (function_exists('may_update')) && may_update();
129 $pay_id = $evt['paiement_id'];
130
131 $query =
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,
134 IF(m.origine != 'X','extérieur',u.promo) AS promo,
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,
137 m.perms='admin' AS admin,
138 (m.origine = 'X' OR m.origine IS NULL) AS x,
139 ep.uid, SUM(ep.paid) AS paid, SUM(ep.nb) AS nb,
140 FIND_IN_SET('notify_payment', ep.flags) AS notify_payment
141 FROM groupex.evenements_participants AS ep
142 INNER JOIN groupex.evenements AS e ON (ep.eid = e.eid)
143 LEFT JOIN groupex.membres AS m ON ( ep.uid = m.uid AND e.asso_id = m.asso_id)
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' )
146 WHERE ep.eid = {?}
147 ".(($item_id)?" AND item_id = $item_id":"")."
148 $where
149 GROUP BY ep.uid
150 ORDER BY $tri $limit";
151
152 if ($item_id) {
153 $res = XDB::query($query, $eid);
154 return $res->fetchAllAssoc();
155 }
156
157 $res = XDB::iterator($query, $eid);
158 $tab = array();
159 $user = 0;
160
161 $evt['adminpaid'] = 0;
162 $evt['telepaid'] = 0;
163 $evt['topay'] = 0;
164 $evt['paid'] = 0;
165 while ($u = $res->next()) {
166 if ($u['nb'] == 0) {
167 continue;
168 }
169 $u['adminpaid'] = $u['paid'];
170 $u['montant'] = 0;
171 if ($money && $pay_id) {
172 $res_ = XDB::query(
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) {
179 $p = strtr(substr($m, 0, strpos($m, "EUR")), ",", ".");
180 $u['paid'] += trim($p);
181 }
182 }
183 $u['telepayment'] = $u['paid'] - $u['adminpaid'];
184 $res_ = XDB::iterator(
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 = {?}",
189 $eid, $u['uid']);
190 while ($i = $res_->next()) {
191 $u[$i['item_id']] = $i['nb'];
192 $u['montant'] += $i['montant']*$i['nb'];
193 }
194 $tab[] = $u;
195 $evt['telepaid'] += $u['telepayment'];
196 $evt['adminpaid'] += $u['adminpaid'];
197 $evt['paid'] += $u['paid'];
198 $evt['topay'] += $u['montant'];
199 }
200 return $tab;
201 }
202 // }}}
203
204 // {{{ function subscribe_lists_event()
205 function subscribe_lists_event($participate, $uid, $evt)
206 {
207 require_once('user.func.inc.php');
208 global $globals,$page;
209
210 $participant_list = $evt['participant_list'];
211 $absent_list = $evt['absent_list'];
212
213 $email = get_user_forlife($uid, '_silent_user_callback');
214
215 if ($email) {
216 $email .= '@'.$globals->mail->domain;
217 } else {
218 $res = XDB::query("SELECT email
219 FROM groupex.membres
220 WHERE uid = {?} AND asso_id = {?}",
221 S::v('uid'), $globals->asso('id'));
222 $email = $res->fetchOneCell();
223 }
224
225 function subscribe($list, $email)
226 {
227 if ($list && $email) {
228 XDB::execute("REPLACE INTO virtual_redirect
229 VALUES ({?},{?})",
230 $list, $email);
231 }
232 }
233
234 function unsubscribe($list, $email)
235 {
236 if ($list && $email) {
237 XDB::execute("DELETE FROM virtual_redirect
238 WHERE vid = {?} AND redirect = {?}",
239 $list, $email);
240 }
241 }
242
243 if (is_null($participate)) {
244 unsubscribe($participant_list, $email);
245 subscribe($absent_list, $email);
246 } elseif ($participate) {
247 subscribe($participant_list, $email);
248 unsubscribe($absent_list, $email);
249 } else {
250 unsubscribe($participant_list, $email);
251 unsubscribe($absent_list, $email);
252 }
253 }
254 // }}}
255
256 function event_change_shortname(&$page, $eid, $old, $new)
257 {
258 global $globals;
259
260 if (is_null($old)) {
261 $old = '';
262 }
263 // Quelques vérifications sur l'alias (caractères spéciaux)
264 if ($new && !preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $new)) {
265 $page->trigError("Le raccourci demandé n'est pas valide.
266 Vérifie qu'il comporte entre 3 et 20 caractères
267 et qu'il ne contient que des lettres non accentuées,
268 des chiffres ou les caractères - et .");
269 return $old;
270 }
271
272 //vérifier que l'alias n'est pas déja pris
273 if ($new && $old != $new) {
274 $res = XDB::query('SELECT COUNT(*)
275 FROM groupex.evenements
276 WHERE short_name = {?}',
277 $new);
278 if ($res->fetchOneCell() > 0) {
279 $page->trigError("Le raccourci demandé est déjà utilisé. Choisis en un autre.");
280 return $old;
281 }
282 }
283
284 if ($old == $new) {
285 return $new;
286 }
287
288 if ($old && $new) {
289 // if had a previous shortname change the old lists
290 foreach (array('-absents@', '-participants@') as $v) {
291 $v .= $globals->xnet->evts_domain;
292 XDB::execute("UPDATE virtual SET alias = {?}
293 WHERE type = 'evt' AND alias = {?}",
294 $new.$v, $old.$v);
295 }
296 return $new;
297 }
298
299 if (!$old && $new) {
300 // if we have a first new short_name create the lists
301
302 XDB::execute("INSERT INTO virtual SET type = 'evt', alias = {?}",
303 $new.'-participants@'.$globals->xnet->evts_domain);
304
305 $lastid = XDB::insertId();
306 XDB::execute(
307 "INSERT INTO virtual_redirect (
308 SELECT {?} AS vid, IF(u.nom IS NULL, m.email, CONCAT(a.alias, {?})) AS redirect
309 FROM groupex.evenements_participants AS ep
310 LEFT JOIN groupex.membres AS m ON (ep.uid = m.uid)
311 LEFT JOIN auth_user_md5 AS u ON (u.user_id = ep.uid)
312 LEFT JOIN aliases AS a ON (a.id = ep.uid AND a.type = 'a_vie')
313 WHERE ep.eid = {?} AND ep.nb > 0
314 GROUP BY ep.uid)",
315 $lastid, '@'.$globals->mail->domain, $eid);
316
317 XDB::execute("INSERT INTO virtual SET type = 'evt', alias = {?}",
318 $new.'-absents@'.$globals->xnet->evts_domain);
319
320 $lastid = XDB::insertId();
321 XDB::execute("INSERT INTO virtual_redirect (
322 SELECT {?} AS vid, IF(u.nom IS NULL, m.email, CONCAT(a.alias, {?})) AS redirect
323 FROM groupex.membres AS m
324 LEFT JOIN groupex.evenements_participants AS ep ON (ep.uid = m.uid AND ep.eid = {?})
325 LEFT JOIN auth_user_md5 AS u ON (u.user_id = m.uid)
326 LEFT JOIN aliases AS a ON (a.id = m.uid AND a.type = 'a_vie')
327 WHERE m.asso_id = {?} AND ep.uid IS NULL
328 GROUP BY m.uid)",
329 $lastid, "@".$globals->mail->domain, $eid, $globals->asso('id'));
330
331 return $new;
332 }
333
334 if ($old && !$new) {
335 // if we delete the old short name, delete the lists
336 foreach (array('-absents@', '-participants@') as $v) {
337 $v .= $globals->xnet->evts_domain;
338 XDB::execute("DELETE virtual, virtual_redirect FROM virtual
339 LEFT JOIN virtual_redirect USING(vid)
340 WHERE virtual.alias = {?}",
341 $infos['short_name'].$v);
342 }
343 return $new;
344 }
345
346 // cannot happen
347 return $old;
348 }
349
350 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
351 ?>