autcomplete for city and poste too
[platal.git] / modules / search.php
index 096b61e..a0b7a62 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2006 Polytechnique.org                              *
+ *  Copyright (C) 2003-2007 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -26,7 +26,10 @@ class SearchModule extends PLModule
         return array(
             'search'     => $this->make_hook('quick', AUTH_PUBLIC),
             'search/adv' => $this->make_hook('advanced', AUTH_COOKIE),
+            'search/ajax/region'  => $this->make_hook('region', AUTH_COOKIE, 'user', NO_AUTH),
+            'search/ajax/grade'   => $this->make_hook('grade',  AUTH_COOKIE, 'user', NO_AUTH),
             'advanced_search.php' => $this->make_hook('redir_advanced', AUTH_PUBLIC),
+            'search/autocomplete' => $this->make_hook('autocomplete', AUTH_PUBLIC),
         );
     }
 
@@ -45,11 +48,14 @@ class SearchModule extends PLModule
     function get_quick($offset, $limit, $order)
     {
         global $globals;
+        if (!S::logged()) {
+            Env::kill('with_soundex');
+        }
         $qSearch = new QuickSearch('quick');
         $fields  = new SFieldGroup(true, array($qSearch));
 
         if ($qSearch->isempty()) {
-            new ThrowError('Recherche trop générale.');
+            new ThrowError('Recherche trop générale.');
         }
 
         $sql = 'SELECT SQL_CALC_FOUND_ROWS
@@ -88,8 +94,11 @@ class SearchModule extends PLModule
 
         $page->assign('formulaire',1);
         $page->assign('choix_nats',
-                      XDB::iterator('SELECT a2 AS id,IF(nat=\'\',pays,nat) AS text
-                                                 FROM geoloc_pays ORDER BY text'));
+                      XDB::iterator('SELECT  g.a2 AS id, IF(nat=\'\', g.pays, g.nat) AS text
+                                       FROM  geoloc_pays AS g
+                                 INNER JOIN  auth_user_md5 AS u ON (u.nationalite = g.a2)
+                                   GROUP BY  g.a2
+                                   ORDER BY  text'));
         $page->assign('choix_postes',
                       XDB::iterator('SELECT id,fonction_fr FROM fonctions_def
                                              ORDER BY fonction_fr'));
@@ -103,21 +112,30 @@ class SearchModule extends PLModule
                       XDB::iterator('SELECT id,text FROM applis_def ORDER BY text'));
         $page->assign('choix_secteurs',
                       XDB::iterator('SELECT id,label FROM emploi_secteur ORDER BY label'));
+        $this->get_diplomas();
+    }
+
+    function get_diplomas($school = null)
+    {
+        if (is_null($school) && Env::has('school')) {
+            $school = Env::i('school');
+        }
 
-        if (Env::has('school')) {
-            $sql = 'SELECT type FROM applis_def WHERE id='.Env::i('school');
+        if (!is_null($school)) {
+            $sql = 'SELECT type FROM applis_def WHERE id=' . $school;
         } else {
             $sql = 'DESCRIBE applis_def type';
         }
 
         $res = XDB::query($sql);
         $row = $res->fetchOneRow();
-        if (Env::has('school')) {
+        if (!is_null($school)) {
             $types = $row[0];
         } else {
             $types = explode('(',$row[1]);
             $types = str_replace("'","",substr($types[1],0,-1));
         }
+        global $page;
         $page->assign('choix_diplomas', explode(',',$types));
     }
 
@@ -126,7 +144,7 @@ class SearchModule extends PLModule
         $fields = new SFieldGroup(true, advancedSearchFromInput());
         if ($fields->too_large()) {
             $this->form_prepare();
-            new ThrowError('Recherche trop générale.');
+            new ThrowError('Recherche trop générale.');
         }
         global $globals, $page;
 
@@ -167,7 +185,7 @@ class SearchModule extends PLModule
     {
         global $globals;
 
-        require_once 'search.inc.php';
+        require_once dirname(__FILE__).'/search/search.inc.php';
 
         $page->changeTpl('search/index.tpl');
 
@@ -187,14 +205,21 @@ class SearchModule extends PLModule
             $nb_tot = $search->show();
 
             if (!S::logged() && $nb_tot > $globals->search->public_max) {
-                new ThrowError('Votre recherche a généré trop de résultats pour un affichage public.');
+                new ThrowError('Votre recherche a généré trop de résultats pour un affichage public.');
             } elseif ($nb_tot > $globals->search->private_max) {
-                new ThrowError('Recherche trop générale');
+                new ThrowError('Recherche trop générale');
             } elseif (empty($nb_tot)) {
-                new ThrowError('il n\'existe personne correspondant à ces critères dans la base !');
+                new ThrowError('il n\'existe personne correspondant à ces critères dans la base !');
             }
         } else {
+            $res = XDB::query("SELECT  MIN(diminutif), MAX(diminutif)
+                                 FROM  groupex.asso
+                                WHERE  cat = 'Promotions'");
+            list($min, $max) = $res->fetchOneRow();
+            $page->assign('promo_min', $min);
+            $page->assign('promo_max', $max); 
             $page->assign('formulaire',1);
+            $page->addJsLink('ajax.js');
         }
 
         $page->register_modifier('display_lines', 'display_lines');
@@ -204,7 +229,7 @@ class SearchModule extends PLModule
     {
         global $globals;
 
-        require_once 'search.inc.php' ;
+        require_once dirname(__FILE__).'/search/search.inc.php';
         require_once 'applis.func.inc.php';
         require_once 'geoloc.inc.php';
 
@@ -217,26 +242,80 @@ class SearchModule extends PLModule
         if (!Env::has('rechercher')) {
             $this->form_prepare();
         } else {
-
             $search = new XOrgSearch(array($this, 'get_advanced'));
             $search->setNbLines($globals->search->per_page);
 
             $page->assign('url_search_form', $search->make_url(Array('rechercher'=>0)));
-            if (Env::has('with_soundex')) {
-                $page->assign('with_soundex', $search->make_url(Array())."&with_soundex=1");
+            if (!Env::i('with_soundex')) {
+                $page->assign('with_soundex', $search->make_url(Array()) . "&with_soundex=1");
             }
-
             $nb_tot = $search->show();
 
             if ($nb_tot > $globals->search->private_max) {
                 $this->form_prepare();
-                new ThrowError('Recherche trop générale');
+                new ThrowError('Recherche trop générale');
             }
 
         }
 
+        $page->addJsLink('ajax.js');
         $page->register_modifier('display_lines', 'display_lines');
     }
+
+    function handler_region(&$page, $country = null)
+    {
+        header('Content-Type: text/html; charset="UTF-8"');
+        require_once("geoloc.inc.php");
+        $page->ChangeTpl('search/adv.region.form.tpl', NO_SKIN);
+        $page->assign('region', "");
+        $page->assign('country', $country);
+    }
+
+    function handler_grade(&$page, $school = null)
+    {
+        header('Content-Type: text/html; charset="UTF-8"');
+        $page->ChangeTpl('search/adv.grade.form.tpl', NO_SKIN);
+        $page->assign('grade', '');
+        $this->get_diplomas($school);
+    }
+
+    function handler_autocomplete(&$page, $type = null)
+    {
+        // Autocompletion : according to type required, return
+        // a list of results matching with the number of matches.
+        // The output format is :
+        //   result1|nb1
+        //   result2|nb2
+        //   ...
+        header('Content-Type: text/plain; charset="UTF-8"');
+        $q = $_REQUEST['q'];
+        if (!$q) exit();
+        $unique = 'user_id';
+        $db = 'auth_user_md5';
+        switch ($type) {
+        case 'firstname': $field = 'prenom'; break;
+        case 'name': $field = 'nom'; break;
+        case 'nickname': $field = 'profile_nick'; $db = 'auth_user_quick'; break;
+        case 'entreprise': $db = 'entreprises'; $field = 'entreprise'; $unique='uid'; break;
+        case 'city': $db = 'geoloc_city INNER JOIN adresses ON(geoloc_city.id = adresses.cityid)'; $unique='uid'; $field='geoloc_city.name'; break;
+        case 'poste': $db = 'entreprises'; $field = 'poste'; $unique='uid'; break;
+        default: exit();
+        }
+
+        $liste = XDB::iterator('SELECT '.$field.' AS field, COUNT(DISTINCT '.$unique.') AS nb FROM '.$db.' WHERE '.$field.' LIKE {?} GROUP BY '.$field.' LIMIT 11', $q.'%');
+        $nbResults = 0;
+        while ($result = $liste->next()) {
+            $nbResults++;
+            if ($nbResults == 11) {
+                echo '...|1'."\n";
+            } else {
+                echo $result['field'].'|'.$result['nb']."\n";
+            }
+        }
+
+        exit();
+    }
 }
 
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>