Renames user_id to uid accordingly to our naming convention.
[platal.git] / classes / userfilter.php
index efb03c6..5d07eb0 100644 (file)
@@ -153,6 +153,48 @@ class UFC_Promo implements UserFilterCondition
 }
 // }}}
 
+// {{{ class UFC_SchoolId
+/** Filters users based on their shoold identifier
+ * @param type Parameter type (Xorg, AX, School)
+ * @param value School id value
+ */
+class UFC_SchooldId implements UserFilterCondition
+{
+    const AX     = 'ax';
+    const Xorg   = 'xorg';
+    const School = 'school';
+
+    private $type;
+    private $id;
+
+    static public function assertType($type)
+    {
+        if ($type != self::AX && $type != self::Xorg && $type != self::School) {
+            Platal::page()->killError("Type de matricule invalide: $type");
+        }
+    }
+
+    public function __construct($type, $id)
+    {
+        $this->type = $type;
+        $this->id   = $id;
+        self::assertType($type);
+    }
+
+    public function buildCondition(PlFilter &$uf)
+    {
+        $uf->requireProfiles();
+        $id = $this->id;
+        $type = $this->type;
+        if ($type == self::School) {
+            $type = self::Xorg;
+            $id   = Profile::getXorgId($id);
+        }
+        return XDB::format('p.' . $type . '_id = {?}', $id);
+    }
+}
+// }}}
+
 // {{{ class UFC_EducationSchool
 /** Filters users by formation
  * @param $val The formation to search (either ID or array of IDs)
@@ -1211,7 +1253,7 @@ class UFC_WatchRegistration extends UFC_UserRelated
 {
     public function buildCondition(PlFilter &$uf)
     {
-        if (!$this->user->watch('registration')) {
+        if (!$this->user->watchType('registration')) {
             return PlFilterCondition::COND_FALSE;
         }
         $uids = $this->user->watchUsers();
@@ -1267,6 +1309,25 @@ class UFC_WatchContact extends UFC_Contact
 }
 // }}}
 
+// {{{ class UFC_MarketingHash
+/** Filters users using the hash generated
+ * to send marketing emails to him.
+ */
+class UFC_MarketingHash implements UserFilterCondition
+{
+    private $hash;
+
+    public function __construct($hash)
+    {
+        $this->hash = $hash;
+    }
+
+    public function buildCondition(PlFilter &$uf)
+    {
+        $table = $uf->addMarketingHash();
+        return XDB::format('rm.hash = {?}', $this->hash);
+    }
+}
 
 /******************
  * ORDERS
@@ -1666,17 +1727,47 @@ class UserFilter extends PlFilter
         return $this->getUIDList(null, $limit);
     }
 
+    public function getUID($pos = 0)
+    {
+        $uids =$this->getUIDList(null, new PlFilter(1, $pos));
+        if (count($uids) == 0) {
+            return null;
+        } else {
+            return $uids[0];
+        }
+    }
+
     public function getPIDs($limit = null)
     {
         $limit = self::defaultLimit($limit);
         return $this->getPIDList(null, $limit);
     }
 
+    public function getPID($pos = 0)
+    {
+        $pids =$this->getPIDList(null, new PlFilter(1, $pos));
+        if (count($pids) == 0) {
+            return null;
+        } else {
+            return $pids[0];
+        }
+    }
+
     public function getUsers($limit = null)
     {
         return User::getBulkUsersWithUIDs($this->getUIDs($limit));
     }
 
+    public function getUser($pos = 0)
+    {
+        $uid = $this->getUID($pos);
+        if ($uid == null) {
+            return null;
+        } else {
+            return User::getWithUID($uid);
+        }
+    }
+
     public function iterUsers($limit = null)
     {
         return User::iterOverUIDs($this->getUIDs($limit));
@@ -1687,6 +1778,16 @@ class UserFilter extends PlFilter
         return Profile::getBulkProfilesWithPIDs($this->getPIDs($limit));
     }
 
+    public function getProfile($pos = 0)
+    {
+        $pid = $this->getPID($pos);
+        if ($pid == null) {
+            return null;
+        } else {
+            return Profile::get($pid);
+        }
+    }
+
     public function iterProfiles($limit = null)
     {
         return Profile::iterOverPIDs($this->getPIDs($limit));
@@ -1947,7 +2048,7 @@ class UserFilter extends PlFilter
     static public function assertGrade($grade)
     {
         if (!self::isGrade($grade)) {
-            Platal::page()->killError("Diplôme non valide");
+            Platal::page()->killError("Diplôme non valide: $grade");
         }
     }
 
@@ -1984,11 +2085,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');
             }
@@ -2057,7 +2158,7 @@ class UserFilter extends PlFilter
     {
         $joins = array();
         if ($this->with_bi) {
-            $joins['bi'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_binets', '$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, 'profile_binet_enum', '$ME.id = bi.binet_id');
@@ -2103,13 +2204,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) {
@@ -2203,7 +2304,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');
@@ -2278,7 +2379,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');
@@ -2313,7 +2414,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;
     }
@@ -2334,7 +2435,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;
     }
@@ -2354,7 +2455,7 @@ class UserFilter extends PlFilter
     {
         $joins = array();
         if ($this->with_pmed) {
-            $joins['pmed'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_medals', '$ME.uid = $UID');
+            $joins['pmed'] = new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'profile_medals', '$ME.pid = $PID');
         }
         return $joins;
     }
@@ -2389,7 +2490,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;
     }
@@ -2473,6 +2574,25 @@ class UserFilter extends PlFilter
         }
         return $joins;
     }
+
+
+    /** MARKETING
+     */
+    private $with_rm;
+    public function addMarketingHash()
+    {
+        $this->requireAccounts();
+        $this->with_rm = true;
+    }
+
+    protected function marketingJoins()
+    {
+        if ($this->with_rm) {
+            return array('rm' => new PlSqlJoin(PlSqlJoin::MODE_LEFT, 'register_marketing', '$ME.uid = $UID'));
+        } else {
+            return array();
+        }
+    }
 }
 // }}}