Fix the carnet
[platal.git] / modules / carnet.php
index e643505..a19a50c 100644 (file)
@@ -29,9 +29,9 @@ class CarnetModule extends PLModule
             'carnet/notifs'         => $this->make_hook('notifs',   AUTH_COOKIE),
 
             'carnet/contacts'       => $this->make_hook('contacts', AUTH_COOKIE),
-            'carnet/contacts/pdf'   => $this->make_hook('pdf',      AUTH_COOKIE, 'user', NO_HTTPS),
+            'carnet/contacts/pdf'   => $this->make_hook('pdf',      AUTH_COOKIE),
+            'carnet/contacts/vcard' => $this->make_hook('vcard',    AUTH_COOKIE),
             'carnet/contacts/ical'  => $this->make_hook('ical',     AUTH_PUBLIC, 'user', NO_HTTPS),
-            'carnet/contacts/vcard' => $this->make_hook('vcard',    AUTH_COOKIE, 'user', NO_HTTPS),
 
             'carnet/rss'            => $this->make_hook('rss',      AUTH_PUBLIC, 'user', NO_HTTPS),
         );
@@ -80,7 +80,7 @@ class CarnetModule extends PLModule
     private function getSinglePromotion(PlPage &$page, $promo)
     {
         if (!ctype_digit($promo) || $promo < 1920 || $promo > date('Y')) {
-            $page->trigError('Promotion invalide : ' . $promo);
+            $page->trigError('Promotion invalide&nbsp;: ' . $promo . '.');
             return null;
         }
         return (int)$promo;
@@ -107,7 +107,7 @@ class CarnetModule extends PLModule
             return null;
         }
         if ($promo1 > $promo2) {
-            $page->trigError("Intervale non valide : " . $promo);
+            $page->trigError('Intervalle non valide :&nbsp;' . $promo . '.');
             return null;
         }
         $array = array();
@@ -254,7 +254,7 @@ class CarnetModule extends PLModule
         $page->setTitle('Mes contacts');
         $this->_add_rss_link($page);
 
-        $uid  = S::v('uid');
+        $uid  = S::i('uid');
         $user = Env::v('user');
 
         // For XSRF protection, checks both the normal xsrf token, and the special RSS token.
@@ -267,7 +267,7 @@ class CarnetModule extends PLModule
                 if (($user = User::get(Env::v('user')))) {
                     if (XDB::execute("DELETE FROM  contacts
                                             WHERE  uid = {?} AND contact = {?}", $uid, $user->id())) {
-                        $page->trigSuccess("Contact retiré !");
+                        $page->trigSuccess("Contact retiré&nbsp;!");
                     }
                 }
                 break;
@@ -276,38 +276,42 @@ class CarnetModule extends PLModule
                 if (($user = User::get(Env::v('user')))) {
                     if (XDB::execute("REPLACE INTO  contacts (uid, contact)
                                             VALUES  ({?}, {?})", $uid, $user->id())) {
-                        $page->trigSuccess('Contact ajouté !');
+                        $page->trigSuccess('Contact ajouté&nbsp;!');
                     } else {
-                        $page->trigWarning('Contact déjà dans la liste !');
+                        $page->trigWarning('Contact déjà dans la liste&nbsp;!');
                     }
                 }
                 break;
         }
 
         $search = false;
+        $user = S::user();
+
+        require_once 'userset.inc.php';
+
         if ($action == 'search') {
             $action = $subaction;
             $subaction = $ssaction;
             $search = true;
         }
         if ($search && trim(Env::v('quick'))) {
-            require_once 'userset.inc.php';
             $base = 'carnet/contacts/search';
 
             Platal::load('search', 'classes.inc.php');
-            ThrowError::$throwHook = array($this, 'searchErrorHandler');
-            $view = new SearchSet(true, false, "INNER JOIN contacts AS c2 ON (u.user_id = c2.contact)", "c2.uid = $uid");
+            $view = new SearchSet(true, false, new UFC_Contact($user));
         } else {
             $base = 'carnet/contacts';
-            $view = new UserSet("INNER JOIN contacts AS c2 ON (u.user_id = c2.contact)", " c2.uid = $uid ");
+            $view = new ProfileSet(new UFC_Contact($user));
         }
+
         $view->addMod('minifiche', 'Mini-fiches', true);
         $view->addMod('trombi', 'Trombinoscope', false, array('with_admin' => false, 'with_promo' => true));
-        $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'carnet/contacts/search'));
-        $view->apply($base, $page, $action, $subaction);
-        if ($action != 'geoloc' || ($search && !$ssaction) || (!$search && !$subaction)) {
-            $page->changeTpl('carnet/mescontacts.tpl');
-        }
+        // TODO: Reactivate when the new map is completed.
+        // $view->addMod('geoloc', 'Planisphère', false, array('with_annu' => 'carnet/contacts/search'));
+        $view->apply('carnet/contacts', $page, $action, $subaction);
+        //if ($action != 'geoloc' || ($search && !$ssaction) || (!$search && !$subaction)) {
+        $page->changeTpl('carnet/mescontacts.tpl');
+        //}
     }
 
     function handler_pdf(&$page, $arg0 = null, $arg1 = null)
@@ -367,35 +371,23 @@ class CarnetModule extends PLModule
         $page->changeTpl('carnet/calendar.tpl', NO_SKIN);
         $page->register_function('display_ical', 'display_ical');
 
-        $res = XDB::iterRow(
-                'SELECT u.prenom,
-                        IF(u.nom_usage = \'\',u.nom,u.nom_usage) AS nom,
-                        u.promo,
-                        u.naissance,
-                        DATE_ADD(u.naissance, INTERVAL 1 DAY) AS end,
-                        u.date_ins,
-                        u.hruid
-                   FROM contacts      AS c
-             INNER JOIN auth_user_md5 AS u ON (u.user_id = c.contact)
-             INNER JOIN aliases       AS a ON (u.user_id = a.id AND a.type = \'a_vie\')
-                  WHERE c.uid = {?}', $user->id());
-
+        $filter = new UserFilter(new UFC_Contact($user));
         $annivs = Array();
-        while (list($prenom, $nom, $promo, $naissance, $end, $ts, $hruid) = $res->next()) {
-            $naissance = str_replace('-', '', $naissance);
-            $end       = str_replace('-', '', $end);
+        foreach ($filter->getUsers() as $u) {
+            $profile = $u->profile();
+            $date = strtotime($profile->birthdate);
+            $tomorrow = $date + 86400;
             $annivs[] = array(
-                'timestamp' => strtotime($ts),
-                'date'      => $naissance,
-                'tomorrow'  => $end,
-                'hruid'     => $hruid,
-                'summary'   => 'Anniversaire de '.$prenom
-                                .' '.$nom.' - x '.$promo,
+                'timestamp' => strtotime($user->registration_date),
+                'date' => date('Ymd', $date),
+                'tomorrow' => date('Ymd', $tomorrow),
+                'hruid' => $profile->hrid(),
+                'summary' => 'Anniversaire de ' . $profile->fullName(true)
             );
         }
         $page->assign('events', $annivs);
 
-        header('Content-Type: text/calendar; charset=utf-8');
+        pl_content_headers("text/calendar");
     }
 
     function handler_vcard(&$page, $photos = null)