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