name = $name; $this->sortFields = $sortFields; } /** * Create a SortBox * * @param string $name The name for the SortBox * @param array $sortFields An array containing the columns and their labels to be displayed in the SortBox * * @return SortBox */ public static function create($name, array $sortFields) { return new static($name, $sortFields); } /** * Set the request to fetch sort rules from * * @param Request $request * * @return $this */ public function setRequest($request) { $this->request = $request; return $this; } /** * Set the query to apply sort rules on * * @param Sortable $query * * @return $this */ public function setQuery(Sortable $query) { $this->query = $query; return $this; } /** * Apply the sort rules from the given or current request on the query * * @param Request $request * * @return $this */ public function handleRequest(Request $request = null) { if ($this->query !== null) { if ($request === null) { $request = Icinga::app()->getRequest(); } if (($sort = $request->getParam('sort'))) { $this->query->order($sort, $request->getParam('dir')); } elseif (($dir = $request->getParam('dir'))) { $this->query->order(null, $dir); } } return $this; } /** * Return the default sort rule for the query * * @param string $column An optional column * * @return array An array of two values: $column, $direction */ protected function getSortDefaults($column = null) { $direction = null; if ($this->query !== null && $this->query instanceof SortRules) { $sortRules = $this->query->getSortRules(); if ($column === null) { $column = key($sortRules); } if ($column !== null && isset($sortRules[$column]['order'])) { $direction = strtoupper($sortRules[$column]['order']) === Sortable::SORT_DESC ? 'desc' : 'asc'; } } return array($column, $direction); } /** * Render this SortBox as HTML * * @return string */ public function render() { $columnForm = new Form(); $columnForm->setTokenDisabled(); $columnForm->setName($this->name . '-column'); $columnForm->setAttrib('class', 'inline'); $columnForm->addElement( 'select', 'sort', array( 'autosubmit' => true, 'label' => $this->view()->translate('Sort by'), 'multiOptions' => $this->sortFields, 'decorators' => array( array('ViewHelper'), array('Label') ) ) ); $orderForm = new Form(); $orderForm->setTokenDisabled(); $orderForm->setName($this->name . '-order'); $orderForm->setAttrib('class', 'inline'); $orderForm->addElement( 'select', 'dir', array( 'autosubmit' => true, 'label' => $this->view()->translate('Direction', 'sort direction'), 'multiOptions' => array( 'asc' => $this->view()->translate('Ascending', 'sort direction'), 'desc' => $this->view()->translate('Descending', 'sort direction') ), 'decorators' => array( array('ViewHelper'), array('Label', array('class' => 'no-js')) ) ) ); $column = null; if ($this->request) { $url = $this->request->getUrl(); if ($url->hasParam('sort')) { $column = $url->getParam('sort'); if ($url->hasParam('dir')) { $direction = $url->getParam('dir'); } else { list($_, $direction) = $this->getSortDefaults($column); } } elseif ($url->hasParam('dir')) { $direction = $url->getParam('dir'); list($column, $_) = $this->getSortDefaults(); } } if ($column === null) { list($column, $direction) = $this->getSortDefaults(); } $columnForm->populate(array('sort' => $column)); $orderForm->populate(array('dir' => $direction)); return '
' . $columnForm . $orderForm . '
'; } }