0337d704 |
1 | <?php |
2 | /*************************************************************************** |
50a40a33 |
3 | * Copyright (C) 2003-2006 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 | |
24 | function get_event_detail($eid, $item_id = false) |
25 | { |
0337d704 |
26 | global $globals; |
08cce2ff |
27 | $res = XDB::query( |
d6d580ec |
28 | "SELECT SUM(nb) AS nb_tot, e.*, |
29 | IF(e.deadline_inscription, e.deadline_inscription >= LEFT(NOW(), 10), |
30 | 1) AS inscr_open, |
31 | LEFT(10, e.debut) AS debut_day, LEFT(10, e.fin) AS fin_day, |
32 | LEFT(NOW(), 10) AS now, |
33 | ei.titre, |
0337d704 |
34 | al.vid AS absent_list, pl.vid AS participant_list, |
d6d580ec |
35 | a.nom, a.prenom, a.promo, aa.alias |
36 | FROM groupex.evenements AS e |
37 | INNER JOIN x4dat.auth_user_md5 AS a ON a.user_id = e.organisateur_uid |
38 | INNER JOIN x4dat.aliases AS aa ON (aa.type = 'a_vie' AND aa.id = a.user_id) |
39 | INNER JOIN groupex.evenements_items AS ei ON (e.eid = ei.eid) |
0337d704 |
40 | LEFT JOIN groupex.evenements_participants AS ep ON(e.eid = ep.eid AND ei.item_id = ep.item_id) |
41 | LEFT JOIN virtual AS al ON(al.type = 'evt' AND al.alias = CONCAT(short_name, {?})) |
42 | LEFT JOIN virtual AS pl ON(pl.type = 'evt' AND pl.alias = CONCAT(short_name, {?})) |
0337d704 |
43 | WHERE e.eid = {?} AND ei.item_id = {?} AND e.asso_id = {?} |
44 | GROUP BY ei.item_id", |
45 | '-absents@'.$globals->xnet->evts_domain, |
46 | '-participants@'.$globals->xnet->evts_domain, |
d6d580ec |
47 | $eid, $item_id ? $item_id : 1, $globals->asso('id')); |
48 | |
0337d704 |
49 | $evt = $res->fetchOneAssoc(); |
d6d580ec |
50 | |
51 | if (!$evt) { |
52 | return null; |
53 | } |
0337d704 |
54 | |
55 | // smart calculation of the total number |
56 | if (!$item_id) { |
08cce2ff |
57 | $res = XDB::query( |
0337d704 |
58 | "SELECT MAX(nb) |
d6d580ec |
59 | FROM groupex.evenements AS e |
60 | INNER JOIN groupex.evenements_items AS ei ON (e.eid = ei.eid) |
61 | LEFT JOIN groupex.evenements_participants AS ep |
62 | ON (e.eid = ep.eid AND ei.item_id = ep.item_id) |
0337d704 |
63 | WHERE e.eid = {?} |
64 | GROUP BY ep.uid", $eid); |
65 | $evt['nb_tot'] = array_sum($res->fetchColumn()); |
66 | $evt['titre'] = ''; |
67 | $evt['item_id'] = 0; |
68 | } |
d6d580ec |
69 | |
08cce2ff |
70 | $res = XDB::query( |
d6d580ec |
71 | "SELECT titre, details, montant, ei.item_id, nb |
72 | FROM groupex.evenements_items AS ei |
73 | LEFT JOIN groupex.evenements_participants AS ep |
74 | ON (ep.eid = ei.eid AND ep.item_id = ei.item_id AND uid = {?}) |
75 | WHERE ei.eid = {?}", |
cab08090 |
76 | S::v('uid'), $evt['eid']); |
d6d580ec |
77 | $evt['moments'] = $res->fetchAllAssoc(); |
78 | |
79 | $evt['topay'] = 0; |
80 | foreach ($evt['moments'] as $m) { |
81 | $evt['topay'] += $m['nb'] * $m['montant']; |
54ac7230 |
82 | if ($m['montant']) |
83 | $evt['money'] = true; |
d6d580ec |
84 | } |
85 | |
08cce2ff |
86 | $req = XDB::query( |
d6d580ec |
87 | "SELECT montant |
88 | FROM {$globals->money->mpay_tprefix}transactions AS t |
cab08090 |
89 | WHERE ref = {?} AND uid = {?}", $evt['paiement_id'], S::v('uid')); |
d6d580ec |
90 | $montants = $req->fetchColumn(); |
91 | |
92 | $evt['paid'] = 0; |
93 | foreach ($montants as $m) { |
94 | $p = strtr(substr($m, 0, strpos($m, 'EUR')), ',', '.'); |
95 | $evt['paid'] += trim($p); |
0337d704 |
96 | } |
d6d580ec |
97 | |
0337d704 |
98 | return $evt; |
99 | } |
d6d580ec |
100 | |
0337d704 |
101 | // }}} |
102 | |
103 | // {{{ function get_event_participants() |
ed21e24a |
104 | function get_event_participants($evt, $item_id, $tri, $limit = '') { |
0337d704 |
105 | global $globals; |
ed21e24a |
106 | |
107 | if (Env::has('initiale')) { |
108 | $where = 'AND IF(u.nom IS NULL, m.nom, |
109 | IF(u.nom_usage<>"", u.nom_usage, u.nom)) |
5e2307dc |
110 | LIKE "'.addslashes(Env::v('initiale')).'%"'; |
ed21e24a |
111 | } else { |
112 | $where = ''; |
113 | } |
114 | |
115 | $eid = $evt['eid']; |
116 | $money = $evt['money'] && may_update(); |
117 | $pay_id = $evt['paiement_id']; |
118 | |
0337d704 |
119 | $query = |
120 | "SELECT IF(u.nom IS NULL,m.nom,IF(u.nom_usage<>'', u.nom_usage, u.nom)) AS nom, |
121 | IF(u.nom IS NULL,m.prenom,u.prenom) AS prenom, |
122 | IF(u.nom IS NULL,'extérieur',u.promo) AS promo, |
123 | IF(u.nom IS NULL,m.email,a.alias) AS email, |
124 | IF(u.nom IS NULL,0,FIND_IN_SET('femme', u.flags)) AS femme, |
125 | m.perms='admin' AS admin, |
126 | NOT(u.nom IS NULL) AS x, |
127 | ep.uid, ep.paid, SUM(nb) AS nb |
128 | FROM groupex.evenements_participants AS ep |
129 | INNER JOIN groupex.evenements AS e ON (ep.eid = e.eid) |
130 | LEFT JOIN groupex.membres AS m ON ( ep.uid = m.uid AND e.asso_id = m.asso_id) |
131 | LEFT JOIN auth_user_md5 AS u ON ( u.user_id = ep.uid ) |
132 | LEFT JOIN aliases AS a ON ( a.id = ep.uid AND a.type='a_vie' ) |
133 | WHERE ep.eid = {?} |
134 | ".(($item_id)?" AND item_id = $item_id":"")." |
135 | $where |
136 | GROUP BY ep.uid |
ed21e24a |
137 | ORDER BY $tri $limit"; |
138 | |
0337d704 |
139 | if ($item_id) { |
08cce2ff |
140 | $res = XDB::query($query, $eid); |
0337d704 |
141 | return $res->fetchAllAssoc(); |
142 | } |
ed21e24a |
143 | |
08cce2ff |
144 | $res = XDB::iterator($query, $eid); |
0337d704 |
145 | $tab = array(); |
146 | $user = 0; |
ed21e24a |
147 | |
0337d704 |
148 | while ($u = $res->next()) { |
149 | $u['montant'] = 0; |
150 | if ($money && $pay_id) { |
08cce2ff |
151 | $res_ = XDB::query( |
0337d704 |
152 | "SELECT montant |
153 | FROM {$globals->money->mpay_tprefix}transactions AS t |
154 | WHERE ref = {?} AND uid = {?}", |
155 | $pay_id, $u['uid']); |
156 | $montants = $res_->fetchColumn(); |
157 | foreach ($montants as $m) { |
158 | $p = strtr(substr($m, 0, strpos($m, "EUR")), ",", "."); |
159 | $u['paid'] += trim($p); |
160 | } |
161 | } |
08cce2ff |
162 | $res_ = XDB::iterator( |
0337d704 |
163 | "SELECT ep.nb, ep.item_id, ei.montant |
164 | FROM groupex.evenements_participants AS ep |
165 | INNER JOIN groupex.evenements_items AS ei ON (ei.eid = ep.eid AND ei.item_id = ep.item_id) |
166 | WHERE ep.eid = {?} AND ep.uid = {?}", |
167 | $eid, $u['uid']); |
168 | while ($i = $res_->next()) { |
169 | $u[$i['item_id']] = $i['nb']; |
170 | $u['montant'] += $i['montant']*$i['nb']; |
171 | } |
172 | $tab[] = $u; |
173 | } |
174 | return $tab; |
175 | } |
176 | // }}} |
177 | |
178 | // {{{ function subscribe_lists_event() |
ed21e24a |
179 | function subscribe_lists_event($participate, $uid, $evt) { |
0337d704 |
180 | global $globals,$page; |
ed21e24a |
181 | |
182 | $participant_list = $evt['participant_list']; |
183 | $absent_list = $evt['absent_list']; |
184 | |
cab08090 |
185 | $email = S::v('forlife'); |
0337d704 |
186 | |
187 | if ($email) { |
188 | $email .= '@'.$globals->mail->domain; |
189 | } else { |
cab08090 |
190 | $res = XDB::query("SELECT email FROM groupex.membres WHERE uid = {?} AND asso_id = {?}", S::v('uid'), $globals->asso('id')); |
0337d704 |
191 | $email = $res->fetchOneCell(); |
192 | } |
193 | |
ed21e24a |
194 | $subscribe = $participate ? $participant_list : (is_member()?$absent_list:0); |
195 | $unsubscri = $participate ? $absent_list : $participant_list; |
0337d704 |
196 | |
197 | if ($subscribe) { |
08cce2ff |
198 | XDB::execute( |
0337d704 |
199 | "REPLACE INTO virtual_redirect VALUES({?},{?})", |
200 | $subscribe, $email); |
201 | } |
202 | |
203 | if ($unsubscri) { |
08cce2ff |
204 | XDB::execute( |
0337d704 |
205 | "DELETE FROM virtual_redirect WHERE vid = {?} AND redirect = {?}", |
206 | $unsubscri, $email); |
207 | } |
208 | |
209 | } |
210 | // }}} |
211 | |
5070a22d |
212 | function event_change_shortname(&$page, $old, $new) |
213 | { |
214 | global $globals; |
215 | |
216 | // Quelques vérifications sur l'alias (caractères spéciaux) |
217 | if ($old && !preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $old)) { |
218 | $page->trig("Le raccourci demandé n'est pas valide. |
219 | Vérifie qu'il comporte entre 3 et 20 caractères |
220 | et qu'il ne contient que des lettres non accentuées, |
221 | des chiffres ou les caractères - et ."); |
222 | return $old; |
223 | } |
224 | |
225 | //vérifier que l'alias n'est pas déja pris |
226 | if ($new && $old != $new) { |
08cce2ff |
227 | $res = XDB::query('SELECT COUNT(*) FROM virtual |
5070a22d |
228 | WHERE alias LIKE {?}', |
229 | $new.'-absents@%'); |
230 | if ($res->fetchOneCell() > 0) { |
231 | $page->trig("Le raccourci demandé est déjà utilisé. Choisis en un autre."); |
232 | return $old; |
233 | } |
234 | } |
235 | |
236 | if ($old == $new) { |
237 | return $new; |
238 | } |
239 | |
240 | if ($old && $new) { |
241 | // if had a previous shortname change the old lists |
242 | foreach (array('-absents@', '-participants@') as $v) { |
243 | $v .= $globals->xnet->evts_domain; |
08cce2ff |
244 | XDB::execute("UPDATE virtual SET alias = {?} |
5070a22d |
245 | WHERE type = 'evt' AND alias = {?}", |
246 | $new.$v, $old.$v); |
247 | } |
248 | |
249 | return $new; |
250 | } |
251 | |
252 | if (!$old && $new) { |
253 | // if we have a first new short_name create the lists |
254 | |
08cce2ff |
255 | XDB::execute("INSERT INTO virtual SET type = 'evt', alias = {?}", |
5070a22d |
256 | $new.'-participants@'.$globals->xnet->evts_domain); |
257 | |
258 | $lastid = mysql_insert_id(); |
08cce2ff |
259 | XDB::execute( |
5070a22d |
260 | "INSERT INTO virtual_redirect ( |
261 | SELECT {?} AS vid, IF(u.nom IS NULL, m.email, CONCAT(a.alias, {?})) AS redirect |
262 | FROM groupex.evenements_participants AS ep |
263 | LEFT JOIN groupex.membres AS m ON (ep.uid = m.uid) |
264 | LEFT JOIN auth_user_md5 AS u ON (u.user_id = ep.uid) |
265 | LEFT JOIN aliases AS a ON (a.id = ep.uid AND a.type = 'a_vie') |
266 | WHERE ep.eid = {?} |
267 | GROUP BY ep.uid)", |
268 | $lastid, '@'.$globals->mail->domain, $eid); |
269 | |
08cce2ff |
270 | XDB::execute("INSERT INTO virtual SET type = 'evt', alias = {?}", |
5070a22d |
271 | $new.'-absents@'.$globals->xnet->evts_domain); |
272 | |
273 | $lastid = mysql_insert_id(); |
08cce2ff |
274 | XDB::execute("INSERT INTO virtual_redirect ( |
5070a22d |
275 | SELECT {?} AS vid, IF(u.nom IS NULL, m.email, CONCAT(a.alias, {?})) AS redirect |
276 | FROM groupex.membres AS m |
277 | LEFT JOIN groupex.evenements_participants AS ep ON (ep.uid = m.uid) |
278 | LEFT JOIN auth_user_md5 AS u ON (u.user_id = m.uid) |
279 | LEFT JOIN aliases AS a ON (a.id = m.uid AND a.type = 'a_vie') |
280 | WHERE m.asso_id = {?} AND ep.uid IS NULL |
281 | GROUP BY m.uid)", |
282 | $lastid, "@".$globals->mail->domain, $globals->asso('id')); |
283 | |
284 | return $new; |
285 | } |
286 | |
287 | if ($old && !$new) { |
288 | // if we delete the old short name, delete the lists |
289 | foreach (array('-absents@', '-participants@') as $v) { |
290 | $v .= $globals->xnet->evts_domain; |
08cce2ff |
291 | XDB::execute("DELETE virtual, virtual_redirect FROM virtual |
5070a22d |
292 | LEFT JOIN virtual_redirect USING(vid) |
293 | WHERE virtual.alias = {?}", |
294 | $infos['short_name'].$v); |
295 | } |
296 | return $new; |
297 | } |
298 | |
299 | // cannot happen |
300 | return $old; |
301 | } |
302 | |
0337d704 |
303 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: |
304 | ?> |