Move paiement db in x4dat (tables payment*).
[platal.git] / include / directory.enums.inc.php
index 4a61151..02c013f 100644 (file)
@@ -19,6 +19,7 @@
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
+// {{{ class DirEnum
 /** This class stores all data for the different kinds of fields.
  * It is only a dispatcher for the various DirEnum_XXX classes.
  */
@@ -29,16 +30,23 @@ class DirEnum
      * being DE_$basename).
      */
     const BINETS         = 'binets';
+    const GROUPESX       = 'groupesx';
     const SECTIONS       = 'sections';
-    const SCHOOLS        = 'schools';
-    const DEGREES        = 'degrees';
-    const STUDIESDOMAINS = 'studiesdomains';
+
+    const EDUSCHOOLS     = 'educationschools';
+    const EDUDEGREES     = 'educationdegrees';
+    const EDUFIELDS      = 'educationfields';
+
     const NATIONALITIES  = 'nationalities';
     const COUNTRIES      = 'countries';
-    const GROUPESX       = 'groupesx';
+    const ADMINAREAS     = 'adminareas';
+    const LOCALITIES     = 'localities';
+
+    const COMPANIES      = 'companies';
     const SECTORS        = 'sectors';
+    const JOBDESCRIPTION = 'jobdescription';
+
     const NETWORKS       = 'networking';
-    const ADMINAREAS     = 'adminareas';
 
     static private $enumerations = array();
 
@@ -51,8 +59,6 @@ class DirEnum
     /** Retrieves all options for a given type
      * @param $type Type of enum for which options are requested
      * @return XorgDbIterator over the results
-     * TODO : add support for optional parameters transmitted to enumerations
-     * TODO : Find a way to get either an array, or the adequate PlIterator
      */
     static public function getOptions()
     {
@@ -65,6 +71,42 @@ class DirEnum
         return call_user_func_array(array($obj, 'getOptions'), $args);
     }
 
+    /** Retrieves all options for a given type
+     * @param $type Type of enum for which options are requested
+     * @return Array of the results the results
+     */
+    static public function getOptionsArray()
+    {
+        $args = func_get_args();
+        $type = array_shift($args);
+        if (!array_key_exists($type, self::$enumerations)) {
+            self::init($type);
+        }
+        $obj = self::$enumerations[$type];
+        return call_user_func_array(array($obj, 'getOptionsArray'), $args);
+    }
+
+    /** Retrieves all options with number of profiles for autocompletion
+     * @param $type Type of enum for which options are requested
+     * @param $text Text to autocomplete
+     * @return XorgDbIterator over the results
+     */
+    static public function getAutoComplete()
+    {
+        $args = func_get_args();
+        $type = array_shift($args);
+        if (!array_key_exists($type, self::$enumerations)) {
+            self::init($type);
+        }
+        $obj = self::$enumerations[$type];
+        return call_user_func_array(array($obj, 'getAutoComplete'), $args);
+    }
+
+    /** Retrieves a list of IDs for a given type
+     * @param $type Type of enum for which IDs are requested
+     * @param $text Text to search in enum valuees
+     * @param $mode Mode of search for those IDs (prefix/suffix/infix)
+     */
     static public function getIDs()
     {
         $args = func_get_args();
@@ -76,7 +118,9 @@ class DirEnum
         return call_user_func_array(array($obj, 'getIDs'), $args);
     }
 }
+// }}}
 
