Merge branch 'account'
[platal.git] / include / ufbuilder.inc.php
index ce41d06..9385cb1 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
 {
@@ -28,6 +26,7 @@ class UserFilterBuilder
     private $fields;
     private $valid = true;
     private $ufc = null;
+    private $orders = array();
 
     /** Constructor
      * @param $fields An array of UFB_Field objects
@@ -61,16 +60,32 @@ class UserFilterBuilder
         $this->ufc->addChild($cond);
     }
 
+    public function addOrder(PlFilterOrder &$order)
+    {
+        $this->order[] = $order;
+    }
+
     public function isValid()
     {
         $this->buildUFC();
         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) {
@@ -80,26 +95,67 @@ class UserFilterBuilder
         }
     }
 
+    /** Returns adequate orders
+     */
+    public function getOrders()
+    {
+        $this->buildUFC();
+        return $this->orders;
+    }
+
     /** 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 v($key, $def = null) {
+    public function i($key, $def = 0)
+    {
+        return Env::i($this->envprefix . $key, $def);
+    }
+
+    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 isOn($key)
+    {
+        return $this->has($key) && $this->t($key) == 'on';
+    }
+}
+// }}}
+
+// {{{ class UFB_QuickSearch
+class UFB_QuickSearch extends UserFilterBuilder
+{
+    public function __construct($envprefix = '')
+    {
+        $fields = array(
+            new UFBF_Quick('quick', 'Recherche rapide'),
+        );
+        parent::__construct($fields, $envprefix);
     }
 }
 // }}}
@@ -131,11 +187,29 @@ class UFB_AdvancedSearch extends UserFilterBuilder
             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'),
+        );
+        parent::__construct($fields, $envprefix);
+    }
+}
+// }}}
+
+// {{{ class UFB_MentorSearch
+class UFB_MentorSearch extends UserFilterBuilder
+{
+    public function __construct($envprefix = '')
+    {
+        $fields = array(
+            new UFBF_MentorCountry('pays_sel'),
+            new UFBF_MentorSectorization('sector', '', UFC_Mentor_Sectorization::SECTOR),
+            new UFBF_MentorSectorization('subSector', '', UFC_Mentor_Sectorization::SUBSECTOR),
+            new UFBF_MentorExpertise('expertise'),
         );
         parent::__construct($fields, $envprefix);
     }
@@ -190,6 +264,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
@@ -207,31 +286,32 @@ abstract class UFB_Field
 // {{{ class UFBF_Text
 abstract class UFBF_Text extends UFB_Field
 {
-    private $forbiddenchars;
     private $minlength;
     private $maxlength;
 
-    public function __construct($envfield, $formtext = '', $forbiddenchars = '', $minlength = 2, $maxlength = 255)
+    public function __construct($envfield, $formtext = '', $minlength = 2, $maxlength = 255)
     {
         parent::__construct($envfield, $formtext);
-        $this->forbiddenchars = $forbiddenchars;
         $this->minlength      = $minlength;
         $this->maxlength      = $maxlength;
     }
 
     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)) {
+            return $this->raise('Le champ %s contient un caractère interdit rendant la recherche impossible.');
         }
+
         return true;
     }
 }
@@ -255,7 +335,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;
         }
@@ -278,9 +358,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;
     }
 }
@@ -302,7 +383,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;
         }
@@ -325,12 +406,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;
     }
 }
@@ -355,12 +436,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->blank($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);
@@ -369,8 +450,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;
             }
@@ -381,6 +462,105 @@ abstract class UFBF_Mixed extends UFB_Field
 }
 // }}}
 
+// {{{ class UFBF_Quick
+class UFBF_Quick extends UFB_Field
+{
+    protected function check(UserFilterBuilder &$ufb)
+    {
+        if ($ufb->blank($this->envfield)) {
+            $this->empty = true;
+            return true;
+        }
+
+        $this->val = str_replace('*', '%', replace_accent($ufb->t($this->envfield)));
+
+        return true;
+    }
+
+    protected function buildUFC(UserFilterBuilder &$ufb)
+    {
+
+        $r = $s = $this->val;
+
+        /** Admin: Email, IP
+         */
+        if (S::admin() && strpos($s, '@') !== false) {
+            return new UFC_Email($s);
+        } else if (S::admin() && preg_match('/[0-9]+\.([0-9]+|%)\.([0-9]+|%)\.([0-9]+|%)/', $s)) {
+            $this->conds->addChild(new UFC_Ip($s));
+            return;
+        }
+
+        $conds = new PFC_And();
+
+        /** Name
+         */
+        $s = preg_replace('!\d+!', ' ', $s);
+        $strings = preg_split("![^a-zA-Z%]+!",$s, -1, PREG_SPLIT_NO_EMPTY);
+        if (count($strings) > 5) {
+            Platal::page()->trigWarning("Tu as indiqué trop d'éléments dans ta recherche, seuls les 5 premiers seront pris en compte");
+            $strings = array_slice($strings, 0, 5);
+        }
+
+        if (count($strings)) {
+            if (S::logged()) {
+                $flags = array();
+            } else {
+                $flags = array('public');
+            }
+            if ($ufb->b('with_soundex')) {
+                $soundex = true;
+                $st = array();
+                foreach ($strings as $string) {
+                    $st[] = soundex_fr($string);
+                }
+            } else {
+                $soundex = false;
+                $st = $strings;
+            }
+            $exact =$ufb->b('exact');
+            $conds->addChild(new UFC_NameTokens($st, $flags, $soundex, $exact));
+
+            $ufb->addOrder(new UFO_Score());
+        }
+
+        /** Promo ranges
+         */
+        $s = preg_replace('! *- *!', '-', $r);
+        $s = preg_replace('!([<>]) *!', ' \1', $s);
+        $s = preg_replace('![^0-9\-><]!', ' ', $s);
+        $s = preg_replace('![<>\-] !', '', $s);
+        $ranges = preg_split('! +!', $s, -1, PREG_SPLIT_NO_EMPTY);
+        foreach ($ranges as $r) {
+            if (preg_match('!^\d{4}$!', $r)) {
+                $conds->addChild(new UFC_Promo('=', UserFilter::DISPLAY, 'X' . $r));
+            } elseif (preg_match('!^(\d{4})-(\d{4})$!', $r, $matches)) {
+                $p1=min(intval($matches[1]), intval($matches[2]));
+                $p2=max(intval($matches[1]), intval($matches[2]));
+                $conds->addChild(new PFC_And(
+                    new UFC_Promo('>=', UserFilter::DISPLAY, 'X' . $p1),
+                    new UFC_Promo('<=', UserFilter::DISPLAY, 'X' . $p2)
+                ));
+            } elseif (preg_match('!^<(\d{4})!', $r, $matches)) {
+                $conds->addChild(new UFC_Promo('<=', UserFilter::DISPLAY, 'X' . $matches[1]));
+            } elseif (preg_match('!^>(\d{4})!', $r, $matches)) {
+                $conds->addChild(new UFC_Promo('>=', UserFilter::DISPLAY, 'X' . $matches[1]));
+            }
+        }
+
+        /** Phone number
+         */
+        $t = preg_replace('!(\d{4}-\d{4}|>\d{4}|<\d{4})!', '', $s);
+        $t = preg_replace('![<>\- ]!', '', $t);
+        if (strlen($t) > 4) {
+            $conds->addChild(new UFC_Phone($t));
+        }
+
+        return $conds;
+    }
+}
+// }}}
+
 // {{{ class UFBF_Name
 class UFBF_Name extends UFBF_Text
 {
@@ -399,7 +579,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'));
     }
 }
 // }}}
