X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=classes%2Fplset.php;h=68a06d5d31629193ae511ae04616cd16675172c7;hb=38b7dbd95e5d725eb2b7b34a6a8fe5e0c84073f0;hp=d2267d02ad88b1cf192569184987efa49b1d06f7;hpb=1490093c909c086ce8eba3c0f5c24b62ef20cfb3;p=platal.git diff --git a/classes/plset.php b/classes/plset.php index d2267d0..68a06d5 100644 --- a/classes/plset.php +++ b/classes/plset.php @@ -1,6 +1,6 @@ fetchAllAssoc(); $count = XDB::query('SELECT FOUND_ROWS()'); $this->count = intval($count->fetchOneCell()); return $it; } + public function args() + { + $get = $_GET; + unset($get['n']); + return $get; + } + + protected function encodeArgs(array $args, $encode = false) + { + $qs = '?'; + $sep = '&'; + foreach ($args as $k=>$v) { + if (!$encode) { + $k = urlencode($k); + $v = urlencode($v); + } + $qs .= "$k=$v$sep"; + } + return $encode ? urlencode($qs) : $qs; + } + public function &get($fields, $joins, $where, $groupby, $order, $limitcount = null, $limitfrom = null) { if (!is_null($limitcount)) { @@ -95,7 +117,6 @@ class PlSet $limitcount = "LIMIT $limitcount"; } $joins = $this->joins . ' ' . $joins; - $where = $where; if (trim($this->where)) { if (trim($where)) { $where .= ' AND '; @@ -117,7 +138,8 @@ class PlSet { $view = strtolower($view); if (!$view || !class_exists($view . 'View') || !isset($this->mods[$view])) { - $view = $this->default ? $this->default : $this->mods[0]; + reset($this->mods); + $view = $this->default ? $this->default : key($this->mods); } $this->mod = $view; $class = $view . 'View'; @@ -132,16 +154,22 @@ class PlSet return $view; } - public function apply($baseurl, PlatalPage &$page, $view = null, $data = null) + public function apply($baseurl, PlPage &$page, $view = null, $data = null) { $view =& $this->buildView($view, $data); if (is_null($view)) { return false; } + $args = $view->args(); + if (!isset($args['rechercher'])) { + $args['rechercher'] = 'Chercher'; + } $page->changeTpl('core/plset.tpl'); $page->assign('plset_base', $baseurl); $page->assign('plset_mods', $this->mods); $page->assign('plset_mod', $this->mod); + $page->assign('plset_search', $this->encodeArgs($args)); + $page->assign('plset_search_enc', $this->encodeArgs($args, true)); foreach ($this->modParams[$this->mod] as $param=>$value) { $page->assign($this->mod . '_' . $param, $value); } @@ -154,7 +182,8 @@ class PlSet interface PlView { public function __construct(PlSet &$set, $data, array $params); - public function apply(PlatalPage &$page); + public function apply(PlPage &$page); + public function args(); } abstract class MultipageView implements PlView @@ -165,10 +194,14 @@ abstract class MultipageView implements PlView public $page = 1; public $offset = 0; - protected $order = array(); protected $entriesPerPage = 20; protected $params = array(); + protected $sortkeys = array(); + protected $defaultkey = null; + + protected $bound_field = null; + public function __construct(PlSet &$set, $data, array $params) { $this->set =& $set; @@ -192,34 +225,82 @@ abstract class MultipageView implements PlView return null; } + public function bounds() + { + return null; + } + + protected function addSortKey($name, array $keys, $desc, $default = false) + { + $this->sortkeys[$name] = array('keys' => $keys, 'desc' => $desc); + if (!$this->defaultkey || $default) { + $this->defaultkey = $name; + } + } + public function order() { - foreach ($this->order as &$item) { - if ($item{0} == '-') { - $item = substr($item, 1) . ' DESC'; + $order = Env::v('order', $this->defaultkey); + $invert = ($order{0} == '-'); + if ($invert) { + $order = substr($order, 1); + } + $list = array(); + foreach ($this->sortkeys[$order]['keys'] as $item) { + $desc = ($item{0} == '-'); + if ($desc) { + $item = substr($item, 1); + } + if ($desc xor $invert) { + $item .= ' DESC'; } + $list[] = $item; } - return implode(', ', $this->order); + return implode(', ', $list); } abstract public function templateName(); - public function apply(PlatalPage &$page) + public function apply(PlPage &$page) { + $res = $this->set->get($this->fields(), + $this->joins(), + $this->where(), + $this->groupBy(), + $this->order(), + $this->entriesPerPage, + $this->offset); + $show_bounds = $this->bounds(); + $end = end($res); + if ($show_bounds) { + if ($show_bounds == 1) { + $first = $res[0][$this->bound_field]; + $last = $end[$this->bound_field]; + } elseif ($show_bounds == -1) { + $first = $end[$this->bound_field]; + $last = $res[0][$this->bound_field]; + } + $page->assign('first', $first); + $page->assign('last', $last); + } + + $page->assign('show_bounds', $show_bounds); + $page->assign('order', Env::v('order', $this->defaultkey)); + $page->assign('orders', $this->sortkeys); $page->assign_by_ref('plview', $this); - $page->assign_by_ref('set', - $this->set->get($this->fields(), - $this->joins(), - $this->where(), - $this->groupBy(), - $this->order(), - $this->entriesPerPage, - $this->offset)); + $page->assign_by_ref('set', $res); $count = $this->set->count(); $this->pages = intval(ceil($count / $this->entriesPerPage)); - return 'include/plview.multipage.tpl'; } + + public function args() + { + $list = $this->set->args(); + unset($list['page']); + unset($list['order']); + return $list; + } } // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: