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\widgets; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\InvalidConfigException; |
12
|
|
|
use yii\base\Widget; |
13
|
|
|
use yii\data\Sort; |
14
|
|
|
use yii\helpers\Html; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* LinkSorter renders a list of sort links for the given sort definition. |
18
|
|
|
* |
19
|
|
|
* LinkSorter will generate a hyperlink for every attribute declared in [[sort]]. |
20
|
|
|
* |
21
|
|
|
* For more details and usage information on LinkSorter, see the [guide article on sorting](guide:output-sorting). |
22
|
|
|
* |
23
|
|
|
* @author Qiang Xue <[email protected]> |
24
|
|
|
* @since 2.0 |
25
|
|
|
*/ |
26
|
|
|
class LinkSorter extends Widget |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var Sort the sort definition |
30
|
|
|
*/ |
31
|
|
|
public $sort; |
32
|
|
|
/** |
33
|
|
|
* @var array list of the attributes that support sorting. If not set, it will be determined |
34
|
|
|
* using [[Sort::attributes]]. |
35
|
|
|
*/ |
36
|
|
|
public $attributes; |
37
|
|
|
/** |
38
|
|
|
* @var array HTML attributes for the sorter container tag. |
39
|
|
|
* @see \yii\helpers\Html::ul() for special attributes. |
40
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
41
|
|
|
*/ |
42
|
|
|
public $options = ['class' => 'sorter']; |
43
|
|
|
/** |
44
|
|
|
* @var array HTML attributes for the link in a sorter container tag which are passed to [[Sort::link()]]. |
45
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
46
|
|
|
* @since 2.0.6 |
47
|
|
|
*/ |
48
|
|
|
public $linkOptions = []; |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Initializes the sorter. |
53
|
|
|
*/ |
54
|
3 |
|
public function init() |
55
|
|
|
{ |
56
|
3 |
|
parent::init(); |
57
|
|
|
|
58
|
3 |
|
if ($this->sort === null) { |
59
|
|
|
throw new InvalidConfigException('The "sort" property must be set.'); |
60
|
|
|
} |
61
|
3 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Executes the widget. |
65
|
|
|
* This method renders the sort links. |
66
|
|
|
* @return string the result of widget execution to be outputted. |
67
|
|
|
*/ |
68
|
2 |
|
public function run() |
69
|
|
|
{ |
70
|
2 |
|
return $this->renderSortLinks(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Renders the sort links. |
75
|
|
|
* @return string the rendering result |
76
|
|
|
*/ |
77
|
2 |
|
protected function renderSortLinks() |
78
|
|
|
{ |
79
|
2 |
|
$attributes = empty($this->attributes) ? array_keys($this->sort->attributes) : $this->attributes; |
80
|
2 |
|
$links = []; |
81
|
2 |
|
foreach ($attributes as $name) { |
82
|
2 |
|
$links[] = $this->sort->link($name, $this->linkOptions); |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
return Html::ul($links, array_merge($this->options, ['encode' => false])); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|