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