Changelog for the next release.
[platal.git] / classes / plset.php
index 5b0ecb6..1c29af2 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   *
@@ -26,9 +26,9 @@ abstract class PlSet
 {
     const DEFAULT_MAX_RES = 20;
 
-    private $conds   = null;
-    private $orders  = null;
-    private $limit   = null;
+    protected $conds   = null;
+    protected $orders  = array();
+    protected $limit   = null;
 
     protected $count   = null;
 
@@ -37,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;
@@ -45,11 +45,11 @@ abstract class PlSet
             $this->conds = new PFC_And($cond);
         }
 
-        if ($orders instanceof PlFilterOrder) {
-            $this->orders[] = $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);
             }
         }
     }
@@ -86,14 +86,37 @@ abstract class PlSet
      */
     abstract protected function buildFilter(PlFilterCondition &$cond, $orders);
 
-    public function &get(PlLimit $limit = null)
+    /** This function returns the results of the given filter
+     * wihtin $limit ; available when the PlFilter getter isn't the usual get
+     * @param &$pf The filter
+     * @param $limit The PlLimit
+     * @return The results of the filter
+     */
+    protected function &getFilterResults(PlFilter &$pf, PlLimit $limit)
     {
-        $pf = $this->buildFilter($this->conds, $this->orders);
+        $res = $pf->get($limit);
+        return $res;
+    }
+
+    /** 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())
+    {
+        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);
+        $it          = $this->getFilterResults($pf, $limit);
         $this->count = $pf->getTotalCount();
         return $it;
     }
@@ -183,16 +206,16 @@ 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;
         if (is_null($displaytext)) {
@@ -200,7 +223,7 @@ class PlViewOrder
         } else {
             $this->displaytext = $displaytext;
         }
-        $this->pfo = $pfo;
+        $this->pfos = $pfos;
     }
 }
 
@@ -243,7 +266,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()
     {
@@ -252,22 +275,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
@@ -282,7 +297,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
@@ -292,6 +307,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);
 
@@ -301,19 +317,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);
@@ -323,6 +342,9 @@ abstract class MultipageView implements PlView
         $page->assign('order', Env::v('order', $this->defaultkey));
         $page->assign('orders', $this->sortkeys);
         $page->assign_by_ref('plview', $this);
+        if (is_array($res)) {
+            $page->assign('set_keys', array_keys($res));
+        }
         $page->assign_by_ref('set', $res);
         $count = $this->set->count();
         $this->pages = intval(ceil($count / $this->entriesPerPage));