+// {{{ class DirEnumeration
 abstract class DirEnumeration
 {
     /** Modes for LIKE searches
@@ -88,25 +132,50 @@ abstract class DirEnumeration
 
     /** An internal array of ID => optionTxt
      */
-    protected $options;
+    protected $options = null;
 
     /** Description of the MySQL storage of the fields
      */
     protected $idfield  = 'id';
     protected $valfield = 'text';
+    protected $valfield2 = null;
     protected $from;
     protected $join = '';
     protected $where = '';
 
-    public function __construct() {
-        $this->loadOptions();
+    /** Fields for autocompletion
+     */
+    protected $ac_join = ''; // Additional joins
+    protected $ac_where = null; // Additional where
+    protected $ac_beginwith = true; // Whether to search for 'x%' or for '%x%'
+    protected $ac_unique; // Which field is to be taken as unique
+    protected $ac_distinct = true; // Whether we want to keep only distinct valfield value
+    protected $ac_withid = true; // Do we want to fetch id too ?
+
+    protected function _fetchOptions()
+    {
+        if (is_null($this->options)) {
+            $this->loadOptions();
+        }
     }
 
     public function getOptions()
     {
+        $this->_fetchOptions();
         return $this->options;
     }
 
+    public function getOptionsArray()
+    {
+        $this->_fetchOptions();
+        $options = array();
+        while ($row = $this->options->next()) {
+            $options[$row['id']] = $row['field'];
+        }
+        return $options;
+    }
+
+    // {{{ function getIDs
     /** Retrieves possible IDs for given text
      * @param $text Text to search for IDs
      * @param $mode Mode of search (PREFIX, SUFFIX, CONTAINS)
@@ -123,29 +192,81 @@ abstract class DirEnumeration
             } else {
                 $where = $this->where . ' AND ';
             }
+            $conds = array();
+            $conds[] = $this->valfield . self::makeSqlConcat($text, $mode);
+            if ($this->valfield2 != null) {
+                $conds[] = $this->valfield2 . self::makeSqlConcat($text, $mode);
+            }
+            $where .= '(' . implode(' OR ', $conds) . ')';
+
             return XDB::fetchColumn('SELECT ' . $this->idfield . '
                                        FROM ' . $this->from . '
                                             ' . $this->join . '
-                                            ' . $where . $this->valfield . self::makeSqlConcat($text, $mode) . '
+                                            ' . $where . '
                                    GROUP BY ' . $this->idfield);
         }
     }
+    // }}}
+
+    private function mkTests($field, $text)
+    {
+        $tests = array();
+        $tests[] = $field . self::makeSqlConcat($text, self::MODE_PREFIX);
+        if (!$this->ac_beginwith) {
+            $tests[] = $field . self::makeSqlConcat(' ' . $text, self::MODE_CONTAINS);
+            $tests[] = $field . self::makeSqlConcat('-' . $text, self::MODE_CONTAINS);
+        }
+        return $tests;
+    }
+
+    // {{{ function getAutoComplete
+    public function getAutoComplete($text)
+    {
+        $text = str_replace(array('%', '_'), '', $text);
+
+        if (is_null($this->ac_where) || $this->ac_where == '') {
+            $where = '';
+        } else {
+            $where = $this->ac_where . ' AND ';
+        }
+
+        $tests = $this->mkTests($this->valfield, $text);
+        if (!is_null($this->valfield2)) {
+            $tests = array_merge($tests, $this->mkTests($this->valfield2, $text));
+        }
+
+        $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 11');
+    }
+    // }}}
 
+    // {{{ function makeSqlConcat
     static protected function makeSqlConcat($text, $mode)
     {
         if ($mode == self::MODE_EXACT) {
             return ' = ' . XDB::format('{?}', $text);
         }
-        if (($mode & self::MODE_CONTAINS) == self::MODE_PREFIX) {
+        if ($mode == self::MODE_PREFIX) {
             $right = XDB::format('CONCAT({?}, \'%\')', $text);
-        } else if (($mode & self::MODE_CONTAINS) == self::MODE_SUFFIX) {
+        } else if ($mode == self::MODE_SUFFIX) {
             $right = XDB::format('CONCAT(\'%\', {?})', $text);
         } else {
             $right = XDB::format('CONCAT(\'%\', {?}, \'%\')', $text);
         }
         return ' LIKE ' . $right;
     }
+    // }}}
 
+    // {{{ function loadOptions
     /** The function used to load options
      */
     protected function loadOptions()
@@ -158,23 +279,68 @@ abstract class DirEnumeration
                                       GROUP BY ' . $this->valfield . '
                                       ORDER BY ' . $this->valfield);
     }
+    // }}}
 }
