Fixes lists in miniwiki.
[platal.git] / classes / plset.php
index 106c18a..4d28f86 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2010 Polytechnique.org                              *
+ *  Copyright (C) 2003-2011 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -31,6 +31,11 @@ abstract class PlSet
     protected $limit   = null;
 
     protected $count   = null;
+    protected $total_count = null;
+    protected $groups = false;
+
+    // Handle the "restrict to values of the current order"
+    protected $restrict_to = null;
 
     // A list of available views
     private $mods      = array();
@@ -41,7 +46,7 @@ abstract class PlSet
     // The default view name
     private $default   = null;
 
-    public function __construct(PlFilterCondition &$cond, $orders = null)
+    public function __construct(PlFilterCondition $cond, $orders = null)
     {
         if ($cond instanceof PFC_And) {
             $this->conds = $cond;
@@ -83,36 +88,64 @@ abstract class PlSet
 
     /** Adds a new sort (on the PlFilter)
      */
-    public function addSort(PlFilterOrder &$order)
+    public function addSort(PlFilterOrder $order)
     {
         $this->orders[] = $order;
     }
 
     /** Adds a new condition to the PlFilter
      */
-    public function addCond(PlFilterCondition &$cond)
+    public function addCond(PlFilterCondition $cond)
     {
         $this->conds->addChild($cond);
     }
 
+    /** Restricts a PlFilter to values of a given PlFilterOrder
+     */
+    public function restrictTo($value)
+    {
+        $this->restrict_to = $value;
+    }
+
     /** This function builds the right kind of PlFilter from given data
      * @param $cond The PlFilterCondition for the filter
      * @param $orders An array of PlFilterOrder for the filter
+     * @return a PlFilter
      */
-    abstract protected function buildFilter(PlFilterCondition &$cond, $orders);
+    abstract protected function buildFilter(PlFilterCondition $cond, $orders);
 
     /** This function returns the results of the given filter
-     * wihtin $limit; can be use to replace the default $pf->get call.
-     * @param &$pf The filter
+     * within $limit; can be use to replace the default $pf->get call.
+     * @param $pf The filter
      * @param $limit The PlLimit
      * @return The results of the filter
      */
-    protected function &getFilterResults(PlFilter &$pf, PlLimit $limit)
+    protected function &getFilterResults(PlFilter $pf, PlLimit $limit)
     {
         $res = $pf->get($limit);
         return $res;
     }
 
+    /** Helper function, calls buildFilter with the adequate condition/orders.
+     * @param $orders Additional orders to use before the default ones.
+     * @return A newly created PlFilter.
+     */
+    private function buildFilterHelper($orders = array(), $extra_cond=null)
+    {
+        if (!is_array($orders)) {
+            $orders = array($orders);
+        }
+        $orders = array_merge($orders, $this->orders);
+
+        if ($extra_cond != null) {
+            $conds = clone $this->conds;
+            $conds->addChild($extra_cond);
+        } else {
+            $conds = $this->conds;
+        }
+        return $this->buildFilter($conds, $orders);
+    }
+
     /** This function returns the values of the set, and sets $count with the
      * total number of results.
      * @param $limit A PlLimit for selecting users
@@ -121,20 +154,45 @@ abstract class PlSet
      */
     public function &get(PlLimit $limit = null, $orders = array())
     {
-        if (!is_array($orders)) {
-            $orders = array($orders);
+        if (is_null($limit)) {
+            $limit = new PlLimit(self::DEFAULT_MAX_RES, 0);
+        }
+        $pf_res = $this->buildFilterHelper($orders);
+        $pf_groups = $pf_res;
+        $this->total_count = $pf_res->getTotalCount();
+        if ($this->restrict_to != null
+            && count($this->orders)
+            && $this->orders[0] instanceof PlFilterGroupableOrder)
+        {
+            $main_order = $this->orders[0];
+            $pf_res = $this->buildFilterHelper($orders, $main_order->getCondition($this->restrict_to));
         }
 
-        $orders = array_merge($orders, $this->orders);
-
-        $pf = $this->buildFilter($this->conds, $orders);
+        $it = $this->getFilterResults($pf_res, $limit);
+        $this->count = $pf_res->getTotalCount();
+        if ($pf_groups->hasGroups()) {
+            $this->groups = $pf_groups->getGroups();
+        } else {
+            $this->groups = null;
+        }
+        return $it;
+    }
 
+    /** This function returns the ids of the set, and sets $count with the
+     * total number of results.
+     * @param $limit A PlLimit for selecting profiles
+     * @param $orders An optional array of PFO to use before the "default" ones
+     * @return The result of $pf->getId();
+     */
+    public function &getIds(PlLimit $limit = null, $orders = array())
+    {
         if (is_null($limit)) {
             $limit = new PlLimit(self::DEFAULT_MAX_RES, 0);
         }
-        $it          = $this->getFilterResults($pf, $limit);
-        $this->count = $pf->getTotalCount();
-        return $it;
+        $pf = $this->buildFilterHelper($orders);
+        $result = $pf->getIds($limit);
+        $this->count = count($result);
+        return $result;
     }
 
     /** Return an array containing all pertinent parameters for this page
@@ -201,7 +259,7 @@ abstract class PlSet
      * @param $page The page in which the view should be loaded
      * @param $view The name of the view; if null, the default one will be used.
      */
-    public function apply($baseurl, PlPage &$page, $view = null)
+    public function apply($baseurl, PlPage $page, $view = null)
     {
         $view =& $this->buildView($view);
         if (is_null($view)) {
@@ -219,6 +277,9 @@ abstract class PlSet
         }
         $page->assign('plset_content', $view->apply($page));
         $page->assign('plset_count', $this->count);
+        $page->assign('plset_total_count', $this->total_count);
+        $page->assign('plset_has_groups', $this->groups != null);
+        $page->assign('plset_groups', $this->groups);
         return true;
     }
 }
@@ -229,7 +290,7 @@ interface PlView
      * @param $set The set
      * @param $params Parameters to tune the view (sort by score, include promo...)
      */
-    public function __construct(PlSet &$set, array $params);
+    public function __construct(PlSet $set, array $params);
 
     /** Applies the view to a page
      * The content of the set is fetched here.
@@ -237,7 +298,7 @@ interface PlView
      * @return The name of the global view template (for displaying the view,
      *              not the items of the set)
      */
-    public function apply(PlPage &$page);
+    public function apply(PlPage $page);
 
     /** As PlSet->args(), returns the ?foo=bar part of the URL for generating
      * this PlSet, after adding the necessary components and removing useless ones.
@@ -279,6 +340,7 @@ abstract class MultipageView implements PlView
 
     public $pages  = 1;
     public $page   = 1;
+    public $restrict = null;
     public $offset = 0;
 
     protected $entriesPerPage = 20;
@@ -293,17 +355,18 @@ abstract class MultipageView implements PlView
      * @param $set The associated PlSet
      * @param $params Parameters of the view
      */
-    public function __construct(PlSet &$set, array $params)
+    public function __construct(PlSet $set, array $params)
     {
         $this->set   =& $set;
         $this->page   = Env::i('page', 1);
+        $this->restrict = Env::s('restrict', null);
         $this->offset = $this->entriesPerPage * ($this->page - 1);
         $this->params = $params;
     }
 
     /** Add an order to the view
      */
-    protected function addSort(PlViewOrder &$pvo, $default = false)
+    protected function addSort(PlViewOrder $pvo, $default = false)
     {
         $this->sortkeys[$pvo->name] = $pvo;
         if (!$this->defaultkey || $default) {
@@ -358,13 +421,16 @@ abstract class MultipageView implements PlView
      */
     abstract protected function getBoundValue($obj);
 
-    public function apply(PlPage &$page)
+    public function apply(PlPage $page)
     {
         foreach ($this->order() as $order) {
             if (!is_null($order)) {
                 $this->set->addSort($order);
             }
         }
+        if ($this->restrict != null) {
+            $this->set->restrictTo($this->restrict);
+        }
         $res = $this->set->get($this->limit());
 
         $show_bounds = $this->bounds();
@@ -385,6 +451,7 @@ abstract class MultipageView implements PlView
         $page->assign('show_bounds', $show_bounds);
         $page->assign('order', Env::v('order', $this->defaultkey));
         $page->assign('orders', $this->sortkeys);
+        $page->assign('restrict', $this->restrict);
         $page->assign_by_ref('plview', $this);
         if (is_array($res)) {
             $page->assign('set_keys', array_keys($res));
@@ -403,6 +470,7 @@ abstract class MultipageView implements PlView
         $list = $this->set->args();
         unset($list['page']);
         unset($list['order']);
+        unset($list['restrict']);
         return $list;
     }
 }