public function actionTopic($username) { $this->title = $username . '提的建议 - ' . Yii::$app->name; $this->description = ''; $this->canonical = Yii::$app->params['domain'] . 'member/' . $username . '/topic'; $user = $this->findModel($username); $query = (new Query())->select('topic.*, node.enname, node.name, user.username, user.avatar')->from(Topic::tableName())->leftJoin(Node::tableName(), 'node.id = topic.node_id')->leftJoin(User::tableName(), 'user.id = topic.user_id')->where(['node.is_hidden' => 0])->andWhere(['topic.user_id' => $user->id]); $pagination = new Pagination(['defaultPageSize' => Yii::$app->params['pageSize'], 'totalCount' => $query->count()]); $model = $query->orderBy(['id' => SORT_DESC])->offset($pagination->offset)->limit($pagination->limit)->all(); return $this->render('topic', ['model' => $model, 'user' => $user, 'pagination' => $pagination]); }
public function actionView($nodeName) { $node = $this->findModel($nodeName); if ($node->need_login == 1 && Yii::$app->user->isGuest) { Yii::$app->getSession()->setFlash('danger', '你访问的节点需要登陆之后才能查看'); return $this->redirect('/account/login?next=/node/' . $nodeName); } if (!empty($node->bg) && $node->use_bg == 1) { $this->bg = $node->bg; } if (!empty($node->bg_color)) { $this->bg_color = $node->bg_color; } $this->title = $node->name . ' - ' . Yii::$app->name; $this->description = ''; $this->canonical = Yii::$app->params['domain'] . 'node/' . $nodeName; $query = (new Query())->select('topic.*, node.enname, node.name, user.username, user.avatar')->from(Topic::tableName())->leftJoin(Node::tableName(), 'node.id = topic.node_id')->leftJoin(User::tableName(), 'user.id = topic.user_id')->where(['node.id' => $node->id]); $pagination = new Pagination(['defaultPageSize' => Yii::$app->params['pageSize'], 'totalCount' => $query->count()]); $model = $query->orderBy(['id' => SORT_DESC])->offset($pagination->offset)->limit($pagination->limit)->all(); return $this->render('view', ['model' => $model, 'node' => $node, 'pagination' => $pagination]); }
static function Ranking() { if (!($Ranking = Yii::$app->cache->get('Ranking'))) { $Ranking = (new Query())->select('count(topic.id) topic_count, user.username, user.avatar')->from(Topic::tableName() . ' topic')->leftJoin(Node::tableName(), 'node.id = topic.node_id')->leftJoin(User::tableName() . ' user', 'user.id = topic.user_id')->where(['node.is_hidden' => 0])->groupBy(['topic.user_id'])->orderBy(['topic_count' => SORT_DESC])->limit(10)->all(); Yii::$app->cache->set('Ranking', $Ranking, 86400); } return $Ranking; }
/** * 热门主题 * @param int $num 获取几条热门主题 * @return array|\yii\db\ActiveRecord[] */ static function HotTopic($num = 8) { return $topic = (new Query())->select('topic.*, user.username, user.avatar')->from(Topic::tableName() . ' topic')->leftJoin(Node::tableName() . ' node', 'node.id = topic.node_id')->leftJoin(User::tableName() . ' user', 'user.id = topic.user_id')->where('node.is_hidden = 0')->andWhere(['between', 'topic.created', strtotime(date('Y-m-d H:i', time())) - 86400, strtotime(date('Y-m-d H:i', time()))])->orderBy(['topic.reply' => SORT_DESC])->limit($num)->all(); }
public function actionRecent() { $this->title = '最近的主题 - ' . Yii::$app->name; $this->description = ''; $pagination = new Pagination(['defaultPageSize' => Yii::$app->params['pageSize'], 'totalCount' => (new Query())->from(Topic::tableName() . ' topic')->leftJoin(Node::tableName() . ' node', 'node.id = topic.node_id')->where('node.is_hidden = 0')->count()]); $topic = (new Query())->select('topic.*, node.enname, node.name, user.username, user.avatar')->from(Topic::tableName() . ' topic')->leftJoin(Node::tableName() . ' node', 'node.id = topic.node_id')->leftJoin(User::tableName() . ' user', 'user.id = topic.user_id')->where('node.is_hidden = 0')->orderBy(['topic.id' => SORT_DESC])->offset($pagination->offset)->limit($pagination->limit)->all(); return $this->render('recent', ['topic' => $topic, 'pagination' => $pagination]); }
/** * 最新节点 * @param int $num 获取最新节点 * @return array|\yii\db\ActiveRecord[] */ static function NewNode($num = 20) { if (!($NewNode = Yii::$app->cache->get('NewNode' . $num))) { $NewNode = (new Query())->select('node.enname, node.name')->from(Node::tableName() . ' node')->orderBy('node.id DESC')->limit($num)->all(); Yii::$app->cache->set('NewNode' . $num, $NewNode, 86400); } return $NewNode; }
public function actionTopic() { $this->title = '关注的建议' . ' - ' . Yii::$app->name; $this->description = ''; if (Yii::$app->user->isGuest) { return $this->redirect('/account/login?next=/account/topic'); } $query = (new Query())->select('topic.*, node.enname, node.name, user.username, user.avatar')->from(Topic::tableName())->leftJoin(Node::tableName(), 'node.id = topic.node_id')->leftJoin(User::tableName(), 'user.id = topic.user_id')->where(['in', 'topic.id', Follow::Topic()]); $pagination = new Pagination(['defaultPageSize' => Yii::$app->params['pageSize'], 'totalCount' => $query->count()]); $model = $query->orderBy(['id' => SORT_DESC])->offset($pagination->offset)->limit($pagination->limit)->all(); return $this->render('topic', ['model' => $model, 'pagination' => $pagination]); }
/** * 获取我收藏的节点 * @param bool $onlyId * @return array|\yii\db\ActiveRecord[] */ static function Node($onlyId = true) { if ($onlyId) { if (!($FollowNode = Yii::$app->cache->get('FollowNodeId' . Yii::$app->user->id))) { $FollowNode = ArrayHelper::map(Follow::find()->select('follow_id')->where(['user_id' => Yii::$app->user->id, 'type' => 2])->asArray('follow_id')->all(), 'follow_id', 'follow_id'); Yii::$app->cache->set('FollowNodeId' . Yii::$app->user->id, $FollowNode, 0); } } else { if (!($FollowNode = Yii::$app->cache->get('FollowNode' . Yii::$app->user->id))) { $FollowNode = (new Query())->from(Node::tableName())->select('node.enname, node.name, node.logo')->where(['in', 'id', Follow::Node()])->all(); Yii::$app->cache->set('FollowNode' . Yii::$app->user->id, $FollowNode, 0); } } return $FollowNode; }
/** * 获取相关节点 * @param $node_id * @param $parent_id * @param bool $onlyId * @return array|\yii\db\ActiveRecord[] */ static function RelatedNode($node_id, $parent_id, $onlyId = true) { if ($onlyId) { if (!($NodeRelatedNode = Yii::$app->cache->get('NodeRelatedNodeId' . $node_id))) { $NodeRelatedNode = ArrayHelper::map(Node::find()->select('id')->where(['parent_id' => $parent_id])->andWhere('id != ' . $node_id)->asArray('id')->all(), 'id', 'id'); Yii::$app->cache->set('NodeRelatedNodeId' . $node_id, $NodeRelatedNode, 0); } } else { if (!($NodeRelatedNode = Yii::$app->cache->get('NodeRelatedNode' . $node_id))) { $NodeRelatedNode = (new Query())->from(Node::tableName())->select('enname, name, logo')->where(['parent_id' => $parent_id])->andWhere('id != ' . $node_id)->all(); Yii::$app->cache->set('NodeRelatedNode' . $node_id, $NodeRelatedNode, 0); } } return $NodeRelatedNode; }
/** * @return \yii\db\ActiveQuery */ public function getReplyList() { return (new Query())->select('reply.*, topic.title, node.enname, node.name, user.username, user.avatar')->from(Reply::tableName())->leftJoin(Topic::tableName(), 'topic.id = reply.topic_id')->leftJoin(Node::tableName(), 'node.id = topic.node_id')->leftJoin(User::tableName() . ' user', 'user.id = reply.user_id')->where(['node.is_hidden' => 0])->andWhere(['reply.user_id' => $this->id])->orderBy(['id' => SORT_DESC])->limit(10)->all(); }