1 | <?php |
||
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 the list view if [[dataProvider]] returns no data. |
||
75 | */ |
||
76 | public $showOnEmpty = false; |
||
77 | /** |
||
78 | * @var string the HTML content to be displayed when [[dataProvider]] does not have any data. |
||
79 | */ |
||
80 | public $emptyText; |
||
81 | /** |
||
82 | * @var array the HTML attributes for the emptyText of the list view. |
||
83 | * The "tag" element specifies the tag name of the emptyText element and defaults to "div". |
||
84 | * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
||
85 | */ |
||
86 | public $emptyTextOptions = ['class' => 'empty']; |
||
87 | /** |
||
88 | * @var string the layout that determines how different sections of the list view should be organized. |
||
89 | * The following tokens will be replaced with the corresponding section contents: |
||
90 | * |
||
91 | * - `{summary}`: the summary section. See [[renderSummary()]]. |
||
92 | * - `{items}`: the list items. See [[renderItems()]]. |
||
93 | * - `{sorter}`: the sorter. See [[renderSorter()]]. |
||
94 | * - `{pager}`: the pager. See [[renderPager()]]. |
||
95 | */ |
||
96 | public $layout = "{summary}\n{items}\n{pager}"; |
||
97 | |||
98 | |||
99 | /** |
||
100 | * Renders the data models. |
||
101 | * @return string the rendering result. |
||
102 | */ |
||
103 | abstract public function renderItems(); |
||
104 | |||
105 | /** |
||
106 | * Initializes the view. |
||
107 | */ |
||
108 | 16 | public function init() |
|
120 | |||
121 | /** |
||
122 | * Runs the widget. |
||
123 | */ |
||
124 | 9 | public function run() |
|
125 | { |
||
126 | 9 | if ($this->showOnEmpty || $this->dataProvider->getCount() > 0) { |
|
127 | 9 | $content = preg_replace_callback("/{\\w+}/", function ($matches) { |
|
128 | 9 | $content = $this->renderSection($matches[0]); |
|
129 | |||
130 | 9 | return $content === false ? $matches[0] : $content; |
|
131 | 9 | }, $this->layout); |
|
132 | 9 | } else { |
|
133 | $content = $this->renderEmpty(); |
||
134 | } |
||
135 | |||
136 | 9 | $options = $this->options; |
|
137 | 9 | $tag = ArrayHelper::remove($options, 'tag', 'div'); |
|
138 | 9 | echo Html::tag($tag, $content, $options); |
|
139 | 9 | } |
|
140 | |||
141 | /** |
||
142 | * Renders a section of the specified name. |
||
143 | * If the named section is not supported, false will be returned. |
||
144 | * @param string $name the section name, e.g., `{summary}`, `{items}`. |
||
145 | * @return string|bool the rendering result of the section, or false if the named section is not supported. |
||
146 | */ |
||
147 | 9 | public function renderSection($name) |
|
162 | |||
163 | /** |
||
164 | * Renders the HTML content indicating that the list view has no data. |
||
165 | * @return string the rendering result |
||
166 | * @see emptyText |
||
167 | */ |
||
168 | public function renderEmpty() |
||
169 | { |
||
170 | $options = $this->emptyTextOptions; |
||
171 | $tag = ArrayHelper::remove($options, 'tag', 'div'); |
||
172 | return Html::tag($tag, $this->emptyText, $options); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Renders the summary text. |
||
177 | */ |
||
178 | 7 | public function renderSummary() |
|
179 | { |
||
180 | 7 | $count = $this->dataProvider->getCount(); |
|
181 | 7 | if ($count <= 0) { |
|
182 | return ''; |
||
183 | } |
||
184 | 7 | $summaryOptions = $this->summaryOptions; |
|
185 | 7 | $tag = ArrayHelper::remove($summaryOptions, 'tag', 'div'); |
|
186 | 7 | if (($pagination = $this->dataProvider->getPagination()) !== false) { |
|
187 | 7 | $totalCount = $this->dataProvider->getTotalCount(); |
|
188 | 7 | $begin = $pagination->getPage() * $pagination->pageSize + 1; |
|
189 | 7 | $end = $begin + $count - 1; |
|
190 | 7 | if ($begin > $end) { |
|
191 | $begin = $end; |
||
192 | } |
||
193 | 7 | $page = $pagination->getPage() + 1; |
|
194 | 7 | $pageCount = $pagination->pageCount; |
|
195 | 7 | if (($summaryContent = $this->summary) === null) { |
|
196 | 7 | 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}}.', [ |
|
197 | 7 | 'begin' => $begin, |
|
198 | 7 | 'end' => $end, |
|
199 | 7 | 'count' => $count, |
|
200 | 7 | 'totalCount' => $totalCount, |
|
201 | 7 | 'page' => $page, |
|
202 | 7 | 'pageCount' => $pageCount, |
|
203 | 7 | ]), $summaryOptions); |
|
204 | } |
||
205 | } else { |
||
206 | $begin = $page = $pageCount = 1; |
||
207 | $end = $totalCount = $count; |
||
208 | if (($summaryContent = $this->summary) === null) { |
||
209 | return Html::tag($tag, Yii::t('yii', 'Total <b>{count, number}</b> {count, plural, one{item} other{items}}.', [ |
||
210 | 'begin' => $begin, |
||
211 | 'end' => $end, |
||
212 | 'count' => $count, |
||
213 | 'totalCount' => $totalCount, |
||
214 | 'page' => $page, |
||
215 | 'pageCount' => $pageCount, |
||
216 | ]), $summaryOptions); |
||
217 | } |
||
218 | } |
||
219 | |||
220 | return Yii::$app->getI18n()->format($summaryContent, [ |
||
221 | 'begin' => $begin, |
||
222 | 'end' => $end, |
||
223 | 'count' => $count, |
||
224 | 'totalCount' => $totalCount, |
||
225 | 'page' => $page, |
||
226 | 'pageCount' => $pageCount, |
||
227 | ], Yii::$app->language); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Renders the pager. |
||
232 | * @return string the rendering result |
||
233 | */ |
||
234 | 7 | public function renderPager() |
|
235 | { |
||
236 | 7 | $pagination = $this->dataProvider->getPagination(); |
|
237 | 7 | if ($pagination === false || $this->dataProvider->getCount() <= 0) { |
|
238 | return ''; |
||
239 | } |
||
240 | /* @var $class LinkPager */ |
||
241 | 7 | $pager = $this->pager; |
|
242 | 7 | $class = ArrayHelper::remove($pager, 'class', LinkPager::className()); |
|
243 | 7 | $pager['pagination'] = $pagination; |
|
244 | 7 | $pager['view'] = $this->getView(); |
|
245 | |||
246 | 7 | return $class::widget($pager); |
|
247 | } |
||
248 | |||
249 | /** |
||
250 | * Renders the sorter. |
||
251 | * @return string the rendering result |
||
252 | */ |
||
253 | 2 | public function renderSorter() |
|
267 | } |
||
268 |