|
- <?php
-
-
- namespace yii\widgets;
-
- use Yii;
- use yii\base\InvalidConfigException;
- use yii\base\Widget;
- use yii\helpers\ArrayHelper;
- use yii\helpers\Html;
-
-
- abstract class BaseListView extends Widget
- {
-
-
- public $options = [];
-
-
- public $dataProvider;
-
-
- public $pager = [];
-
-
- public $sorter = [];
-
-
- public $summary;
-
-
- public $summaryOptions = ['class' => 'summary'];
-
-
- public $showOnEmpty = false;
-
-
- public $emptyText;
-
-
- public $emptyTextOptions = ['class' => 'empty'];
-
-
- public $layout = "{summary}\n{items}\n{pager}";
-
-
-
-
- abstract public function renderItems();
-
-
-
- public function init()
- {
- if ($this->dataProvider === null) {
- throw new InvalidConfigException('The "dataProvider" property must be set.');
- }
- if ($this->emptyText === null) {
- $this->emptyText = Yii::t('yii', 'No results found.');
- }
- if (!isset($this->options['id'])) {
- $this->options['id'] = $this->getId();
- }
- }
-
-
-
- public function run()
- {
- if ($this->showOnEmpty || $this->dataProvider->getCount() > 0) {
- $content = preg_replace_callback("/{\\w+}/", function ($matches) {
- $content = $this->renderSection($matches[0]);
-
- return $content === false ? $matches[0] : $content;
- }, $this->layout);
- } else {
- $content = $this->renderEmpty();
- }
-
- $options = $this->options;
- $tag = ArrayHelper::remove($options, 'tag', 'div');
- echo Html::tag($tag, $content, $options);
- }
-
-
-
- public function renderSection($name)
- {
- switch ($name) {
- case '{summary}':
- return $this->renderSummary();
- case '{items}':
- return $this->renderItems();
- case '{pager}':
- return $this->renderPager();
- case '{sorter}':
- return $this->renderSorter();
- default:
- return false;
- }
- }
-
-
-
- public function renderEmpty()
- {
- $options = $this->emptyTextOptions;
- $tag = ArrayHelper::remove($options, 'tag', 'div');
- return Html::tag($tag, $this->emptyText, $options);
- }
-
-
-
- public function renderSummary()
- {
- $count = $this->dataProvider->getCount();
- if ($count <= 0) {
- return '';
- }
- $summaryOptions = $this->summaryOptions;
- $tag = ArrayHelper::remove($summaryOptions, 'tag', 'div');
- if (($pagination = $this->dataProvider->getPagination()) !== false) {
- $totalCount = $this->dataProvider->getTotalCount();
- $begin = $pagination->getPage() * $pagination->pageSize + 1;
- $end = $begin + $count - 1;
- if ($begin > $end) {
- $begin = $end;
- }
- $page = $pagination->getPage() + 1;
- $pageCount = $pagination->pageCount;
- if (($summaryContent = $this->summary) === null) {
- 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}}.', [
- 'begin' => $begin,
- 'end' => $end,
- 'count' => $count,
- 'totalCount' => $totalCount,
- 'page' => $page,
- 'pageCount' => $pageCount,
- ]), $summaryOptions);
- }
- } else {
- $begin = $page = $pageCount = 1;
- $end = $totalCount = $count;
- if (($summaryContent = $this->summary) === null) {
- return Html::tag($tag, Yii::t('yii', 'Total <b>{count, number}</b> {count, plural, one{item} other{items}}.', [
- 'begin' => $begin,
- 'end' => $end,
- 'count' => $count,
- 'totalCount' => $totalCount,
- 'page' => $page,
- 'pageCount' => $pageCount,
- ]), $summaryOptions);
- }
- }
-
- return Yii::$app->getI18n()->format($summaryContent, [
- 'begin' => $begin,
- 'end' => $end,
- 'count' => $count,
- 'totalCount' => $totalCount,
- 'page' => $page,
- 'pageCount' => $pageCount,
- ], Yii::$app->language);
- }
-
-
-
- public function renderPager()
- {
- $pagination = $this->dataProvider->getPagination();
- if ($pagination === false || $this->dataProvider->getCount() <= 0) {
- return '';
- }
-
- $pager = $this->pager;
- $class = ArrayHelper::remove($pager, 'class', LinkPager::className());
- $pager['pagination'] = $pagination;
- $pager['view'] = $this->getView();
-
- return $class::widget($pager);
- }
-
-
-
- public function renderSorter()
- {
- $sort = $this->dataProvider->getSort();
- if ($sort === false || empty($sort->attributes) || $this->dataProvider->getCount() <= 0) {
- return '';
- }
-
- $sorter = $this->sorter;
- $class = ArrayHelper::remove($sorter, 'class', LinkSorter::className());
- $sorter['sort'] = $sort;
- $sorter['view'] = $this->getView();
-
- return $class::widget($sorter);
- }
- }
|