Add interface PlIteraface and the corresponding abstract class
[platal.git] / classes / plfilter.php
index 15bcaf5..af8141d 100644 (file)
@@ -19,6 +19,9 @@
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
+__autoload('xdb');
+
+// {{{ class PlLimit
 class PlLimit
 {
     private $count = null;
@@ -42,88 +45,26 @@ class PlLimit
         return '';
     }
 }
+// }}}
 
-class PlSqlJoin
-{
-    private $mode;
-    private $table;
-    private $condition;
-
-    const MODE_LEFT  = 'LEFT';
-    const MODE_RIGHT = 'RIGHT';
-    const MODE_INNER = 'INNER';
-
-    public function __construct($mode, $table, $condition)
-    {
-        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())
-    {
-        $joinMetas['$ME'] = $key;
-        return str_replace(array_keys($joinMetas), $joinMetas, $this->condition);
-    }
-
-    /** 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)
-    {
-        $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";
-        }
-        return $str;
-    }
-}
-
+// {{{ 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
 {
     protected $desc = false;
     public function __construct($desc = false)
     {
         $this->desc = $desc;
+        $this->_tokens = null;
     }
 
     public function toggleDesc()
     {
-        $this->desc = !$desc;
+        $this->desc = !$this->desc;
     }
 
     public function setDescending($desc = true)
@@ -134,6 +75,7 @@ abstract class PlFilterOrder
     public function buildSort(PlFilter &$pf)
     {
         $sel = $this->getSortTokens($pf);
+        $this->_tokens = $sel;
         if (!is_array($sel)) {
             $sel = array($sel);
         }
@@ -145,8 +87,52 @@ abstract class PlFilterOrder
         return $sel;
     }
 
-    abstract protected function getSortTokens(&$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;
+    }
+}
+// }}}
+
+// {{{ class PFO_Random
+class PFO_Random extends PlFilterOrder
+{
+    private $seed = null;
+
+    public function __construct($seed = null, $desc = false)
+    {
+        parent::__construct($desc);
+        $this->seed = $seed;
+    }
+
+    protected function getSortTokens(PlFilter &$pf)
+    {
+        if ($this->seed == null) {
+            return 'RAND()';
+        } else {
+            return XDB::format('RAND({?})', $this->seed);
+        }
+    }
 }
+// }}}
 
 // {{{ interface PlFilterCondition
 interface PlFilterCondition
@@ -184,10 +170,14 @@ abstract class PFC_NChildren implements PlFilterCondition
 
     public function __construct()
     {
-        $children = func_get_args();
-        foreach ($children as &$child) {
-            if (!is_null($child) && ($child instanceof PlFilterCondition)) {
-                $this->addChild($child);
+        $this->addChildren(pl_flatten(func_get_args()));
+    }
+
+    public function addChildren(array $conds)
+    {
+        foreach ($conds as &$cond) {
+            if (!is_null($cond) && ($cond instanceof PlFilterCondition)) {
+                $this->addChild($cond);
             }
         }
     }
@@ -299,14 +289,14 @@ class PFC_Or extends PFC_NChildren
 }
 // }}}
 
-
+// {{{ class PlFilter
 abstract class PlFilter
 {
     /** Filters objects matching the PlFilter
      * @param $objects The objects to filter
      * @param $limit The portion of the matching objects to show
      */
-    public abstract function filter(array $objects, PlLimit &$limit);
+    public abstract function filter(array $objects, $limit = null);
 
     public abstract function setCondition(PlFilterCondition &$cond);
 
@@ -314,10 +304,20 @@ abstract class PlFilter
 
     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(PlLimit &$limit);
+    public abstract function get($limit = null);
 
     /** PRIVATE FUNCTIONS
      */
@@ -345,5 +345,7 @@ abstract class PlFilter
     }
 
 }
+// }}}
 
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>