VerbFilter::className(), 'actions' => [ 'comment' => ['POST'], ] ], [ 'class' => HttpCache::className(), 'only' => ['view'], 'lastModified' => function ($action, $params) { $id = Yii::$app->getRequest()->get('id'); $model = Article::findOne(['id' => $id, 'type' => Article::ARTICLE, 'status' => Article::ARTICLE_PUBLISHED]); if( $model === null ) throw new NotFoundHttpException(Yii::t("frontend", "Article id {id} is not exists", ['id' => $id])); Article::updateAllCounters(['scan_count' => 1], ['id' => $id]); if($model->visibility == Constants::ARTICLE_VISIBILITY_PUBLIC) return $model->updated_at; }, ], ]; } /** * article list page * * @param string $cat category name * @return string * @throws NotFoundHttpException * @throws yii\base\InvalidConfigException */ public function actionIndex($cat = '') { if ($cat == '') { $cat = Yii::$app->getRequest()->getPathInfo(); } $where = ['type' => Article::ARTICLE, 'status' => Article::ARTICLE_PUBLISHED]; if ($cat != '' && $cat != 'index') { if ($cat == Yii::t('app', 'UnClassified')) { $where['cid'] = 0; } else { if (! $category = Category::findOne(['alias' => $cat])) { throw new NotFoundHttpException(Yii::t('frontend', 'None category named {name}', ['name' => $cat])); } $descendants = $category->getDescendants($category['id']); if( empty($descendants) ) { $where['cid'] = $category['id']; }else{ $cids = ArrayHelper::getColumn($descendants, 'id'); $cids[] = $category['id']; $where['cid'] = $cids; } } } $query = Article::find()->with('category')->where($where); $dataProvider = new ActiveDataProvider([ 'query' => $query, 'sort' => [ 'defaultOrder' => [ 'sort' => SORT_ASC, 'created_at' => SORT_DESC, 'id' => SORT_DESC, ] ] ]); $template = "index"; isset($category) && $category->template != "" && $template = $category->template; $data = array_merge([ 'dataProvider' => $dataProvider, 'type' => ( !empty($cat) ? Yii::t('frontend', 'Category {cat} articles', ['cat'=>$cat]) : Yii::t('frontend', 'Latest Articles') ), 'category' => isset($category) ? $category->name : "", ], Helper::getCommonInfos()); return $this->render($template, $data); } /** * article detail page * * @param $id * @return string * @throws \yii\web\NotFoundHttpException * @throws \yii\base\InvalidConfigException */ public function actionView($id) { /** @var ArticleServiceInterface $articleService */ $articleService = Yii::$app->get(ArticleServiceInterface::ServiceName); $model = $articleService->getArticleById($id); /** @var Article $model */ if( $model === null ) throw new NotFoundHttpException(Yii::t("frontend", "Article id {id} is not exists", ['id' => $id])); $prev = Article::find() ->where(['cid' => $model->cid, 'type' => Article::ARTICLE, 'status' => Article::ARTICLE_PUBLISHED]) ->andWhere(['>', 'id', $id]) ->orderBy("sort asc,id asc") ->limit(1) ->one(); $next = Article::find() ->where(['cid' => $model->cid, 'type' => Article::ARTICLE, 'status' => Article::ARTICLE_PUBLISHED]) ->andWhere(['<', 'id', $id]) ->orderBy("sort asc,id desc") ->limit(1) ->one();//->createCommand()->getRawSql(); $commentModel = new Comment(); $commentList = $commentModel->getCommentByAid($id); $recommends = Article::find() ->where(['type' => Article::ARTICLE, 'status' => Article::ARTICLE_PUBLISHED]) ->andWhere(['<>', 'thumb', '']) ->orderBy("scan_count") ->limit(8) ->with('category') ->all(); switch ($model->visibility){ case Constants::ARTICLE_VISIBILITY_COMMENT://评论可见 if( Yii::$app->getUser()->getIsGuest() ){ $result = Comment::find()->where(['aid'=>$model->id, 'ip'=>Yii::$app->getRequest()->getUserIP()])->one(); }else{ $result = Comment::find()->where(['aid'=>$model->id, 'uid'=>Yii::$app->getUser()->getId()])->one(); } if( $result === null ) { $model->articleContent->content = "
" . Yii::t('frontend', "Only commented user can visit this article") . "
"; } break; case Constants::ARTICLE_VISIBILITY_SECRET://加密文章 $authorized = Yii::$app->getSession()->get("article_password_" . $model->id, null); if( $authorized === null ) $this->redirect(Url::toRoute(['password', 'id'=>$id])); break; case Constants::ARTICLE_VISIBILITY_LOGIN://登录可见 if( Yii::$app->getUser()->getIsGuest() ) { $model->articleContent->content = "" . Yii::t('frontend', "Only login user can visit this article") . "
"; } break; } $template = "view"; isset($model->category) && $model->category->article_template != "" && $template = $model->category->article_template; $model->template != "" && $template = $model->template; /** @var AdServiceInterface $adService */ $adService = Yii::$app->get(AdServiceInterface::ServiceName); return $this->render($template, [ 'model' => $model, 'prev' => $prev, 'next' => $next, 'recommends' => $recommends, 'commentModel' => $commentModel, 'commentList' => $commentList, 'rightAd1' => $adService->getAdByName("sidebar_right_1"), 'rightAd2' => $adService->getAdByName("sidebar_right_2"), ]); } /** * article likes, scan, comment count * * @param $id * @return array * @throws NotFoundHttpException * @throws \yii\base\InvalidConfigException */ public function actionViewAjax($id) { Yii::$app->getResponse()->format = Response::FORMAT_JSON; /** @var ArticleServiceInterface $articleService */ $articleService = Yii::$app->get(ArticleServiceInterface::ServiceName); $model = $articleService->getArticleById($id); if( $model === null ) throw new NotFoundHttpException("None exists article id"); return [ 'likeCount' => (int)$model->getArticleLikeCount(), 'scanCount' => $model->scan_count * 100, 'commentCount' => $model->comment_count, ]; } /** * comment * */ public function actionComment() { Yii::$app->getResponse()->format = Response::FORMAT_HTML; /** @var CommentServiceInterface $service */ $service = Yii::$app->get(CommentServiceInterface::ServiceName); $commentModel = $service->newModel(); if ($commentModel->load(Yii::$app->getRequest()->post()) && $commentModel->save()) { $avatar = 'https://secure.gravatar.com/avatar?s=50'; if ($commentModel->email != '') { $avatar = "https://secure.gravatar.com/avatar/" . md5($commentModel->email) . "?s=50"; } $tips = ''; if (Yii::$app->feehi->website_comment_need_verify) { $tips = "" . Yii::t('frontend', 'Comment waiting for approved.') . "{$commentModel->content}
{$tips}