Adds search on subadministrativearea (Closes #1312).
[platal.git] / include / ufbuilder.inc.php
index 41e1cf4..0e2b872 100644 (file)
@@ -19,8 +19,6 @@
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
-require_once 'directory.enums.inc.php';
-
 // {{{ class UserFilterBuilder
 class UserFilterBuilder
 {
@@ -73,10 +71,21 @@ class UserFilterBuilder
         return $this->valid;
     }
 
+    public function isEmpty()
+    {
+        $this->buildUFC();
+        foreach ($this->fields as $field) {
+            if (! $field->isEmpty()) {
+                return false;
+            }
+        }
+        return true;
+    }
+
     /** Returns the built UFC
      * @return The UFC, or PFC_False() if an error happened
      */
-    public function &getUFC()
+    public function getUFC()
     {
         $this->buildUFC();
         if ($this->valid) {
@@ -96,24 +105,56 @@ class UserFilterBuilder
 
     /** Wrappers around Env::i/s/..., to add envprefix
      */
-    public function s($key, $def = '') {
-        return trim(Env::s($this->envprefix . $key, $def));
+    public function s($key, $def = '')
+    {
+        return Env::s($this->envprefix . $key, $def);
     }
 
-    public function i($key, $def = 0) {
-        return intval(trim(Env::i($this->envprefix . $key, $def)));
+    public function t($key, $def = '')
+    {
+        return Env::t($this->envprefix . $key, $def);
+    }
+
+    public function i($key, $def = 0)
+    {
+        return Env::i($this->envprefix . $key, $def);
     }
 
-    public function v($key, $def = null) {
+    public function v($key, $def = null)
+    {
         return Env::v($this->envprefix . $key, $def);
     }
 
-    public function has($key) {
-        return (Env::has($this->envprefix . $key) && strlen($this->s($key, '')) > 0);
+    public function b($key, $def = false)
+    {
+        return Env::b($this->envprefix . $key, $def);
+    }
+
+    public function has($key)
+    {
+        return Env::has($this->envprefix . $key);
+    }
+
+    public function blank($key, $strict = false)
+    {
+        return Env::blank($key, $strict);
     }
 
-    public function isOn($key) {
-        return (Env::has($this->envprefix . $key) && $this->s($key) == 'on');
+    public function hasAlnum($key)
+    {
+        $str = $this->s($key);
+        return preg_match('/[a-z0-9]/i', $str);
+    }
+
+    public function hasAlpha($key)
+    {
+        $str = $this->s($key);
+        return preg_match('/[a-z]/i', $str);
+    }
+
+    public function isOn($key)
+    {
+        return $this->has($key) && $this->t($key) == 'on';
     }
 }
 // }}}
@@ -146,25 +187,43 @@ class UFB_AdvancedSearch extends UserFilterBuilder
 
             new UFBF_Town('city', 'Ville / Code Postal'),
             new UFBF_Country('countryTxt', 'country', 'Pays'),
-            new UFBF_AdminArea('region', 'Région'),
+            new UFBF_AdminArea('administrativearea', 'Région'),
+            new UFBF_SubAdminArea('subadministrativearea', 'Département'),
 
             new UFBF_JobCompany('entreprise', 'Entreprise'),
-            new UFBF_JobSector('sector', 'Poste'),
             new UFBF_JobDescription('jobdescription', 'Fonction'),
             new UFBF_JobCv('cv', 'CV'),
+            new UFBF_JobTerms('jobterm', 'Mots-clefs'),
 
             new UFBF_Nationality('nationaliteTxt', 'nationalite', 'Nationalité'),
             new UFBF_Binet('binetTxt', 'binet', 'Binet'),
             new UFBF_Group('groupexTxt', 'groupex', 'Groupe X'),
             new UFBF_Section('sectionTxt', 'section', 'Section'),
 
-            new UFBF_Formation('schoolTxt', 'school', "École d'application"),
-            new UFBF_Diploma('diplomaTxt', 'diploma', 'Diplôme'),
-            new UFBF_StudiesDomain('fieldTxt', 'field', "Domaine d'études"),
+            new UFBF_EducationSchool('schoolTxt', 'school', "École d'application"),
+            new UFBF_EducationDegree('diplomaTxt', 'diploma', 'Diplôme'),
+            new UFBF_EducationField('fieldTxt', 'field', "Domaine d'études"),
 
             new UFBF_Comment('free', 'Commentaire'),
             new UFBF_Phone('phone_number', 'Téléphone'),
             new UFBF_Networking('networking_address', 'networking_type', 'Networking et sites webs'),
+
+            new UFBF_Mentor('only_referent', 'Référent'),
+        );
+        parent::__construct($fields, $envprefix);
+    }
+}
+// }}}
+
+// {{{ class UFB_MentorSearch
+class UFB_MentorSearch extends UserFilterBuilder
+{
+    public function __construct($envprefix = '')
+    {
+        $fields = array(
+            new UFBF_MentorCountry('country'),
+            new UFBF_MentorTerm('jobterm', 'jobtermText'),
+            new UFBF_MentorExpertise('expertise'),
         );
         parent::__construct($fields, $envprefix);
     }
@@ -210,7 +269,7 @@ abstract class UFB_Field
             return false;
         }
 
-        if (!$this->empty) {
+        if (!$this->isEmpty()) {
             $ufc = $this->buildUFC($ufb);
             if ($ufc != null) {
                 $ufb->addCond($ufc);
@@ -219,6 +278,11 @@ abstract class UFB_Field
         return true;
     }
 
+    public function isEmpty()
+    {
+        return $this->empty;
+    }
+
     /** Create the UFC associated to the field; won't be called
      * if the field is "empty"
      * @param &$ufb UFB to which fields must be added
@@ -248,17 +312,17 @@ abstract class UFBF_Text extends UFB_Field
 
     protected function check(UserFilterBuilder &$ufb)
     {
-        if (!$ufb->has($this->envfield)) {
+        if ($ufb->blank($this->envfield)) {
             $this->empty = true;
             return true;
         }
 
-        $this->val = $ufb->s($this->envfield);
+        $this->val = $ufb->t($this->envfield);
         if (strlen($this->val) < $this->minlength) {
             return $this->raise("Le champ %s est trop court (minimum {$this->minlength}).");
         } else if (strlen($this->val) > $this->maxlength) {
             return $this->raise("Le champ %s est trop long (maximum {$this->maxlength}).");
-        } else if (preg_match(":[\]\[<>{}~/§_`|%$^=+]|\*\*:u", $this->val)) {
+        } else if (preg_match(":[\]\[<>{}~§_`|%$^=]|\*\*:u", $this->val)) {
             return $this->raise('Le champ %s contient un caractère interdit rendant la recherche impossible.');
         }
 
@@ -285,7 +349,7 @@ abstract class UFBF_Range extends UFB_Field
 
     protected function check(UserFilterBuilder &$ufb)
     {
-        if (!$ufb->has($this->envfield)) {
+        if ($ufb->blank($this->envfield)) {
             $this->empty = true;
             return true;
         }
@@ -308,9 +372,10 @@ abstract class UFBF_Index extends UFB_Field
 {
     protected function check(UserFilterBuilder &$ufb)
     {
-        if (!$ufb->has($this->envfield)) {
+        if ($ufb->blank($this->envfield)) {
             $this->empty = true;
         }
+        $this->val = $ufb->i($this->envfield);
         return true;
     }
 }
@@ -332,7 +397,7 @@ abstract class UFBF_Enum extends UFB_Field
 
     protected function check(UserFilterBuilder &$ufb)
     {
-        if (!$ufb->has($this->envfield)) {
+        if ($ufb->blank($this->envfield)) {
             $this->empty = true;
             return true;
         }
@@ -355,12 +420,12 @@ abstract class UFBF_Bool extends UFB_Field
 {
     protected function check(UserFilterBuilder &$ufb)
     {
-        if (!$ufb->has($this->envfield)) {
+        if ($ufb->blank($this->envfield)) {
             $this->empty = true;
             return true;
         }
 
-        $this->val = ($ufb->i($this->envfield) != 0);
+        $this->val = $ufb->b($this->envfield);
         return true;
     }
 }
@@ -385,12 +450,12 @@ abstract class UFBF_Mixed extends UFB_Field
 
     protected function check(UserFilterBuilder &$ufb)
     {
-        if (!$ufb->has($this->envfieldindex) && !$ufb->has($this->envfield)) {
+        if ($ufb->blank($this->envfieldindex) && !$ufb->hasAlnum($this->envfield)) {
             $this->empty = true;
             return true;
         }
 
-        if ($ufb->has($this->envfieldindex)) {
+        if (!$ufb->blank($this->envfieldindex)) {
             $index = $ufb->v($this->envfieldindex);
             if (is_int($index)) {
                 $index = intval($index);
@@ -399,8 +464,8 @@ abstract class UFBF_Mixed extends UFB_Field
             }
             $this->val = array($index);
         } else {
-            $indexes = DirEnum::getIDs($this->direnum, $ufb->s($this->envfield), 
-                $ufb->i('exact') ? DirEnumeration::MODE_EXACT : DirEnumeration::MODE_CONTAINS);
+            $indexes = DirEnum::getIDs($this->direnum, $ufb->t($this->envfield),
+                $ufb->b('exact') ? XDB::WILDCARD_EXACT : XDB::WILDCARD_CONTAINS);
             if (count($indexes) == 0) {
                 return false;
             }
@@ -416,31 +481,31 @@ class UFBF_Quick extends UFB_Field
 {
     protected function check(UserFilterBuilder &$ufb)
     {
-        if (!$ufb->has($this->envfield)) {
+        if ($ufb->blank($this->envfield)) {
             $this->empty = true;
             return true;
         }
 
-        $this->val = str_replace('*', '%', replace_accent($ufb->s($this->envfield)));
+        $this->val = str_replace('*', '%', replace_accent($ufb->t($this->envfield)));
+
+        return true;
     }
 
     protected function buildUFC(UserFilterBuilder &$ufb)
     {
-        $conds = new PFC_And();
 
-        $s = $this->val;
+        $r = $s = $this->val;
 
         /** Admin: Email, IP
          */
         if (S::admin() && strpos($s, '@') !== false) {
-            $conds->addChild(new UFC_Email($s));
-            return $conds;
+            return new UFC_Email($s);
         } else if (S::admin() && preg_match('/[0-9]+\.([0-9]+|%)\.([0-9]+|%)\.([0-9]+|%)/', $s)) {
-            // TODO: create UFC_Ip
-//            $this->conds->addChild(new UFC_Ip($s));
-            return $conds;
+            return new UFC_Ip($s);
         }
 
+        $conds = new PFC_And();
+
         /** Name
          */
         $s = preg_replace('!\d+!', ' ', $s);
@@ -456,24 +521,10 @@ class UFBF_Quick extends UFB_Field
             } else {
                 $flags = array('public');
             }
-            if ($ufb->i('soundex')) {
-                $soundex = true;
-                $st = array();
-                foreach ($strings as $string) {
-                    $st[] = soundex_fr($string);
-                }
-            } else {
-                $soundex = false;
-                $st = $strings;
-            }
-            if ($ufb->i('exact')) {
-                $exact = true;
-            } else {
-                $exact = false;
-            }
-            $conds->addChild(new UFC_NameTokens($st, $flags, $soundex, $exact));
+            $exact =$ufb->b('exact');
+            $conds->addChild(new UFC_NameTokens($strings, $flags, $ufb->b('with_soundex'), $exact));
 
-            $ufb->addSort(new UFO_Score());
+            $ufb->addOrder(new UFO_Score());
         }
 
         /** Promo ranges
@@ -496,7 +547,7 @@ class UFBF_Quick extends UFB_Field
             } elseif (preg_match('!^<(\d{4})!', $r, $matches)) {
                 $conds->addChild(new UFC_Promo('<=', UserFilter::DISPLAY, 'X' . $matches[1]));
             } elseif (preg_match('!^>(\d{4})!', $r, $matches)) {
-                $this->conds->addChild(new UFC_Promo('>=', UserFilter::DISPLAY, 'X' . $matches[1]));
+                $conds->addChild(new UFC_Promo('>=', UserFilter::DISPLAY, 'X' . $matches[1]));
             }
         }
 
@@ -531,7 +582,7 @@ class UFBF_Name extends UFBF_Text
 
     protected function buildUFC(UserFilterBuilder &$ufb)
     {
-        return new UFC_NameTokens($this->val, array(), $ufb->i('with_soundex'), $ufb->i('exact'));
+        return new UFC_NameTokens($this->val, array(), $ufb->b('with_soundex'), $ufb->b('exact'));
     }
 }
 // }}}
@@ -551,13 +602,13 @@ class UFBF_Promo extends UFB_Field
 
     protected function check(UserFilterBuilder &$ufb)
     {
-        if (!$ufb->has($this->envfield) || !$ufb->has($this->envfieldcomp)) {
+        if ($ufb->blank($this->envfield) || $ufb->blank($this->envfieldcomp)) {
             $this->empty = true;
             return true;
         }
 
-        $this->val  = $ubf->i($this->envfield);
-        $this->comp = $ubf->v($this->envfieldcomp);
+        $this->val  = $ufb->i($this->envfield);
+        $this->comp = $ufb->v($this->envfieldcomp);
 
         if (!in_array($this->comp, self::$validcomps)) {
             return $this->raise("Le critère {$this->comp} n'est pas valide pour le champ %s");
@@ -573,7 +624,7 @@ class UFBF_Promo extends UFB_Field
     }
 
     protected function buildUFC(UserFilterBuilder &$ufb) {
-        return new UFC_Promo($this->comp, UserFilter::DISPLAY, 'X' . $this->val);
+        return new UFC_Promo($this->comp, UserFilter::GRADE_ING, $this->val);
     }
 }
 // }}}
@@ -618,7 +669,7 @@ class UFBF_Registered extends UFBF_Enum
         if ($this->val == 1) {
             return new UFC_Registered();
         } else if ($this->val == 2) {
-            return new PFC_Not(UFC_Registered());
+            return new PFC_Not(new UFC_Registered());
         }
     }
 }
@@ -635,7 +686,7 @@ class UFBF_Dead extends UFBF_Enum
     protected function buildUFC(UserFilterBuilder &$ufb)
     {
         if ($this->val == 1) {
-            return new PFC_Not(UFC_Dead());
+            return new PFC_Not(new UFC_Dead());
         } else if ($this->val == 2) {
             return new UFC_Dead();
         }
@@ -677,7 +728,7 @@ class UFBF_Town extends UFBF_Text
                 return new PFC_False();
             }
         } else {
-            $byname = new UFC_AddressText(null, UFC_Address::CONTAINS, UFC_Address::TYPE_ANY, $flags, null, $this->val);
+            $byname = new UFC_AddressText(null, XDB::WILDCARD_CONTAINS, UFC_Address::TYPE_ANY, $flags, null, $this->val);
             $byzip  = new UFC_AddressField($this->val, UFC_AddressField::FIELD_ZIPCODE, UFC_Address::TYPE_ANY, $flags);
             if ($this->type & self::TYPE_ANY) {
                 return new PFC_Or($byname, $byzip);
@@ -717,14 +768,14 @@ class UFBF_Country extends UFBF_Mixed
 // }}}
 
 // {{{ class UFBF_AdminArea
-class UFBF_AdminArea extends UFBF_Mixed
+class UFBF_AdminArea extends UFBF_Index
 {
     protected $direnum = DirEnum::ADMINAREAS;
     protected $onlycurrentfield;
 
-    public function __construct($envfieldtext, $envfieldindex, $formtext = '', $onlycurrentfield = 'only_current')
+    public function __construct($envfield, $formtext = '', $onlycurrentfield = 'only_current')
     {
-        parent::__construct($envfieldtext, $envfieldindex, $formtext);
+        parent::__construct($envfield, $formtext);
         $this->onlycurrentfield = $onlycurrentfield;
     }
 
@@ -742,6 +793,32 @@ class UFBF_AdminArea extends UFBF_Mixed
 }
 // }}}
 
+// {{{ class UFBF_SubAdminArea
+class UFBF_SubAdminArea extends UFBF_Index
+{
+    protected $direnum = DirEnum::SUBADMINAREAS;
+    protected $onlycurrentfield;
+
+    public function __construct($envfield, $formtext = '', $onlycurrentfield = 'only_current')
+    {
+        parent::__construct($envfield, $formtext);
+        $this->onlycurrentfield = $onlycurrentfield;
+    }
+
+
+    protected function buildUFC(UserFilterBuilder &$ufb)
+    {
+        if ($ufb->isOn($this->onlycurrentfield)) {
+            $flags = UFC_Address::FLAG_CURRENT;
+        } else {
+            $flags = UFC_Address::FLAG_ANY;
+        }
+
+        return new UFC_AddressField($this->val, UFC_AddressField::FIELD_SUBADMAREA, UFC_Address::TYPE_ANY, $flags);
+    }
+}
+// }}}
+
 // {{{ class UFBF_JobCompany
 class UFBF_JobCompany extends UFBF_Text
 {
@@ -772,25 +849,12 @@ class UFBF_JobCompany extends UFBF_Text
 }
 // }}}
 
-// {{{ class UFBF_JobSector
-class UFBF_JobSector extends UFBF_Mixed
+// {{{ class UFBF_JobTerms
+class UFBF_JobTerms extends UFBF_Index
 {
-    protected $direnum = DirEnum::SECTORS;
-    private $onlymentorfield;
-
-    public function __construct($envfieldtext, $envfieldindex, $formtext = '', $onlymentorfield = 'only_referent')
-    {
-        parent::__construct($envfieldtext, $envfieldindex, $formtext);
-        $this->onlymentorfield = $onlymentorfield;
-    }
-
     protected function buildUFC(UserFilterBuilder &$ufb)
     {
-        if ($ufb->isOn($this->onlymentorfield)) {
-            return new UFC_Mentor_Sectorization($this->val, UserFilter::JOB_SUBSECTOR);
-        } else {
-            return new UFC_Job_Sectorization($this->val, UserFilter::JOB_SUBSUBSECTOR);
-        }
+        return new UFC_Job_Terms($this->val);
     }
 }
 // }}}
@@ -884,7 +948,7 @@ class UFBF_Group extends UFBF_Mixed
 // }}}
 
 // {{{ class UFBF_Section
-class UFBF_Section extends UFBF_Index
+class UFBF_Section extends UFBF_Mixed
 {
     protected $direnum = DirEnum::SECTIONS;
 
@@ -895,38 +959,38 @@ class UFBF_Section extends UFBF_Index
 }
 // }}}
 
-// {{{ class UFBF_Formation
-class UFBF_Formation extends UFBF_Mixed
+// {{{ class UFBF_EducationSchool
+class UFBF_EducationSchool extends UFBF_Mixed
 {
-    protected $direnum = DirEnum::SCHOOLS;
+    protected $direnum = DirEnum::EDUSCHOOLS;
 
     protected function buildUFC(UserFilterBuilder &$ufb)
     {
-        return new UFC_Formation($this->val);
+        return new UFC_EducationSchool($this->val);
     }
 }
 // }}}
 
-// {{{ class UFBF_Diploma
-class UFBF_Diploma extends UFBF_Mixed
+// {{{ class UFBF_EducationDegree
+class UFBF_EducationDegree extends UFBF_Mixed
 {
-    protected $direnum = DirEnum::DEGREES;
+    protected $direnum = DirEnum::EDUDEGREES;
 
     protected function buildUFC(UserFilterBuilder &$ufb)
     {
-        return new UFC_Diploma($this->val);
+        return new UFC_EducationDegree($this->val);
     }
 }
 // }}}
 
-// {{{ class UFBF_StudiesDomain
-class UFBF_StudiesDomain extends UFBF_Mixed
+// {{{ class UFBF_EducationField
+class UFBF_EducationField extends UFBF_Mixed
 {
-    protected $direnum = DirEnum::STUDIESDOMAINS;
+    protected $direnum = DirEnum::EDUFIELDS;
 
     protected function buildUFC(UserFilterBuilder &$ufb)
     {
-        return new UFC_StudyField($this->val);
+        return new UFC_EducationField($this->val);
     }
 }
 // }}}
@@ -973,10 +1037,57 @@ class UFBF_Networking extends UFBF_Text
         }
     }
 
+    public function isEmpty()
+    {
+        return parent::isEmpty() || $this->nwtype == 0;
+    }
+
     public function buildUFC(UserFilterBuilder &$ufb)
     {
         return new UFC_Networking($this->nwtype, $this->val);
     }
 }
 // }}}
+
+// {{{ class UFBF_Mentor
+class UFBF_Mentor extends UFBF_Bool
+{
+    protected function buildUFC(UserFilterBuilder &$ufb)
+    {
+        return new UFC_Mentor();
+    }
+}
+// }}}
+
+// {{{ class UFBF_MentorCountry
+class UFBF_MentorCountry extends UFBF_Text
+{
+    protected function buildUFC(UserFilterBuilder &$ufb)
+    {
+        return new UFC_Mentor_Country($this->val);
+    }
+}
+// }}}
+
+// {{{ class UFBF_Mentorterm
+class UFBF_MentorTerm extends UFBF_Index
+{
+    protected function buildUFC(UserFilterBuilder &$ufb)
+    {
+        return new UFC_Mentor_Terms($this->val);
+    }
+}
+// }}}
+
+// {{{ class UFBF_MentorExpertise
+class UFBF_MentorExpertise extends UFBF_Text
+{
+    protected function buildUFC(UserFilterBuilder &$ufb)
+    {
+        return new UFC_Mentor_Expertise($this->val);
+    }
+}
+// }}}
+
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>