|
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\helpers\ArrayHelper; |
|
14
|
|
|
use yii\helpers\Html; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* BaseListView is a base class for widgets displaying data from data provider |
|
18
|
|
|
* such as ListView and GridView. |
|
19
|
|
|
* |
|
20
|
|
|
* It provides features like sorting, paging and also filtering the data. |
|
21
|
|
|
* |
|
22
|
|
|
* For more details and usage information on BaseListView, see the [guide article on data widgets](guide:output-data-widgets). |
|
23
|
|
|
* |
|
24
|
|
|
* @author Qiang Xue <[email protected]> |
|
25
|
|
|
* @since 2.0 |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract class BaseListView extends Widget |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var array the HTML attributes for the container tag of the list view. |
|
31
|
|
|
* The "tag" element specifies the tag name of the container element and defaults to "div". |
|
32
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
|
33
|
|
|
*/ |
|
34
|
|
|
public $options = []; |
|
35
|
|
|
/** |
|
36
|
|
|
* @var \yii\data\DataProviderInterface the data provider for the view. This property is required. |
|
37
|
|
|
*/ |
|
38
|
|
|
public $dataProvider; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var array the configuration for the pager widget. By default, [[LinkPager]] will be |
|
41
|
|
|
* used to render the pager. You can use a different widget class by configuring the "class" element. |
|
42
|
|
|
* Note that the widget must support the `pagination` property which will be populated with the |
|
43
|
|
|
* [[\yii\data\BaseDataProvider::pagination|pagination]] value of the [[dataProvider]]. |
|
44
|
|
|
*/ |
|
45
|
|
|
public $pager = []; |
|
46
|
|
|
/** |
|
47
|
|
|
* @var array the configuration for the sorter widget. By default, [[LinkSorter]] will be |
|
48
|
|
|
* used to render the sorter. You can use a different widget class by configuring the "class" element. |
|
49
|
|
|
* Note that the widget must support the `sort` property which will be populated with the |
|
50
|
|
|
* [[\yii\data\BaseDataProvider::sort|sort]] value of the [[dataProvider]]. |
|
51
|
|
|
*/ |
|
52
|
|
|
public $sorter = []; |
|
53
|
|
|
/** |
|
54
|
|
|
* @var string the HTML content to be displayed as the summary of the list view. |
|
55
|
|
|
* If you do not want to show the summary, you may set it with an empty string. |
|
56
|
|
|
* |
|
57
|
|
|
* The following tokens will be replaced with the corresponding values: |
|
58
|
|
|
* |
|
59
|
|
|
* - `{begin}`: the starting row number (1-based) currently being displayed |
|
60
|
|
|
* - `{end}`: the ending row number (1-based) currently being displayed |
|
61
|
|
|
* - `{count}`: the number of rows currently being displayed |
|
62
|
|
|
* - `{totalCount}`: the total number of rows available |
|
63
|
|
|
* - `{page}`: the page number (1-based) current being displayed |
|
64
|
|
|
* - `{pageCount}`: the number of pages available |
|
65
|
|
|
*/ |
|
66
|
|
|
public $summary; |
|
67
|
|
|
/** |
|
68
|
|
|
* @var array the HTML attributes for the summary of the list view. |
|
69
|
|
|
* The "tag" element specifies the tag name of the summary element and defaults to "div". |
|
70
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
|
71
|
|
|
*/ |
|
72
|
|
|
public $summaryOptions = ['class' => 'summary']; |
|
73
|
|
|
/** |
|
74
|
|
|
* @var bool whether to show an empty list view if [[dataProvider]] returns no data. |
|
75
|
|
|
* The default value is false which displays an element according to the [[emptyText]] |
|
76
|
|
|
* and [[emptyTextOptions]] properties. |
|
77
|
|
|
*/ |
|
78
|
|
|
public $showOnEmpty = false; |
|
79
|
|
|
/** |
|
80
|
|
|
* @var string|false the HTML content to be displayed when [[dataProvider]] does not have any data. |
|
81
|
|
|
* When this is set to `false` no extra HTML content will be generated. |
|
82
|
|
|
* The default value is the text "No results found." which will be translated to the current application language. |
|
83
|
|
|
* @see showOnEmpty |
|
84
|
|
|
* @see emptyTextOptions |
|
85
|
|
|
*/ |
|
86
|
|
|
public $emptyText; |
|
87
|
|
|
/** |
|
88
|
|
|
* @var array the HTML attributes for the emptyText of the list view. |
|
89
|
|
|
* The "tag" element specifies the tag name of the emptyText element and defaults to "div". |
|
90
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
|
91
|
|
|
*/ |
|
92
|
|
|
public $emptyTextOptions = ['class' => 'empty']; |
|
93
|
|
|
/** |
|
94
|
|
|
* @var string the layout that determines how different sections of the list view should be organized. |
|
95
|
|
|
* The following tokens will be replaced with the corresponding section contents: |
|
96
|
|
|
* |
|
97
|
|
|
* - `{summary}`: the summary section. See [[renderSummary()]]. |
|
98
|
|
|
* - `{items}`: the list items. See [[renderItems()]]. |
|
99
|
|
|
* - `{sorter}`: the sorter. See [[renderSorter()]]. |
|
100
|
|
|
* - `{pager}`: the pager. See [[renderPager()]]. |
|
101
|
|
|
*/ |
|
102
|
|
|
public $layout = "{summary}\n{items}\n{pager}"; |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Renders the data models. |
|
107
|
|
|
* @return string the rendering result. |
|
108
|
|
|
*/ |
|
109
|
|
|
abstract public function renderItems(); |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Initializes the view. |
|
113
|
|
|
*/ |
|
114
|
34 |
|
public function init() |
|
115
|
|
|
{ |
|
116
|
34 |
|
parent::init(); |
|
117
|
34 |
|
if ($this->dataProvider === null) { |
|
118
|
|
|
throw new InvalidConfigException('The "dataProvider" property must be set.'); |
|
119
|
|
|
} |
|
120
|
34 |
|
if ($this->emptyText === null) { |
|
121
|
30 |
|
$this->emptyText = Yii::t('yii', 'No results found.'); |
|
122
|
|
|
} |
|
123
|
34 |
|
if (!isset($this->options['id'])) { |
|
124
|
33 |
|
$this->options['id'] = $this->getId(); |
|
125
|
|
|
} |
|
126
|
34 |
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Runs the widget. |
|
130
|
|
|
*/ |
|
131
|
17 |
|
public function run() |
|
132
|
|
|
{ |
|
133
|
17 |
|
if ($this->showOnEmpty || $this->dataProvider->getCount() > 0) { |
|
134
|
15 |
|
$content = preg_replace_callback('/{\\w+}/', function ($matches) { |
|
135
|
15 |
|
$content = $this->renderSection($matches[0]); |
|
136
|
|
|
|
|
137
|
15 |
|
return $content === false ? $matches[0] : $content; |
|
138
|
15 |
|
}, $this->layout); |
|
139
|
|
|
} else { |
|
140
|
2 |
|
$content = $this->renderEmpty(); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
17 |
|
$options = $this->options; |
|
144
|
17 |
|
$tag = ArrayHelper::remove($options, 'tag', 'div'); |
|
145
|
17 |
|
return Html::tag($tag, $content, $options); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Renders a section of the specified name. |
|
150
|
|
|
* If the named section is not supported, false will be returned. |
|
151
|
|
|
* @param string $name the section name, e.g., `{summary}`, `{items}`. |
|
152
|
|
|
* @return string|bool the rendering result of the section, or false if the named section is not supported. |
|
153
|
|
|
*/ |
|
154
|
15 |
|
public function renderSection($name) |
|
155
|
|
|
{ |
|
156
|
|
|
switch ($name) { |
|
157
|
15 |
|
case '{summary}': |
|
158
|
13 |
|
return $this->renderSummary(); |
|
159
|
15 |
|
case '{items}': |
|
160
|
13 |
|
return $this->renderItems(); |
|
161
|
15 |
|
case '{pager}': |
|
162
|
13 |
|
return $this->renderPager(); |
|
163
|
2 |
|
case '{sorter}': |
|
164
|
2 |
|
return $this->renderSorter(); |
|
165
|
|
|
default: |
|
166
|
|
|
return false; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Renders the HTML content indicating that the list view has no data. |
|
172
|
|
|
* @return string the rendering result |
|
173
|
|
|
* @see emptyText |
|
174
|
|
|
*/ |
|
175
|
4 |
|
public function renderEmpty() |
|
176
|
|
|
{ |
|
177
|
4 |
|
if ($this->emptyText === false) { |
|
178
|
1 |
|
return ''; |
|
179
|
|
|
} |
|
180
|
3 |
|
$options = $this->emptyTextOptions; |
|
181
|
3 |
|
$tag = ArrayHelper::remove($options, 'tag', 'div'); |
|
182
|
3 |
|
return Html::tag($tag, $this->emptyText, $options); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Renders the summary text. |
|
187
|
|
|
*/ |
|
188
|
13 |
|
public function renderSummary() |
|
189
|
|
|
{ |
|
190
|
13 |
|
$count = $this->dataProvider->getCount(); |
|
191
|
13 |
|
if ($count <= 0) { |
|
192
|
4 |
|
return ''; |
|
193
|
|
|
} |
|
194
|
9 |
|
$summaryOptions = $this->summaryOptions; |
|
195
|
9 |
|
$tag = ArrayHelper::remove($summaryOptions, 'tag', 'div'); |
|
196
|
9 |
|
if (($pagination = $this->dataProvider->getPagination()) !== false) { |
|
197
|
9 |
|
$totalCount = $this->dataProvider->getTotalCount(); |
|
198
|
9 |
|
$begin = $pagination->getPage() * $pagination->pageSize + 1; |
|
199
|
9 |
|
$end = $begin + $count - 1; |
|
200
|
9 |
|
if ($begin > $end) { |
|
201
|
|
|
$begin = $end; |
|
202
|
|
|
} |
|
203
|
9 |
|
$page = $pagination->getPage() + 1; |
|
204
|
9 |
|
$pageCount = $pagination->pageCount; |
|
205
|
9 |
|
if (($summaryContent = $this->summary) === null) { |
|
206
|
9 |
|
return Html::tag($tag, Yii::t('yii', 'Showing <b>{begin, number}-{end, number}</b> of <b>{totalCount, number}</b> {totalCount, plural, one{item} other{items}}.', [ |
|
207
|
9 |
|
'begin' => $begin, |
|
208
|
9 |
|
'end' => $end, |
|
209
|
9 |
|
'count' => $count, |
|
210
|
9 |
|
'totalCount' => $totalCount, |
|
211
|
9 |
|
'page' => $page, |
|
212
|
9 |
|
'pageCount' => $pageCount, |
|
213
|
9 |
|
]), $summaryOptions); |
|
214
|
|
|
} |
|
215
|
|
|
} else { |
|
216
|
|
|
$begin = $page = $pageCount = 1; |
|
217
|
|
|
$end = $totalCount = $count; |
|
218
|
|
|
if (($summaryContent = $this->summary) === null) { |
|
219
|
|
|
return Html::tag($tag, Yii::t('yii', 'Total <b>{count, number}</b> {count, plural, one{item} other{items}}.', [ |
|
220
|
|
|
'begin' => $begin, |
|
221
|
|
|
'end' => $end, |
|
222
|
|
|
'count' => $count, |
|
223
|
|
|
'totalCount' => $totalCount, |
|
224
|
|
|
'page' => $page, |
|
225
|
|
|
'pageCount' => $pageCount, |
|
226
|
|
|
]), $summaryOptions); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
return Yii::$app->getI18n()->format($summaryContent, [ |
|
231
|
|
|
'begin' => $begin, |
|
232
|
|
|
'end' => $end, |
|
233
|
|
|
'count' => $count, |
|
234
|
|
|
'totalCount' => $totalCount, |
|
235
|
|
|
'page' => $page, |
|
236
|
|
|
'pageCount' => $pageCount, |
|
237
|
|
|
], Yii::$app->language); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Renders the pager. |
|
242
|
|
|
* @return string the rendering result |
|
243
|
|
|
*/ |
|
244
|
13 |
|
public function renderPager() |
|
245
|
|
|
{ |
|
246
|
13 |
|
$pagination = $this->dataProvider->getPagination(); |
|
247
|
13 |
|
if ($pagination === false || $this->dataProvider->getCount() <= 0) { |
|
248
|
4 |
|
return ''; |
|
249
|
|
|
} |
|
250
|
|
|
/* @var $class LinkPager */ |
|
251
|
9 |
|
$pager = $this->pager; |
|
252
|
9 |
|
$class = ArrayHelper::remove($pager, 'class', LinkPager::class); |
|
253
|
9 |
|
$pager['pagination'] = $pagination; |
|
254
|
9 |
|
$pager['view'] = $this->getView(); |
|
255
|
|
|
|
|
256
|
9 |
|
return $class::widget($pager); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Renders the sorter. |
|
261
|
|
|
* @return string the rendering result |
|
262
|
|
|
*/ |
|
263
|
2 |
|
public function renderSorter() |
|
264
|
|
|
{ |
|
265
|
2 |
|
$sort = $this->dataProvider->getSort(); |
|
266
|
2 |
|
if ($sort === false || empty($sort->attributes) || $this->dataProvider->getCount() <= 0) { |
|
267
|
|
|
return ''; |
|
268
|
|
|
} |
|
269
|
|
|
/* @var $class LinkSorter */ |
|
270
|
2 |
|
$sorter = $this->sorter; |
|
271
|
2 |
|
$class = ArrayHelper::remove($sorter, 'class', LinkSorter::class); |
|
272
|
2 |
|
$sorter['sort'] = $sort; |
|
273
|
2 |
|
$sorter['view'] = $this->getView(); |
|
274
|
|
|
|
|
275
|
2 |
|
return $class::widget($sorter); |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
|