Small fix
[platal.git] / modules / xnetevents.php
index 29f9b07..977ea1f 100644 (file)
@@ -29,33 +29,52 @@ class XnetEventsModule extends PLModule
             '%grp/events'       => $this->make_hook('events',  AUTH_MDP),
             '%grp/events/sub'   => $this->make_hook('sub',     AUTH_MDP),
             '%grp/events/csv'   => $this->make_hook('csv',     AUTH_MDP),
+            '%grp/events/ical'  => $this->make_hook('ical',    AUTH_MDP),
             '%grp/events/edit'  => $this->make_hook('edit',    AUTH_MDP),
             '%grp/events/admin' => $this->make_hook('admin',   AUTH_MDP),
         );
     }
 
-    function handler_events(&$page)
+    function handler_events(&$page, $archive = null)
     {
         global $globals;
 
-        new_group_page('xnetevents/index.tpl');
+        if ($archive == 'archive') {
+            $archive = true;
+            new_groupadmin_page('xnetevents/index.tpl');
+        } else {
+            $archive = false;
+            new_group_open_page('xnetevents/index.tpl');
+        }
 
+        $action = null;
         if (Post::has('del')) {
+            $action = 'del';
+            $eid = Post::v('del');
+        } elseif (Post::has('archive')) {
+            $action = 'archive';
+            $eid = Post::v('archive');
+        } elseif (Post::has('unarchive')) {
+            $action = 'unarchive';
+            $eid = Post::v('unarchive');
+        }
+
+        if (!is_null($action)) {
             if (!may_update()) {
                 return PL_NOT_ALLOWED;
             }
 
-            $eid = Post::v('del');
-
             $res = XDB::query("SELECT asso_id, short_name FROM groupex.evenements
-                                        WHERE eid = {?} AND asso_id = {?}",
-                                        $eid, $globals->asso('id'));
+                                WHERE eid = {?} AND asso_id = {?}",
+                              $eid, $globals->asso('id'));
 
             $tmp = $res->fetchOneRow();
             if (!$tmp) {
                 return PL_NOT_ALLOWED;
             }
+        }
 