+// }}}
 
+/** GROUPS
+ */
+// {{{ class DE_Binets
 class DE_Binets extends DirEnumeration
 {
     protected $from = 'binets_def';
+
+    protected $ac_join = 'INNER JOIN binets_ins ON (binets_def.id = binets_ins.binet_id)';
+    protected $ac_unique = 'binets_ins.user_id';
 }
+// }}}
+
+// {{{ class DE_Sections
 class DE_Sections extends DirEnumeration
 {
     protected $from = 'sections';
+
+    protected $ac_join = 'INNER JOIN profiles ON (profiles.section = sections.id)';
+    protected $ac_unique = 'profiles.pid';
 }
-class DE_Schools extends DirEnumeration
+// }}}
+
+// {{{ class DE_GroupesX
+class DE_GroupesX extends DirEnumeration
+{
+    protected $idfield   = 'asso.id';
+    protected $valfield  = 'asso.nom';
+    protected $valfield2 = 'asso.diminutif';
+    protected $from      = '#groupex#.asso AS asso';
+    protected $where     = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
+
+    protected $ac_join   = "INNER JOIN #groupex#.membres AS memb ON (asso.id = memb.asso_id
+                                    AND (asso.cat = 'GroupesX' OR asso.cat = 'Institutions')
+                                    AND asso.pub = 'public')";
+    protected $ac_unique = 'memb.uid';
+}
+// }}}
+
+/** EDUCATION
+ */
+// {{{ class DE_EducationSchools
+class DE_EducationSchools extends DirEnumeration
 {
-    protected $valfield = 'name';
-    protected $from = 'profile_education_enum';
+    protected $valfield  = 'name';
+    protected $valfield2 = 'abbreviation';
+    protected $from      = 'profile_education_enum';
+
+    protected $ac_join   = 'INNER JOIN profile_education ON (profile_education.eduid = profile_education_enum.id)';
+    protected $ac_unique = 'profile_education.uid';
 }
-class DE_Degrees extends DirEnumeration
+// }}}
+
+// {{{ class DE_EducationDegrees
+class DE_EducationDegrees extends DirEnumeration
 {
+    protected $from = 'profile_education_degree_enum';
+    protected $valfield = 'degree';
+
     protected $suboptions = array();
 
     protected function loadOptions()
@@ -196,6 +362,7 @@ class DE_Degrees extends DirEnumeration
 
     public function getOptions($eduid = null)
     {
+        $this->_fetchOptions();
         if ($eduid == null) {
             return PlIteratorUtils::fromArray($this->options, 1, true);
         }
@@ -220,24 +387,49 @@ class DE_Degrees extends DirEnumeration
         }
     }
 }
-class DE_StudiesSector extends DirEnumeration
+// }}}
+
+// {{{ class DE_EducationFields
+class DE_EducationFields extends DirEnumeration
 {
     protected $valfield = 'field';
-    protected $from = 'profile_education_field_enum';
+    protected $from     = 'profile_education_field_enum';
+
+    protected $ac_join   = 'INNER JOIN profile_education ON (profile_education.fieldid = profile_education_field_enum.id)';
+    protected $ac_unique = 'profile_education.uid';
 }
+// }}}
+
+/** GEOLOC
+ */
+// {{{ class DE_Nationalities
 class DE_Nationalities extends DirEnumeration
 {
-    protected $idfield  = 'iso_3166_1_a2';
-    protected $valfield = 'nationalityFR';
-    protected $from     = 'geoloc_countries AS gc';
-    protected $join     = 'INNER JOIN profiles AS p ON (gc.iso_3166_1_a2 IN (p.nationality1, p.nationality2, p.nationality3))';
+    protected $idfield   = 'iso_3166_1_a2';
+    protected $valfield  = 'nationalityFR';
+    protected $valfield2 = 'nationality';
+    protected $from      = 'geoloc_countries AS gc';
+    protected $join      = 'INNER JOIN profiles AS p ON (gc.iso_3166_1_a2 IN (p.nationality1, p.nationality2, p.nationality3))';
+
+    protected $ac_join   = 'INNER JOIN profiles AS p ON (gc.iso_3166_1_a2 IN (p.nationality1, p.nationality2, p.nationality3))';
+    protected $ac_unique = 'profiles.pid';
 }
