1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\data; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\InvalidConfigException; |
12
|
|
|
use yii\base\Object; |
13
|
|
|
use yii\helpers\Html; |
14
|
|
|
use yii\helpers\Inflector; |
15
|
|
|
use yii\web\Request; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Sort represents information relevant to sorting. |
19
|
|
|
* |
20
|
|
|
* When data needs to be sorted according to one or several attributes, |
21
|
|
|
* we can use Sort to represent the sorting information and generate |
22
|
|
|
* appropriate hyperlinks that can lead to sort actions. |
23
|
|
|
* |
24
|
|
|
* A typical usage example is as follows, |
25
|
|
|
* |
26
|
|
|
* ```php |
27
|
|
|
* public function actionIndex() |
28
|
|
|
* { |
29
|
|
|
* $sort = new Sort([ |
30
|
|
|
* 'attributes' => [ |
31
|
|
|
* 'age', |
32
|
|
|
* 'name' => [ |
33
|
|
|
* 'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC], |
34
|
|
|
* 'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC], |
35
|
|
|
* 'default' => SORT_DESC, |
36
|
|
|
* 'label' => 'Name', |
37
|
|
|
* ], |
38
|
|
|
* ], |
39
|
|
|
* ]); |
40
|
|
|
* |
41
|
|
|
* $models = Article::find() |
42
|
|
|
* ->where(['status' => 1]) |
43
|
|
|
* ->orderBy($sort->orders) |
44
|
|
|
* ->all(); |
45
|
|
|
* |
46
|
|
|
* return $this->render('index', [ |
47
|
|
|
* 'models' => $models, |
48
|
|
|
* 'sort' => $sort, |
49
|
|
|
* ]); |
50
|
|
|
* } |
51
|
|
|
* ``` |
52
|
|
|
* |
53
|
|
|
* View: |
54
|
|
|
* |
55
|
|
|
* ```php |
56
|
|
|
* // display links leading to sort actions |
57
|
|
|
* echo $sort->link('name') . ' | ' . $sort->link('age'); |
58
|
|
|
* |
59
|
|
|
* foreach ($models as $model) { |
60
|
|
|
* // display $model here |
61
|
|
|
* } |
62
|
|
|
* ``` |
63
|
|
|
* |
64
|
|
|
* In the above, we declare two [[attributes]] that support sorting: `name` and `age`. |
65
|
|
|
* We pass the sort information to the Article query so that the query results are |
66
|
|
|
* sorted by the orders specified by the Sort object. In the view, we show two hyperlinks |
67
|
|
|
* that can lead to pages with the data sorted by the corresponding attributes. |
68
|
|
|
* |
69
|
|
|
* For more details and usage information on Sort, see the [guide article on sorting](guide:output-sorting). |
70
|
|
|
* |
71
|
|
|
* @property array $attributeOrders Sort directions indexed by attribute names. Sort direction can be either |
72
|
|
|
* `SORT_ASC` for ascending order or `SORT_DESC` for descending order. Note that the type of this property |
73
|
|
|
* differs in getter and setter. See [[getAttributeOrders()]] and [[setAttributeOrders()]] for details. |
74
|
|
|
* @property array $orders The columns (keys) and their corresponding sort directions (values). This can be |
75
|
|
|
* passed to [[\yii\db\Query::orderBy()]] to construct a DB query. This property is read-only. |
76
|
|
|
* |
77
|
|
|
* @author Qiang Xue <[email protected]> |
78
|
|
|
* @since 2.0 |
79
|
|
|
*/ |
80
|
|
|
class Sort extends Object |
81
|
|
|
{ |
82
|
|
|
/** |
83
|
|
|
* @var bool whether the sorting can be applied to multiple attributes simultaneously. |
84
|
|
|
* Defaults to `false`, which means each time the data can only be sorted by one attribute. |
85
|
|
|
*/ |
86
|
|
|
public $enableMultiSort = false; |
87
|
|
|
/** |
88
|
|
|
* @var array list of attributes that are allowed to be sorted. Its syntax can be |
89
|
|
|
* described using the following example: |
90
|
|
|
* |
91
|
|
|
* ```php |
92
|
|
|
* [ |
93
|
|
|
* 'age', |
94
|
|
|
* 'name' => [ |
95
|
|
|
* 'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC], |
96
|
|
|
* 'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC], |
97
|
|
|
* 'default' => SORT_DESC, |
98
|
|
|
* 'label' => 'Name', |
99
|
|
|
* ], |
100
|
|
|
* ] |
101
|
|
|
* ``` |
102
|
|
|
* |
103
|
|
|
* In the above, two attributes are declared: `age` and `name`. The `age` attribute is |
104
|
|
|
* a simple attribute which is equivalent to the following: |
105
|
|
|
* |
106
|
|
|
* ```php |
107
|
|
|
* 'age' => [ |
108
|
|
|
* 'asc' => ['age' => SORT_ASC], |
109
|
|
|
* 'desc' => ['age' => SORT_DESC], |
110
|
|
|
* 'default' => SORT_ASC, |
111
|
|
|
* 'label' => Inflector::camel2words('age'), |
112
|
|
|
* ] |
113
|
|
|
* ``` |
114
|
|
|
* |
115
|
|
|
* The `name` attribute is a composite attribute: |
116
|
|
|
* |
117
|
|
|
* - The `name` key represents the attribute name which will appear in the URLs leading |
118
|
|
|
* to sort actions. |
119
|
|
|
* - The `asc` and `desc` elements specify how to sort by the attribute in ascending |
120
|
|
|
* and descending orders, respectively. Their values represent the actual columns and |
121
|
|
|
* the directions by which the data should be sorted by. |
122
|
|
|
* - The `default` element specifies by which direction the attribute should be sorted |
123
|
|
|
* if it is not currently sorted (the default value is ascending order). |
124
|
|
|
* - The `label` element specifies what label should be used when calling [[link()]] to create |
125
|
|
|
* a sort link. If not set, [[Inflector::camel2words()]] will be called to get a label. |
126
|
|
|
* Note that it will not be HTML-encoded. |
127
|
|
|
* |
128
|
|
|
* Note that if the Sort object is already created, you can only use the full format |
129
|
|
|
* to configure every attribute. Each attribute must include these elements: `asc` and `desc`. |
130
|
|
|
*/ |
131
|
|
|
public $attributes = []; |
132
|
|
|
/** |
133
|
|
|
* @var string the name of the parameter that specifies which attributes to be sorted |
134
|
|
|
* in which direction. Defaults to `sort`. |
135
|
|
|
* @see params |
136
|
|
|
*/ |
137
|
|
|
public $sortParam = 'sort'; |
138
|
|
|
/** |
139
|
|
|
* @var array the order that should be used when the current request does not specify any order. |
140
|
|
|
* The array keys are attribute names and the array values are the corresponding sort directions. For example, |
141
|
|
|
* |
142
|
|
|
* ```php |
143
|
|
|
* [ |
144
|
|
|
* 'name' => SORT_ASC, |
145
|
|
|
* 'created_at' => SORT_DESC, |
146
|
|
|
* ] |
147
|
|
|
* ``` |
148
|
|
|
* |
149
|
|
|
* @see attributeOrders |
150
|
|
|
*/ |
151
|
|
|
public $defaultOrder; |
152
|
|
|
/** |
153
|
|
|
* @var string the route of the controller action for displaying the sorted contents. |
154
|
|
|
* If not set, it means using the currently requested route. |
155
|
|
|
*/ |
156
|
|
|
public $route; |
157
|
|
|
/** |
158
|
|
|
* @var string the character used to separate different attributes that need to be sorted by. |
159
|
|
|
*/ |
160
|
|
|
public $separator = ','; |
161
|
|
|
/** |
162
|
|
|
* @var array parameters (name => value) that should be used to obtain the current sort directions |
163
|
|
|
* and to create new sort URLs. If not set, `$_GET` will be used instead. |
164
|
|
|
* |
165
|
|
|
* In order to add hash to all links use `array_merge($_GET, ['#' => 'my-hash'])`. |
166
|
|
|
* |
167
|
|
|
* The array element indexed by [[sortParam]] is considered to be the current sort directions. |
168
|
|
|
* If the element does not exist, the [[defaultOrder|default order]] will be used. |
169
|
|
|
* |
170
|
|
|
* @see sortParam |
171
|
|
|
* @see defaultOrder |
172
|
|
|
*/ |
173
|
|
|
public $params; |
174
|
|
|
/** |
175
|
|
|
* @var \yii\web\UrlManager the URL manager used for creating sort URLs. If not set, |
176
|
|
|
* the `urlManager` application component will be used. |
177
|
|
|
*/ |
178
|
|
|
public $urlManager; |
179
|
|
|
|
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Normalizes the [[attributes]] property. |
183
|
|
|
*/ |
184
|
58 |
|
public function init() |
185
|
|
|
{ |
186
|
58 |
|
$attributes = []; |
187
|
58 |
|
foreach ($this->attributes as $name => $attribute) { |
188
|
13 |
|
if (!is_array($attribute)) { |
189
|
10 |
|
$attributes[$attribute] = [ |
190
|
10 |
|
'asc' => [$attribute => SORT_ASC], |
191
|
10 |
|
'desc' => [$attribute => SORT_DESC], |
192
|
|
|
]; |
193
|
10 |
|
} elseif (!isset($attribute['asc'], $attribute['desc'])) { |
194
|
|
|
$attributes[$name] = array_merge([ |
195
|
|
|
'asc' => [$name => SORT_ASC], |
196
|
|
|
'desc' => [$name => SORT_DESC], |
197
|
|
|
], $attribute); |
198
|
|
|
} else { |
199
|
10 |
|
$attributes[$name] = $attribute; |
200
|
|
|
} |
201
|
|
|
} |
202
|
58 |
|
$this->attributes = $attributes; |
203
|
58 |
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Returns the columns and their corresponding sort directions. |
207
|
|
|
* @param bool $recalculate whether to recalculate the sort directions |
208
|
|
|
* @return array the columns (keys) and their corresponding sort directions (values). |
209
|
|
|
* This can be passed to [[\yii\db\Query::orderBy()]] to construct a DB query. |
210
|
|
|
*/ |
211
|
49 |
|
public function getOrders($recalculate = false) |
212
|
|
|
{ |
213
|
49 |
|
$attributeOrders = $this->getAttributeOrders($recalculate); |
214
|
49 |
|
$orders = []; |
215
|
49 |
|
foreach ($attributeOrders as $attribute => $direction) { |
216
|
5 |
|
$definition = $this->attributes[$attribute]; |
217
|
5 |
|
$columns = $definition[$direction === SORT_ASC ? 'asc' : 'desc']; |
218
|
5 |
|
foreach ($columns as $name => $dir) { |
219
|
5 |
|
$orders[$name] = $dir; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
49 |
|
return $orders; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @var array the currently requested sort order as computed by [[getAttributeOrders]]. |
228
|
|
|
*/ |
229
|
|
|
private $_attributeOrders; |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Returns the currently requested sort information. |
233
|
|
|
* @param bool $recalculate whether to recalculate the sort directions |
234
|
|
|
* @return array sort directions indexed by attribute names. |
235
|
|
|
* Sort direction can be either `SORT_ASC` for ascending order or |
236
|
|
|
* `SORT_DESC` for descending order. |
237
|
|
|
*/ |
238
|
58 |
|
public function getAttributeOrders($recalculate = false) |
239
|
|
|
{ |
240
|
58 |
|
if ($this->_attributeOrders === null || $recalculate) { |
241
|
57 |
|
$this->_attributeOrders = []; |
242
|
57 |
|
if (($params = $this->params) === null) { |
243
|
50 |
|
$request = Yii::$app->getRequest(); |
244
|
50 |
|
$params = $request instanceof Request ? $request->getQueryParams() : []; |
245
|
|
|
} |
246
|
57 |
|
if (isset($params[$this->sortParam])) { |
247
|
7 |
|
$attributes = $this->parseSortParam($params[$this->sortParam]); |
248
|
7 |
|
foreach ($attributes as $attribute) { |
249
|
7 |
|
$descending = false; |
250
|
7 |
|
if (strncmp($attribute, '-', 1) === 0) { |
251
|
7 |
|
$descending = true; |
252
|
7 |
|
$attribute = substr($attribute, 1); |
253
|
|
|
} |
254
|
|
|
|
255
|
7 |
|
if (isset($this->attributes[$attribute])) { |
256
|
7 |
|
$this->_attributeOrders[$attribute] = $descending ? SORT_DESC : SORT_ASC; |
257
|
7 |
|
if (!$this->enableMultiSort) { |
258
|
2 |
|
return $this->_attributeOrders; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
} |
263
|
57 |
|
if (empty($this->_attributeOrders) && is_array($this->defaultOrder)) { |
264
|
4 |
|
$this->_attributeOrders = $this->defaultOrder; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
58 |
|
return $this->_attributeOrders; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Parses the value of [[sortParam]] into an array of sort attributes. |
273
|
|
|
* |
274
|
|
|
* The format must be the attribute name only for ascending |
275
|
|
|
* or the attribute name prefixed with `-` for descending. |
276
|
|
|
* |
277
|
|
|
* For example the following return value will result in ascending sort by |
278
|
|
|
* `category` and descending sort by `created_at`: |
279
|
|
|
* |
280
|
|
|
* ```php |
281
|
|
|
* [ |
282
|
|
|
* 'category', |
283
|
|
|
* '-created_at' |
284
|
|
|
* ] |
285
|
|
|
* ``` |
286
|
|
|
* |
287
|
|
|
* @param string $param the value of the [[sortParam]]. |
288
|
|
|
* @return array the valid sort attributes. |
289
|
|
|
* @since 2.0.12 |
290
|
|
|
* @see $separator for the attribute name separator. |
291
|
|
|
* @see $sortParam |
292
|
|
|
*/ |
293
|
6 |
|
protected function parseSortParam($param) |
294
|
|
|
{ |
295
|
6 |
|
return is_scalar($param) ? explode($this->separator, $param) : []; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Sets up the currently sort information. |
300
|
|
|
* @param array|null $attributeOrders sort directions indexed by attribute names. |
301
|
|
|
* Sort direction can be either `SORT_ASC` for ascending order or |
302
|
|
|
* `SORT_DESC` for descending order. |
303
|
|
|
* @param bool $validate whether to validate given attribute orders against [[attributes]] and [[enableMultiSort]]. |
304
|
|
|
* If validation is enabled incorrect entries will be removed. |
305
|
|
|
* @since 2.0.10 |
306
|
|
|
*/ |
307
|
1 |
|
public function setAttributeOrders($attributeOrders, $validate = true) |
308
|
|
|
{ |
309
|
1 |
|
if ($attributeOrders === null || !$validate) { |
310
|
1 |
|
$this->_attributeOrders = $attributeOrders; |
|
|
|
|
311
|
|
|
} else { |
312
|
1 |
|
$this->_attributeOrders = []; |
313
|
1 |
|
foreach ($attributeOrders as $attribute => $order) { |
314
|
1 |
|
if (isset($this->attributes[$attribute])) { |
315
|
1 |
|
$this->_attributeOrders[$attribute] = $order; |
316
|
1 |
|
if (!$this->enableMultiSort) { |
317
|
1 |
|
break; |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
} |
322
|
1 |
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Returns the sort direction of the specified attribute in the current request. |
326
|
|
|
* @param string $attribute the attribute name |
327
|
|
|
* @return bool|null Sort direction of the attribute. Can be either `SORT_ASC` |
328
|
|
|
* for ascending order or `SORT_DESC` for descending order. Null is returned |
329
|
|
|
* if the attribute is invalid or does not need to be sorted. |
330
|
|
|
*/ |
331
|
5 |
|
public function getAttributeOrder($attribute) |
332
|
|
|
{ |
333
|
5 |
|
$orders = $this->getAttributeOrders(); |
334
|
|
|
|
335
|
5 |
|
return isset($orders[$attribute]) ? $orders[$attribute] : null; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Generates a hyperlink that links to the sort action to sort by the specified attribute. |
340
|
|
|
* Based on the sort direction, the CSS class of the generated hyperlink will be appended |
341
|
|
|
* with "asc" or "desc". |
342
|
|
|
* @param string $attribute the attribute name by which the data should be sorted by. |
343
|
|
|
* @param array $options additional HTML attributes for the hyperlink tag. |
344
|
|
|
* There is one special attribute `label` which will be used as the label of the hyperlink. |
345
|
|
|
* If this is not set, the label defined in [[attributes]] will be used. |
346
|
|
|
* If no label is defined, [[\yii\helpers\Inflector::camel2words()]] will be called to get a label. |
347
|
|
|
* Note that it will not be HTML-encoded. |
348
|
|
|
* @return string the generated hyperlink |
349
|
|
|
* @throws InvalidConfigException if the attribute is unknown |
350
|
|
|
*/ |
351
|
3 |
|
public function link($attribute, $options = []) |
352
|
|
|
{ |
353
|
3 |
|
if (($direction = $this->getAttributeOrder($attribute)) !== null) { |
354
|
1 |
|
$class = $direction === SORT_DESC ? 'desc' : 'asc'; |
355
|
1 |
|
if (isset($options['class'])) { |
356
|
|
|
$options['class'] .= ' ' . $class; |
357
|
|
|
} else { |
358
|
1 |
|
$options['class'] = $class; |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
|
362
|
3 |
|
$url = $this->createUrl($attribute); |
363
|
3 |
|
$options['data-sort'] = $this->createSortParam($attribute); |
364
|
|
|
|
365
|
3 |
|
if (isset($options['label'])) { |
366
|
|
|
$label = $options['label']; |
367
|
|
|
unset($options['label']); |
368
|
|
|
} else { |
369
|
3 |
|
if (isset($this->attributes[$attribute]['label'])) { |
370
|
2 |
|
$label = $this->attributes[$attribute]['label']; |
371
|
|
|
} else { |
372
|
1 |
|
$label = Inflector::camel2words($attribute); |
373
|
|
|
} |
374
|
|
|
} |
375
|
|
|
|
376
|
3 |
|
return Html::a($label, $url, $options); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* Creates a URL for sorting the data by the specified attribute. |
381
|
|
|
* This method will consider the current sorting status given by [[attributeOrders]]. |
382
|
|
|
* For example, if the current page already sorts the data by the specified attribute in ascending order, |
383
|
|
|
* then the URL created will lead to a page that sorts the data by the specified attribute in descending order. |
384
|
|
|
* @param string $attribute the attribute name |
385
|
|
|
* @param bool $absolute whether to create an absolute URL. Defaults to `false`. |
386
|
|
|
* @return string the URL for sorting. False if the attribute is invalid. |
387
|
|
|
* @throws InvalidConfigException if the attribute is unknown |
388
|
|
|
* @see attributeOrders |
389
|
|
|
* @see params |
390
|
|
|
*/ |
391
|
4 |
|
public function createUrl($attribute, $absolute = false) |
392
|
|
|
{ |
393
|
4 |
|
if (($params = $this->params) === null) { |
394
|
2 |
|
$request = Yii::$app->getRequest(); |
395
|
2 |
|
$params = $request instanceof Request ? $request->getQueryParams() : []; |
396
|
|
|
} |
397
|
4 |
|
$params[$this->sortParam] = $this->createSortParam($attribute); |
398
|
4 |
|
$params[0] = $this->route === null ? Yii::$app->controller->getRoute() : $this->route; |
399
|
4 |
|
$urlManager = $this->urlManager === null ? Yii::$app->getUrlManager() : $this->urlManager; |
400
|
4 |
|
if ($absolute) { |
401
|
|
|
return $urlManager->createAbsoluteUrl($params); |
402
|
|
|
} else { |
403
|
4 |
|
return $urlManager->createUrl($params); |
404
|
|
|
} |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Creates the sort variable for the specified attribute. |
409
|
|
|
* The newly created sort variable can be used to create a URL that will lead to |
410
|
|
|
* sorting by the specified attribute. |
411
|
|
|
* @param string $attribute the attribute name |
412
|
|
|
* @return string the value of the sort variable |
413
|
|
|
* @throws InvalidConfigException if the specified attribute is not defined in [[attributes]] |
414
|
|
|
*/ |
415
|
5 |
|
public function createSortParam($attribute) |
416
|
|
|
{ |
417
|
5 |
|
if (!isset($this->attributes[$attribute])) { |
418
|
|
|
throw new InvalidConfigException("Unknown attribute: $attribute"); |
419
|
|
|
} |
420
|
5 |
|
$definition = $this->attributes[$attribute]; |
421
|
5 |
|
$directions = $this->getAttributeOrders(); |
422
|
5 |
|
if (isset($directions[$attribute])) { |
423
|
3 |
|
$direction = $directions[$attribute] === SORT_DESC ? SORT_ASC : SORT_DESC; |
424
|
3 |
|
unset($directions[$attribute]); |
425
|
|
|
} else { |
426
|
2 |
|
$direction = isset($definition['default']) ? $definition['default'] : SORT_ASC; |
427
|
|
|
} |
428
|
|
|
|
429
|
5 |
|
if ($this->enableMultiSort) { |
430
|
3 |
|
$directions = array_merge([$attribute => $direction], $directions); |
431
|
|
|
} else { |
432
|
2 |
|
$directions = [$attribute => $direction]; |
433
|
|
|
} |
434
|
|
|
|
435
|
5 |
|
$sorts = []; |
436
|
5 |
|
foreach ($directions as $attribute => $direction) { |
437
|
5 |
|
$sorts[] = $direction === SORT_DESC ? '-' . $attribute : $attribute; |
438
|
|
|
} |
439
|
|
|
|
440
|
5 |
|
return implode($this->separator, $sorts); |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* Returns a value indicating whether the sort definition supports sorting by the named attribute. |
445
|
|
|
* @param string $name the attribute name |
446
|
|
|
* @return bool whether the sort definition supports sorting by the named attribute. |
447
|
|
|
*/ |
448
|
|
|
public function hasAttribute($name) |
449
|
|
|
{ |
450
|
|
|
return isset($this->attributes[$name]); |
451
|
|
|
} |
452
|
|
|
} |
453
|
|
|
|
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needle
but will only accept an array as$haystack
.