예제 #1
0
 /**
  * 创建评论
  * @param $id
  * @return PostComment|\yii\web\Response
  */
 public function actionCreate($id)
 {
     $post = Topic::findTopic($id);
     $model = new PostComment();
     if ($model->load(Yii::$app->request->post())) {
         $topService = new TopicService();
         if (!$topService->filterContent($model->comment)) {
             $this->flash('回复内容请勿回复无意义的内容,如你想收藏或赞等功能,请直接操作这篇帖子。', 'warning');
             return $this->redirect(['/topic/default/view', 'id' => $id]);
         }
         $model->user_id = Yii::$app->user->id;
         $model->post_id = $id;
         $model->ip = Yii::$app->getRequest()->getUserIP();
         $rawComment = $model->comment;
         $model->comment = $model->replace($rawComment);
         if ($model->save()) {
             (new UserMeta())->saveNewMeta('topic', $id, 'follow');
             (new NotificationService())->newReplyNotify(Yii::$app->user->identity, $post, $model, $rawComment);
             // 更新回复时间
             $post->lastCommentToUpdate(Yii::$app->user->identity->username);
             // 评论计数器
             Topic::updateAllCounters(['comment_count' => 1], ['id' => $post->id]);
             // 更新个人总统计
             UserInfo::updateAllCounters(['comment_count' => 1], ['user_id' => $model->user_id]);
             $this->flash("评论成功", 'success');
         } else {
             $this->flash(array_values($model->getFirstErrors())[0], 'warning');
         }
         return $this->redirect(['/topic/default/view', 'id' => $post->id]);
     }
     return $model;
 }
예제 #2
0
 public function actionUserInfo()
 {
     UserInfo::updateAll(['thanks_count' => 0, 'like_count' => 0, 'hate_count' => 0]);
     $meta = UserMeta::find()->all();
     foreach ($meta as $key => $value) {
         if (in_array($value->type, ['thanks', 'like', 'hate'])) {
             switch ($value->target_type) {
                 case 'topic':
                 case 'post':
                     $this->stdout("同步文章操作……\n");
                     $topic = Topic::findOne($value->target_id);
                     if (UserInfo::updateAllCounters([$value->type . '_count' => 1], ['user_id' => $topic->user_id])) {
                         $this->stdout("同步评论成功`(*∩_∩*)′\n");
                     } else {
                         $this->stdout("同步评论失败::>_<::\n");
                     }
                     break;
                 case 'comment':
                     $this->stdout("同步评论操作……\n");
                     $comment = PostComment::findOne($value->target_id);
                     if (UserInfo::updateAllCounters([$value->type . '_count' => 1], ['user_id' => $comment->user_id])) {
                         $this->stdout("同步评论成功`(*∩_∩*)′\n");
                     } else {
                         $this->stdout("同步评论失败::>_<:: \n");
                     }
                     break;
                 default:
                     # code...
                     break;
             }
         }
     }
     return;
 }
예제 #3
0
 /**
  * Shows user's profile.
  * @param  string $username
  * @return \yii\web\Response
  * @throws \yii\web\NotFoundHttpException
  */
 public function actionShow($username = '')
 {
     $user = $this->user($username);
     // 个人主页浏览次数
     $currentUserId = \Yii::$app->getUser()->getId();
     if (null != $currentUserId && $user->id != $currentUserId) {
         UserInfo::updateAllCounters(['view_count' => 1], ['user_id' => $user->id]);
     }
     return $this->render('show', ['user' => $user, 'dataProvider' => $this->comment($user->id)]);
 }
예제 #4
0
 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();
 }
예제 #5
0
 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();
 }
예제 #6
0
 public function actionSync()
 {
     UserInfo::updateAll(['thanks_count' => 0, 'like_count' => 0, 'hate_count' => 0]);
     $meta = UserMeta::find()->all();
     foreach ($meta as $key => $value) {
         if (in_array($value->type, ['thanks', 'like', 'hate'])) {
             switch ($value->target_type) {
                 case 'topic':
                 case 'post':
                     echo '同步文章操作</br>';
                     $topic = Topic::findOne($value->target_id);
                     UserInfo::updateAllCounters([$value->type . '_count' => 1], ['user_id' => $topic->user_id]);
                     break;
                 case 'comment':
                     echo '同步评论操作</br>';
                     $comment = PostComment::findOne($value->target_id);
                     UserInfo::updateAllCounters([$value->type . '_count' => 1], ['user_id' => $comment->user_id]);
                     break;
                 default:
                     # code...
                     break;
             }
         }
     }
     return;
 }
