When adding a new address to a profile, tick the "can receive mail" checkbox by default
[platal.git] / modules / profile.php
index e200bd5..7f2136a 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -135,10 +135,10 @@ class ProfileModule extends PLModule
             if (!$user->hasProfile()) {
                 return PL_NOT_FOUND;
             } else {
-                $profile = $user->profile();
+                $profile = $user->profile(false,0,Visibility::get(Visibility::VIEW_ADMIN));
             }
         } else {
-            $profile = Profile::get($hrpid);
+            $profile = Profile::get($hrpid,0,Visibility::get(Visibility::VIEW_ADMIN));
         }
 
         if (!$profile) {
@@ -224,6 +224,8 @@ class ProfileModule extends PLModule
         }
 
         // Determines the access level at which the profile will be displayed.
+        // Note: VIEW_HIDDEN can NOT be selected. The admins who want to read
+        // information need to use the "edit profile" pages instead.
         if (Env::v('view') == 'public') {
             $view = Visibility::VIEW_PUBLIC;
         } else if (Env::v('view') == 'ax') {
@@ -294,7 +296,7 @@ class ProfileModule extends PLModule
         if (!$user->ax_id) {
             $page->kill("Le matricule AX de {$user->hrid()} est inconnu");
         }
-        http_redirect("http://www.polytechniciens.com/?page=AX_FICHE_ANCIEN&ancc_id=" . $user->ax_id);
+        http_redirect("http://kx.polytechniciens.com/?page=AX_FICHE_ANCIEN&ancc_id=" . $user->ax_id);
     }
 
     function handler_p_edit($page, $hrpid = null, $opened_tab = null, $mode = null, $success = null)
@@ -318,6 +320,7 @@ class ProfileModule extends PLModule
         }
 
         // Build the page
+        $page->addJsLink('jquery.ui.xorg.js');
         $page->addJsLink('education.js', true, false); /* dynamic content */
         $page->addJsLink('grades.js', true, false);    /* dynamic content */
         $page->addJsLink('profile.js');
@@ -328,14 +331,15 @@ class ProfileModule extends PLModule
         $wiz->addPage('ProfilePageGeneral', 'Général', 'general');
         $wiz->addPage('ProfilePageAddresses', 'Adresses personnelles', 'adresses');
         $wiz->addPage('ProfilePageJobs', 'Informations professionnelles', 'emploi');
-        if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
+        $viewPrivate = S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE);
+        if ($viewPrivate) {
             $wiz->addPage('ProfilePageGroups', 'Groupes X - Binets', 'poly');
         }
         $wiz->addPage('ProfilePageDecos', 'Décorations - Medailles', 'deco');
