PlFlagset and PlDBTableEntry are exportable.
[platal.git] / classes / plfilter.php
index c1139df..1a6d525 100644 (file)
@@ -47,117 +47,13 @@ class PlLimit
 }
 // }}}
 
-// {{{ class PlSqlJoin
-class PlSqlJoin
-{
-    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())
-    {
-        $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;
-    }
-
-    /** 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);
-    }
-}
-// }}}
-
 // {{{ 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
+abstract class PlFilterOrder implements PlExportable
 {
     protected $desc = false;
     public function __construct($desc = false)
@@ -203,7 +99,7 @@ abstract class PlFilterOrder
 /** Extension of a PlFilterOrder, for orders where the value on which ordering
  * is done could be used for grouping results (promo, country, ...)
  */
-class PlFilterGroupableOrder extends PlFilterOrder
+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.
@@ -235,11 +131,19 @@ 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';
@@ -264,6 +168,11 @@ abstract class PFC_OneChild implements PlFilterCondition
     {
         $this->child =& $cond;
     }
+
+    public function export()
+    {
+        return array('child' => $child->export());
+    }
 }
 // }}}
 
@@ -301,6 +210,14 @@ 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);
+    }
 }
 // }}}
 
@@ -311,6 +228,11 @@ class PFC_True implements PlFilterCondition
     {
         return self::COND_TRUE;
     }
+
+    public function export()
+    {
+        return array('type' => 'true');
+    }
 }
 // }}}
 
@@ -321,6 +243,11 @@ class PFC_False implements PlFilterCondition
     {
         return self::COND_FALSE;
     }
+
+    public function export()
+    {
+        return array('type' => 'false');
+    }
 }
 // }}}
 
@@ -338,6 +265,13 @@ class PFC_Not extends PFC_OneChild
             return 'NOT (' . $val . ')';
         }
     }
+
+    public function export()
+    {
+        $export = parent::export();
+        $export['type'] = 'not';
+        return $export;
+    }
 }
 // }}}
 
@@ -364,6 +298,12 @@ class PFC_And extends PFC_NChildren
             return $this->catConds($conds, 'AND', $true);
         }
     }
+
+    public function export() {
+        $export = parent::export();
+        $export['type'] = 'and';
+        return $export;
+    }
 }
 // }}}
 
@@ -390,11 +330,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