Add UFC_Phone
authorRaphaël Barrois <raphael.barrois@polytechnique.org>
Sun, 17 Jan 2010 01:48:19 +0000 (02:48 +0100)
committerRaphaël Barrois <raphael.barrois@polytechnique.org>
Sun, 17 Jan 2010 01:56:17 +0000 (02:56 +0100)
For filtering users by phone number

Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
classes/userfilter.php

index 5470d05..943a0a4 100644 (file)
@@ -776,6 +776,52 @@ class UFC_Networking extends UserFilterCondition
 }
 // }}}
 
+// {{{ class UFC_Phone
+/** Filters users based on their phone number
+ * @param $num_type Type of number (pro/user/home)
+ * @param $phone_type Type of phone (fixed/mobile/fax)
+ * @param $number Phone number
+ */
+class UFC_Phone extends UserFilterCondition
+{
+    const NUM_PRO   = 'pro';
+    const NUM_USER  = 'user';
+    const NUM_HOME  = 'address';
+    const NUM_ANY   = 'any';
+
+    const PHONE_FIXED   = 'fixed';
+    const PHONE_MOBILE  = 'mobile';
+    const PHONE_FAX     = 'fax';
+    const PHONE_ANY     = 'any';
+
+    private $num_type;
+    private $phone_type;
+    private $number;
+
+    public function __construct($number, $num_type = self::NUM_ANY, $phone_type = self::PHONE_ANY)
+    {
+        require_once('profil.inc.php');
+        $this->number = $number;
+        $this->num_type = $num_type;
+        $this->phone_type = format_phone_number($phone_type);
+    }
+
+    public function buildCondition(UserFilter &$uf)
+    {
+        $sub = $uf->addPhoneFilter();
+        $conds = array();
+        $conds[] = $sub . '.search_tel = ' . XDB::format('{?}', $this->number);
+        if ($this->num_type != self::NUM_ANY) {
+            $conds[] = $sub . '.link_type = ' . XDB::format('{?}', $this->num_type);
+        }
+        if ($this->phone_type != self::PHONE_ANY) {
+            $conds[] = $sub . '.tel_type = ' . XDB::format('{?}', $this->phone_type);
+        }
+        return implode(' AND ', $conds);
+    }
+}
+// }}}
+
 // {{{ class UFC_UserRelated
 /** Filters users based on a relation toward on user
  * @param $user User to which searched users are related
@@ -1657,6 +1703,26 @@ class UserFilter
         return $joins;
     }
 
+    /** PHONE
+     */
+
+    private $with_phone = false;
+
+    public function addPhoneFilter()
+    {
+        $this->with_phone = true;
+        return 'ptel';
+    }
+
+    private function phoneJoins()
+    {
+        $joins = array();
+        if ($this->with_phone) {
+            $joins['ptel'] = array('left', 'profile_phone', '$ME.uid = $UID');
+        }
+        return $joins;
+    }
+
     /** CONTACTS
      */
     private $cts = array();