aliases.id => aliases.uid
[platal.git] / classes / userfilter.php
index 52ad12d..63157e5 100644 (file)
@@ -111,7 +111,7 @@ class UFC_Comment implements UserFilterCondition
     public function buildCondition(PlFilter &$uf)
     {
         $uf->requireProfiles();
-        return 'p.freetext LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->text);
+        return 'p.freetext ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->text);
     }
 }
 // }}}
@@ -227,11 +227,11 @@ class UFC_EducationField implements UserFilterCondition
  */
 class UFC_Name implements UserFilterCondition
 {
-    const PREFIX   = 1;
-    const SUFFIX   = 2;
-    const PARTICLE = 7;
-    const VARIANTS = 8;
-    const CONTAINS = 3;
+    const PREFIX   = XDB::WILDCARD_PREFIX; // 0x001
+    const SUFFIX   = XDB::WILDCARD_SUFFIX; // 0x002
+    const CONTAINS = XDB::WILDCARD_CONTAINS; // 0x003
+    const PARTICLE = 0x007; // self::CONTAINS | 0x004
+    const VARIANTS = 0x008;
 
     private $type;
     private $text;
@@ -253,21 +253,12 @@ class UFC_Name implements UserFilterCondition
     public function buildCondition(PlFilter &$uf)
     {
         $left = '$ME.name';
-        $op   = ' LIKE ';
         if (($this->mode & self::PARTICLE) == self::PARTICLE) {
             $left = 'CONCAT($ME.particle, \' \', $ME.name)';
         }
-        if (($this->mode & self::CONTAINS) == 0) {
-            $right = XDB::format('{?}', $this->text);
-            $op    = ' = ';
-        } else if (($this->mode & self::CONTAINS) == self::PREFIX) {
-            $right = XDB::format('CONCAT({?}, \'%\')', $this->text);
-        } else if (($this->mode & self::CONTAINS) == self::SUFFIX) {
-            $right = XDB::format('CONCAT(\'%\', {?})', $this->text);
-        } else {
-            $right = XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->text);
-        }
-        $cond = $left . $op . $right;
+        $right = XDB::formatWildcards($this->mode & self::CONTAINS, $this->text);
+
+        $cond = $left . $right;
         $conds = array($this->buildNameQuery($this->type, null, $cond, $uf));
         if (($this->mode & self::VARIANTS) != 0 && isset(Profile::$name_variants[$this->type])) {
             foreach (Profile::$name_variants[$this->type] as $var) {
@@ -318,7 +309,7 @@ class UFC_NameTokens implements UserFilterCondition
         } else {
             $tokconds = array();
             foreach ($this->tokens as $token) {
-                $tokconds[] = $sub . '.token LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $token);
+                $tokconds[] = $sub . '.token ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $token);
             }
             $conds[] = implode(' OR ', $tokconds);
         }
@@ -718,7 +709,7 @@ abstract class UFC_Address implements UserFilterCondition
 // {{{ class UFC_AddressText
 /** Select users based on their address, using full text search
  * @param $text Text for filter in fulltext search
- * @param $textSearchMode Mode for search (PREFIX, SUFFIX, ...)
+ * @param $textSearchMode Mode for search (one of XDB::WILDCARD_*)
  * @param $type Filter on address type
  * @param $flags Filter on address flags
  * @param $country Filter on address country
@@ -726,16 +717,11 @@ abstract class UFC_Address implements UserFilterCondition
  */
 class UFC_AddressText extends UFC_Address
 {
-    /** Flags for text search
-     */
-    const PREFIX    = 0x0001;
-    const SUFFIX    = 0x0002;
-    const CONTAINS  = 0x0003;
 
     private $text;
     private $textSearchMode;
 
-    public function __construct($text = null, $textSearchMode = self::CONTAINS,
+    public function __construct($text = null, $textSearchMode = XDB::WILDCARD_CONTAINS,
         $type = null, $flags = self::FLAG_ANY, $country = null, $locality = null)
     {
         parent::__construct($type, $flags);
@@ -747,18 +733,7 @@ class UFC_AddressText extends UFC_Address
 
     private function mkMatch($txt)
     {
-        $op = ' LIKE ';
-        if (($this->textSearchMode & self::CONTAINS) == 0) {
-            $right = XDB::format('{?}', $this->text);
-            $op = ' = ';
-        } else if (($this->mode & self::CONTAINS) == self::PREFIX) {
-            $right = XDB::format('CONCAT({?}, \'%\')', $this->text);
-        } else if (($this->mode & self::CONTAINS) == self::SUFFIX) {
-            $right = XDB::format('CONCAT(\'%\', {?})', $this->text);
-        } else {
-            $right = XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->text);
-        }
-        return $op . $right;
+        return XDB::formatWildcards($this->textSearchMode, $txt);
     }
 
     public function buildCondition(PlFilter &$uf)
@@ -1000,25 +975,25 @@ class UFC_Job_Description implements UserFilterCondition
         $conds = array();
         if ($this->fields & UserFilter::JOB_USERDEFINED) {
             $sub = $uf->addJobFilter();
-            $conds[] = $sub . '.description LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description);
+            $conds[] = $sub . '.description ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
         }
         if ($this->fields & UserFilter::JOB_CV) {
             $uf->requireProfiles();
-            $conds[] = 'p.cv LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description);
+            $conds[] = 'p.cv ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
         }
         if ($this->fields & UserFilter::JOB_SECTOR) {
             $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SECTOR);
-            $conds[] = $sub . '.name LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description);
+            $conds[] = $sub . '.name ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
         }
         if ($this->fields & UserFilter::JOB_SUBSECTOR) {
             $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SUBSECTOR);
-            $conds[] = $sub . '.name LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description);
+            $conds[] = $sub . '.name ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
         }
         if ($this->fields & UserFilter::JOB_SUBSUBSECTOR) {
             $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_SUBSUBSECTOR);
-            $conds[] = $sub . '.name LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description);
+            $conds[] = $sub . '.name ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
             $sub = $uf->addJobSectorizationFilter(UserFilter::JOB_ALTERNATES);
-            $conds[] = $sub . '.name LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->description);
+            $conds[] = $sub . '.name ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->description);
         }
         return implode(' OR ', $conds);
     }
