Merge commit 'origin/fusionax' into account
[platal.git] / modules / xnetevents / xnetevents.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2009 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 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);
47 $evt = $res->fetchOneAssoc();
48
49 if (!$evt) {
50 return null;
51 }
52 if ($GLOBALS['IS_XNET_SITE'] && $evt['accept_nonmembre'] == 0 && !is_member() && !may_update()) {
53 return false;
54 }
55
56 // smart calculation of the total number
57 if (!$item_id) {
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']);
64 $evt['nb_tot'] = array_sum($res->fetchColumn());
65 $evt['titre'] = '';
66 $evt['item_id'] = 0;
67 }
68
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']);
76 $evt['topay'] = 0;
77 $evt['paid'] = 0;
78 $evt['notify_payment'] = false;
79 foreach ($evt['moments'] as $m) {
80 $evt['topay'] += $m['nb'] * $m['montant'];
81 if ($m['montant']) {
82 $evt['money'] = true;
83 }
84 $evt['paid'] = $m['paid'];
85 $evt['notify_payment'] = $evt['notify_payment'] || $m['notify_payment'];
86 }
87
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'));
92 $evt['telepaid'] = 0;
93 foreach ($montants as $m) {
94 $p = strtr(substr($m, 0, strpos($m, 'EUR')), ',', '.');
95 $evt['paid'] += trim($p);
96 $evt['telepaid'] += trim($p);
97 }
98
99 make_event_date($evt);
100
101 return $evt;
102 }
103
104 // }}}
105
106 // {{{ function get_event_participants()
107 function get_event_participants(&$evt, $item_id, array $tri = array(), $count = null, $offset = null)
108 {
109 global $globals;
110
111 $eid = $evt['eid'];
112 $money = $evt['money'] && (function_exists('may_update')) && may_update();
113 $pay_id = $evt['paiement_id'];
114
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 }
129
130 if ($item_id) {
131 return $tab;
132 }
133
134 $evt['adminpaid'] = 0;
135 $evt['telepaid'] = 0;
136 $evt['topay'] = 0;
137 $evt['paid'] = 0;
138 foreach ($tab as $uid=>&$u) {
139 $u['adminpaid'] = $u['paid'];
140 $u['montant'] = 0;
141 if ($money && $pay_id) {
142 $montants = XDB::fetchColumn('SELECT montant
143 FROM ' . $globals->money->mpay_tprefix . 'transactions AS t
144 WHERE ref = {?} AND uid = {?}',
145 $pay_id, $uid);
146 foreach ($montants as $m) {
147 $p = strtr(substr($m, 0, strpos($m, "EUR")), ",", ".");
148 $u['paid'] += trim($p);
149 }
150 }
151 $u['telepayment'] = $u['paid'] - $u['adminpaid'];
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);
157 while ($i = $res_->next()) {
158 $u[$i['item_id']] = $i['nb'];
159 $u['montant'] += $i['montant']*$i['nb'];
160 }
161 $evt['telepaid'] += $u['telepayment'];
162 $evt['adminpaid'] += $u['adminpaid'];
163 $evt['paid'] += $u['paid'];
164 $evt['topay'] += $u['montant'];
165 }
166 return $tab;
167 }
168 // }}}
169
170 // {{{ function subscribe_lists_event()
171 function subscribe_lists_event($participate, $uid, $evt)
172 {
173 global $globals;
174 $page =& Platal::page();
175
176 $participant_list = $evt['participant_list'];
177 $absent_list = $evt['absent_list'];
178
179 $user = User::getSilent($uid);
180 if ($user) {
181 $email = $user->forlifeEmail();
182 } else {
183 $res = XDB::query("SELECT email
184 FROM groupex.membres
185 WHERE uid = {?} AND asso_id = {?}",
186 $uid, $globals->asso('id'));
187 $email = $res->fetchOneCell();
188 }
189
190 function subscribe($list, $email)
191 {
192 if ($list && $email) {
193 XDB::execute("REPLACE INTO virtual_redirect
194 VALUES ({?},{?})",
195 $list, $email);
196 }
197 }
198
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 }
206 }
207
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 }
218 }
219 // }}}
220
221 // {{{ function event_change_shortname()
222 function event_change_shortname(&$page, $eid, $old, $new)
223 {
224 global $globals;
225
226 if (is_null($old)) {
227 $old = '';
228 }
229 // Quelques vérifications sur l'alias (caractères spéciaux)
230 if ($new && !preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $new)) {
231 $page->trigError("Le raccourci demandé n'est pas valide.
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 .");
235 return $old;
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;
241 }
242
243 //vérifier que l'alias n'est pas déja pris
244 if ($new && $old != $new) {
245 $res = XDB::query('SELECT COUNT(*)
246 FROM groupex.evenements
247 WHERE short_name = {?}',
248 $new);
249 if ($res->fetchOneCell() > 0) {
250 $page->trigError("Le raccourci demandé est déjà utilisé. Choisis en un autre.");
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;
263 XDB::execute("UPDATE virtual SET alias = {?}
264 WHERE type = 'evt' AND alias = {?}",
265 $new.$v, $old.$v);
266 }
267 return $new;
268 }
269
270 if (!$old && $new) {
271 // if we have a first new short_name create the lists
272
273 XDB::execute("INSERT INTO virtual SET type = 'evt', alias = {?}",
274 $new.'-participants@'.$globals->xnet->evts_domain);
275
276 $lastid = XDB::insertId();
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)',
284 $lastid, '@'.$globals->mail->domain, $eid);
285
286 XDB::execute("INSERT INTO virtual SET type = 'evt', alias = {?}",
287 $new.'-absents@'.$globals->xnet->evts_domain);
288
289 $lastid = XDB::insertId();
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)",
298 $lastid, "@".$globals->mail->domain, $eid, $globals->asso('id'));
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;
307 XDB::execute("DELETE virtual, virtual_redirect FROM virtual
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 }
318 // }}}
319
320 // {{{ function make_event_date()
321 function make_event_date(&$e)
322 {
323 $start = strtotime($e['debut']);
324 $end = strtotime($e['fin']);
325 $first_day = @strtotime($e['first_day']);
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 // }}}
344
345 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
346 ?>