Moving to GitHub.
[platal.git] / classes / direnum.php
index 8a3829d..777653b 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   *
@@ -119,16 +119,16 @@ class DirEnum
      * @param $text Text to autocomplete
      * @return PlIterator over the results
      */
-    static public function getAutoComplete($type, $text)
+    static public function getAutoComplete($type, $text, $sub_id = null)
     {
         if (!array_key_exists($type, self::$enumerations)) {
             self::init($type);
         }
         $obj = self::$enumerations[$type];
         if ($obj->capabilities & DirEnumeration::HAS_AUTOCOMP) {
-            return call_user_func(array($obj, 'getAutoComplete'), $text);
+            return call_user_func_array(array($obj, 'getAutoComplete'), array($text, $sub_id));
         } else {
-            return PlIteratorUtils::fromArray(array());
+            return array();
         }
     }
 
@@ -252,7 +252,7 @@ abstract class DirEnumeration
     /** Builds a list of query parts for searching @$text in @$field :
      * field LIKE 'text%', field LIKE '% text%', field LIKE '%-text%'
      */
-    private function mkTests($field, $text)
+    protected function mkTests($field, $text)
     {
         $tests = array();
         $tests[] = $field . XDB::formatWildcards(XDB::WILDCARD_PREFIX, $text);
@@ -276,7 +276,7 @@ abstract class DirEnumeration
     }
 
     // {{{ function getAutoComplete