@@ -1045,7 +1020,7 @@ class UFC_Networking implements UserFilterCondition
     {
         $sub = $uf->addNetworkingFilter();
         $conds = array();
-        $conds[] = $sub . '.address = ' . XDB::format('CONCAT(\'%\', {?}, \'%\')', $this->value);
+        $conds[] = $sub . '.address ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->value);
         if ($this->type != -1) {
             $conds[] = $sub . '.network_type = ' . XDB::format('{?}', $this->type);
         }
@@ -1145,7 +1120,7 @@ class UFC_Mentor_Expertise implements UserFilterCondition
     public function buildCondition(PlFilter &$uf)
     {
         $sub = $uf->addMentorFilter(UserFilter::MENTOR_EXPERTISE);
-        return $sub . '.expertise LIKE ' . XDB::format('CONCAT(\'%\', {?}, \'%\'', $this->expertise);
+        return $sub . '.expertise ' . XDB::formatWildcards(XDB::WILDCARD_CONTAINS, $this->expertise);
     }
 }
 // }}}
@@ -1755,12 +1730,12 @@ class UserFilter extends PlFilter
         if ($promo_min != 0) {
             $min = new UFC_Promo('>=', self::GRADE_ING, intval($promo_min));
         } else {
-            $min = new UFC_True();
+            $min = new PFC_True();
         }
         if ($promo_max != 0) {
             $max = new UFC_Promo('<=', self::GRADE_ING, intval($promo_max));
         } else {
-            $max = new UFC_True();
+            $max = new PFC_True();
         }
         return new UserFilter(new PFC_And($min, $max));
     }
