public function testCanFilterWhenGettingSortableFields() { /* @var $field Field */ foreach ($this->fields as $field) { $field->setSortable(true); } $this->assertEquals(4, count($this->fields->getSortableFields())); $filter = new CallbackFilter(function (Field $field) { return 'visible' === $field->getId(); }); $this->assertEquals(1, count($this->fields->getSortableFields($filter))); }
/** * Provide the Fields and Listing objects that will be tested. * * @param Fields $fields * @param Listing $listing * @throws Exception */ public function __construct(Fields $fields, Listing $listing) { $this->fields = $fields->getSortableFields(); $this->listing = $listing; $this->testSortHelperPresence(); }
/** * Given the supplied $fields and \Dewdrop\Request object, find the field * referenced in the query string and apply its sort callback to the query. * * @param Fields $fields * @param Select $select * @throws \Dewdrop\Fields\Exception * @return Select */ public function modifySelect(Fields $fields, Select $select) { $this->sortedField = null; $this->sortedDirection = null; /* @var $field FieldInterface */ foreach ($fields->getSortableFields() as $field) { if ($field->getQueryStringId() === urlencode($this->request->getQuery($this->prefix . 'sort'))) { if ('ASC' === $this->defaultDirection) { $dir = 'DESC' === strtoupper($this->request->getQuery($this->prefix . 'dir')) ? 'DESC' : 'ASC'; } else { $dir = 'ASC' === strtoupper($this->request->getQuery($this->prefix . 'dir')) ? 'ASC' : 'DESC'; } $select = call_user_func($this->getFieldAssignment($field), $select, $dir); if (!$select instanceof Select) { throw new Exception('Your SelectSort callback must return the modified Select object.'); } $this->sortedField = $field; $this->sortedDirection = $dir; return $select; } } // Sort by the first visible field that is also sortable, if no other sort was performed foreach ($fields->getVisibleFields() as $field) { if ($field->isSortable() && (null === $this->defaultField || $this->defaultField === $field)) { $this->sortedField = $field; $this->sortedDirection = $this->defaultDirection; return call_user_func($this->getFieldAssignment($field), $select, $this->defaultDirection); } } return $select; }