Fixes payment for group non member.
[platal.git] / classes / userfilter / orders.inc.php
index baa587f..b64a8e8 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2010 Polytechnique.org                              *
+ *  Copyright (C) 2003-2011 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
+// {{{ abstract class UserFilterOrder
+/** Class providing factories for the UserFilterOrders.
+ */
+abstract class UserFilterOrders
+{
+    public static function fromExport(array $export)
+    {
+        $export = new PlDict($export);
+        if (!$export->has('type')) {
+            throw new Exception("Missing type in export");
+        }
+        $type = $export->s('type');
+        $desc = ($export->s('order') == 'desc');
+        switch ($type) {
+          case 'promo':
+            return new UFO_Promo($export->v('grade'), $desc);
+
+          case 'name':
+            return new UFO_Name($desc);
+
+          case 'score':
+          case 'registration':
+          case 'birthday':
+          case 'profile_update':
+          case 'death':
+          case 'uid':
+          case 'hruid':
+          case 'pid':
+          case 'hrpid':
+          case 'is_admin':
+            $class = 'UFO_' . str_replace('_', '', $type);
+            return new $class($desc);
+
+          default:
+            throw new Exception("Unknown order field: $type");
+        }
+    }
+}
+// }}}
 // {{{ class UFO_Promo
 /** Orders users by promotion
  * @param $grade Formation whose promotion users should be sorted by (restricts results to users of that formation)
@@ -44,53 +83,57 @@ class UFO_Promo extends PlFilterGroupableOrder
             return 'pd' . $sub . '.promo';
         }
     }
+
+    public function getCondition($promo)
+    {
+        return new UFC_Promo(UserFilterCondition::OP_EQUALS, $this->grade, $promo);
+    }
+
+    public function export()
+    {
+        $export = $this->buildExport('promo');
+        if (!is_null($this->grade)) {
+            $export['grade'] = $this->grade;
+        }
+        return $export;
+    }
 }
 // }}}
-
 // {{{ class UFO_Name
 /** Sorts users by name
- * @param $type Type of name on which to sort (firstname...)
- * @param $variant Variant of that name to use (marital, ordinary...)
- * @param $particle Set to true if particles should be included in the sorting order
  * @param $desc If sort order should be descending
  */
-class UFO_Name extends PlFilterOrder
+class UFO_Name extends PlFilterGroupableOrder
 {
-    private $type;
-    private $variant;
-    private $particle;
+    protected function getSortTokens(PlFilter $uf)
+    {
+        $sub = $uf->addDisplayFilter();
+        $token = 'pd.sort_name';
+        if ($uf->accountsRequired()) {
+            $account_token = Profile::getAccountEquivalentName('sort_name');
+            return 'IFNULL(' . $token . ', a.' . $account_token . ')';
+        } else {
+            return $token;
+        }
+    }
 
-    public function __construct($type, $variant = null, $particle = false, $desc = false)
+    public function getGroupToken(PlFilter $pf)
     {
-        parent::__construct($desc);
-        $this->type = $type;
-        $this->variant = $variant;
-        $this->particle = $particle;
+        return 'SUBSTRING(' . $this->_tokens . ', 1, 1)';
     }
 
-    protected function getSortTokens(PlFilter $uf)
+    public function getCondition($initial)
     {
-        if (Profile::isDisplayName($this->type)) {
-            $sub = $uf->addDisplayFilter();
-            $token = 'pd' . $sub . '.' . $this->type;
-            if ($uf->accountsRequired()) {
-                $account_token = Profile::getAccountEquivalentName($this->type);
-                return 'IFNULL(' . $token . ', a.' . $account_token . ')';
-            } else {
-                return $token;
-            }
-        } else {
-            $sub = $uf->addNameFilter($this->type, $this->variant);
-            if ($this->particle) {
-                return 'CONCAT(pn' . $sub . '.particle, \' \', pn' . $sub . '.name)';
-            } else {
-                return 'pn' . $sub . '.name';
-            }
-        }
+        return new UFC_NameInitial($initial);
+    }
+
+    public function export()
+    {
+        $export = $this->buildExport();
+        return $export;
     }
 }
 // }}}
-
 // {{{ class UFO_Score
 class UFO_Score extends PlFilterOrder
 {
@@ -109,9 +152,13 @@ class UFO_Score extends PlFilterOrder
         }
         return implode(' + ', $scores);
     }
+
+    public function export()
+    {
+        return $this->buildExport('score');
+    }
 }
 // }}}
-
 // {{{ class UFO_Registration
 /** Sorts users based on registration date
  */
@@ -122,9 +169,13 @@ class UFO_Registration extends PlFilterOrder
         $uf->requireAccounts();
         return 'a.registration_date';
     }
+
+    public function export()
+    {
+        return $this->buildExport('registration');
+    }
 }
 // }}}
-
 // {{{ class UFO_Birthday
 /** Sorts users based on next birthday date
  */
@@ -135,9 +186,13 @@ class UFO_Birthday extends PlFilterOrder
         $uf->requireProfiles();
         return 'p.next_birthday';
     }
+
+    public function export()
+    {
+        return $this->buildExport('birthday');
+    }
 }
 // }}}
-
 // {{{ class UFO_ProfileUpdate
 /** Sorts users based on last profile update
  */
@@ -148,9 +203,13 @@ class UFO_ProfileUpdate extends PlFilterOrder
         $uf->requireProfiles();
         return 'p.last_change';
     }
+
+    public function export()
+    {
+        return $this->buildExport('profile_update');
+    }
 }
 // }}}
-
 // {{{ class UFO_Death
 /** Sorts users based on death date
  */
@@ -161,9 +220,13 @@ class UFO_Death extends PlFilterOrder
         $uf->requireProfiles();
         return 'p.deathdate';
     }
+
+    public function export()
+    {
+        return $this->buildExport('death');
+    }
 }
 // }}}
-
 // {{{ class UFO_Uid
 /** Sorts users based on their uid
  */
@@ -174,9 +237,13 @@ class UFO_Uid extends PlFilterOrder
         $uf->requireAccounts();
         return '$UID';
     }
+
+    public function export()
+    {
+        return $this->buildExport('uid');
+    }
 }
 // }}}
-
 // {{{ class UFO_Hruid
 /** Sorts users based on their hruid
  */
@@ -187,9 +254,13 @@ class UFO_Hruid extends PlFilterOrder
         $uf->requireAccounts();
         return 'a.hruid';
     }
+
+    public function export()
+    {
+        return $this->buildExport('hruid');
+    }
 }
 // }}}
-
 // {{{ class UFO_Pid
 /** Sorts users based on their pid
  */
@@ -200,9 +271,13 @@ class UFO_Pid extends PlFilterOrder
         $uf->requireProfiles();
         return '$PID';
     }
+
+    public function export()
+    {
+        return $this->buildExport('pid');
+    }
 }
 // }}}
-
 // {{{ class UFO_Hrpid
 /** Sorts users based on their hrpid
  */
@@ -213,6 +288,28 @@ class UFO_Hrpid extends PlFilterOrder
         $uf->requireProfiles();
         return 'p.hrpid';
     }
+
+    public function export()
+    {
+        return $this->buildExport('hrpid');
+    }
+}
+// }}}
+// {{{ class UFO_IsAdmin
+/** Sorts users, putting admins first
+ */
+class UFO_IsAdmin extends PlFilterOrder
+{
+    protected function getSortTokens(PlFilter $uf)
+    {
+        $uf->requireAccounts();
+        return 'a.is_admin';
+    }
+
+    public function export()
+    {
+        return $this->buildExport('is_admin');
+    }
 }
 // }}}