<?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;
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;
$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);
}
}
}
*/
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;
*/
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;
}
}
}
}
- /** 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()
{
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
/** 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);
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());