Add sort_name to pluser.
[platal.git] / classes / plfilter.php
index 7771fa4..e67405d 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   *
@@ -47,117 +47,33 @@ class PlLimit
 }
 // }}}
 
-// {{{ class PlSqlJoin
-class PlSqlJoin
+// {{{ class PlFilterOrder
+/** Base class for ordering results of a query.
+ * Parameters for the ordering must be given to the constructor ($desc for a
+ *     descending order).
+ * The getSortTokens function is used to get actual ordering part of the query.
+ */
+abstract class PlFilterOrder implements PlExportable
 {
-    private $mode;
-    private $table;
-    private $condition;
-
-    const MODE_LEFT  = 'LEFT';
-    const MODE_RIGHT = 'RIGHT';
-    const MODE_INNER = 'INNER';
-
-    private function __construct($mode, $params)
-    {
-        $table = array_shift($params);
-        $condition = call_user_func_array(array('XDB', 'format'), $params);
-        if ($mode != self::MODE_LEFT && $mode != self::MODE_RIGHT && $mode != self::MODE_INNER) {
-            Platal::page()->kill("Join mode error: unknown mode $mode");
-            return;
-        }
-        $this->mode = $mode;
-        $this->table = $table;
-        $this->condition = $condition;
-    }
-
-    public function mode()
-    {
-        return $this->mode;
-    }
-
-    public function table()
-    {
-        return $this->table;
-    }
-
-    public function condition()
-    {
-        return $this->condition;
-    }
-
-    /** Replace all "metas" in the condition with their translation.
-     * $ME always becomes the alias of the table
-     * @param $key The name the joined table will have in the final query
-     * @param $joinMetas An array of meta => conversion to apply to the condition
-     */
-    public function replaceJoinMetas($key, $joinMetas = array())
+    protected $desc = false;
+    public function __construct($desc = false)
     {
-        $joinMetas['$ME'] = $key;
-        return str_replace(array_keys($joinMetas), $joinMetas, $this->condition);
+        $this->desc = $desc;
+        $this->_tokens = null;
     }
 
-    /** Create a join command from an array of PlSqlJoin
-     * @param $joins The list of 'join' to convert into an SQL query
-     * @param $joinMetas An array of ('$META' => 'conversion') to apply to the joins.
-     */
-    public static function formatJoins(array $joins, array $joinMetas)
+    protected function buildExport($type)
     {
-        $str = '';
-        foreach ($joins as $key => $join) {
-            if (!($join instanceof PlSqlJoin)) {
-                Platal::page()->kill("Error: not a join: $join");
-            }
-            $mode  = $join->mode();
-            $table = $join->table();
-            $str .= ' ' . $mode . ' JOIN ' . $table . ' AS ' . $key;
-            if ($join->condition() != null) {
-                $str .= ' ON (' . $join->replaceJoinMetas($key, $joinMetas) . ')';
-            }
-            $str .= "\n";
+        $export = array('type' => $type);
+        if ($this->desc) {
+            $export['order'] = 'desc';
         }
-        return $str;
-    }
-
-    /** Build a left join
-     * @param table The name of the table.
-     * @param condition The condition of the jointure
-     */
-    public static function left()
-    {
-        $params = func_get_args();
-        return new PlSqlJoin(self::MODE_LEFT, $params);
-    }
-
-    /** Build a right join
-     * @param table The name of the table.
-     * @param condition The condition of the jointure
-     */
-    public static function right()
-    {
-        $params = func_get_args();
-        return new PlSqlJoin(self::MODE_RIGHT, $params);
-    }
-
-    /** Build a inner join
-     * @param table The name of the table.
-     * @param condition The condition of the jointure
-     */
-    public static function inner()
-    {
-        $params = func_get_args();
-        return new PlSqlJoin(self::MODE_INNER, $params);
+        return $export;
     }
-}
-// }}}
 
-// {{{ class PlFilterOrder
-abstract class PlFilterOrder
-{
-    protected $desc = false;
-    public function __construct($desc = false)
+    public function export()
     {
-        $this->desc = $desc;
+        throw new Exception("This instance is not exportable");
     }
 
     public function toggleDesc()
@@ -170,9 +86,10 @@ abstract class PlFilterOrder
         $this->desc = $desc;
     }
 
-    public function buildSort(PlFilter &$pf)
+    public function buildSort(PlFilter $pf)
     {
         $sel = $this->getSortTokens($pf);
+        $this->_tokens = $sel;
         if (!is_array($sel)) {
             $sel = array($sel);
         }
@@ -184,7 +101,38 @@ abstract class PlFilterOrder
         return $sel;
     }
 
-    abstract protected function getSortTokens(PlFilter &$pf);
+    /** This function must return the tokens to use for ordering
+     * @param $pf The PlFilter whose results must be ordered
+     * @return The name of the field to use for ordering results
+     */
+    abstract protected function getSortTokens(PlFilter $pf);
+}
+// }}}
+
+// {{{ class PlFilterGroupableOrder
+/** Extension of a PlFilterOrder, for orders where the value on which ordering
+ * is done could be used for grouping results (promo, country, ...)
+ */
+abstract class PlFilterGroupableOrder extends PlFilterOrder
+{
+    /** This function will be called when trying to retrieve groups;
+     * the returned token will be used to group the values.
+     * It will always be called AFTER getSortTokens().
+     */
+    public function getGroupToken(PlFilter $pf)
+    {
+        return $this->_tokens;
+    }
+
+    /** This function is called when trying to restrict to one of the
+     * specific values of the group token.
+     *
+     * @return A PlFilterCondition.
+     */
+    public function getCondition($group_value)
+    {
+        return null;
+    }
 }
 // }}}
 
@@ -199,7 +147,7 @@ class PFO_Random extends PlFilterOrder
         $this->seed = $seed;
     }
 
-    protected function getSortTokens(PlFilter &$pf)
+    protected function getSortTokens(PlFilter $pf)
     {
         if ($this->seed == null) {
             return 'RAND()';
@@ -207,16 +155,24 @@ class PFO_Random extends PlFilterOrder
             return XDB::format('RAND({?})', $this->seed);
         }
     }
+
+    public function export()
+    {
+        $export = array('type' => 'random',);
+        if ($this->seed !== null)
+            $export['seed'] = $this->seed;
+        return $export;
+    }
 }
 // }}}
 
 // {{{ interface PlFilterCondition
-interface PlFilterCondition
+interface PlFilterCondition extends PlExportable
 {
     const COND_TRUE  = 'TRUE';
     const COND_FALSE = 'FALSE';
 
-    public function buildCondition(PlFilter &$pf);
+    public function buildCondition(PlFilter $pf);
 }
 // }}}
 
@@ -225,16 +181,21 @@ abstract class PFC_OneChild implements PlFilterCondition
 {
     protected $child;
 
-    public function __construct(&$child = null)
+    public function __construct($child = null)
     {
         if (!is_null($child) && ($child instanceof PlFilterCondition)) {
             $this->setChild($child);
         }
     }
 
-    public function setChild(PlFilterCondition &$cond)
+    public function setChild(PlFilterCondition $cond)
     {
-        $this->child =& $cond;
+        $this->child = $cond;
+    }
+
+    public function export()
+    {
+        return array('child' => $this->child->export());
     }
 }
 // }}}
@@ -251,16 +212,16 @@ abstract class PFC_NChildren implements PlFilterCondition
 
     public function addChildren(array $conds)
     {
-        foreach ($conds as &$cond) {
+        foreach ($conds as $cond) {
             if (!is_null($cond) && ($cond instanceof PlFilterCondition)) {
                 $this->addChild($cond);
             }
         }
     }
 
-    public function addChild(PlFilterCondition &$cond)
+    public function addChild(PlFilterCondition $cond)
     {
-        $this->children[] =& $cond;
+        $this->children[] = $cond;
     }
 
     protected function catConds(array $cond, $op, $fallback)
@@ -273,33 +234,52 @@ abstract class PFC_NChildren implements PlFilterCondition
             return '(' . implode(') ' . $op . ' (', $cond) . ')';
         }
     }
