Start assigning permissions to the hooks.
[platal.git] / classes / direnum.php
index be5d83b..c6fbf0d 100644 (file)
@@ -30,6 +30,7 @@ class DirEnum
      * being DE_$basename).
      */
     const NAMETYPES      = 'nametypes';
+    const NAMES          = 'names';
 
     const BINETS         = 'binets';
     const GROUPESX       = 'groupesx';
@@ -39,6 +40,9 @@ class DirEnum
     const EDUDEGREES     = 'educationdegrees';
     const EDUFIELDS      = 'educationfields';
 
+    const CORPS          = 'corps';
+    const CORPSRANKS     = 'corpsranks';
+
     const NATIONALITIES  = 'nationalities';
     const COUNTRIES      = 'countries';
     const ADMINAREAS     = 'adminareas';
@@ -50,28 +54,36 @@ class DirEnum
 
     const NETWORKS       = 'networking';
 
+    const MEDALS         = 'medals';
+
     static private $enumerations = array();
 
     static private function init($type)
     {
-        $cls = "DE_" . ucfirst($type);
-        self::$enumerations[$type] = new $cls();
+        if (S::has('__DE_' . $type)) {
+            self::$enumerations[$type] = S::v('__DE_' . $type);
+        } else {
+            $cls = "DE_" . ucfirst($type);
+            $obj = new $cls();
+            self::$enumerations[$type] = $obj;
+            if ($obj->capabilities & DirEnumeration::SAVE_IN_SESSION) {
+                S::set('__DE_' . $type, $obj);
+            }
+        }
     }
 
     /** Retrieves all options for a given type
      * @param $type Type of enum for which options are requested
      * @return Array of the results
      */
-    static public function getOptions()
+    static public function getOptions($type)
     {
-        $args = func_get_args();
-        $type = array_shift($args);
         if (!array_key_exists($type, self::$enumerations)) {
             self::init($type);
         }
         $obj = self::$enumerations[$type];
         if ($obj->capabilities & DirEnumeration::HAS_OPTIONS) {
-            return call_user_func_array(array($obj, 'getOptions'), $args);
+            return call_user_func(array($obj, 'getOptions'));
         } else {
             return array();
         }
@@ -81,14 +93,14 @@ class DirEnum
      * @param $type Type of enum for which options are requested
      * @return PlIterator over the results
      */
-    static public function getOptionsIter()
+    static public function getOptionsIter($type)
     {
-        $args = func_get_args();
-        $type = array_shift($args);
         if (!array_key_exists($type, self::$enumerations)) {
             self::init($type);
         }
         $obj = self::$enumerations[$type];
+        $args = func_get_args();
+        array_shift($args);
         if ($obj->capabilities & DirEnumeration::HAS_OPTIONS) {
             return call_user_func_array(array($obj, 'getOptionsIter'), $args);
         } else {
@@ -101,16 +113,14 @@ class DirEnum
      * @param $text Text to autocomplete
      * @return PlIterator over the results
      */
-    static public function getAutoComplete()
+    static public function getAutoComplete($type, $text)
     {
-        $args = func_get_args();
-        $type = array_shift($args);
         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(array($obj, 'getAutoComplete'), $args);
+            return call_user_func(array($obj, 'getAutoComplete'), $text);
         } else {
             return PlIteratorUtils::fromArray(array());
         }
@@ -141,7 +151,8 @@ class DirEnum
      */
     static public function getID($type, $text, $mode = XDB::WILDCARD_EXACT)
     {
-        return array_shift(self::getIDs($type, $text, $mode));
+        $ids = self::getIDs($type, $text, $mode);
+        return array_shift($ids);
     }
 }
 // }}}
@@ -153,6 +164,7 @@ abstract class DirEnumeration
 
     const HAS_OPTIONS  = 0x001;
     const HAS_AUTOCOMP = 0x002;
+    const SAVE_IN_SESSION = 0x004;
 
     public $capabilities = 0x003; // self::HAS_OPTIONS | self::HAS_AUTOCOMP;
 
@@ -193,7 +205,9 @@ abstract class DirEnumeration
 
     public function getOptionsIter()
     {
-        return PlIteratorUtils::fromArray(self::expandArray($this->getOptions()), 1, true);
+        $options = $this->getOptions();
+        $options = self::expandArray($options);
+        return PlIteratorUtils::fromArray($options, 1, true);
     }
 
     // {{{ function getIDs
@@ -229,6 +243,9 @@ 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)
     {
         $tests = array();
@@ -337,7 +354,7 @@ abstract class DE_WithSuboption extends DirEnumeration
         } else if (array_key_exists($subid, $this->suboptions)) {
             return $this->suboptions[$subid];
         } else {
-            return false;
+            return array();
         }
     }
 
@@ -414,13 +431,25 @@ abstract class DE_WithSuboption extends DirEnumeration
 // returns 'system' names ('lastname', 'lastname_marital', ...)
 class DE_NameTypes extends DirEnumeration
 {
-    public $capabilities = self::HAS_OPTIONS;
+    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
@@ -452,7 +481,7 @@ class DE_GroupesX extends DirEnumeration
     protected $from      = 'groups';
     protected $where     = 'WHERE (cat = \'GroupesX\' OR cat = \'Institutions\') AND pub = \'public\'';
 
-    protected $ac_join   = "INNER JOIN group_members ON (groups.id = memb.asso_id
+    protected $ac_join   = "INNER JOIN group_members ON (groups.id = group_members.asso_id
                                     AND (groups.cat = 'GroupesX' OR groups.cat = 'Institutions')
                                     AND groups.pub = 'public')";
     protected $ac_unique = 'group_members.uid';
@@ -464,6 +493,7 @@ class DE_GroupesX extends DirEnumeration
 // {{{ class DE_EducationSchools
 class DE_EducationSchools extends DirEnumeration
 {
+    protected $idfield   = 'profile_education_enum.id';
     protected $valfield  = 'profile_education_enum.name';
     protected $valfield2 = 'profile_education_enum.abbreviation';
     protected $from      = 'profile_education_enum';
@@ -498,6 +528,32 @@ class DE_EducationFields extends DirEnumeration
 }
 // }}}
 
+// {{{ class DE_Corps
+class DE_Corps extends DirEnumeration
+{
+    protected $idfield   = 'corps_enum.id';
+    protected $valfield  = 'corps_enum.name';
+    protected $valfield2 = 'corps_enum.abbrev';
+    protected $from      = 'corps_enum';
+
+    protected $ac_unique = 'corps.pid';
+    protected $ac_join   = 'INNER JOIN corps ON (corps.current_corpsid = corps_enum.id)';
+}
+// }}}
+
+// {{{ class DE_CorpsRanks
+class DE_CorpsRanks extends DirEnumeration
+{
+    protected $idfield   = 'corps_rank_enum.id';
+    protected $valfield  = 'corps_rank_enum.name';
+    protected $valfield2 = 'corps_rank_enum.abbrev';
+    protected $from      = 'corps_rank_enum';
+
+    protected $ac_unique = 'corps.pid';
+    protected $ac_join   = 'INNER JOIN corps ON (corps.rankid = corps_rank_enum.id)';
+}
+// }}}
+
 /** GEOLOC
  */
 // {{{ class DE_Nationalities
@@ -522,7 +578,7 @@ class DE_Countries extends DirEnumeration
     protected $valfield2 = 'geoloc_countries.country';
     protected $from      = 'geoloc_countries';
 
-    protected $ac_join   = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryFR';
+    protected $ac_join   = 'INNER JOIN profile_addresses ON (geoloc_countries.iso_3166_1_a2 = profile_addresses.countryId)';
     protected $ac_unique = 'profile_addresses.pid';
 }
 // }}}
@@ -543,10 +599,11 @@ class DE_AdminAreas extends DE_WithSuboption
 // {{{ class DE_Localities
 class DE_Localities extends DirEnumeration
 {
+    protected $idfield   = 'geoloc_localities.id';
     protected $valfield  = 'geoloc_localities.name';
     protected $from      = 'geoloc_localities';
 
-    protected $ac_join   = 'profile_addresses ON (profile_addresses.localityID = geoloc_localities.id)';
+    protected $ac_join   = 'INNER JOIN profile_addresses ON (profile_addresses.localityID = geoloc_localities.id)';
     protected $ac_unique = 'profile_addresses.pid';
 }
 // }}}
@@ -556,6 +613,7 @@ class DE_Localities extends DirEnumeration
 // {{{ class DE_Companies
 class DE_Companies extends DirEnumeration
 {
+    protected $idfield   = 'profile_job_enum.id';
     protected $valfield  = 'profile_job_enum.name';
     protected $valfield2 = 'profile_job_enum.acronym';
     protected $from      = 'profile_job_enum';
@@ -568,6 +626,7 @@ class DE_Companies extends DirEnumeration
 // {{{ class DE_Sectors
 class DE_Sectors extends DirEnumeration
 {
+    protected $idfield   = 'profile_job_sector_enum.id';
     protected $valfield  = 'profile_job_sector_enum.name';
     protected $from      = 'profile_job_sector_enum';
 
@@ -577,7 +636,7 @@ class DE_Sectors extends DirEnumeration
 // }}}
 
 // {{{ class DE_JobDescription
-class DE_JobDescription
+class DE_JobDescription extends DirEnumeration
 {
     protected $valfield = 'profile_job.description';
     protected $from     = 'profile_job';
@@ -592,15 +651,27 @@ class DE_JobDescription
 // {{{ class DE_Networking
 class DE_Networking extends DirEnumeration
 {
-    protected $idfield  = 'profile_networking_enum.network_type';
+    protected $idfield  = 'profile_networking_enum.nwid';
     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_join   = 'INNER JOIN profile_networking ON (profile_networking.nwid = profile_networking_enum.nwid)';
     protected $ac_unique = 'profile_networking.pid';
 }
 // }}}
 
+/** MEDALS
+ */
+// {{{ class DE_Medals
+class DE_Medals extends DirEnumeration
+{
+    protected $from = 'profile_medal_enum';
+
+    protected $ac_join = 'INNER JOIN profile_medals ON (profile_medals.mid = profile_medal_enum.id)';
+    protected $ac_unique = 'profile_medals.pid';
+}
+// }}}
+
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>