-    public function getAutoComplete($text)
+    public function getAutoComplete($text, $sub_id = null)
     {
         $text = str_replace(array('%', '_'), '', $text);
 
@@ -293,15 +293,15 @@ abstract class DirEnumeration
 
         $where .= '(' . implode(' OR ', $tests) . ')';
 
-        return XDB::iterator('SELECT ' . $this->valfield . ' AS field'
-                                       . ($this->ac_distinct ? (', COUNT(DISTINCT ' . $this->ac_unique . ') AS nb') : '')
-                                       . ($this->ac_withid ? (', ' . $this->idfield . ' AS id') : '') . '
-                                FROM ' . $this->from . '
-                                     ' . $this->ac_join . '
-                               WHERE ' . $where . '
-                            GROUP BY ' . $this->valfield . '
-                            ORDER BY ' . ($this->ac_distinct ? 'nb DESC' : $this->valfield) . '
-                               LIMIT ' . self::AUTOCOMPLETE_LIMIT);
+        return XDB::fetchAllAssoc('SELECT ' . $this->valfield . ' AS field'
+                                            . ($this->ac_distinct ? (', COUNT(DISTINCT ' . $this->ac_unique . ') AS nb') : '')
+                                            . ($this->ac_withid ? (', ' . $this->idfield . ' AS id') : '') . '
+                                     FROM ' . $this->from . '
+                                          ' . $this->ac_join . '
+                                    WHERE ' . $where . '
+                                 GROUP BY ' . $this->valfield . '
+                                 ORDER BY ' . ($this->ac_distinct ? 'nb DESC' : $this->valfield) . '
+                                    LIMIT ' . self::AUTOCOMPLETE_LIMIT);
     }
     // }}}
 
@@ -421,15 +421,15 @@ abstract class DE_WithSuboption extends DirEnumeration
 
         $where .= '(' . implode(' OR ', $tests) . ')';
 
-        return XDB::iterator('SELECT ' . $this->valfield . ' AS field'
-                                       . ($this->ac_distinct ? (', COUNT(DISTINCT ' . $this->ac_unique . ') AS nb') : '')
-                                       . ($this->ac_withid ? (', ' . $this->idfield . ' AS id') : '') . '
-                                FROM ' . $this->from . '
-                                     ' . $this->ac_join . '
-                               WHERE ' . $where . '
-                            GROUP BY ' . $this->valfield . '
-                            ORDER BY ' . ($this->ac_distinct ? 'nb DESC' : $this->valfield) . '
-                               LIMIT ' . self::AUTOCOMPLETE_LIMIT);
+        return XDB::fetchAllAssoc('SELECT ' . $this->valfield . ' AS field'
+                                            . ($this->ac_distinct ? (', COUNT(DISTINCT ' . $this->ac_unique . ') AS nb') : '')
+                                            . ($this->ac_withid ? (', ' . $this->idfield . ' AS id') : '') . '
+                                     FROM ' . $this->from . '
+                                          ' . $this->ac_join . '
+                                    WHERE ' . $where . '
+                                 GROUP BY ' . $this->valfield . '
+                                 ORDER BY ' . ($this->ac_distinct ? 'nb DESC' : $this->valfield) . '
+                                    LIMIT ' . self::AUTOCOMPLETE_LIMIT);
     }
 }
 // }}}
@@ -609,6 +609,30 @@ class DE_Localities extends DE_AddressesComponents
 {
     protected $where = 'WHERE  FIND_IN_SET(\'locality\', profile_addresses_components_enum.types)';
     protected $ac_where  = 'profile_addresses_components.type = \'home\' AND FIND_IN_SET(\'locality\', profile_addresses_components_enum.types)';
+
+    // {{{ function getAutoComplete
+    public function getAutoComplete($text, $sub_id = null)
+    {
+        if (is_null($sub_id)) {
+            return parent::getAutoComplete($text);
+        } else {
+            $tests = $this->mkTests('pace1.long_name', $text);
+            $where .= '(' . implode(' OR ', $tests) . ')';
+            $query = "SELECT  pace1.id AS id, pace1.long_name AS field, COUNT(DISTINCT(pac1.pid)) AS nb
+                        FROM  profile_addresses_components_enum AS pace1
+                  INNER JOIN  profile_addresses_components      AS pac1  ON (pac1.component_id = pace1.id)
+                  INNER JOIN  profile_addresses_components      AS pac2  ON (pac1.pid = pac2.pid AND pac1.jobid = pac2.jobid AND pac1.id = pac2.id
+                                                                             AND pac1.groupid = pac2.groupid AND pac1.type = pac2.type)
+                  INNER JOIN  profile_addresses_components_enum AS pace2 ON (pac2.component_id = pace2.id AND FIND_IN_SET('country', pace2.types))
+                       WHERE  pace2.id = {?} AND FIND_IN_SET('locality', pace1.types) AND pac1.type = 'home' AND " . $where . "
+                    GROUP BY  pace1.long_name
+                    ORDER BY  nb DESC, field
+                       LIMIT  " . self::AUTOCOMPLETE_LIMIT;
+            return XDB::fetchAllAssoc($query, $sub_id);
+        }
+    }
+    // }}}
+
 }
 
 class DE_Sublocalities extends DE_AddressesComponents
@@ -654,22 +678,26 @@ class DE_JobDescription extends DirEnumeration
 // {{{ class DE_JobTerms
 class DE_JobTerms extends DirEnumeration
 {
+    protected $valfield = 'profile_job_term_enum.name';
+    protected $from = 'profile_job_term_enum';
+    protected $idfield = 'profile_job_term_enum.jtid';
+
     // {{{ function getAutoComplete
-    public function getAutoComplete($text)
+    public function getAutoComplete($text, $sub_id = null)
     {
         $tokens = JobTerms::tokenize($text.'%');
         if (count($tokens) == 0) {
-            return PlIteratorUtils::fromArray(array());
+            return array();
         }
         $token_join = JobTerms::token_join_query($tokens, 'e');
-        return XDB::iterator('SELECT  e.jtid AS id, e.full_name AS field, COUNT(DISTINCT p.pid) AS nb
-                                 FROM  profile_job_term_enum AS e
-                           INNER JOIN  profile_job_term_relation AS r ON (r.jtid_1 = e.jtid)
-                           INNER JOIN  profile_job_term AS p ON (r.jtid_2 = p.jtid)
-                           '.$token_join.'
-                             GROUP BY  e.jtid
-                             ORDER BY  nb DESC, field
-                                LIMIT ' . self::AUTOCOMPLETE_LIMIT);
+        return XDB::fetchAllAssoc('SELECT  e.jtid AS id, e.full_name AS field, COUNT(DISTINCT p.pid) AS nb
+                                     FROM  profile_job_term_enum AS e
+                               INNER JOIN  profile_job_term_relation AS r ON (r.jtid_1 = e.jtid)
+                               INNER JOIN  profile_job_term AS p ON (r.jtid_2 = p.jtid)
+                               '.$token_join.'
+                                 GROUP BY  e.jtid
+                                 ORDER BY  nb DESC, field
+                                    LIMIT ' . self::AUTOCOMPLETE_LIMIT);
     }
     // }}}
 }
@@ -726,5 +754,5 @@ class DE_Skins extends DirEnumeration
 }
 // }}}
 
-// 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:
 ?>