Moving to GitHub.
[platal.git] / classes / direnum.php
index 0c22dc6..777653b 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2010 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -29,9 +29,6 @@ class DirEnum
      * Each of these consts contains the basename of the class (its full name
      * being DE_$basename).
      */
-    const NAMETYPES      = 'nametypes';
-    const NAMES          = 'names';
-
     const BINETS         = 'binets';
     const GROUPESX       = 'groupesx';
     const SECTIONS       = 'sections';
@@ -40,13 +37,18 @@ class DirEnum
     const EDUDEGREES     = 'educationdegrees';
     const EDUFIELDS      = 'educationfields';
 
-    const CORPS          = 'corps';
+    const CURRENTCORPS   = 'currentcorps';
+    const ORIGINCORPS    = 'origincorps';
     const CORPSRANKS     = 'corpsranks';
 
-    const NATIONALITIES  = 'nationalities';
-    const COUNTRIES      = 'countries';
-    const ADMINAREAS     = 'adminareas';
-    const LOCALITIES     = 'localities';
+    const NATIONALITIES       = 'nationalities';
+    const POSTALCODES         = 'postalcodes';
+    const SUBLOCALITIES       = 'sublocalities';
+    const LOCALITIES          = 'localities';
+    const ADMNISTRATIVEAREAS3 = 'admnistrativeareas3';
+    const ADMNISTRATIVEAREAS2 = 'admnistrativeareas2';
+    const ADMNISTRATIVEAREAS1 = 'admnistrativeareas1';
+    const COUNTRIES           = 'countries';
 
     const COMPANIES      = 'companies';
     const JOBDESCRIPTION = 'jobdescription';
@@ -56,6 +58,9 @@ class DirEnum
 
     const MEDALS         = 'medals';
 
+    const ACCOUNTTYPES   = 'accounttypes';
+    const SKINS          = 'skins';
+
     static private $enumerations = array();
 
     static private function init($type)
@@ -114,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();
         }
     }
 
@@ -247,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);
@@ -271,7 +276,7 @@ abstract class DirEnumeration
     }
 
     // {{{ function getAutoComplete
-    public function getAutoComplete($text)
+    public function getAutoComplete($text, $sub_id = null)
     {
         $text = str_replace(array('%', '_'), '', $text);
 
@@ -288,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);
     }
     // }}}
 
@@ -326,37 +331,38 @@ abstract class DE_WithSuboption extends DirEnumeration
 
     protected $suboptions = null;
 
-    protected function loadOptions()
+    protected function _fetchSubOptions($subid)
     {
-        $opts = XDB::fetchAllAssoc('id', 'SELECT ' . $this->valfield . ' AS field,
-                                                 ' . $this->optfield . ' AS subid,
-                                                 ' . $this->idfield . ' AS id
-                                            FROM ' . $this->from . '
-                                                 ' . $this->join . '
-                                                 ' . $this->where . '
-                                        GROUP BY ' . $this->valfield . '
-                                        ORDER BY ' . $this->valfield);
-        $this->options = array();
-        $this->suboptions = array();
-        foreach ($opts as $id => $opt) {
-            $this->options[$id] = $opt['field'];
-            if (!array_key_exists($opt['subid'], $this->suboptions)) {
-                $this->suboptions[$opt['subid']] = array();
-            }
-            $this->suboptions[$opt['subid']][$id] = $opt['field'];
+        if (is_null($this->suboptions)) {
+            $this->loadSubOptions($subid);
         }
     }
 
+    protected function loadSubOptions($subid)
+    {
+        $where = ($this->where == '') ? '' : $this->where . ' AND ';
+        $this->suboptions = XDB::fetchAllAssoc('id', 'SELECT ' . $this->valfield . ' AS field,
+                                                             ' . $this->idfield . ' AS id
+                                                        FROM ' . $this->from . '
+                                                             ' . $this->join . '
+                                                       WHERE ' . $where . $this->optfield . ' = ' . $subid . '
+                                                    GROUP BY ' . $this->valfield . '
+                                                    ORDER BY ' . $this->valfield);
+    }
+
     public function getOptions($subid = null)
     {
-        $this->_fetchOptions();
         if ($subid == null) {
+            $this->_fetchOptions();
             return $this->options;
-        } else if (array_key_exists($subid, $this->suboptions)) {
-            return $this->suboptions[$subid];
-        } else {
-            return array();
         }
+
+        $this->_fetchSubOptions($subid);
+        if (is_array($this->suboptions)) {
+            return $this->suboptions;
+        }
+
+        return array();
     }
 
     public function getOptionsIter($subid = null)
@@ -415,42 +421,19 @@ 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);
     }
 }
 // }}}
 
