name = $name; $this->sortFields = $sortFields; $this->sortDefaults = $sortDefaults; } /** * 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 * @param array $sortDefaults An array containing default sort directions for specific columns * * @return SortBox */ public static function create($name, array $sortFields, array $sortDefaults = null) { return new static($name, $sortFields, $sortDefaults); } /** * 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; } /** * 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 (! empty($this->sortDefaults) && ($column === null || isset($this->sortDefaults[$column]))) { if ($column === null) { reset($this->sortDefaults); $column = key($this->sortDefaults); } $direction = $this->sortDefaults[$column]; } elseif ($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'; } } elseif ($column === null) { reset($this->sortFields); $column = key($this->sortFields); } return array($column, $direction); } /** * 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'))) { list($sort, $dir) = $this->getSortDefaults(); } else { list($_, $dir) = $this->getSortDefaults($sort); } $this->query->order($sort, $request->getParam('dir', $dir)); } return $this; } /** * Render this SortBox as HTML * * @return string */ public function render() { $columnForm = new Form(); $columnForm->setTokenDisabled(); $columnForm->setName($this->name . '-column'); $columnForm->setAttrib('class', 'icinga-controls inline'); $columnForm->addElement( 'select', 'sort', array( 'autosubmit' => true, 'label' => $this->view()->translate('Sort by'), 'multiOptions' => $this->sortFields, 'decorators' => array( array('ViewHelper'), array('Label') ) ) ); $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(); } // TODO(el): ToggleButton :) $toggle = array('asc' => 'sort-name-down', 'desc' => 'sort-name-up'); unset($toggle[isset($direction) ? strtolower($direction) : 'asc']); $newDirection = key($toggle); $icon = current($toggle); $orderForm = new Form(); $orderForm->setTokenDisabled(); $orderForm->setName($this->name . '-order'); $orderForm->setAttrib('class', 'inline sort-direction-control'); $orderForm->addElement( 'hidden', 'dir' ); $orderForm->addElement( 'button', 'btn_submit', array( 'ignore' => true, 'type' => 'submit', 'label' => $this->view()->icon($icon), 'decorators' => array('ViewHelper'), 'escape' => false, 'class' => 'link-button spinner', 'value' => 'submit', 'title' => t('Change sort direction'), ) ); $columnForm->populate(array('sort' => $column)); $orderForm->populate(array('dir' => $newDirection)); return '
' . $columnForm . $orderForm . '
'; } }