Merge branch 'platal-0.10.0'
[platal.git] / modules / xnetevents / xnetevents.inc.php
index 9c0ad58..fd17320 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2007 Polytechnique.org                              *
+ *  Copyright (C) 2003-2009 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -31,7 +31,7 @@ function get_event_detail($eid, $item_id = false, $asso_id = null)
         "SELECT SUM(nb) AS nb_tot, COUNT(DISTINCT ep.uid) AS nb, e.*,
                 IF(e.deadline_inscription, e.deadline_inscription >= LEFT(NOW(), 10),
                    1) AS inscr_open,
-                LEFT(10, e.debut) AS debut_day, LEFT(10, e.fin) AS fin_day,
+                LEFT(10, e.debut) AS first_day, LEFT(10, e.fin) AS last_day,
                 LEFT(NOW(), 10) AS now,
                 ei.titre,
                 al.vid AS absent_list, pl.vid AS participant_list,
@@ -51,9 +51,12 @@ function get_event_detail($eid, $item_id = false, $asso_id = null)
 
     $evt = $res->fetchOneAssoc();
 
-    if (!$evt || ($GLOBALS['IS_XNET_SITE'] && $evt['accept_nonmembre'] == 0 && !is_member() && !may_update())) {
+    if (!$evt) {
         return null;
     }
+    if ($GLOBALS['IS_XNET_SITE'] && $evt['accept_nonmembre'] == 0 && !is_member() && !may_update()) {
+        return false;
+    }
 
     // smart calculation of the total number
     if (!$item_id) {
@@ -104,13 +107,15 @@ function get_event_detail($eid, $item_id = false, $asso_id = null)
         $evt['telepaid'] += trim($p);
     }
 
+    make_event_date($evt);
+
     return $evt;
 }
 
 // }}}
 
 // {{{ function get_event_participants()
