Merge commit 'origin/fusionax' into account
[platal.git] / classes / userfilter.php
index 332f16a..ebe69f4 100644 (file)
@@ -161,6 +161,14 @@ class UFC_Or extends UFC_NChildren
     }
 }
 
+class UFC_Profile implements UserFilterCondition
+{
+    public function buildCondition(UserFilter &$uf)
+    {
+        return '$PID IS NOT NULL';
+    }
+}
+
 class UFC_Promo implements UserFilterCondition
 {
 
@@ -433,18 +441,10 @@ class UFC_EmailList implements UserFilterCondition
 
 abstract class UFC_UserRelated implements UserFilterCondition
 {
-    protected $uid;
-    public function __construct($uid = null)
-    {
-        if (is_null($uid)) {
-            $this->uid = S::i('uid');
-        } else if ($uid instanceof PlUser) {
-            $this->uid = $uid->id();
-        } else if (ctype_digit($uid)) {
-            $this->uid = (int)$uid;
-        } else {
-            Platal::page()->kill("Invalid contact type");
-        }
+    protected $user;
+    public function __construct(PlUser &$user)
+    {
+        $this->user =& $user;
     }
 }
 
@@ -452,7 +452,7 @@ class UFC_Contact extends UFC_UserRelated
 {
     public function buildCondition(UserFilter &$uf)
     {
-        $sub = $uf->addContactFilter($this->uid);
+        $sub = $uf->addContactFilter($this->user->id());
         return 'c' . $sub . '.contact IS NOT NULL';
     }
 }
@@ -461,41 +461,52 @@ class UFC_WatchRegistration extends UFC_UserRelated
 {
     public function buildCondition(UserFilter &$uf)
     {
-        $sub = $uf->addWatchRegistrationFilter($this->uid);
-        $su  = $uf->addWatchFilter($this->uid);
-        return 'FIND_IN_SET(\'registration\', w' . $su . '.flags) OR wn' . $sub . '.uid IS NOT NULL';
+        if (!$this->user->watch('registration')) {
+            return UserFilterCondition::COND_FALSE;
+        }
+        $uids = $this->user->watchUsers();
+        if (count($uids) == 0) {
+            return UserFilterCondition::COND_FALSE;
+        } else {
+            return '$UID IN ' . XDB::formatArray($uids);
+        }
     }
 }
 
 class UFC_WatchPromo extends UFC_UserRelated
 {
     private $grade;
-    public function __construct($uid = null, $grade = UserFilter::GRADE_ING)
+    public function __construct(PlUser &$user, $grade = UserFilter::GRADE_ING)
     {
-        parent::__construct($uid);
+        parent::__construct($user);
         $this->grade = $grade;
     }
 
     public function buildCondition(UserFilter &$uf)
     {
-        $sube = $uf->addEducationFilter(true, $this->grade);
-        $subw = $uf->addWatchPromoFilter($this->uid);
-        $field = 'pe' . $sube . '.' . UserFilter::promoYear($this->grade);
-        return $field . ' IS NOT NULL AND ' . $field . ' = wp' . $subw . '.promo';
+        $promos = $this->user->watchPromos();
+        if (count($promos) == 0) {
+            return UserFilterCondition::COND_FALSE;
+        } else {
+            $sube = $uf->addEducationFilter(true, $this->grade);
+            $field = 'pe' . $sube . '.' . UserFilter::promoYear($this->grade);
+            return $field . ' IN ' . XDB::formatArray($promos);
+        }
     }
 }
 
-class UFC_WatchContacts extends UFC_Contact
+class UFC_WatchContact extends UFC_Contact
 {
     public function buildCondition(UserFilter &$uf)
     {
-        $sub = $uf->addWatchFilter($this->uid);
-        return 'FIND_IN_SET(\'contacts\', w' . $sub . '.flags) AND ' . parent::buildCondition($uf);
+        if (!$this->user->watchContacts()) {
+            return UserFilterCondition::COND_FALSE;
+        }
+        return parent::buildCondition($uf);
     }
 }
 
 
-
 /******************
  * ORDERS
  ******************/
@@ -503,6 +514,10 @@ class UFC_WatchContacts extends UFC_Contact
 abstract class UserFilterOrder
 {
     protected $desc = false;
+    public function __construct($desc = false)
+    {
+        $this->desc = $desc;
+    }
 
     public function buildSort(UserFilter &$uf)
     {
@@ -527,8 +542,8 @@ class UFO_Promo extends UserFilterOrder
 
     public function __construct($grade = null, $desc = false)
     {
+        parent::__construct($desc);
         $this->grade = $grade;
-        $this->desc  = $desc;
     }
 
     protected function getSortTokens(UserFilter &$uf)
@@ -551,10 +566,10 @@ class UFO_Name extends UserFilterOrder
 
     public function __construct($type, $variant = null, $particle = false, $desc = false)
     {
+        parent::__construct($desc);
         $this->type = $type;
         $this->variant = $variant;
         $this->particle = $particle;
-        $this->desc = $desc;
     }
 
     protected function getSortTokens(UserFilter &$uf)
@@ -575,17 +590,37 @@ class UFO_Name extends UserFilterOrder
 
 class UFO_Registration extends UserFilterOrder
 {
-    public function __construct($desc = false)
+    protected function getSortTokens(UserFilter &$uf)
     {
-        $this->desc = $desc;
+        return 'a.registration_date';
     }
+}
 
+class UFO_Birthday extends UserFilterOrder
+{
     protected function getSortTokens(UserFilter &$uf)
     {
-        return 'a.registration_date';
+        return 'p.next_birthday';
+    }
+}
+
+class UFO_ProfileUpdate extends UserFilterOrder
+{
+    protected function getSortTokens(UserFilter &$uf)
+    {
+        return 'p.last_change';
+    }
+}
+
+class UFO_Death extends UserFilterOrder
+{
+    protected function getSortTokens(UserFilter &$uf)
+    {
+        return 'p.deathdate';
     }
 }
 
+
 /***********************************
   *********************************
           USER FILTER CLASS
@@ -647,8 +682,8 @@ class UserFilter
             $where = $this->root->buildCondition($this);
             $joins = $this->buildJoins();
             $this->query = 'FROM  accounts AS a
-                      INNER JOIN  account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
-                      INNER JOIN  profiles AS p ON (p.pid = ap.pid)
+                       LEFT JOIN  account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
+                       LEFT JOIN  profiles AS p ON (p.pid = ap.pid)
                                ' . $joins . '
                            WHERE  (' . $where . ')';
         }
@@ -691,14 +726,14 @@ class UserFilter
         $limit = '';
         if (!is_null($count)) {
             if (!is_null($offset)) {
-                $limit = XDB::format('LIMIT {?}, {?}', $offset, $count);
+                $limit = XDB::format('LIMIT {?}, {?}', (int)$offset, (int)$count);
             } else {
-                $limit = XDB::format('LIMIT {?}', $count);
+                $limit = XDB::format('LIMIT {?}', (int)$count);
             }
         }
         $cond = '';
         if (!is_null($uids)) {
-            $cond = ' AND a.uid IN (' . implode(', ', $uids) . ')';
+            $cond = ' AND a.uid IN ' . XDB::formatArray($uids);
         }
         $fetched = XDB::fetchColumn('SELECT SQL_CALC_FOUND_ROWS  a.uid
                                     ' . $this->query . $cond . '
@@ -727,8 +762,13 @@ class UserFilter
         $table = array();
         $uids  = array();
         foreach ($users as $user) {
-            $uids[] = $user->id();
-            $table[$user->id()] = $user;
+            if ($user instanceof PlUser) {
+                $uid = $user->id();
+            } else {
+                $uid = $user;
+            }
+            $uids[] = $uid;
+            $table[$uid] = $user;
         }
         $fetched = $this->getUIDList($uids, $count, $offset);
         $output = array();
@@ -786,6 +826,16 @@ class UserFilter
         return new UserFilter(new UFC_And($min, $max));
     }
 
+    static public function sortByName()
+    {
+        return array(new UFO_Name(self::LASTNAME), new UFO_Name(self::FIRSTNAME));
+    }
+
+    static public function sortByPromo()
+    {
+        return array(new UFO_Promo(), new UFO_Name(self::LASTNAME), new UFO_Name(self::FIRSTNAME));
+    }
+
     static private function getDBSuffix($string)
     {
         return preg_replace('/[^a-z0-9]/i', '', $string);