/**
* Generates a hyperlink that can be clicked to cause sorting.
* @param Sort $sort the current Sort instance
* @param string $attribute the attribute name
* @param array $options additional HTML attributes for the hyperlink tag.
* There is one special attribute `label` which will be used as the label of the hyperlink.
* If this is not set, the label defined in [[attributes]] will be used.
* If no label is defined, [[\yii\helpers\Inflector::camel2words()]] will be called to get a label.
* Note that it will not be HTML-encoded.
* @return string
*/
public static function sortLink(Sort $sort, $attribute, $options = [])
{
if (($direction = $sort->getAttributeOrder($attribute)) !== null) {
$class = $direction === SORT_DESC ? 'current-sort desc' : 'current-sort asc';
$icon = $direction === SORT_DESC ? 'glyphicon-sort-by-alphabet-alt' : 'glyphicon-sort-by-alphabet';
if (isset($options['class'])) {
$options['class'] .= ' ' . $class;
} else {
$options['class'] = $class;
}
} else {
$icon = 'glyphicon-sort-by-alphabet';
if (isset($options['class'])) {
$options['class'] .= ' asc';
} else {
$options['class'] = 'asc';
}
}
$url = $sort->createUrl($attribute);
$options['data-sort'] = $sort->createSortParam($attribute);
if (isset($options['label'])) {
$label = $options['label'];
unset($options['label']);
} else {
if (isset($sort->attributes[$attribute]['label'])) {
$label = $sort->attributes[$attribute]['label'];
} else {
$label = Inflector::camel2words($attribute);
}
}
$label .= ' <span class="glyphicon ' . $icon . '"></span>';
return Html::a($label, $url, $options);
}