/** * 话题详细页 * @param integer $id * @return mixed */ public function actionView($id) { $model = Topic::findTopic($id); $dataProvider = new ActiveDataProvider(['query' => PostComment::findCommentList($id), 'pagination' => ['pageSize' => self::PAGE_SIZE]]); // 文章浏览次数 Topic::updateAllCounters(['view_count' => 1], ['id' => $id]); $user = Yii::$app->user->identity; $admin = $user && ($user->isAdmin($user->username) || $user->isSuperAdmin($user->username)) ? true : false; return $this->render('view', ['model' => $model, 'dataProvider' => $dataProvider, 'comment' => new PostComment(), 'admin' => $admin]); }
public function generateFakeData($num) { Console::startProgress(0, 100); $topic = new Topic(); $comment = new PostComment(); $node = new PostMeta(); $faker = Faker\Factory::create('zh_CN'); $nodeData = [['name' => '分享', 'alias' => '', 'parent' => 0], ['name' => '招聘', 'alias' => 'jobs', 'parent' => 1], ['name' => '瞎扯淡', 'alias' => 'booshit', 'parent' => 1], ['name' => '健康', 'alias' => 'health', 'parent' => 1], ['name' => '创业', 'alias' => 'startup', 'parent' => 1]]; $transaction = Yii::$app->db->beginTransaction(); try { for ($j = 0; $j < count($nodeData); $j++) { $_node = clone $node; $_node->setAttributes($nodeData[$j] + ['type' => 'topic_category']); $_node->save(); } $this->execute("INSERT INTO {{%merit_template}} (`id`, `type`, `title`, `unique_id`, `method`, `event`, `action_type`, `rule_key`, `rule_value`, `increment`, `status`, `created_at`, `updated_at`) VALUES\n(1, 1, '登录', 'site/login', 2, 0, 2, 1, 1, 2, 1, 1458657160, 1458823425),\n(2, 1, '发帖', 'topic/default/create', 2, 0, 2, 0, NULL, 6, 1, 1458657218, 1458657218),\n(3, 1, '回复', 'topic/comment/create', 2, 0, 2, 0, NULL, 4, 1, 1458657251, 1458657251),\n(4, 1, '发动弹', 'tweet/default/create', 2, 0, 2, 0, NULL, 4, 1, 1458657296, 1468647701);\n"); /** @var User $user */ $user = User::find()->where(['role' => User::ROLE_SUPER_ADMIN])->one(); Yii::$app->user->setIdentity($user); for ($i = 1; $i <= $num; $i++) { $_topic = clone $topic; $_topic->setAttributes(['type' => Topic::TYPE, 'title' => $faker->text(rand(10, 50)), 'post_meta_id' => rand(2, 4), 'status' => rand(1, 2), 'content' => $faker->text(rand(100, 2000)), 'user_id' => 1]); if (!$_topic->save()) { throw new Exception(array_values($_topic->getFirstErrors())[0]); } for ($_i = 1; $_i <= rand(1, 20); $_i++) { $_comment = clone $comment; $_comment->setAttributes(['comment' => $faker->text(rand(100, 2000)), 'post_id' => $_topic->id, 'ip' => '127.0.0.1', 'user_id' => 1]); if (!$_comment->save()) { throw new Exception(array_values($_comment->getFirstErrors())[0]); } // 更新回复时间 $_topic->lastCommentToUpdate($user['username']); // 评论计数器 Topic::updateAllCounters(['comment_count' => 1], ['id' => $_topic->id]); // 更新个人总统计 UserInfo::updateAllCounters(['comment_count' => 1], ['user_id' => $_topic->user_id]); } Console::updateProgress($i / $num * 100, 100); } $transaction->commit(); } catch (\Exception $e) { $transaction->rollBack(); throw $e; } Console::endProgress(); }
/** * 伪删除 * @param $id * @return \yii\web\Response * @throws NotFoundHttpException */ public function actionDelete($id) { $model = PostComment::findComment($id); if (!$model->isCurrent()) { throw new NotFoundHttpException(); } // 事物 暂时数据库类型不支持 无效 $transaction = \Yii::$app->db->beginTransaction(); $updateComment = $model->updateCounters(['status' => -1]); $updateNotify = Notification::updateAll(['status' => 0], ['comment_id' => $model->id]); $updateTopic = Topic::updateAllCounters(['comment_count' => -1], ['id' => $model->post_id]); if ($updateNotify && $updateComment && $updateTopic) { $transaction->commit(); } else { $transaction->rollback(); } return $this->redirect(['/topic/default/view', 'id' => $model->post_id]); }
/** * 话题详细页 * @param integer $id * @return mixed */ public function actionView($id) { $model = Topic::findTopic($id); //登录才能访问的节点内容 if (\Yii::$app->user->isGuest && in_array($model->category->alias, params('loginNode'))) { $this->flash('查看本主题需要登录!', 'warning'); return $this->redirect(['/site/login']); } $dataProvider = new ActiveDataProvider(['query' => PostComment::findCommentList($id), 'pagination' => ['pageSize' => self::PAGE_SIZE], 'sort' => ['defaultOrder' => ['created_at' => SORT_ASC]]]); // 文章浏览次数 Topic::updateAllCounters(['view_count' => 1], ['id' => $id]); //内容页面打赏 if (in_array($model->category->alias, params('donateNode')) || array_intersect(explode(',', $model->tags), params('donateTag'))) { $donate = Donate::findOne(['user_id' => $model->user_id, 'status' => Donate::STATUS_ACTIVE]); } /** @var User $user */ $user = Yii::$app->user->identity; $admin = $user && ($user->isAdmin($user->username) || $user->isSuperAdmin($user->username)) ? true : false; return $this->render('view', ['model' => $model, 'dataProvider' => $dataProvider, 'comment' => new PostComment(), 'admin' => $admin, 'donate' => isset($donate) ? $donate : []]); }
public function generateFakeData($num) { Console::startProgress(0, 100); $topic = new Topic(); $comment = new PostComment(); $node = new PostMeta(); $faker = Faker\Factory::create('zh_CN'); $nodeData = [['name' => '分享', 'alias' => '', 'parent' => 0], ['name' => '招聘', 'alias' => 'recruit', 'parent' => 1], ['name' => '瞎扯淡', 'alias' => 'booshit', 'parent' => 1], ['name' => '健康', 'alias' => 'health', 'parent' => 1], ['name' => '创业', 'alias' => 'startup', 'parent' => 1]]; for ($j = 0; $j < count($nodeData); $j++) { $_node = clone $node; $_node->setAttributes($nodeData[$j] + ['type' => 'topic_category']); $_node->save(); } $user = User::find()->where(['role' => User::ROLE_SUPER_ADMIN])->one(); for ($i = 1; $i <= $num; $i++) { $_topic = clone $topic; $_topic->setAttributes(['type' => Topic::TYPE, 'title' => $faker->text(rand(10, 50)), 'post_meta_id' => rand(2, 4), 'status' => rand(1, 2), 'content' => $faker->text(rand(100, 2000)), 'user_id' => 1]); if (!$_topic->save()) { throw new Exception(array_values($_topic->getFirstErrors())[0]); } for ($_i = 1; $_i <= rand(1, 20); $_i++) { $_comment = clone $comment; $_comment->setAttributes(['comment' => $faker->text(rand(100, 2000)), 'post_id' => $_topic->id, 'ip' => '127.0.0.1', 'user_id' => 1]); if (!$_comment->save()) { throw new Exception(array_values($_comment->getFirstErrors())[0]); } // 更新回复时间 $_topic->lastCommentToUpdate($user['username']); // 评论计数器 Topic::updateAllCounters(['comment_count' => 1], ['id' => $_topic->id]); // 更新个人总统计 UserInfo::updateAllCounters(['comment_count' => 1], ['user_id' => $_topic->user_id]); } Console::updateProgress($i / $num * 100, 100); } Console::endProgress(); }
/** * 创建评论 * @param Post $post * @return array|PostComment|string */ protected function newComment(Post $post) { $model = new PostComment(); if ($model->load(Yii::$app->request->post())) { $topService = new TopicService(); if (!$topService->filterContent($model->comment)) { $this->flash('回复内容请勿回复无意义的内容,如你想收藏或赞等功能,请直接操作这篇帖子。', 'warning'); return $this->redirect(['view', 'id' => $post->id]); } $model->user_id = Yii::$app->user->id; $model->post_id = $post->id; $model->ip = Yii::$app->getRequest()->getUserIP(); $rawComment = $model->comment; $model->comment = $model->replace($rawComment); if ($model->save()) { (new UserMeta())->saveNewMeta('topic', $post->id, 'follow'); (new NotificationService())->newReplyNotify(Yii::$app->user->identity, $post, $model, $rawComment); // 评论计数器 Topic::updateAllCounters(['comment_count' => 1], ['id' => $post->id]); // 更新个人总统计 UserInfo::updateAllCounters(['comment_count' => 1], ['user_id' => $model->user_id]); $this->flash("评论成功", 'success'); return $this->redirect(['view', 'id' => $post->id]); } } return $model; }
public function afterSave($insert, $changedAttributes) { parent::afterSave($insert, $changedAttributes); $post = $this->topic; (new UserMeta())->saveNewMeta('topic', $this->post_id, 'follow'); (new NotificationService())->newReplyNotify(\Yii::$app->user->identity, $post, $this, $this->atUsers); // 更新回复时间 $post->lastCommentToUpdate(\Yii::$app->user->identity->username); if ($insert) { // 评论计数器 Topic::updateAllCounters(['comment_count' => 1], ['id' => $post->id]); // 更新个人总统计 UserInfo::updateAllCounters(['comment_count' => 1], ['user_id' => $this->user_id]); } \Yii::$app->cache->set('comment' . $this->id, $this, 0); }