Payments - better recognition of dates
[platal.git] / modules / xnetevents / xnetevents.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
5e1513f6 3 * Copyright (C) 2003-2011 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 }
7852229b
SJ
30 $evt = XDB::fetchOneAssoc('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, e.subscription_notification
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 WHERE (e.eid = {?} OR e.short_name = {?}) AND ei.item_id = {?} AND e.asso_id = {?}
41 GROUP BY ei.item_id',
42 $eid, $eid, $item_id ? $item_id : 1, $asso_id);
d6d580ec 43
df1cf596 44 if (!$evt) {
d6d580ec 45 return null;
46 }
df1cf596
FB
47 if ($GLOBALS['IS_XNET_SITE'] && $evt['accept_nonmembre'] == 0 && !is_member() && !may_update()) {
48 return false;
49 }
0337d704 50
51 // smart calculation of the total number
52 if (!$item_id) {
07eb5b0e 53 $res = XDB::query('SELECT MAX(nb)
eb41eda9
FB
54 FROM group_events AS e
55 INNER JOIN group_event_items AS ei ON (e.eid = ei.eid)
56 LEFT JOIN group_event_participants AS ep ON (e.eid = ep.eid AND ei.item_id = ep.item_id)
07eb5b0e
FB
57 WHERE e.eid = {?}
58 GROUP BY ep.uid', $evt['eid']);
0337d704 59 $evt['nb_tot'] = array_sum($res->fetchColumn());
60 $evt['titre'] = '';
61 $evt['item_id'] = 0;
62 }
d6d580ec 63
07eb5b0e
FB
64 $evt['moments'] = XDB::fetchAllAssoc('SELECT titre, details, montant, ei.item_id, nb,
65 ep.paid, FIND_IN_SET(\'notify_payment\', ep.flags) AS notify_payment
eb41eda9
FB
66 FROM group_event_items AS ei
67 LEFT JOIN group_event_participants AS ep ON (ep.eid = ei.eid AND ep.item_id = ei.item_id
07eb5b0e
FB
68 AND uid = {?})
69 WHERE ei.eid = {?}',
70 S::i('uid'), $evt['eid']);
d6d580ec 71 $evt['topay'] = 0;
98a7e9dc 72 $evt['paid'] = 0;
2ac0bcee 73 $evt['notify_payment'] = false;
d6d580ec 74 foreach ($evt['moments'] as $m) {
75 $evt['topay'] += $m['nb'] * $m['montant'];
98a7e9dc 76 if ($m['montant']) {
54ac7230 77 $evt['money'] = true;
98a7e9dc 78 }
79 $evt['paid'] = $m['paid'];
2ac0bcee 80 $evt['notify_payment'] = $evt['notify_payment'] || $m['notify_payment'];
d6d580ec 81 }
82
b3cd1320
DB
83 $montants = XDB::fetchColumn('SELECT amount
84 FROM payment_transactions AS t
07eb5b0e
FB
85 WHERE ref = {?} AND uid = {?}',
86 $evt['paiement_id'], S::v('uid'));
8bac35d8 87 $evt['telepaid'] = 0;
d6d580ec 88 foreach ($montants as $m) {
89 $p = strtr(substr($m, 0, strpos($m, 'EUR')), ',', '.');
90 $evt['paid'] += trim($p);
8bac35d8 91 $evt['telepaid'] += trim($p);
0337d704 92 }
ed996b5a 93 $evt['organizer'] = User::getSilent($evt['uid']);
d6d580ec 94
20c5c7e6
SJ
95 make_event_date($evt);
96
5a6bcfb7
SJ
97 $evt['show_participants'] = ($evt['show_participants'] && (is_member() || may_update()));
98
0337d704 99 return $evt;
100}
d6d580ec 101
0337d704 102// }}}
103
104// {{{ function get_event_participants()
d1965ef6 105function get_event_participants(&$evt, $item_id, array $tri = array(), $limit = null)
07eb5b0e 106{
0337d704 107 global $globals;
ed21e24a 108
ed21e24a 109 $eid = $evt['eid'];
2ac0bcee 110 $money = $evt['money'] && (function_exists('may_update')) && may_update();
ed21e24a 111 $pay_id = $evt['paiement_id'];
112
c81e03c0 113 $append = $item_id ? XDB::format(' AND ep.item_id = {?}', $item_id) : '';
07eb5b0e
FB
114 $query = XDB::fetchAllAssoc('uid', 'SELECT ep.uid, SUM(ep.paid) AS paid, SUM(ep.nb) AS nb,
115 FIND_IN_SET(\'notify_payment\', ep.flags) AS notify_payment
eb41eda9 116 FROM group_event_participants AS ep
07eb5b0e
FB
117 WHERE ep.eid = {?} AND nb > 0 ' . $append . '
118 GROUP BY ep.uid', $eid);
c8763ca3 119 $uf = new UserFilter(new PFC_True(), $tri);
4935bab0 120 $users = User::getBulkUsersWithUIDs($uf->filter(array_keys($query), new PlLimit($count, $offset)));
07eb5b0e
FB
121 $tab = array();
122 foreach ($users as $user) {
123 $uid = $user->id();
124 $tab[$uid] = $query[$uid];
125 $tab[$uid]['user'] = $user;
126 }
ed21e24a 127
0337d704 128 if ($item_id) {
07eb5b0e 129 return $tab;
0337d704 130 }
ed21e24a 131
e01ebe65
FB
132 $evt['adminpaid'] = 0;
133 $evt['telepaid'] = 0;
134 $evt['topay'] = 0;
135 $evt['paid'] = 0;
07eb5b0e 136 foreach ($tab as $uid=>&$u) {
58591700 137 $u['adminpaid'] = $u['paid'];
0337d704 138 $u['montant'] = 0;
2ac0bcee 139 if ($money && $pay_id) {
b3cd1320
DB
140 $montants = XDB::fetchColumn('SELECT amount
141 FROM payment_transactions AS t
07eb5b0e
FB
142 WHERE ref = {?} AND uid = {?}',
143 $pay_id, $uid);
0337d704 144 foreach ($montants as $m) {
e01ebe65
FB
145 $p = strtr(substr($m, 0, strpos($m, "EUR")), ",", ".");
146 $u['paid'] += trim($p);
0337d704 147 }
2ac0bcee 148 }
58591700 149 $u['telepayment'] = $u['paid'] - $u['adminpaid'];
07eb5b0e 150 $res_ = XDB::iterator('SELECT ep.nb, ep.item_id, ei.montant
eb41eda9
FB
151 FROM group_event_participants AS ep
152 INNER JOIN group_event_items AS ei ON (ei.eid = ep.eid AND ei.item_id = ep.item_id)
07eb5b0e
FB
153 WHERE ep.eid = {?} AND ep.uid = {?}',
154 $eid, $uid);
0337d704 155 while ($i = $res_->next()) {
156 $u[$i['item_id']] = $i['nb'];
157 $u['montant'] += $i['montant']*$i['nb'];
158 }
e01ebe65
FB
159 $evt['telepaid'] += $u['telepayment'];
160 $evt['adminpaid'] += $u['adminpaid'];
161 $evt['paid'] += $u['paid'];
162 $evt['topay'] += $u['montant'];
0337d704 163 }
164 return $tab;
165}
166// }}}
167
168// {{{ function subscribe_lists_event()
50208d22
SJ
169/** Subscribes user to various event related mailing lists.
170 *
171 * @param $uid: user's id.
7852229b 172 * @param short_name: event's short_name, which corresponds to the beginning of the emails.
50208d22
SJ
173 * @param participate: indicates if the user takes part at the event or not;
174 * -1 means he did not answer, 0 means no, and 1 means yes.
175 * @param paid: has the user already payed anything?
176 * 0 means no, a positive amount means yes.
177 * @param payment: is this function called from a payment page?
178 * If true, only payment related lists should be updated.
179 */
7852229b 180function subscribe_lists_event($uid, $short_name, $participate, $paid, $payment = false)
9193e8f7 181{
7852229b
SJ
182 global $globals;
183 require_once 'emails.inc.php';
0337d704 184
7852229b
SJ
185 if (is_null($short_name)) {
186 return;
0337d704 187 }
188
50208d22
SJ
189 /** If $payment is not null, we do not retrieve the value of $participate,
190 * thus we do not alter participant and absent lists.
191 */
192 if ($payment === true) {
193 if ($paid > 0) {
7e82b545
SJ
194 delete_from_list_alias($uid, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
195 add_to_list_alias($uid, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
50208d22
SJ
196 }
197 } else {
198 switch ($participate) {
199 case -1:
7e82b545
SJ
200 delete_from_list_alias($uid, $short_name . $globals->xnet->participant_list, $globals->xnet->evts_domain, 'event');
201 delete_from_list_alias($uid, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
202 delete_from_list_alias($uid, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
203 add_to_list_alias($uid, $short_name . $globals->xnet->absent_list, $globals->xnet->evts_domain, 'event');
50208d22
SJ
204 break;
205 case 0:
7e82b545
SJ
206 delete_from_list_alias($uid, $short_name . $globals->xnet->participant_list, $globals->xnet->evts_domain, 'event');
207 delete_from_list_alias($uid, $short_name . $globals->xnet->absent_list, $globals->xnet->evts_domain, 'event');
208 delete_from_list_alias($uid, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
209 delete_from_list_alias($uid, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
50208d22
SJ
210 break;
211 case 1:
7e82b545
SJ
212 add_to_list_alias($uid, $short_name . $globals->xnet->participant_list, $globals->xnet->evts_domain, 'event');
213 delete_from_list_alias($uid, $short_name . $globals->xnet->absent_list, $globals->xnet->evts_domain, 'event');
50208d22 214 if ($paid > 0) {
7e82b545
SJ
215 delete_from_list_alias($uid, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
216 add_to_list_alias($uid, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
50208d22 217 } else {
7e82b545
SJ
218 add_to_list_alias($uid, $short_name . $globals->xnet->unpayed_list, $globals->xnet->evts_domain, 'event');
219 delete_from_list_alias($uid, $short_name . $globals->xnet->payed_list, $globals->xnet->evts_domain, 'event');
50208d22
SJ
220 }
221 break;
1cf7d38b 222 }
9193e8f7 223 }
0337d704 224}
225// }}}
226
20c5c7e6 227// {{{ function event_change_shortname()
26ba053e 228function event_change_shortname($page, $eid, $old, $new)
5070a22d 229{
230 global $globals;
7852229b 231 require_once 'emails.inc.php';
5070a22d 232
f56e5e53 233 if (is_null($old)) {
234 $old = '';
235 }
a7de4ef7 236 // Quelques vérifications sur l'alias (caractères spéciaux)
2179ffa2 237 if ($new && !preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $new)) {
a7d35093 238 $page->trigError("Le raccourci demandé n'est pas valide.
a7de4ef7 239 Vérifie qu'il comporte entre 3 et 20 caractères
240 et qu'il ne contient que des lettres non accentuées,
241 des chiffres ou les caractères - et .");
5070a22d 242 return $old;
4aae4d2c 243 } elseif ($new && (is_int($new) || ctype_digit($new))) {
940ae3a0
SJ
244 $page->trigError("Le raccourci demandé ne peut être accepté car il
245 ne contient que des chiffres. Rajoute-lui par exemple
246 une lettre.");
247 return $old;
5070a22d 248 }
249
a7de4ef7 250 //vérifier que l'alias n'est pas déja pris
5070a22d 251 if ($new && $old != $new) {
9193e8f7 252 $res = XDB::query('SELECT COUNT(*)
eb41eda9 253 FROM group_events
9193e8f7 254 WHERE short_name = {?}',
255 $new);
5070a22d 256 if ($res->fetchOneCell() > 0) {
a7d35093 257 $page->trigError("Le raccourci demandé est déjà utilisé. Choisis en un autre.");
5070a22d 258 return $old;
259 }
260 }
261
262 if ($old == $new) {
263 return $new;
264 }
265
266 if ($old && $new) {
267 // if had a previous shortname change the old lists
7852229b
SJ
268 foreach (explode(',', $globals->xnet->event_lists) as $suffix) {
269 XDB::execute('UPDATE email_virtual
270 SET email = {?}
271 WHERE type = \'event\' AND email = {?}',
272 $new . $suffix, $old . $suffix);
5070a22d 273 }
7852229b 274
5070a22d 275 return $new;
276 }
277
278 if (!$old && $new) {
279 // if we have a first new short_name create the lists
9ff5b337
SJ
280 $lastid = array();
281 $where = array(
7852229b
SJ
282 $globals->xnet->participant_list => 'nb > 0',
283 $globals->xnet->payed_list => 'paid > 0',
284 $globals->xnet->unpayed_list => 'nb > 0 AND paid = 0'
9ff5b337
SJ
285 );
286
7852229b
SJ
287 foreach (array($globals->xnet->participant_list, $globals->xnet->payed_list, $globals->xnet->unpayed_list) as $suffix) {
288 $uids = XDB::fetchColumn('SELECT uid
289 FROM group_event_participants
a2e601ac 290 WHERE eid = {?} AND ' . $where[$suffix],
7852229b 291 $eid);
7e82b545
SJ
292 foreach ($uids as $uid) {
293 add_to_list_alias($uid, $new . $suffix, $globals->xnet->evts_domain, 'event');
7852229b 294 }
9ff5b337 295 }
7852229b
SJ
296 $uids = XDB::fetchColumn('SELECT m.uid
297 FROM group_members AS m
298 LEFT JOIN group_event_participants AS e ON (e.uid = m.uid AND e.eid = {?})
299 WHERE m.asso_id = {?} AND e.uid IS NULL',
300 $eid, $globals->asso('id'));
7e82b545
SJ
301 foreach ($uids as $uid) {
302 add_to_list_alias($uid, $new . $globals->xnet->absent_list, $globals->xnet->evts_domain, 'event');
9ff5b337 303 }
5070a22d 304
305 return $new;
306 }
307
308 if ($old && !$new) {
309 // if we delete the old short name, delete the lists
7852229b
SJ
310 foreach (explode(',', $globals->xnet->event_lists) as $suffix) {
311 delete_list_alias($old . $suffix, $globals->xnet->evts_domain);
5070a22d 312 }
7852229b 313
5070a22d 314 return $new;
315 }
316
317 // cannot happen
318 return $old;
319}
20c5c7e6
SJ
320// }}}
321
322// {{{ function make_event_date()
323function make_event_date(&$e)
324{
325 $start = strtotime($e['debut']);
326 $end = strtotime($e['fin']);
07eb5b0e 327 $first_day = @strtotime($e['first_day']);
20c5c7e6
SJ
328 $last_day = strtotime($e['last_day']);
329 unset($e['debut'], $e['fin'], $e['first_day'], $e['last_day']);
330
331 $date = "";
332 if ($start && $end != $start) {
333 if ($first_day == $last_day) {
334 $date .= "le " . strftime("%d %B %Y", $start) . " de "
335 . strftime("%H:%M", $start) . " à " . strftime("%H:%M", $end);
336 } else {
337 $date .= "du " . strftime("%d %B %Y à %H:%M", $start)
338 . "\nau " . strftime("%d %B %Y à %H:%M", $end);
339 }
340 } else {
341 $date .= "le " . strftime("%d %B %Y à %H:%M", $start);
342 }
343 $e['date'] = $date;
344}
345// }}}
5070a22d 346
a7de4ef7 347// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 348?>