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