/** * Displays a single Topic model. * @param integer $id * @return mixed * @throws BadRequestHttpException * @throws NotFoundHttpException * @throws \Exception */ public function actionView($id) { $model = $this->findModel($id); if ($model->need_login == 1 && Yii::$app->user->isGuest) { Yii::$app->getSession()->setFlash('danger', '你访问的主题需要登陆之后才能查看'); return $this->redirect('/account/login?next=/topic/' . $id); } if (!empty($model->node->bg) && $model->node->use_bg == 1) { $this->bg = $model->node->bg; } if (!empty($model->node->bg_color)) { $this->bg_color = $model->node->bg_color; } if (isset($model->content->content)) { $this->description = $model->node->name . ' - ' . $model->user->username . ' - ' . Helper::truncateUtf8String($model->content->content, 200); } else { $this->description = $model->node->name . ' - ' . $model->user->username . Helper::truncateUtf8String($model->title, 200); } $replyQuery = Reply::find()->where(['topic_id' => $model->id]); $pagination = new Pagination(['defaultPageSize' => Yii::$app->params['pageSize'], 'totalCount' => $replyQuery->count()]); $replyList = $replyQuery->offset($pagination->offset)->limit($pagination->limit)->all(); $reply = new Reply(); if (!Yii::$app->user->isGuest) { $model->updateCounters(['click' => 1]); $reply = new Reply(); if ($reply->load(Yii::$app->request->post()) && $reply->save()) { $this->redirect('/topic/' . $id . '#Reply'); } return $this->render('view', ['model' => $model, 'reply' => $reply, 'replyList' => $replyList, 'pagination' => $pagination]); } else { return $this->render('view', ['model' => $model, 'reply' => $reply, 'replyList' => $replyList, 'pagination' => $pagination]); } }
/** * Displays a single Topic model. * @param integer $id * @return mixed * @throws BadRequestHttpException * @throws NotFoundHttpException * @throws \Exception */ public function actionView($id) { $model = $this->findModel($id); if ($model->need_login == 1 && Yii::$app->user->isGuest) { Yii::$app->getSession()->setFlash('danger', '你访问的建议需要登陆之后才能查看'); return $this->redirect('/account/login?next=/topic/' . $id); } if (!empty($model->node->bg) && $model->node->use_bg == 1) { $this->bg = $model->node->bg; } if (!empty($model->node->bg_color)) { $this->bg_color = $model->node->bg_color; } $this->title = $model->title . ' - ' . Yii::$app->name; if (isset($model->content->content)) { $this->description = $model->node->name . ' - ' . $model->user->username . ' - ' . Helper::truncateUtf8String($model->content->content, 200); } else { $this->description = $model->node->name . ' - ' . $model->user->username . Helper::truncateUtf8String($model->title, 200); } $this->canonical = Yii::$app->params['domain'] . 'topic/' . $id; $replyQuery = (new Query())->select('reply.*, user.username, user.avatar, user.role')->from(Reply::tableName())->leftJoin(User::tableName(), 'user.id = reply.user_id')->where(['reply.topic_id' => $id]); $pagination = new Pagination(['defaultPageSize' => Yii::$app->params['ReplyPageSize'], 'totalCount' => $replyQuery->count()]); $replyList = $replyQuery->offset($pagination->offset)->limit($pagination->limit)->all(); $reply = new Reply(); if (!Yii::$app->user->isGuest) { $model->updateCounters(['click' => 1]); $reply = new Reply(); if ($reply->load(Yii::$app->request->post()) && $reply->save()) { Yii::$app->cache->delete('ReplyCount'); $this->redirect('/topic/' . $id . '#Reply'); } return $this->render('view', ['model' => $model, 'reply' => $reply, 'replyList' => $replyList, 'pagination' => $pagination]); } else { return $this->render('view', ['model' => $model, 'reply' => $reply, 'replyList' => $replyList, 'pagination' => $pagination]); } }