/** * @param $id topic identificator. * @return string */ public function actionView($id) { /** @var Topic $topic */ $topic = Topic::find()->where(['id' => $id])->one(); if (!$topic) { throw new NotFoundHttpException(); } $topic->updateCounters(['number_views' => 1]); $topic->save(); $dataProvider = Post::getDataProviderByTopic($topic->id); $posts = $dataProvider->getModels(); if (!Yii::$app->getUser()->getIsGuest()) { $userMentions = UserMention::findAll(['topic_id' => $id, 'mention_user_id' => Yii::$app->getUser()->getId(), 'status' => UserMention::MENTION_SATUS_UNVIEWED]); // user mention update foreach ($userMentions as $userMention) { $userMention->status = UserMention::MENTION_SATUS_VIEWED; $userMention->save(); } $model = new PostForm(); if ($model->load(Yii::$app->getRequest()->post()) && $model->create($topic)) { $this->redirect(['/topic/post/view', 'id' => $model->post->id, '#' => 'p' . $model->post->id]); } return $this->render('view', ['dataProvider' => $dataProvider, 'model' => $model, 'topic' => $topic, 'posts' => $posts]); } else { return $this->render('view', ['dataProvider' => $dataProvider, 'topic' => $topic, 'posts' => $posts]); } }
/** * @return string */ public function actionUpdate() { if (Yii::$app->getRequest()->getIsAjax()) { Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $text = Yii::$app->getRequest()->post('text'); $id = substr(Yii::$app->getRequest()->post('id'), 1); /** @var Post $post */ $post = Post::findOne(['id' => $id]); if (!$post || !Yii::$app->getUser()->can('updatePost', ['post' => $post])) { throw new NotFoundHttpException(); } $model = new PostForm(); $model->message = $text; if ($model->validate()) { $post->message = $text; $post->edited_at = time(); $post->edited_by = Yii::$app->getUser()->getIdentity()->getId(); $post->save(); } return $post->displayMessage; } throw new NotFoundHttpException(); }