-function get_event_participants($evt, $item_id, $tri, $limit = '') {
+function get_event_participants(&$evt, $item_id, $tri, $limit = '') {
     global $globals;
 
     if (Env::has('initiale')) {
@@ -155,6 +160,10 @@ function get_event_participants($evt, $item_id, $tri, $limit = '') {
     $tab = array();
     $user = 0;
 
+    $evt['adminpaid'] = 0;
+    $evt['telepaid']  = 0;
+    $evt['topay']     = 0;
+    $evt['paid']      = 0;
     while ($u = $res->next()) {
         if ($u['nb'] == 0) {
             continue;
@@ -169,8 +178,8 @@ function get_event_participants($evt, $item_id, $tri, $limit = '') {
                 $pay_id, $u['uid']);
             $montants = $res_->fetchColumn();
             foreach ($montants as $m) {
-                    $p = strtr(substr($m, 0, strpos($m, "EUR")), ",", ".");
-                    $u['paid'] += trim($p);
+                $p = strtr(substr($m, 0, strpos($m, "EUR")), ",", ".");
+                $u['paid'] += trim($p);
             }
         }
         $u['telepayment'] = $u['paid'] - $u['adminpaid'];
@@ -185,6 +194,10 @@ function get_event_participants($evt, $item_id, $tri, $limit = '') {
             $u['montant'] += $i['montant']*$i['nb'];
         }
         $tab[] = $u;
+        $evt['telepaid']  += $u['telepayment'];
+        $evt['adminpaid'] += $u['adminpaid'];
+        $evt['paid']      += $u['paid'];
+        $evt['topay']     += $u['montant'];
     }
     return $tab;
 }
@@ -193,21 +206,20 @@ function get_event_participants($evt, $item_id, $tri, $limit = '') {
 //  {{{ function subscribe_lists_event()
 function subscribe_lists_event($participate, $uid, $evt)
 {
-    require_once('user.func.inc.php');
-    global $globals,$page;
+    global $globals;
+    $page =& Platal::page();
 
     $participant_list = $evt['participant_list'];
     $absent_list      = $evt['absent_list'];
 
-    $email = get_user_forlife($uid, '_silent_user_callback');
-
-    if ($email) {
-        $email .= '@'.$globals->mail->domain;
+    $user = User::getSilent($uid);
+    if ($user) {
+        $email = $user->forlifeEmail();
     } else {
         $res = XDB::query("SELECT email
                              FROM groupex.membres
                             WHERE uid = {?} AND asso_id = {?}",
-                            S::v('uid'), $globals->asso('id'));
+                          $uid, $globals->asso('id'));
         $email = $res->fetchOneCell();
     }
 
@@ -242,7 +254,8 @@ function subscribe_lists_event($participate, $uid, $evt)
 }
 // }}}
 
-function event_change_shortname(&$page, $old, $new)
+//  {{{ function event_change_shortname()
+function event_change_shortname(&$page, $eid, $old, $new)
 {
     global $globals;
 
@@ -251,11 +264,16 @@ function event_change_shortname(&$page, $old, $new)
     }
     // Quelques vérifications sur l'alias (caractères spéciaux)
     if ($new && !preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $new)) {
-        $page->trig("Le raccourci demandé n'est pas valide.
+        $page->trigError("Le raccourci demandé n'est pas valide.
                     Vérifie qu'il comporte entre 3 et 20 caractères
                     et qu'il ne contient que des lettres non accentuées,
                     des chiffres ou les caractères - et .");
         return $old;
+    } elseif ($new && ctype_digit($new)) {
+        $page->trigError("Le raccourci demandé ne peut être accepté car il
+                         ne contient que des chiffres. Rajoute-lui par exemple
+                         une lettre.");
+        return $old;
     }
 
     //vérifier que l'alias n'est pas déja pris
@@ -265,7 +283,7 @@ function event_change_shortname(&$page, $old, $new)
                             WHERE short_name = {?}',
                            $new);
         if ($res->fetchOneCell() > 0) {
-            $page->trig("Le raccourci demandé est déjà utilisé. Choisis en un autre.");
+            $page->trigError("Le raccourci demandé est déjà utilisé. Choisis en un autre.");
             return $old;
         }
     }
@@ -293,13 +311,13 @@ function event_change_shortname(&$page, $old, $new)
 
         $lastid = XDB::insertId();
         XDB::execute(
-          "INSERT INTO virtual_redirect (
+          "INSERT IGNORE INTO virtual_redirect (
                 SELECT {?} AS vid, IF(u.nom IS NULL, m.email, CONCAT(a.alias, {?})) AS redirect
                   FROM groupex.evenements_participants AS ep
              LEFT JOIN groupex.membres AS m ON (ep.uid = m.uid)
              LEFT JOIN auth_user_md5   AS u ON (u.user_id = ep.uid)
              LEFT JOIN aliases         AS a ON (a.id = ep.uid AND a.type = 'a_vie')
-                 WHERE ep.eid = {?}
+                 WHERE ep.eid = {?} AND ep.nb > 0
               GROUP BY ep.uid)",
               $lastid, '@'.$globals->mail->domain, $eid);
 
@@ -307,15 +325,15 @@ function event_change_shortname(&$page, $old, $new)
                 $new.'-absents@'.$globals->xnet->evts_domain);
 
         $lastid = XDB::insertId();
-        XDB::execute("INSERT INTO virtual_redirect (
-            SELECT {?} AS vid, IF(u.nom IS NULL, m.email, CONCAT(a.alias, {?})) AS redirect
+        XDB::execute("INSERT IGNORE INTO virtual_redirect (
+            SELECT {?} AS vid, IF(a.alias IS NULL, m.email, CONCAT(a.alias, {?})) AS redirect
                   FROM groupex.membres AS m
-             LEFT JOIN groupex.evenements_participants AS ep ON (ep.uid = m.uid)
+             LEFT JOIN groupex.evenements_participants AS ep ON (ep.uid = m.uid AND ep.eid = {?})
              LEFT JOIN auth_user_md5   AS u ON (u.user_id = m.uid)
              LEFT JOIN aliases         AS a ON (a.id = m.uid AND a.type = 'a_vie')
                  WHERE m.asso_id = {?} AND ep.uid IS NULL
               GROUP BY m.uid)",
-             $lastid, "@".$globals->mail->domain, $globals->asso('id'));
+             $lastid, "@".$globals->mail->domain, $eid, $globals->asso('id'));
 
         return $new;
     }
@@ -335,6 +353,32 @@ function event_change_shortname(&$page, $old, $new)
     // cannot happen
     return $old;
 }
+// }}}
+
+//  {{{ function make_event_date()
+function make_event_date(&$e)
+{
+    $start     = strtotime($e['debut']);
+    $end       = strtotime($e['fin']);
+    $first_day = strtotime($e['first_day']);
+    $last_day  = strtotime($e['last_day']);
+    unset($e['debut'], $e['fin'], $e['first_day'], $e['last_day']);
+
+    $date = "";
+    if ($start && $end != $start) {
+        if ($first_day == $last_day) {
+          $date .= "le " . strftime("%d %B %Y", $start) . " de "
+                . strftime("%H:%M", $start) . " à " . strftime("%H:%M", $end);
+        } else {
+          $date .= "du " . strftime("%d %B %Y à %H:%M", $start)
+                . "\nau " . strftime("%d %B %Y à %H:%M", $end);
+        }
+    } else {
+        $date .= "le " . strftime("%d %B %Y à %H:%M", $start);
+    }
+    $e['date'] = $date;
+}
+// }}}
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>