|
- <?php
-
-
- namespace yii\data;
-
- use Yii;
- use yii\base\InvalidConfigException;
- use yii\base\Object;
- use yii\helpers\Html;
- use yii\helpers\Inflector;
- use yii\web\Request;
-
-
- class Sort extends Object
- {
-
-
- public $enableMultiSort = false;
-
-
- public $attributes = [];
-
-
- public $sortParam = 'sort';
-
-
- public $defaultOrder;
-
-
- public $route;
-
-
- public $separator = ',';
-
-
- public $params;
-
-
- public $urlManager;
-
-
-
-
- public function init()
- {
- $attributes = [];
- foreach ($this->attributes as $name => $attribute) {
- if (!is_array($attribute)) {
- $attributes[$attribute] = [
- 'asc' => [$attribute => SORT_ASC],
- 'desc' => [$attribute => SORT_DESC],
- ];
- } elseif (!isset($attribute['asc'], $attribute['desc'])) {
- $attributes[$name] = array_merge([
- 'asc' => [$name => SORT_ASC],
- 'desc' => [$name => SORT_DESC],
- ], $attribute);
- } else {
- $attributes[$name] = $attribute;
- }
- }
- $this->attributes = $attributes;
- }
-
-
-
- public function getOrders($recalculate = false)
- {
- $attributeOrders = $this->getAttributeOrders($recalculate);
- $orders = [];
- foreach ($attributeOrders as $attribute => $direction) {
- $definition = $this->attributes[$attribute];
- $columns = $definition[$direction === SORT_ASC ? 'asc' : 'desc'];
- foreach ($columns as $name => $dir) {
- $orders[$name] = $dir;
- }
- }
-
- return $orders;
- }
-
-
-
- private $_attributeOrders;
-
-
-
- public function getAttributeOrders($recalculate = false)
- {
- if ($this->_attributeOrders === null || $recalculate) {
- $this->_attributeOrders = [];
- if (($params = $this->params) === null) {
- $request = Yii::$app->getRequest();
- $params = $request instanceof Request ? $request->getQueryParams() : [];
- }
- if (isset($params[$this->sortParam]) && is_scalar($params[$this->sortParam])) {
- $attributes = explode($this->separator, $params[$this->sortParam]);
- foreach ($attributes as $attribute) {
- $descending = false;
- if (strncmp($attribute, '-', 1) === 0) {
- $descending = true;
- $attribute = substr($attribute, 1);
- }
-
- if (isset($this->attributes[$attribute])) {
- $this->_attributeOrders[$attribute] = $descending ? SORT_DESC : SORT_ASC;
- if (!$this->enableMultiSort) {
- return $this->_attributeOrders;
- }
- }
- }
- }
- if (empty($this->_attributeOrders) && is_array($this->defaultOrder)) {
- $this->_attributeOrders = $this->defaultOrder;
- }
- }
-
- return $this->_attributeOrders;
- }
-
-
-
- public function setAttributeOrders($attributeOrders, $validate = true)
- {
- if ($attributeOrders === null || !$validate) {
- $this->_attributeOrders = $attributeOrders;
- } else {
- $this->_attributeOrders = [];
- foreach ($attributeOrders as $attribute => $order) {
- if (isset($this->attributes[$attribute])) {
- $this->_attributeOrders[$attribute] = $order;
- if (!$this->enableMultiSort) {
- break;
- }
- }
- }
- }
- }
-
-
-
- public function getAttributeOrder($attribute)
- {
- $orders = $this->getAttributeOrders();
-
- return isset($orders[$attribute]) ? $orders[$attribute] : null;
- }
-
-
-
- public function link($attribute, $options = [])
- {
- if (($direction = $this->getAttributeOrder($attribute)) !== null) {
- $class = $direction === SORT_DESC ? 'desc' : 'asc';
- if (isset($options['class'])) {
- $options['class'] .= ' ' . $class;
- } else {
- $options['class'] = $class;
- }
- }
-
- $url = $this->createUrl($attribute);
- $options['data-sort'] = $this->createSortParam($attribute);
-
- if (isset($options['label'])) {
- $label = $options['label'];
- unset($options['label']);
- } else {
- if (isset($this->attributes[$attribute]['label'])) {
- $label = $this->attributes[$attribute]['label'];
- } else {
- $label = Inflector::camel2words($attribute);
- }
- }
-
- return Html::a($label, $url, $options);
- }
-
-
-
- public function createUrl($attribute, $absolute = false)
- {
- if (($params = $this->params) === null) {
- $request = Yii::$app->getRequest();
- $params = $request instanceof Request ? $request->getQueryParams() : [];
- }
- $params[$this->sortParam] = $this->createSortParam($attribute);
- $params[0] = $this->route === null ? Yii::$app->controller->getRoute() : $this->route;
- $urlManager = $this->urlManager === null ? Yii::$app->getUrlManager() : $this->urlManager;
- if ($absolute) {
- return $urlManager->createAbsoluteUrl($params);
- } else {
- return $urlManager->createUrl($params);
- }
- }
-
-
-
- public function createSortParam($attribute)
- {
- if (!isset($this->attributes[$attribute])) {
- throw new InvalidConfigException("Unknown attribute: $attribute");
- }
- $definition = $this->attributes[$attribute];
- $directions = $this->getAttributeOrders();
- if (isset($directions[$attribute])) {
- $direction = $directions[$attribute] === SORT_DESC ? SORT_ASC : SORT_DESC;
- unset($directions[$attribute]);
- } else {
- $direction = isset($definition['default']) ? $definition['default'] : SORT_ASC;
- }
-
- if ($this->enableMultiSort) {
- $directions = array_merge([$attribute => $direction], $directions);
- } else {
- $directions = [$attribute => $direction];
- }
-
- $sorts = [];
- foreach ($directions as $attribute => $direction) {
- $sorts[] = $direction === SORT_DESC ? '-' . $attribute : $attribute;
- }
-
- return implode($this->separator, $sorts);
- }
-
-
-
- public function hasAttribute($name)
- {
- return isset($this->attributes[$name]);
- }
- }
|