@@ -419,7 +599,7 @@ 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;
         }
@@ -527,7 +707,7 @@ class UFBF_Town extends UFBF_Text
     {
         $this->type = $type;
         $this->onlycurrentfield = $onlycurrentfield;
-        parent::__construct($envfield, $formtext, '', 2, 30);
+        parent::__construct($envfield, $formtext, 2, 30);
     }
 
     protected function buildUFC(UserFilterBuilder &$ufb)
@@ -763,38 +943,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);
     }
 }
 // }}}
@@ -808,4 +988,83 @@ class UFBF_Comment extends UFBF_Text
     }
 }
 // }}}
+
+// {{{ class UFBF_Phone
+class UFBF_Phone extends UFBF_Text
+{
+    protected function buildUFC(UserFilterBuilder &$ufb)
+    {
+        return new UFC_Phone($this->val);
+    }
+}
+// }}}
+
+// {{{ class UFBF_Networking
+class UFBF_Networking extends UFBF_Text
+{
+    private $networktypefield;
+    private $nwtype;
+
+    public function __construct($envfield, $networktypefield, $formtext = '')
+    {
+        parent::__construct($envfield, $formtext);
+        $this->networktypefield  = $networktypefield;
+    }
+
+    public function check(UserFilterBuilder &$ufb)
+    {
+        if (parent::check($ufb)) {
+            $this->nwtype = $ufb->i($this->networktypefield);
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+    public function buildUFC(UserFilterBuilder &$ufb)
+    {
+        return new UFC_Networking($this->nwtype, $this->val);
+    }
+}
+// }}}
+
+// {{{ class UFBF_MentorCountry
+class UFBF_MentorCountry extends UFBF_Index
+{
+    protected function buildUFC(UserFilterBuilder &$ufb)
+    {
+        return new UFC_Mentor_Country($this->val);
+    }
+}
+// }}}
+
+// {{{ class UFBF_MentorSectorization
+class UFBF_MentorSectorization extends UFBF_Index
+{
+    protected $type;
+
+    public function __construct($envfield, $formtext = '', $type = UFC_Mentor_Sectorization::SECTOR)
+    {
+        parent::__construct($envfield, $formtext);
+        $this->type = $type;
+    }
+
+    protected function buildUFC(UserFilterBuilder &$ufb)
+    {
+        return new UFC_Mentor_Sectorization($this->val, $this->type);
+    }
+}
+// }}}
+
+// {{{ 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:
 ?>