+// }}}
+
+// {{{ class DE_Countries
 class DE_Countries extends DirEnumeration
 {
-    protected $idfield  = 'iso_3166_1_a2';
-    protected $valfield = 'countryFR';
-    protected $from     = 'geoloc_countries';
+    protected $idfield   = 'iso_3166_1_a2';
+    protected $valfield  = 'countryFR';
+    protected $valfield2 = 'country';
+    protected $from      = 'geoloc_countries';
+
+    protected $ac_join   = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryFR';
+    protected $ac_unique = 'profile_addresses.pid';
 }
+// }}}
+
+// {{{ class DE_AdminAreas
 class DE_AdminAreas extends DirEnumeration
 {
     protected $suboptions = array();
@@ -260,6 +452,8 @@ class DE_AdminAreas extends DirEnumeration
 
     public function getOptions($country = null)
     {
+        $this->_fetchOptions();
+
         if ($country == null) {
             return PlIteratorUtils::fromArray($this->options, 1, true);
         }
@@ -283,21 +477,67 @@ class DE_AdminAreas extends DirEnumeration
         }
     }
 }
-class DE_GroupesX extends DirEnumeration
+// }}}
+
+// {{{ class DE_Localities
+class DE_Localities extends DirEnumeration
+{
+    protected $valfield  = 'gl.name';
+    protected $from      = 'geoloc_localities AS gl';
+
+    protected $ac_join   = 'profile_addresses AS pa ON (pa.localityID = gl.id)';
+    protected $ac_unique = 'pa.pid';
+}
+// }}}
+
+/** JOBS
+ */
+// {{{ class DE_Companies
+class DE_Companies extends DirEnumeration
 {
-    protected $valfield = 'nom';
-    protected $from     = '#groupex#.asso';
-    protected $where    = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
+    protected $valfield  = 'pje.name';
+    protected $valfield2 = 'pje.acronym';
+    protected $from      = 'profile_job_enum AS pje';
+
+    protected $ac_join   = 'INNER JOIN profile_job AS pj ON (pj.jobid = pje.id)';
+    protected $ac_unique = 'pj.uid';
 }
+// }}}
+
+// {{{ class DE_Sectors
 class DE_Sectors extends DirEnumeration
 {
-    protected $valfield = 'name';
-    protected $from     = 'profile_job_sector_enum';
+    protected $valfield  = 'name';
+    protected $from      = 'profile_job_sector_enum';
+
+    protected $ac_join   = 'INNER JOIN profile_job ON (profile_job_sector_enum.id = profile_job.sectorid)';
+    protected $ac_unique = 'profile_job.uid';
+}
+// }}}
+
+// {{{ class DE_JobDescription
+class DE_JobDescription
+{
+    protected $valfield = 'pj.description';
+    protected $from     = 'profile_job AS pj';
+    protected $idfield  = 'pj.pid';
+
+    protected $ac_unique = 'pj.pid';
 }
+// }}}
+
+/** NETWORKING
+ */
+// {{{ class DE_Networking
 class DE_Networking extends DirEnumeration
 {
-    protected $idfield  = 'network_type';
-    protected $valfield = 'name';
+    protected $idfield  = 'profile_networking_enum.network_type';
+    protected $valfield = 'profile_networking_enum.name';
     protected $from     = 'profile_networking_enum';
+
+
+    protected $ac_join   = 'INNER JOIN profile_networking ON (profile_networking.network_type = profile_networking_enum.network_type';
+    protected $ac_unique = 'profile_networking.uid';
 }
+// }}}
 ?>