/** * Render a sorting link for a particular column. If the collumn is * currently selected, which we can detect using a SelectSort helper, we use the * carets provided in Bootstrap to indicate in which direction it is sorted. * * @param string $content * @param string $fieldId * @param string $direction * @param SelectSort $sorter * @return string */ protected function renderSortLink($content, $fieldId, $direction, SelectSort $sorter = null) { $caret = ''; if ($fieldId === $sorter->getSortedField()->getQueryStringId()) { $activeDirection = $this->getActiveSortDirection($fieldId, $sorter); if ('asc' === $activeDirection) { $caret = ' <span class="caret caret-up"></span>'; } else { $caret = '<span class="caret"></span>'; } } return sprintf('<a href="%s">%s%s</a>', $this->view->escapeHtmlAttr($this->assembleSortUrl($fieldId, $direction)), $this->view->escapeHtml($content), $caret); }
/** * Render a sorting link for a particular column. If the collumn is * currently selected, which we can detect using a SelectSort helper, we use the * carets provided in Bootstrap to indicate in which direction it is sorted. * * @param string $content * @param string $fieldId * @param string $direction * @param SelectSort $sorter * @return string */ protected function renderSortLink($content, $fieldId, $direction, SelectSort $sorter = null) { $request = $this->view->getRequest(); $caret = ''; if ($sorter && $sorter->isSorted() && $sorter->getSortedField()->getQueryStringId() === $fieldId || $fieldId === $request->getQuery('sort')) { if ($sorter) { $activeDir = strtolower($sorter->getSortedDirection()); } else { $activeDir = 'asc' === $request->getQuery('dir') ? 'asc' : 'desc'; } if ('asc' === $activeDir) { $caret = ' <span class="caret caret-up"></span>'; } else { $caret = '<span class="caret"></span>'; } } return sprintf('<a href="%s">%s%s</a>', $this->view->escapeHtmlAttr($this->url($fieldId, $direction)), $this->view->escapeHtml($content), $caret); }
/** * Sort the listing in both ascending and descending order for the * supplied field, catching any exceptions if the sorted query causes * a problem. * * @param FieldInterface $field * @return array */ public function runOnSingleField(FieldInterface $field) { $directions = ['ASC', 'DESC']; $results = []; foreach ($directions as $direction) { $results[$direction] = array('success' => false, 'message' => null, 'sql' => ''); $request = new Request(array(), array($this->listing->getPrefix() . 'sort' => urldecode($field->getQueryStringId()), $this->listing->getPrefix() . 'dir' => $direction)); $this->sortHelper->setRequest($request); try { // Don't need much data here, just need to run the query. $select = $this->listing->getModifiedSelect($this->fields)->limit(1); $results[$direction]['sql'] = (string) $select->reset(Select::LIMIT_COUNT); $select->getAdapter()->fetchAll($select); $results[$direction]['success'] = true; } catch (CorePhpException $e) { $results[$direction]['message'] = $e->getMessage(); } } return $results; }
/** * Render all the <th> cells that will be contained in the <thead>. * * @param Fields $fields * @param TableCellHelper $renderer * @param SelectSort $sorter * @return string */ protected function renderHeadCells(Fields $fields, TableCellHelper $renderer, SelectSort $sorter = null) { $out = ''; foreach ($fields as $index => $field) { $out .= '<th scope="col">'; $content = $renderer->getHeaderRenderer()->render($field); if (!$field->isSortable() || !$sorter) { $out .= $content; } else { $direction = 'asc'; if ($sorter && $sorter->getSortedField() === $field && 'ASC' === $sorter->getSortedDirection()) { $direction = 'desc'; } $out .= $this->renderSortLink($content, $field->getQueryStringId(), $direction, $sorter); } $out .= '</th>'; } return $out; }
protected function getActiveSortDirection($fieldId, SelectSort $sorter = null) { if (!$sorter) { return 'asc' === $this->view->getRequest()->getQuery('dir') ? 'asc' : 'desc'; } else { if ($sorter->isSorted() && $sorter->getSortedField()->getQueryStringId() === $fieldId) { return strtolower($sorter->getSortedDirection()); } } return 'asc'; }