+
+    public function export()
+    {
+        $export = array();
+        foreach ($this->children as $child) {
+            $export[] = $child->export();
+        }
+        return array('children' => $export);
+    }
 }
 // }}}
 
 // {{{ class PFC_True
 class PFC_True implements PlFilterCondition
 {
-    public function buildCondition(PlFilter &$uf)
+    public function buildCondition(PlFilter $uf)
     {
         return self::COND_TRUE;
     }
+
+    public function export()
+    {
+        return array('type' => 'true');
+    }
 }
 // }}}
 
 // {{{ class PFC_False
 class PFC_False implements PlFilterCondition
 {
-    public function buildCondition(PlFilter &$uf)
+    public function buildCondition(PlFilter $uf)
     {
         return self::COND_FALSE;
     }
+
+    public function export()
+    {
+        return array('type' => 'false');
+    }
 }
 // }}}
 
 // {{{ class PFC_Not
 class PFC_Not extends PFC_OneChild
 {
-    public function buildCondition(PlFilter &$uf)
+    public function buildCondition(PlFilter $uf)
     {
         $val = $this->child->buildCondition($uf);
         if ($val == self::COND_TRUE) {
@@ -310,20 +290,27 @@ class PFC_Not extends PFC_OneChild
             return 'NOT (' . $val . ')';
         }
     }
+
+    public function export()
+    {
+        $export = parent::export();
+        $export['type'] = 'not';
+        return $export;
+    }
 }
 // }}}
 
 // {{{ class PFC_And
 class PFC_And extends PFC_NChildren
 {
-    public function buildCondition(PlFilter &$uf)
+    public function buildCondition(PlFilter $uf)
     {
         if (empty($this->children)) {
             return self::COND_FALSE;
         } else {
             $true = self::COND_FALSE;
             $conds = array();
-            foreach ($this->children as &$child) {
+            foreach ($this->children as $child) {
                 $val = $child->buildCondition($uf);
                 if ($val == self::COND_TRUE) {
                     $true = self::COND_TRUE;
@@ -336,20 +323,26 @@ class PFC_And extends PFC_NChildren
             return $this->catConds($conds, 'AND', $true);
         }
     }
+
+    public function export() {
+        $export = parent::export();
+        $export['type'] = 'and';
+        return $export;
+    }
 }
 // }}}
 
 // {{{ class PFC_Or
 class PFC_Or extends PFC_NChildren
 {
-    public function buildCondition(PlFilter &$uf)
+    public function buildCondition(PlFilter $uf)
     {
         if (empty($this->children)) {
             return self::COND_TRUE;
         } else {
             $true = self::COND_TRUE;
             $conds = array();
-            foreach ($this->children as &$child) {
+            foreach ($this->children as $child) {
                 $val = $child->buildCondition($uf);
                 if ($val == self::COND_TRUE) {
                     return self::COND_TRUE;
@@ -362,11 +355,17 @@ class PFC_Or extends PFC_NChildren
             return $this->catConds($conds, 'OR', $true);
         }
     }
+
+    public function export() {
+        $export = parent::export();
+        $export['type'] = 'or';
+        return $export;
+    }
 }
 // }}}
 
 // {{{ class PlFilter
-abstract class PlFilter
+abstract class PlFilter implements PlExportable
 {
     /** Filters objects matching the PlFilter
      * @param $objects The objects to filter
@@ -374,17 +373,35 @@ abstract class PlFilter
      */
     public abstract function filter(array $objects, $limit = null);
 
-    public abstract function setCondition(PlFilterCondition &$cond);
+    public abstract function setCondition(PlFilterCondition $cond);
 
-    public abstract function addSort(PlFilterOrder &$sort);
+    public abstract function addSort(PlFilterOrder $sort);
 
     public abstract function getTotalCount();
 
+    /** Whether this PlFilter can return grouped results through
+     * $this->getGroups();
+     */
+    public abstract function hasGroups();
+
+    /** Used to retrieve value/amount resulting from grouping by the first
+     * given order.
+     */
+    public abstract function getGroups();
+
     /** Get objects, selecting only those within a limit
      * @param $limit The portion of the matching objects to select
      */
     public abstract function get($limit = null);
 
+    /** Get ids, selecting only those within a limit
+     * @param $limit The portion of the matching objects to select
+     */
+    public function getIds($limit = null)
+    {
+        return $this->get($limit);
+    }
+
     /** PRIVATE FUNCTIONS
      */
 
@@ -413,5 +430,5 @@ abstract class PlFilter
 }
 // }}}
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>