-// {{{ class DE_NameTypes
-// returns 'system' names ('lastname', 'lastname_marital', ...)
-class DE_NameTypes extends DirEnumeration
-{
-    public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
-
-    protected $from     = 'profile_name_enum';
-    protected $valfield = 'type';
-}
-// }}}
-
-// {{{ class DE_Names
-// returns 'system' names ('lastname', 'lastname_marital', ...)
-class DE_Names extends DirEnumeration
-{
-    public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
-
-    protected $from     = 'profile_name_enum';
-    protected $idfield  = 'type';
-    protected $valfield = 'name';
-}
-// }}}
-
 /** GROUPS
  */
 // {{{ class DE_Binets
@@ -494,6 +477,7 @@ class DE_GroupesX extends DirEnumeration
 // {{{ class DE_EducationSchools
 class DE_EducationSchools extends DirEnumeration
 {
+    protected $ac_beginwith = false;
     protected $idfield   = 'profile_education_enum.id';
     protected $valfield  = 'profile_education_enum.name';
     protected $valfield2 = 'profile_education_enum.abbreviation';
@@ -505,7 +489,7 @@ class DE_EducationSchools extends DirEnumeration
 // }}}
 
 // {{{ class DE_EducationDegrees
-class DE_EducationDegrees extends DirEnumeration
+class DE_EducationDegrees extends DE_WithSuboption
 {
     public $capabilities = self::HAS_OPTIONS;
 
@@ -529,18 +513,32 @@ class DE_EducationFields extends DirEnumeration
 }
 // }}}
 
-// {{{ class DE_Corps
-class DE_Corps extends DirEnumeration
+// {{{ class DE_CurrentCorps
+class DE_CurrentCorps extends DirEnumeration
 {
     protected $idfield   = 'profile_corps_enum.id';
     protected $valfield  = 'profile_corps_enum.name';
     protected $valfield2 = 'profile_corps_enum.abbrev';
     protected $from      = 'profile_corps_enum';
+    protected $where     = 'WHERE profile_corps_enum.still_exists = 1';
 
     protected $ac_unique = 'profile_corps.pid';
     protected $ac_join   = 'INNER JOIN profile_corps ON (profile_corps.current_corpsid = profile_corps_enum.id)';
 }
 // }}}
+//
+// {{{ class DE_OriginCorps
+class DE_OriginCorps extends DirEnumeration
+{
+    protected $idfield   = 'profile_corps_enum.id';
+    protected $valfield  = 'profile_corps_enum.name';
+    protected $valfield2 = 'profile_corps_enum.abbrev';
+    protected $from      = 'profile_corps_enum';
+
+    protected $ac_unique = 'profile_corps.pid';
+    protected $ac_join   = 'INNER JOIN profile_corps ON (profile_corps.original_corpsid = profile_corps_enum.id)';
+}
+// }}}
 
 // {{{ class DE_CorpsRanks
 class DE_CorpsRanks extends DirEnumeration
@@ -561,8 +559,8 @@ class DE_CorpsRanks extends DirEnumeration
 class DE_Nationalities extends DirEnumeration
 {
     protected $idfield   = 'geoloc_countries.iso_3166_1_a2';
-    protected $valfield  = 'geoloc_countries.nationalityFR';
-    protected $valfield2 = 'geoloc_countries.nationality';
+    protected $valfield  = 'geoloc_countries.nationality';
+    protected $valfield2 = 'geoloc_countries.nationalityEn';
     protected $from      = 'geoloc_countries';
     protected $join      = 'INNER JOIN profiles ON (geoloc_countries.iso_3166_1_a2 IN (profiles.nationality1, profiles.nationality2, profiles.nationality3))';
 
@@ -571,42 +569,84 @@ class DE_Nationalities extends DirEnumeration
 }
 // }}}
 
-// {{{ class DE_Countries
-class DE_Countries extends DirEnumeration
+// {{{ class DE_AddressesComponents
+class DE_AddressesComponents extends DirEnumeration
 {
-    protected $idfield   = 'geoloc_countries.iso_3166_1_a2';
-    protected $valfield  = 'geoloc_countries.countryFR';
-    protected $valfield2 = 'geoloc_countries.country';
-    protected $from      = 'geoloc_countries';
+    protected $idfield   = 'profile_addresses_components_enum.id';
+    protected $valfield  = 'profile_addresses_components_enum.long_name';
+    protected $from      = 'profile_addresses_components_enum';
 
-    protected $ac_join   = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryId)';
-    protected $ac_unique = 'profile_addresses.pid';
+    protected $ac_join   = 'INNER JOIN profile_addresses_components ON (profile_addresses_components.component_id = profile_addresses_components_enum.id)';
+    protected $ac_unique = 'profile_addresses_components.pid';
 }
 // }}}