@@ -2009,11 +1984,11 @@ class UserFilter extends PlFilter
         }
         foreach ($this->pepe as $grade => $sub) {
             if ($this->isGrade($grade)) {
-                $joins['pe' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_education', '$ME.eduid = pee.id AND $ME.uid = $PID');
+                $joins['pe' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_education', '$ME.eduid = pee.id AND $ME.pid = $PID');
                 $joins['pede' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_INNER, 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid AND $ME.abbreviation LIKE ' .
                                                   XDB::format('{?}', $grade));
             } else {
-                $joins['pe' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_education', '$ME.uid = $PID');
+                $joins['pe' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_education', '$ME.pid = $PID');
                 $joins['pee' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_INNER, 'profile_education_enum', '$ME.id = pe' . $sub . '.eduid');
                 $joins['pede' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_INNER, 'profile_education_degree_enum', '$ME.id = pe' . $sub . '.degreeid');
             }
@@ -2082,10 +2057,10 @@ class UserFilter extends PlFilter
     {
         $joins = array();
         if ($this->with_bi) {
-            $joins['bi'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'binets_ins', '$ME.user_id = $PID');
+            $joins['bi'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_binets', '$ME.pid = $PID');
         }
         if ($this->with_bd) {
-            $joins['bd'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'binets_def', '$ME.id = bi.binet_id');
+            $joins['bd'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_binet_enum', '$ME.id = bi.binet_id');
         }
         return $joins;
     }
@@ -2128,13 +2103,13 @@ class UserFilter extends PlFilter
         }
         foreach ($this->al as $sub=>$key) {
             if (is_null($key)) {
-                $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
+                $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\')');
             } else if ($key == self::ALIAS_BEST) {
-                $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'aliases', '$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND  FIND_IN_SET(\'bestalias\', $ME.flags)');
+                $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'aliases', '$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND  FIND_IN_SET(\'bestalias\', $ME.flags)');
             } else if ($key == self::ALIAS_FORLIFE) {
-                $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'aliases', '$ME.id = $UID AND $ME.type = \'a_vie\'');
+                $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'aliases', '$ME.uid = $UID AND $ME.type = \'a_vie\'');
             } else {
-                $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'aliases', XDB::format('$ME.id = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND $ME.alias = {?}', $key));
+                $joins['al' . $sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'aliases', XDB::format('$ME.uid = $UID AND $ME.type IN (\'alias\', \'a_vie\') AND $ME.alias = {?}', $key));
             }
         }
         foreach ($this->ve as $sub=>$key) {
@@ -2228,7 +2203,7 @@ class UserFilter extends PlFilter
     {
         $joins = array();
         if ($this->pc) {
-            $joins['pc'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_corps', '$ME.uid = $UID');
+            $joins['pc'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_corps', '$ME.pid = $PID');
         }
         if ($this->pcr) {
             $joins['pcr'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_corps_rank_enum', '$ME.id = pc.rankid');
@@ -2303,7 +2278,7 @@ class UserFilter extends PlFilter
     {
         $joins = array();
         if ($this->with_pj) {
-            $joins['pj'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_job', '$ME.uid = $UID');
+            $joins['pj'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_job', '$ME.pid = $PID');
         }
         if ($this->with_pje) {
             $joins['pje'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_job_enum', '$ME.id = pj.jobid');
@@ -2338,7 +2313,7 @@ class UserFilter extends PlFilter
     {
         $joins = array();
         if ($this->with_pnw) {
-            $joins['pnw'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_networking', '$ME.uid = $UID');
+            $joins['pnw'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_networking', '$ME.pid = $PID');
         }
         return $joins;
     }
@@ -2359,7 +2334,7 @@ class UserFilter extends PlFilter
     {
         $joins = array();
         if ($this->with_ptel) {
-            $joins['ptel'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_phones', '$ME.uid = $UID');
+            $joins['ptel'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_phones', '$ME.pid = $PID');
         }
         return $joins;
     }
@@ -2379,7 +2354,7 @@ class UserFilter extends PlFilter
     {
         $joins = array();
         if ($this->with_pmed) {
-            $joins['pmed'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_medals_sub', '$ME.uid = $UID');
+            $joins['pmed'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_medals', '$ME.pid = $PID');
         }
         return $joins;
     }
@@ -2414,7 +2389,7 @@ class UserFilter extends PlFilter
     {
         $joins = array();
         foreach ($this->pms as $sub => $tab) {
-            $joins[$sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, $tab, '$ME.uid = $UID');
+            $joins[$sub] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, $tab, '$ME.pid = $PID');
         }
         return $joins;
     }