Add PlImage, short wrapper to send images.
[platal.git] / classes / plset.php
index 93afc3f..baac072 100644 (file)
  */
 abstract class PlSet
 {
-    private $conds   = null;
-    private $orders  = null;
-    private $limit   = null;
+    const DEFAULT_MAX_RES = 20;
+
+    protected $conds   = null;
+    protected $orders  = array();
+    protected $limit   = null;
 
     protected $count   = null;
 
@@ -35,7 +37,7 @@ abstract class PlSet
     private $mod       = null;
     private $default   = null;
 
-    public function __construct(PlFilterCondition &$cond, $orders)
+    public function __construct(PlFilterCondition &$cond, $orders = null)
     {
         if ($cond instanceof PFC_And) {
             $this->conds = $cond;
@@ -43,11 +45,11 @@ abstract class PlSet
             $this->conds = new PFC_And($cond);
         }
 
-        if ($orders instanceof PlFilterOrder) {
-            $this->orders = array($order);
-        } else {
+        if (!is_null($orders) && $orders instanceof PlFilterOrder) {
+            $this->addSort($orders);
+        } else if (is_array($orders)){
             foreach ($orders as $order) {
-                $this->orders[] = $order;
+                $this->addSort($order);
             }
         }
     }
@@ -84,12 +86,23 @@ abstract class PlSet
      */
     abstract protected function buildFilter(PlFilterCondition &$cond, $orders);
 
-    public function &get(PlLimit $limit = null)
+    /** This function returns the values of the set
+     * @param $limit A PlLimit for selecting users
+     * @param $orders An optional array of PFO to use before the "default" ones
+     * @return The result of $pf->get();
+     */
+    public function &get(PlLimit $limit = null, $orders = array())
     {
-        $pf = $this->buildFilter($this->conds, $this->orders);
+        if (!is_array($orders)) {
+            $orders = array($orders);
+        }
+
+        $orders = array_merge($orders, $this->orders);
+
+        $pf = $this->buildFilter($this->conds, $orders);
 
         if (is_null($limit)) {
-            $limit = new PlLimit(20, 0);
+            $limit = new PlLimit(self::DEFAULT_MAX_RES, 0);
         }
         $it          = $pf->get($limit);
         $this->count = $pf->getTotalCount();
@@ -181,24 +194,24 @@ interface PlView
  */
 class PlViewOrder
 {
-    public $pfo         = null;
+    public $pfos        = null;
     public $name        = null;
     public $displaytext = null;
 
     /** Build a PlViewOrder
      * @param $name Name of the order (key)
      * @param $displaytext Text to display
-     * @param $pfo PlFilterOrder for the order
+     * @param $pfos Array of PlFilterOrder for the order
      */
-    public function __construct($name, PlFilterOrder &$pfo, $displaytext = null)
+    public function __construct($name, $pfos, $displaytext = null)
     {
-        $this->name         = $name;
+        $this->name = $name;
         if (is_null($displaytext)) {
             $this->displaytext = ucfirst($name);
         } else {
-            $this->displaytext  = $displaytext;
+            $this->displaytext = $displaytext;
         }
-        $this->pfo        = $pfo;
+        $this->pfos = $pfos;
     }
 }
 
@@ -241,7 +254,7 @@ abstract class MultipageView implements PlView
         }
     }
 
-    /** Returns a list of PFO objects, the "default" one first
+    /** Returns a list of PFO objects in accordance with the user's choice
      */
     public function order()
     {
@@ -250,22 +263,14 @@ abstract class MultipageView implements PlView
         if ($invert) {
             $order = substr($order, 1);
         }
-        $list = array();
-        if (count($this->sortkeys)) {
-            $list[0] = null;
-        }
-        foreach ($this->sortkeys as $name => $sort) {
-            $desc = $sort->pfo->isDescending();;
-            if ($invert) {
-                $sort->pfo->toggleDesc();
-            }
-            if ($name == $order) {
-                $list[0] = $sort->pfo;
-            } else {
-                $list[] = $sort->pfo;
+
+        $ordering = $this->sortkeys[$order];
+        if ($invert) {
+            foreach ($ordering->pfos as $pfo) {
+                $pfo->toggleDesc();
             }
         }
-        return $list;
+        return $ordering->pfos;
     }
 
     /** Returns information on the order of bounds
@@ -290,6 +295,7 @@ abstract class MultipageView implements PlView
     /** Returns the value of a boundary of the current view (in order
      * to show "from C to F")
      * @param $obj The boundary result whose value must be shown to the user
+     * @return The bound
      */
     abstract protected function getBoundValue($obj);
 
@@ -299,7 +305,9 @@ abstract class MultipageView implements PlView
     public function apply(PlPage &$page)
     {
         foreach ($this->order() as $order) {
-            $this->set->addSort($order->pfo);
+            if (!is_null($order)) {
+                $this->set->addSort($order);
+            }
         }
         $res = $this->set->get($this->limit());