Add PlEmptyIterator
[platal.git] / classes / plset.php
index 8ef6ba8..b48fc92 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2009 Polytechnique.org                              *
+ *  Copyright (C) 2003-2010 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                *
  ***************************************************************************/
 
+
 /** UserSet is a light-weight Model/View tool for displaying a set of items
  */
 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;
 
@@ -34,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;
@@ -42,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);
             }
         }
     }
@@ -83,10 +86,24 @@ abstract class PlSet
      */
     abstract protected function buildFilter(PlFilterCondition &$cond, $orders);
 
-    public function &get(PlFilterLimit $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(self::DEFAULT_MAX_RES, 0);
+        }
         $it          = $pf->get($limit);
         $this->count = $pf->getTotalCount();
         return $it;
@@ -177,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;
     }
 }
 
@@ -237,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()
     {
@@ -246,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
@@ -276,7 +285,7 @@ abstract class MultipageView implements PlView
 
     public function limit()
     {
-        return null;
+        return new PlLimit($this->entriesPerPage, $this->offset);
     }
 
     /** Name of the template to use for displaying items of the view
@@ -286,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);
 
@@ -295,19 +305,22 @@ 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());
 
         $show_bounds = $this->bounds();
-        $end         = end($res);
         if ($show_bounds) {
+            $start = current($res);
+            $end   = end($res);
             if ($show_bounds == 1) {
-                $first = $this->getBoundValue($res[0]);
+                $first = $this->getBoundValue($start);
                 $last  = $this->getBoundValue($end);
             } elseif ($show_bounds == -1) {
                 $first = $this->getBoundValue($end);
-                $last  = $this->getBoundValue($res[0]);
+                $last  = $this->getBoundValue($start);
             }
             $page->assign('first', $first);
             $page->assign('last', $last);