razonyang /
yii2-app-template
| 1 | <?php |
||
| 2 | namespace App\Http\Controller; |
||
| 3 | |||
| 4 | use App\Model\Article; |
||
| 5 | use App\Model\ArticleComment; |
||
| 6 | use App\Model\ArticleLike; |
||
| 7 | use App\Model\StatusInterface; |
||
| 8 | use yii\data\Pagination; |
||
| 9 | use yii\helpers\ArrayHelper; |
||
| 10 | use yii\helpers\HtmlPurifier; |
||
| 11 | use yii\web\NotFoundHttpException; |
||
| 12 | |||
| 13 | class ArticleController extends Controller |
||
| 14 | { |
||
| 15 | public function afterAction($action, $result) |
||
| 16 | { |
||
| 17 | if ($action->id === 'view') { |
||
| 18 | $this->model->updateCounters([ |
||
| 19 | 'views' => 1 |
||
| 20 | ]); |
||
| 21 | } |
||
| 22 | |||
| 23 | return parent::afterAction($action, $result); |
||
| 24 | } |
||
| 25 | |||
| 26 | public function actionIndex() |
||
| 27 | { |
||
| 28 | $query = Article::find() |
||
| 29 | ->alias('a') |
||
| 30 | ->joinWith('category') |
||
| 31 | ->where([ |
||
| 32 | 'a.status' => StatusInterface::STATUS_ACTIVE, |
||
| 33 | 'a.is_deleted' => 0, |
||
| 34 | ]) |
||
| 35 | ->andWhere(['<=', 'a.release_time', time()]) |
||
| 36 | ->orderBy(['a.release_time' => SORT_DESC]); |
||
| 37 | |||
| 38 | $count = $query->count(); |
||
| 39 | $pagination = new Pagination(['totalCount' => $count]); |
||
| 40 | |||
| 41 | $articles = $query->offset($pagination->offset) |
||
| 42 | ->limit($pagination->limit) |
||
| 43 | ->all(); |
||
| 44 | |||
| 45 | $likes = ArticleLike::find() |
||
| 46 | ->select([ |
||
| 47 | 'article_id', |
||
| 48 | 'count' => 'count(*)' |
||
| 49 | ]) |
||
| 50 | ->where(['IN', 'article_id', array_column($articles, 'id')]) |
||
| 51 | ->groupBy(['article_id']) |
||
| 52 | ->asArray() |
||
| 53 | ->all(); |
||
| 54 | $likesCount = ArrayHelper::map($likes, 'article_id', 'count'); |
||
| 55 | |||
| 56 | return $this->render('index', [ |
||
| 57 | 'articles' => $articles, |
||
| 58 | 'likesCount' => $likesCount, |
||
| 59 | 'pagination' => $pagination, |
||
| 60 | ]); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function actionView($id) |
||
| 64 | { |
||
| 65 | $model = $this->findModel($id); |
||
| 66 | |||
| 67 | return $this->render('view', [ |
||
| 68 | 'model' => $model, |
||
| 69 | 'content' => HtmlPurifier::process($model->meta->content), |
||
| 70 | ]); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var Article $model |
||
| 75 | */ |
||
| 76 | private $model; |
||
| 77 | |||
| 78 | private function findModel($id) |
||
| 79 | { |
||
| 80 | $model = Article::findOne($id); |
||
| 81 | if (!$model) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 82 | throw new NotFoundHttpException('Article does not exists'); |
||
| 83 | } |
||
| 84 | |||
| 85 | $this->model = $model; |
||
| 86 | return $this->model; |
||
| 87 | } |
||
| 88 | |||
| 89 | public function actionComments($id, $page = 1, $limit = 10) |
||
| 90 | { |
||
| 91 | $this->layout = false; |
||
| 92 | $comments = ArticleComment::find() |
||
| 93 | ->alias('c') |
||
| 94 | ->joinWith('user') |
||
| 95 | ->where([ |
||
| 96 | 'c.article_id' => $id, |
||
| 97 | ]) |
||
| 98 | ->orderBy(['c.create_time' => SORT_DESC]) |
||
| 99 | ->offset(($page - 1) * $limit) |
||
| 100 | ->all(); |
||
| 101 | |||
| 102 | return $this->render('comments', [ |
||
| 103 | 'comments' => $comments, |
||
| 104 | ]); |
||
| 105 | } |
||
| 106 | } |
||
| 107 |