public function userDoAction($id, $action) { $topic = Topic::findTopic($id); $user = \Yii::$app->user->getIdentity(); if (in_array($action, ['like', 'hate'])) { return UserService::TopicActionA($user, $topic, $action); } else { return UserService::TopicActionB($user, $topic, $action); } }
public function actionPost() { $update = Topic::updateAll(['last_comment_time' => new Expression('created_at')], ['and', ['type' => Topic::TYPE], ['or', ['last_comment_username' => ''], ['last_comment_username' => null]]]); $this->stdout("同步最后回复时间,同步{$update}条数据\n"); $subQuery = new Query(); $subQuery->from(PostComment::tableName())->where(['status' => PostComment::STATUS_ACTIVE])->orderBy(['created_at' => SORT_DESC]); $comment = PostComment::find()->from(['tmpA' => $subQuery])->groupBy('post_id')->all(); Topic::updateAll(['comment_count' => 0], ['type' => Topic::TYPE]); $updateComment = []; foreach ($comment as $value) { $commentCount = PostComment::find()->where(['post_id' => $value->post_id, 'status' => PostComment::STATUS_ACTIVE])->count(); $updateComment[] = Topic::updateAll(['last_comment_time' => $value->created_at, 'last_comment_username' => $value->user->username, 'comment_count' => $commentCount], ['id' => $value->post_id, 'type' => Topic::TYPE]); } $this->stdout("校正最后回复时间和回复会员还有评论条数,校正" . count($updateComment) . "条数据\n"); }
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(); }
public function run() { $tipsModel = ArrayHelper::map(RightLink::find()->where(['type' => RightLink::RIGHT_LINK_TYPE_TIPS])->all(), 'content', 'title'); $tips = array_rand($tipsModel); $recommendResources = ArrayHelper::map(RightLink::find()->where(['type' => RightLink::RIGHT_LINK_TYPE_RSOURCES])->all(), 'title', 'url'); $links = RightLink::find()->where(['type' => RightLink::RIGHT_LINK_TYPE_LINKS])->all(); $sameTopics = []; if ($this->node) { $sameTopics = ArrayHelper::map(Topic::find()->where('status >= :status', [':status' => Topic::STATUS_ACTIVE])->andWhere(['post_meta_id' => $this->node->id, 'type' => 'topic'])->limit(200)->all(), 'title', function ($e) { return Url::to(['/topic/default/view', 'id' => $e->id]); }); if (count($sameTopics) > 10) { $sameTopics = Arr::arrayRandomAssoc($sameTopics, 10); } } return $this->render('topicSidebar', ['category' => PostMeta::blogCategory(), 'config' => ['type' => $this->type, 'node' => $this->node], 'sameTopics' => $sameTopics, 'tips' => $tips, 'recommendResources' => $recommendResources, 'links' => $links]); }
/** * 伪删除 * @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]); }
public function run() { $tipsModel = ArrayHelper::map(RightLink::find()->where(['type' => RightLink::RIGHT_LINK_TYPE_TIPS])->all(), 'content', 'title'); $tips = array_rand($tipsModel); $recommendResources = ArrayHelper::map(RightLink::find()->where(['type' => RightLink::RIGHT_LINK_TYPE_RSOURCES])->all(), 'title', 'url'); $links = RightLink::find()->where(['type' => RightLink::RIGHT_LINK_TYPE_LINKS])->all(); $sameTopics = []; if ($this->node) { $sameTopics = ArrayHelper::map(Topic::find()->where('status >= :status', [':status' => Topic::STATUS_ACTIVE])->andWhere(['post_meta_id' => $this->node->id, 'type' => 'topic'])->limit(200)->all(), 'title', function ($e) { return Url::to(['/topic/default/view', 'id' => $e->id]); }); if (count($sameTopics) > 10) { $sameTopics = Arr::arrayRandomAssoc($sameTopics, 10); } if ($this->type == 'view' && (in_array($this->node->alias, params('donateNode')) || array_intersect(explode(',', $this->tags), params('donateTag')))) { $donate = Donate::findOne(['user_id' => Topic::findOne(['id' => request()->get('id')])->user_id, 'status' => Donate::STATUS_ACTIVE]); } } return $this->render('topicSidebar', ['category' => PostMeta::blogCategory(), 'config' => ['type' => $this->type, 'node' => $this->node], 'sameTopics' => $sameTopics, 'tips' => $tips, 'donate' => isset($donate) ? $donate : [], 'recommendResources' => $recommendResources, 'links' => $links]); }
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(); }
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; }
/** * 最近主题 * @param string $username * @return string * @throws NotFoundHttpException */ public function actionPost($username = '') { $user = $this->user($username); $dataProvider = new ActiveDataProvider(['query' => Topic::find()->where(['user_id' => $user->id, 'type' => Topic::TYPE])->andWhere('status > :status ', [':status' => Topic::STATUS_DELETED])->orderBy(['created_at' => SORT_DESC])]); return $this->render('show', ['user' => $user, 'dataProvider' => $dataProvider]); }
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); }
public function getTopic() { return $this->hasOne(Topic::className(), ['id' => 'target_id']); }
/** * 加精华 * @param Topic $topic */ public static function excellent(Topic $topic) { $action = $topic->status == Topic::STATUS_ACTIVE ? Topic::STATUS_EXCELLENT : Topic::STATUS_ACTIVE; $topic->setAttributes(['status' => $action]); $topic->save(); }
/** * 加精华 * @param $id * @return \yii\web\Response * @throws NotFoundHttpException */ public function actionExcellent($id) { $user = Yii::$app->user->identity; $model = Topic::findTopic($id); if ($user && ($user->isAdmin($user->username) || $user->isSuperAdmin($user->username))) { TopicService::excellent($model); $this->flash("操作成功", 'success'); return $this->redirect(['/topic/default/view', 'id' => $model->id]); } else { throw new NotFoundHttpException(); } }