Table editor fills the new entry form with the default values of the fields
[platal.git] / modules / xnetevents / xnetevents.inc.php
index 1c3b147..ed5794c 100644 (file)
@@ -68,7 +68,7 @@ function get_event_detail($eid, $item_id = false)
     }
 
     $res = XDB::query(
-        "SELECT titre, details, montant, ei.item_id, nb
+        "SELECT titre, details, montant, ei.item_id, nb, ep.paid
            FROM groupex.evenements_items        AS ei
       LEFT JOIN groupex.evenements_participants AS ep
                 ON (ep.eid = ei.eid AND ep.item_id = ei.item_id AND uid = {?})
@@ -77,10 +77,13 @@ function get_event_detail($eid, $item_id = false)
     $evt['moments'] = $res->fetchAllAssoc();
 
     $evt['topay'] = 0;
+    $evt['paid'] = 0;
     foreach ($evt['moments'] as $m) {
         $evt['topay'] += $m['nb'] * $m['montant'];
-        if ($m['montant'])
+        if ($m['montant']) {
             $evt['money'] = true;
+        }
+        $evt['paid']  = $m['paid'];
     }
 
     $req = XDB::query(
@@ -89,7 +92,6 @@ function get_event_detail($eid, $item_id = false)
          WHERE ref = {?} AND uid = {?}", $evt['paiement_id'], S::v('uid'));
     $montants = $req->fetchColumn();
 
-    $evt['paid'] = 0;
     foreach ($montants as $m) {
         $p = strtr(substr($m, 0, strpos($m, 'EUR')), ',', '.');
         $evt['paid'] += trim($p);
@@ -176,36 +178,54 @@ function get_event_participants($evt, $item_id, $tri, $limit = '') {
 // }}}
 
 //  {{{ function subscribe_lists_event()
-function subscribe_lists_event($participate, $uid, $evt) {
+function subscribe_lists_event($participate, $uid, $evt)
+{
+    require_once('user.func.inc.php');
     global $globals,$page;
 
     $participant_list = $evt['participant_list'];
     $absent_list      = $evt['absent_list'];
 
-    $email = S::v('forlife');
+    $email = get_user_forlife($uid);
 
     if ($email) {
         $email .= '@'.$globals->mail->domain;
     } else {
-        $res = XDB::query("SELECT email FROM groupex.membres WHERE uid = {?} AND asso_id = {?}", S::v('uid'), $globals->asso('id'));
+        $res = XDB::query("SELECT email
+                             FROM groupex.membres
+                            WHERE uid = {?} AND asso_id = {?}",
+                            S::v('uid'), $globals->asso('id'));
         $email = $res->fetchOneCell();
     }
 
-    $subscribe = $participate ? $participant_list : (is_member()?$absent_list:0);
-    $unsubscri = $participate ? $absent_list : $participant_list;
-
-    if ($subscribe) {
-        XDB::execute(
-            "REPLACE INTO virtual_redirect VALUES({?},{?})",
-                $subscribe, $email);
+    function subscribe($list, $email)
+    {
+        if ($list && $email) {
+            XDB::execute("REPLACE INTO virtual_redirect
+                                VALUES ({?},{?})",
+                         $list, $email);
+        }
     }
 
-    if ($unsubscri) {
-        XDB::execute(
-            "DELETE FROM virtual_redirect WHERE vid = {?} AND redirect = {?}",
-                $unsubscri, $email);
+    function unsubscribe($list, $email)
+    {
+        if ($list && $email) {
+            XDB::execute("DELETE FROM virtual_redirect
+                                WHERE vid = {?} AND redirect = {?}",
+                         $list, $email);
+        }
     }
 
+    if (is_null($participate)) {
+        unsubscribe($participant_list, $email);
+        subscribe($absent_list, $email);
+    } elseif ($participate) {
+        subscribe($participant_list, $email);
+        unsubscribe($absent_list, $email);
+    } else {
+        unsubscribe($participant_list, $email);
+        unsubscribe($absent_list, $email);
+    }
 }
 // }}}
 
@@ -224,9 +244,10 @@ function event_change_shortname(&$page, $old, $new)
 
     //vérifier que l'alias n'est pas déja pris
     if ($new && $old != $new) {
-        $res = XDB::query('SELECT COUNT(*) FROM virtual
-                                    WHERE alias LIKE {?}',
-                                    $new.'-absents@%');
+        $res = XDB::query('SELECT COUNT(*)
+                             FROM groupex.evenements
+                            WHERE short_name = {?}',
+                           $new);
         if ($res->fetchOneCell() > 0) {
             $page->trig("Le raccourci demandé est déjà utilisé. Choisis en un autre.");
             return $old;
@@ -242,10 +263,9 @@ function event_change_shortname(&$page, $old, $new)
         foreach (array('-absents@', '-participants@') as $v) {
             $v .= $globals->xnet->evts_domain;
             XDB::execute("UPDATE virtual SET alias = {?}
-                                     WHERE type = 'evt' AND alias = {?}",
-                                     $new.$v, $old.$v);
+                           WHERE type = 'evt' AND alias = {?}",
+                         $new.$v, $old.$v);
         }
-
         return $new;
     }
 
@@ -255,7 +275,7 @@ function event_change_shortname(&$page, $old, $new)
         XDB::execute("INSERT INTO virtual SET type = 'evt', alias = {?}",
                 $new.'-participants@'.$globals->xnet->evts_domain);
 
-        $lastid = mysql_insert_id();
+        $lastid = XDB::insertId();
         XDB::execute(
           "INSERT INTO virtual_redirect (
                 SELECT {?} AS vid, IF(u.nom IS NULL, m.email, CONCAT(a.alias, {?})) AS redirect
@@ -270,7 +290,7 @@ function event_change_shortname(&$page, $old, $new)
         XDB::execute("INSERT INTO virtual SET type = 'evt', alias = {?}",
                 $new.'-absents@'.$globals->xnet->evts_domain);
 
-        $lastid = mysql_insert_id();
+        $lastid = XDB::insertId();
         XDB::execute("INSERT INTO virtual_redirect (
             SELECT {?} AS vid, IF(u.nom IS NULL, m.email, CONCAT(a.alias, {?})) AS redirect
                   FROM groupex.membres AS m