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