123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- <?php
- namespace yii\data;
- use Yii;
- use yii\base\BaseObject;
- use yii\base\InvalidConfigException;
- use yii\helpers\Html;
- use yii\helpers\Inflector;
- use yii\web\Request;
- class Sort extends BaseObject
- {
-
- 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'];
- if (is_array($columns) || $columns instanceof \Traversable) {
- foreach ($columns as $name => $dir) {
- $orders[$name] = $dir;
- }
- } else {
- $orders[] = $columns;
- }
- }
- 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])) {
- foreach ($this->parseSortParam($params[$this->sortParam]) 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;
- }
-
- protected function parseSortParam($param)
- {
- return is_scalar($param) ? explode($this->separator, $param) : [];
- }
-
- 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);
- }
- 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]);
- }
- }
|