ArticleWidget.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: NODELOG
  5. * Date: 2016/12/14
  6. * Time: 上午10:51
  7. */
  8. namespace common\modules\area\widgets;
  9. use common\models\Article;
  10. use yii\base\Widget;
  11. use yii\helpers\Html;
  12. class ArticleWidget extends Widget
  13. {
  14. public $model;
  15. public function run()
  16. {
  17. $html = \Yii::$app->cache->get([__CLASS__, $this->model->block_id]);
  18. if (!$this->model->cache || $html === false) {
  19. $template = $this->model->template;
  20. $articles = Article::find()->published()
  21. ->andFilterWhere(['module' => $template['module']])
  22. ->andFilterWhere(['category_id' => $template['category']])
  23. ->orderBy([$template['order'] => SORT_DESC])
  24. ->limit($template['limit'])
  25. ->all();
  26. $items = [];
  27. foreach ($articles as $article) {
  28. $items[] = Html::a($article->title, ['/article/view', 'id' => $article->id]);
  29. }
  30. $html = Html::ul($items, ['class' => 'post-list', 'encode' => false]);
  31. \Yii::$app->cache->set([__CLASS__, $this->model->block_id], $html);
  32. }
  33. return $html;
  34. }
  35. }