+// {{{ class DE_AddressesComponents extensions
+class DE_Countries extends DE_AddressesComponents
+{
+    protected $where = 'WHERE  FIND_IN_SET(\'country\', profile_addresses_components_enum.types)';
+    protected $ac_where  = 'profile_addresses_components.type = \'home\' AND FIND_IN_SET(\'country\', profile_addresses_components_enum.types)';
+}
 
-// {{{ class DE_AdminAreas
-class DE_AdminAreas extends DE_WithSuboption
+class DE_Admnistrativeareas1 extends DE_AddressesComponents
 {
-    protected $idfield   = 'geoloc_administrativeareas.id';
-    protected $optfield  = 'geoloc_administrativeareas.country';
-    protected $valfield  = 'geoloc_administrativeareas.name';
-    protected $from      = 'geoloc_administrativeareas';
+    protected $where = 'WHERE  FIND_IN_SET(\'admnistrative_area_1\', profile_addresses_components_enum.types)';
+    protected $ac_where  = 'profile_addresses_components.type = \'home\' AND FIND_IN_SET(\'admnistrative_area_1\', profile_addresses_components_enum.types)';
+}
 
-    protected $ac_join   = 'INNER JOIN profile_addresses ON (profile_addresses.administrativeAreaId = geoloc_administrativeareas.id)';
-    protected $ac_unique = 'profile_addresses.pid';
+class DE_Admnistrativeareas2 extends DE_AddressesComponents
+{
+    protected $where = 'WHERE  FIND_IN_SET(\'admnistrative_area_2\', profile_addresses_components_enum.types)';
+    protected $ac_where  = 'profile_addresses_components.type = \'home\' AND FIND_IN_SET(\'admnistrative_area_2\', profile_addresses_components_enum.types)';
 }
-// }}}
 
-// {{{ class DE_Localities
-class DE_Localities extends DirEnumeration
+class DE_Admnistrativeareas3 extends DE_AddressesComponents
 {
-    protected $idfield   = 'geoloc_localities.id';
-    protected $valfield  = 'geoloc_localities.name';
-    protected $from      = 'geoloc_localities';
+    protected $where = 'WHERE  FIND_IN_SET(\'admnistrative_area_3\', profile_addresses_components_enum.types)';
+    protected $ac_where  = 'profile_addresses_components.type = \'home\' AND FIND_IN_SET(\'admnistrative_area_3\', profile_addresses_components_enum.types)';
+}
+
+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);
+        }
+    }
+    // }}}
 
-    protected $ac_join   = 'INNER JOIN profile_addresses ON (profile_addresses.localityID = geoloc_localities.id)';
-    protected $ac_unique = 'profile_addresses.pid';
 }
+
+class DE_Sublocalities extends DE_AddressesComponents
+{
+    protected $where = 'WHERE  FIND_IN_SET(\'sublocality\', profile_addresses_components_enum.types)';
+    protected $ac_where  = 'profile_addresses_components.type = \'home\' AND FIND_IN_SET(\'sublocality\', profile_addresses_components_enum.types)';
+}
+
+class DE_Postalcodes extends DE_AddressesComponents
+{
+    protected $where = 'WHERE  FIND_IN_SET(\'postal_code\', profile_addresses_components_enum.types)';
+    protected $ac_where  = 'profile_addresses_components.type = \'home\' AND FIND_IN_SET(\'postal_code\', profile_addresses_components_enum.types)';
+}
+
 // }}}
 
 /** JOBS
@@ -638,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);
     }
     // }}}
 }
@@ -686,5 +730,29 @@ class DE_Medals extends DirEnumeration
 }
 // }}}
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+/** ACCOUNTS
+ */
+// {{{ class DE_AccountTypes
+class DE_AccountTypes extends DirEnumeration
+{
+    public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
+
+    protected $from     = 'account_types';
+    protected $valfield = 'perms';
+    protected $idfield  = 'type';
+}
+// }}}
+
+// {{{ class DE_Skins
+class DE_Skins extends DirEnumeration
+{
+    public $capabilities = 0x005; // self::HAS_OPTIONS | self::SAVE_IN_SESSION;
+
+    protected $from      = 'skins';
+    protected $valfield  = 'name';
+    protected $idfield   = 'skin_tpl';
+}
+// }}}
+
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>