* $this->view->sortControl = new SortBox( * $this->getRequest()->getActionName(), * $columns * ); * $this->view->sortControl->setRequest($this->getRequest()); * */ class SortBox extends AbstractWidget { /** * An array containing all sort columns with their associated labels * * @var array */ protected $sortFields; /** * The name of the form that will be created * * @var string */ protected $name; /** * A request object used for initial form population * * @var Request */ protected $request; /** * What to apply sort parameters on * * @var Sortable */ protected $query = null; /** * Create a SortBox with the entries from $sortFields * * @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 */ public function __construct($name, array $sortFields) { $this->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); } /** * Apply the parameters from the given request on this SortBox * * @param Request $request The request to use for populating the form * * @return $this */ public function setRequest($request) { $this->request = $request; return $this; } /** * @param Sortable $query * * @return $this */ public function setQuery(Sortable $query) { $this->query = $query; return $this; } public function handleRequest(Request $request = null) { if ($request === null) { $request = Icinga::app()->getFrontController()->getRequest(); } if ($sort = $request->getParam('sort')) { $this->query->order($sort, $request->getParam('dir')); } return $this; } /** * Render this SortBox as HTML * * @return string */ public function render() { $form = new Form(); $form->setTokenDisabled(); $form->setName($this->name); $form->setAttrib('class', 'sort-control inline'); $form->addElement( 'select', 'sort', array( 'autosubmit' => true, 'label' => $this->view()->translate('Sort by'), 'multiOptions' => $this->sortFields ) ); $form->getElement('sort')->setDecorators(array( array('ViewHelper'), array('Label') )); $form->addElement( 'select', 'dir', array( 'autosubmit' => true, 'multiOptions' => array( 'asc' => 'Asc', 'desc' => 'Desc', ), 'decorators' => array( array('ViewHelper') ) ) ); if ($this->request) { $form->populate($this->request->getParams()); } return $form; } }