ArticleListView.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2016-06-19 00:21
  7. */
  8. namespace frontend\widgets;
  9. use yii;
  10. use yii\helpers\Url;
  11. use yii\helpers\ArrayHelper;
  12. use yii\widgets\LinkPager;
  13. use yii\helpers\StringHelper;
  14. class ArticleListView extends \yii\widgets\ListView
  15. {
  16. /**
  17. * @var string 布局
  18. */
  19. public $layout = "{items}\n<div class=\"pagination\">{pager}</div>";
  20. /**
  21. * @var int 标题截取长度
  22. */
  23. public $titleLength = 28;
  24. /**
  25. * @var int summary截取长度
  26. */
  27. public $summaryLength = 70;
  28. /**
  29. * @var int 缩率图宽
  30. */
  31. public $thumbWidth = 220;
  32. /**
  33. * @var int 缩略图高
  34. */
  35. public $thumbHeight = 150;
  36. public $itemOptions = [
  37. 'tag' => 'article',
  38. 'class' => 'excerpt'
  39. ];
  40. public $pagerOptions = [
  41. 'firstPageLabel' => '首页',
  42. 'lastPageLabel' => '尾页',
  43. 'prevPageLabel' => '上一页',
  44. 'nextPageLabel' => '下一页',
  45. 'options' => [
  46. 'class' => '',
  47. ],
  48. ];
  49. /**
  50. * @var string 模板
  51. */
  52. public $template = "<div class='focus'>
  53. <a target='_blank' href='{article_url}'>
  54. <img width='186px' height='112px' class='thumb' src='{img_url}' alt='{title}'></a>
  55. </div>
  56. <header>
  57. <a class='label label-important' href='{category_url}'>{category}<i class='label-arrow'></i></a>
  58. <h2><a target='_blank' href='{article_url}' title='{title}'>{title}</a></h2>
  59. </header>
  60. <p class='auth-span'>
  61. <span class='muted'><i class='fa fa-clock-o'></i> {pub_date}</span>
  62. <span class='muted'><i class='fa fa-eye'></i> {scan_count}℃</span>
  63. <span class='muted'><i class='fa fa-comments-o'></i> <a target='_blank' href='{comment_url}'>{comment_count}评论</a></span>
  64. </p>
  65. <span class='note'> {summary}</span>";
  66. /**
  67. * @inheritdoc
  68. */
  69. public function init()
  70. {
  71. parent::init();
  72. $this->pagerOptions = [
  73. 'firstPageLabel' => yii::t('app', 'first'),
  74. 'lastPageLabel' => yii::t('app', 'last'),
  75. 'prevPageLabel' => yii::t('app', 'previous'),
  76. 'nextPageLabel' => yii::t('app', 'next'),
  77. 'options' => [
  78. 'class' => 'pagination',
  79. ]
  80. ];
  81. if( empty($this->itemView) ) {
  82. $this->itemView = function ($model, $key, $index) {
  83. /** @var $model \common\models\Article */
  84. $categoryName = $model->category ? $model->category->name : yii::t('app', 'UnClassified');
  85. $categoryAlias = $model->category ? $model->category->alias : yii::t('app', 'UnClassified');
  86. $categoryUrl = Url::to(['article/index', 'cat' => $categoryAlias]);
  87. $imgUrl = $model->getThumbUrlBySize($this->thumbWidth, $this->thumbHeight);
  88. $articleUrl = Url::to(['article/view', 'id' => $model->id]);
  89. $summary = StringHelper::truncate($model->summary, $this->summaryLength);
  90. $title = StringHelper::truncate($model->title, $this->titleLength);
  91. return str_replace([
  92. '{article_url}',
  93. '{img_url}',
  94. '{category_url}',
  95. '{title}',
  96. '{summary}',
  97. '{pub_date}',
  98. '{scan_count}',
  99. '{comment_count}',
  100. '{category}',
  101. '{comment_url}'
  102. ], [
  103. $articleUrl,
  104. $imgUrl,
  105. $categoryUrl,
  106. $title,
  107. $summary,
  108. date('Y-m-d', $model->created_at),
  109. $model->scan_count * 100,
  110. $model->comment_count,
  111. $categoryName,
  112. $articleUrl . "#comments"
  113. ], $this->template);
  114. };
  115. }
  116. }
  117. /**
  118. * @inheritdoc
  119. */
  120. public function renderPager()
  121. {
  122. $pagination = $this->dataProvider->getPagination();
  123. if ($pagination === false || $this->dataProvider->getCount() <= 0) {
  124. return '';
  125. }
  126. /* @var $class LinkPager */
  127. $pager = $this->pager;
  128. $class = ArrayHelper::remove($pager, 'class', LinkPager::className());
  129. $pager['pagination'] = $pagination;
  130. $pager['view'] = $this->getView();
  131. $pager = array_merge($pager, $this->pagerOptions);
  132. return $class::widget($pager);
  133. }
  134. }