예제 #7
0
 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);
 }
예제 #8
0
파일: Topic.php 프로젝트: iiyii/getyii
 public function afterSave($insert, $changedAttributes)
 {
     parent::afterSave($insert, $changedAttributes);
     if (isset(Yii::$app->params['setting']) && Yii::$app->params['setting']['xunsearch']) {
         if ($insert) {
             $search = new Search();
             $search->topic_id = $this->id;
             $search->status = self::STATUS_ACTIVE;
         } else {
             $search = Search::findOne($this->id);
             if (!$search) {
                 // 如果立即修改 会因为在 xunsearch 找不到而不能 save
                 return false;
             }
             $search->status = $this->status;
         }
         $search->title = $this->title;
         $search->content = $this->content;
         $search->updated_at = $this->updated_at;
         $search->save();
     }
     (new NotificationService())->newPostNotify(\Yii::$app->user->identity, $this, $this->atUsers);
     if ($insert) {
         // 保存 meta data
         (new UserMeta())->saveNewMeta('topic', $this->id, 'follow');
         // 更新个人总统计
         UserInfo::updateAllCounters(['post_count' => 1], ['user_id' => $this->user_id]);
     }
 }
예제 #9
0
 /**
  * 新建话题
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Topic();
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         $topService = new TopicService();
         if (!$topService->filterContent($model->title) || !$topService->filterContent($model->content)) {
             $this->flash('请勿发表无意义的内容', 'warning');
             return $this->redirect('create');
         }
         $model->user_id = Yii::$app->user->id;
         $model->type = 'topic';
         if ($model->tags) {
             $model->addTags(explode(',', $model->tags));
         }
         $rawContent = $model->content;
         $model->content = TopicService::replace($rawContent);
         if ($model->save()) {
             (new UserMeta())->saveNewMeta('topic', $model->id, 'follow');
             (new NotificationService())->newPostNotify(Yii::$app->user->identity, $model, $rawContent);
             // 更新个人总统计
             UserInfo::updateAllCounters(['post_count' => 1], ['user_id' => $model->user_id]);
             $this->flash('发表文章成功!', 'success');
             return $this->redirect(['view', 'id' => $model->id]);
         }
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
예제 #10
0
 /**
  * 喝倒彩或者赞
  * @param User $user
  * @param Post $model
  * @param $action 动作
  * @return array
  */
 protected static function toggleType(User $user, Post $model, $action)
 {
     $data = ['target_id' => $model->id, 'target_type' => $model::TYPE, 'user_id' => $user->id, 'value' => '1'];
     if (!UserMeta::deleteOne($data + ['type' => $action])) {
         // 删除数据有行数则代表有数据,无行数则添加数据
         $userMeta = new UserMeta();
         $userMeta->setAttributes($data + ['type' => $action]);
         $result = $userMeta->save();
         if ($result) {
             // 如果是新增数据, 删除掉Hate的同类型数据
             $attributeName = $action == 'like' ? 'hate' : 'like';
             $attributes = [$action . '_count' => 1];
             if (UserMeta::deleteOne($data + ['type' => $attributeName])) {
                 // 如果有删除hate数据, hate_count也要-1
                 $attributes[$attributeName . '_count'] = -1;
             }
             //更新版块统计
             $model->updateCounters($attributes);
             // 更新个人总统计
             UserInfo::updateAllCounters($attributes, ['user_id' => $model->user_id]);
         }
         return [$result, $userMeta];
     }
     $model->updateCounters([$action . '_count' => -1]);
     UserInfo::updateAllCounters([$action . '_count' => -1], ['user_id' => $model->user_id]);
     return [true, null];
 }