/** * 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; $this->description = $model->node['name'] . ' - ' . $model->user->username . ' - ' . Helper::truncateUtf8String($model->content->content, 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]); } }
/** * Lists all TopicContent models. * @return mixed */ public function actionIndex() { $searchModel = new TopicContentSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); $gridColumns[] = ['class' => 'kartik\\grid\\SerialColumn', 'contentOptions' => ['class' => 'kartik-sheet-style'], 'width' => '36px', 'header' => '序号', 'headerOptions' => ['class' => 'kartik-sheet-style']]; $gridColumns[] = ['attribute' => 'topic_id', 'vAlign' => 'middle', 'width' => '180px', 'value' => function ($searchModel, $key, $index, $widget) { return $searchModel->topic->title; }, 'filterType' => GridView::FILTER_SELECT2, 'filter' => ArrayHelper::map(Topic::find()->orderBy('id')->asArray()->all(), 'id', 'title'), 'filterWidgetOptions' => ['pluginOptions' => ['allowClear' => true]], 'filterInputOptions' => ['placeholder' => '建议标题'], 'format' => 'raw']; $gridColumns[] = ['attribute' => 'content', 'value' => function ($searchModel, $key, $index, $widget) { return Helper::truncateUtf8String($searchModel->content, 100); }]; $gridColumns[] = ['class' => 'kartik\\grid\\BooleanColumn', 'attribute' => 'is_append', 'vAlign' => 'middle', 'trueLabel' => '是', 'falseLabel' => '否']; $gridColumns[] = ['mergeHeader' => true, 'attribute' => 'created', 'format' => 'datetime']; $gridColumns[] = ['class' => '\\kartik\\grid\\ActionColumn', 'template' => '{view} {update}', 'buttons' => ['view' => function ($url, $model) { return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', Yii::$app->params['domain'] . '/topic/' . $model->topic_id, ['title' => '查看', 'target' => '_blank']); }]]; return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'gridColumns' => $gridColumns]); }
/** * 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]); } }