-        if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE)) {
+        if ($viewPrivate) {
             $wiz->addPage('ProfilePageMentor', 'Mentoring', 'mentor');
         }
-        if (S::user()->checkPerms(User::PERM_DIRECTORY_PRIVATE) && $profile->isDeltatenEnabled(Profile::DELTATEN_OLD)) {
+        if ($viewPrivate && $profile->isDeltatenEnabled(Profile::DELTATEN_OLD)) {
             $wiz->addPage('ProfilePageDeltaten', 'Opération N N-10', 'deltaten');
         }
         $wiz->apply($page, 'profile/edit/' . $profile->hrid(), $opened_tab, $mode);
@@ -347,6 +351,8 @@ class ProfileModule extends PLModule
 
        $page->setTitle('Mon Profil');
        $page->assign('hrpid', $profile->hrid());
+       $page->assign('viewPrivate', $viewPrivate);
+       $page->assign('isMe', S::user()->isMyProfile($profile));
        if (isset($success) && $success) {
            $page->trigSuccess('Ton profil a bien été mis à jour.');
        }
@@ -354,7 +360,7 @@ class ProfileModule extends PLModule
 
     function handler_education_js($page)
     {
-        pl_cached_content_headers("text/javascript", "utf-8");
+        pl_cached_dynamic_content_headers('text/javascript', 'utf-8');
         $page->changeTpl('profile/education.js.tpl', NO_SKIN);
         require_once 'education.func.inc.php';
     }
@@ -387,7 +393,7 @@ class ProfileModule extends PLModule
         pl_content_headers("text/html");
         $page->changeTpl('profile/adresses.address.tpl', NO_SKIN);
         $page->assign('i', $id);
-        $page->assign('address', array());
+        $page->assign('address', array('mail' => true));
         $page->assign('profile', Profile::get($pid));
         $page->assign('isMe', true);
         $page->assign('geocoding_removal', true);
@@ -512,6 +518,11 @@ class ProfileModule extends PLModule
             return PL_NOT_FOUND;
         }
 
+        // Referent view are logged.
+        if (S::logged()) {
+            S::logger()->log('view_referent', $pf->hrid());
+        }
+
         $page->assign_by_ref('profile', $pf);
 
         // Retrieves referents' countries.
@@ -560,7 +571,7 @@ class ProfileModule extends PLModule
     {
         pl_content_headers("text/plain");
 
-        $q = Env::v('q').'%';
+        $q = Env::v('term') . '%';
         $tokens = JobTerms::tokenize($q);
         if (count($tokens) == 0) {
             exit;
@@ -568,51 +579,79 @@ class ProfileModule extends PLModule
         sort($tokens);
         $q_normalized = implode(' ', $tokens);
 
-        // try to look in cached results
+        // Try to look in cached results.
+        $cached = false;
         $cache = XDB::query('SELECT  result
                                FROM  search_autocomplete
-                              WHERE  name = {?} AND
-                                     query = {?} AND
-                                     generated > NOW() - INTERVAL 1 DAY',
-                            $type, $q_normalized);
-        if ($res = $cache->fetchOneCell()) {
-            echo $res;
-            die();
-        }
-
-        $joins = JobTerms::token_join_query($tokens, 'e');
-        if ($type == 'mentor') {
-            $count = ', COUNT(DISTINCT pid) AS nb';
-            $countjoin = ' INNER JOIN  profile_job_term_relation AS r ON(r.jtid_1 = e.jtid) INNER JOIN  profile_mentor_term AS m ON(r.jtid_2 = m.jtid)';
-            $countorder = 'nb DESC, ';
+                              WHERE  name = {?} AND query = {?} AND generated > NOW() - INTERVAL 1 DAY',
+                             $type, $q_normalized);
+
+        if ($cache->numRows() > 0) {
+            $cached = true;
+            $data = explode("\n", $cache->fetchOneCell());
+            $list = array();
+            foreach ($data as $line) {
+                if ($line != '') {
+                    $aux = explode("\t", $line);
+                    if ($type == 'mentor') {
+                        $item = array(
+                            'field' => $aux[0],
+                            'nb'    => $aux[1],
+                            'id'    => $aux[2]
+                        );
+                        $item['value'] = SearchModule::format_autocomplete($item);
+                    } else {
+                        $item = array(
+                            'value' => $aux[0],
+                            'id'    => $aux[1]
+                        );
+                    }
+                    array_push($list, $item);
+                }
+            }
         } else {
-            $count = $countjoin = $countorder = '';
-        }
-        $list = XDB::iterator('SELECT  e.jtid AS id, e.full_name AS field'.$count.'
-                                 FROM  profile_job_term_enum AS e '.$joins.$countjoin.'
-                             GROUP BY  e.jtid
-                             ORDER BY  '.$countorder.'field
-                                LIMIT  11');
-        $nbResults = 0;
-        $res = '';
-        while ($result = $list->next()) {
-            $nbResults++;
-            if ($nbResults == 11) {
-                $res .= $q."|-1\n";
+            $joins = JobTerms::token_join_query($tokens, 'e');
+            if ($type == 'mentor') {
+                $count = ', COUNT(DISTINCT pid) AS nb';
+                $countjoin = ' INNER JOIN  profile_job_term_relation AS r ON(r.jtid_1 = e.jtid) INNER JOIN  profile_mentor_term AS m ON(r.jtid_2 = m.jtid)';
+                $countorder = 'nb DESC, ';
             } else {
-                $res .= $result['field'].'|';
-                if ($count) {
-                    $res .= $result['nb'].'|';
+                $count = $countjoin = $countorder = '';
+            }
+            $list = XDB::fetchAllAssoc('SELECT  e.jtid AS id, e.full_name AS field' . $count . '
+                                          FROM  profile_job_term_enum AS e ' . $joins . $countjoin . '
+                                      GROUP BY  e.jtid
+                                      ORDER BY  ' . $countorder . 'field
+                                         LIMIT  ' . DirEnumeration::AUTOCOMPLETE_LIMIT);
+            $to_cache = '';
+            if ($type == 'mentor') {
+                foreach ($list as &$item) {
+                    $to_cache .= $item['field'] . "\t" . $item['nb'] . "\t" . $item['id'] . "\n";
+                    $item['value'] = SearchModule::format_autocomplete($item);
+                }
+            } else {
+                foreach ($list as &$item) {
+                    $to_cache .= $item['field'] . "\t" . $item['id'] . "\n";
+                    $item['value'] = $item['field'];
                 }
-                $res .= $result['id'];
             }
-            $res .= "\n";
         }
-        XDB::query('INSERT INTO  search_autocomplete (name, query, result, generated)
-                         VALUES  ({?}, {?}, {?}, NOW())
-        ON DUPLICATE KEY UPDATE  result = VALUES(result), generated = VALUES(generated)',
-                    $type, $q_normalized, $res);
-        echo $res;
+
+        if (count($list) == DirEnumeration::AUTOCOMPLETE_LIMIT && $type == 'nomentor') {
+            $list[] = array(
+                'value' => '… parcourir les résultats dans un arbre …',
+                'field' => '',
+                'id'    => -1
+            );
+        }
+
+        if (!$cached) {
+            XDB::query('INSERT INTO  search_autocomplete (name, query, result, generated)
+                             VALUES  ({?}, {?}, {?}, NOW())
+            ON DUPLICATE KEY UPDATE  result = VALUES(result), generated = VALUES(generated)',
+                       $type, $q_normalized, $to_cache);
+        }
+        echo json_encode($list);
         exit();
     }
 
@@ -832,5 +871,5 @@ class ProfileModule extends PLModule
     }
 }
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>