+        if ($action == 'del') {
             // deletes the event mailing aliases
             if ($tmp[1]) {
                 XDB::execute(
@@ -85,6 +104,21 @@ class XnetEventsModule extends PLModule
                                    PayReq::same_event($eid, $globals->asso('id')));
         }
 
+        if ($action == 'archive') {
+            XDB::execute("UPDATE groupex.evenements
+                             SET archive = 1
+                           WHERE eid = {?} AND asso_id = {?}",
+                         $eid, $globals->asso('id'));
+        }
+
+        if ($action == 'unarchive') {
+            XDB::execute("UPDATE groupex.evenements
+                             SET archive = 0
+                           WHERE eid = {?} AND asso_id = {?}",
+                         $eid, $globals->asso('id'));
+        }
+
+        $page->assign('archive', $archive);
         $page->assign('admin', may_update());
 
         $evenements = XDB::iterator(
@@ -98,12 +132,15 @@ class XnetEventsModule extends PLModule
             INNER JOIN  x4dat.aliases       AS a ON (a.type = 'a_vie' AND a.id = u.user_id)
              LEFT JOIN  groupex.evenements_participants AS ep ON (ep.eid = e.eid AND ep.uid = {?})
                  WHERE  asso_id = {?}
-              GROUP BY  e.eid
-              ORDER BY  debut", S::v('uid'), $globals->asso('id'));
+                   AND  archive = " . ($archive ? "1 " : "0 ")
+            . (is_member() || may_update() ? "" : " AND accept_nonmembre != 0 ")
+              . "GROUP BY  e.eid
+                 ORDER BY  inscr_open DESC, debut DESC", S::v('uid'), $globals->asso('id'));
 
         $evts = array();
 
         while ($e = $evenements->next()) {
+            $e['show_participants'] = ($e['show_participants'] && (is_member() || may_update()));
             $res = XDB::query(
                 "SELECT titre, details, montant, ei.item_id, nb
                    FROM groupex.evenements_items AS ei
@@ -140,7 +177,7 @@ class XnetEventsModule extends PLModule
     {
         require_once dirname(__FILE__).'/xnetevents/xnetevents.inc.php';
 
-        new_group_page('xnetevents/subscribe.tpl');
+        new_group_open_page('xnetevents/subscribe.tpl');
 
         $evt = get_event_detail($eid);
         if (!$evt) {
@@ -150,6 +187,9 @@ class XnetEventsModule extends PLModule
         if (!$evt['inscr_open']) {
             $page->kill('Les inscriptions pour cet événement sont closes');
         }
+        if (!$evt['accept_nonmembre'] && !is_member() && !may_update()) {
+            $page->kill('Cet événement est fermé aux non-membres du groupe');
+        }
 
         $page->assign('event', $evt);
 
@@ -238,6 +278,38 @@ class XnetEventsModule extends PLModule
         $page->assign('tout', !Env::v('item_id', false));
     }
 
+    function handler_ical(&$page, $eid = null)
+    {
+        global $globals;
+
+        require_once dirname(__FILE__).'/xnetevents/xnetevents.inc.php';
+        $evt = get_event_detail($eid);
+        if (!$evt) {
+            return PL_NOT_FOUND;
+        }
+        $evt['debut'] = preg_replace('/(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/', "\\1\\2\\3T\\4\\5\\6", $evt['debut']);
+        $evt['fin'] = preg_replace('/(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/', "\\1\\2\\3T\\4\\5\\6", $evt['fin']);
+
+        foreach ($evt['moments'] as $m) {
+            $evt['descriptif'] .= "\n\n** " . $m['titre'] . " **\n" . $m['details'];
+        }
+
+        $page->changeTpl('xnetevents/calendar.tpl', NO_SKIN);
+
+        require_once('ical.inc.php');
+        $page->assign('asso', $globals->asso());
+        $page->assign('timestamp', time());
+        $page->assign('admin', may_update());
+
+        if (may_update()) {
+            $page->assign('participants', get_event_participants($evt, null, 'promo, nom, prenom'));
+        }
+        $page->register_function('display_ical', 'display_ical');
+        $page->assign_by_ref('e', $evt);
+    
+        header('Content-Type: text/calendar; charset=utf-8');
+    }
+
     function handler_edit(&$page, $eid = null)
     {
         global $globals;
@@ -266,7 +338,6 @@ class XnetEventsModule extends PLModule
             $evt = array(
                 'eid'              => $eid,
                 'asso_id'          => $globals->asso('id'),
-                'organisateur_uid' => S::v('uid'),
                 'paiement_id'      => Post::v('paiement_id') > 0 ? Post::v('paiement_id') : null,
                 'debut'            => Post::v('deb_Year').'-'.Post::v('deb_Month')
                                       .'-'.Post::v('deb_Day').' '.Post::v('deb_Hour')
@@ -278,10 +349,13 @@ class XnetEventsModule extends PLModule
             );
 
             $trivial = array('intitule', 'descriptif', 'noinvite',
-                             'show_participants');
+                             'show_participants', 'accept_nonmembre', 'organisateur_uid');
             foreach ($trivial as $k) {
                 $evt[$k] = Post::v($k);
             }
+            if (!$eid) {
+                $evt['organisateur_uid'] = S::v('uid');
+            }
 
             if (Post::v('deadline')) {
                 $evt['deadline_inscription'] = Post::v('inscr_Year').'-'
@@ -296,12 +370,13 @@ class XnetEventsModule extends PLModule
                 SET eid={?}, asso_id={?}, organisateur_uid={?}, intitule={?},
                     paiement_id = {?}, descriptif = {?}, debut = {?},
                     fin = {?}, show_participants = {?}, short_name = {?},
-                    deadline_inscription = {?}, noinvite = {?}',
+                    deadline_inscription = {?}, noinvite = {?},
+                    accept_nonmembre = {?}',
                     $evt['eid'], $evt['asso_id'], $evt['organisateur_uid'],
                     $evt['intitule'], $evt['paiement_id'], $evt['descriptif'],
                     $evt['debut'], $evt['fin'], $evt['show_participants'],
                     $evt['short_name'], $evt['deadline_inscription'],
-                    $evt['noinvite']);
+                    $evt['noinvite'], $evt['accept_nonmembre']);
 
             // if new event, get its id
             if (!$eid) {
@@ -346,7 +421,7 @@ class XnetEventsModule extends PLModule
             }
 
             if (is_null($evt['eid'])) {
-                pl_redirect('events/'.$eid);
+                pl_redirect('events');
             }
         }
 
@@ -362,9 +437,9 @@ class XnetEventsModule extends PLModule
         // when modifying an old event retreive the old datas
         if ($eid) {
             $res = XDB::query(
-                    "SELECT    eid, intitule, descriptif, debut, fin,
-                                show_participants, paiement_id, short_name,
-                                deadline_inscription, noinvite
+                    "SELECT    eid, intitule, descriptif, debut, fin, organisateur_uid,
+                            show_participants, paiement_id, short_name,
+                            deadline_inscription, noinvite, accept_nonmembre
                        FROM    groupex.evenements
                       WHERE eid = {?}", $eid);
             $evt = $res->fetchOneAssoc();