I should have merged before last commit...
[platal.git] / modules / search.php
index 7c2ed2e..fc2b811 100644 (file)
@@ -213,12 +213,12 @@ class SearchModule extends PLModule
         $page->assign('public_directory',0);
     }
 
-    private function format_autocomplete(array $item)
+    static public function format_autocomplete(array $item)
     {
         return $item['field'] . ' (' . $item['nb'] . ' camarade' . ($item['nb'] > 1 ? 's' : '') . ')';
     }
 
-    function handler_autocomplete($page, $type = null)
+    function handler_autocomplete($page, $type = null, $sub_id = null)
     {
         // Autocompletion : according to type required, return
         // a list of results matching with the number of matches.
@@ -237,13 +237,18 @@ class SearchModule extends PLModule
         if (!$q) {
             exit();
         }
+        if (!is_null($sub_id)) {
+            $query = $q . "\t" . $sub_id;
+        } else {
+            $query = $q;
+        }
 
         // 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);
+                             $type, $query);
 
         if ($cache->numRows() > 0) {
             $cached = true;
@@ -257,7 +262,7 @@ class SearchModule extends PLModule
                         'nb'    => $aux[1],
                         'id'    => $aux[2]
                     );
-                    $item['value'] = $this->format_autocomplete($item);
+                    $item['value'] = self::format_autocomplete($item);
                     array_push($list, $item);
                 }
             }
@@ -279,11 +284,15 @@ class SearchModule extends PLModule
                 exit();
             }
 
-            $list = DirEnum::getAutoComplete($enums[$type], $q);
+            if (is_null($sub_id)) {
+                $list = DirEnum::getAutoComplete($enums[$type], $q);
+            } else {
+                $list = DirEnum::getAutoComplete($enums[$type], $q, $sub_id);
+            }
             $to_cache = '';
             foreach ($list as &$item) {
                 $to_cache .= $item['field'] . "\t" . $item['nb'] . "\t" . $item['id'] . "\n";
-                $item['value'] = $this->format_autocomplete($item);
+                $item['value'] = self::format_autocomplete($item);
             }
         }
 
@@ -308,7 +317,7 @@ class SearchModule extends PLModule
             XDB::query('INSERT INTO  search_autocomplete (name, query, result, generated)
                              VALUES  ({?}, {?}, {?}, NOW())
             ON DUPLICATE KEY UPDATE  result = VALUES(result), generated = VALUES(generated)',
-                       $type, $q, $to_cache);
+                       $type, $query, $to_cache);
         }
         echo json_encode($list);
         exit();
@@ -405,6 +414,7 @@ class SearchModule extends PLModule
         $wp->buildCache();
 
         $page->setTitle('Emploi et Carrières');
+        $page->addJsLink('jquery.ui.xorg.js');
 
         // Count mentors
         $res = XDB::query("SELECT count(distinct pid) FROM profile_mentor_term");