public function actionCreate($node = null) { if (Yii::$app->user->isGuest) { Yii::$app->getSession()->setFlash('danger', '你需要登陆之后才能创作新主题'); return $this->redirect('/account/login?next=/topic/create'); } if (Yii::$app->user->identity->email_status == 0) { $this->goHome(); } $this->title = '创作新主题 - ' . Yii::$app->name; $this->description = ''; if (!empty($node)) { if (($model = Node::findOne(['enname' => $node])) !== null) { $node = $model; } else { $node = null; } } $model = new Topic(); if (!empty($node)) { $model->node_id = $node->id; } $topicContent = new TopicContent(); if ($model->load(Yii::$app->request->post()) && $model->create()) { $this->redirect('/topic/' . $model->id); } else { return $this->render('create', ['model' => $model, 'node' => $node, 'topicContent' => $topicContent]); } }
/** * @return array */ public function scenarios() { $scenarios = parent::scenarios(); $default = $scenarios['default']; unset($default['cm_id'], $default['parent'], $default['parent_txt']); $scenarios['update'] = $default; return $scenarios; }
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]); }
/** * Lists all NodeLink models. * @return mixed */ public function actionIndex() { $searchModel = new NodeLinkSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); $gridColumns[] = ['class' => 'kartik\\grid\\SerialColumn', 'contentOptions' => ['class' => 'kartik-sheet-style'], 'width' => '36px', 'header' => '序号', 'headerOptions' => ['class' => 'kartik-sheet-style']]; $gridColumns[] = ['attribute' => 'id', 'vAlign' => 'middle']; $gridColumns[] = ['attribute' => 'node_id', 'vAlign' => 'middle', 'width' => '180px', 'value' => function ($searchModel, $key, $index, $widget) { return empty($searchModel->node->name) ? null : $searchModel->node->name; }, 'filterType' => GridView::FILTER_SELECT2, 'filter' => ArrayHelper::map(Node::find()->orderBy('name')->asArray()->all(), 'id', 'name'), 'filterWidgetOptions' => ['pluginOptions' => ['allowClear' => true]], 'filterInputOptions' => ['placeholder' => 'node'], 'format' => 'raw']; $gridColumns[] = 'content'; $gridColumns[] = ['class' => 'kartik\\grid\\BooleanColumn', 'attribute' => 'status', 'width' => '100px', 'vAlign' => 'middle', 'trueLabel' => '启用', 'falseLabel' => '禁用']; $gridColumns[] = ['class' => '\\kartik\\grid\\ActionColumn', 'template' => '{view} {update} {delete}']; return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'gridColumns' => $gridColumns]); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Node::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'tab_id' => $this->tab_id, 'parent_id' => $this->parent_id, 'is_hidden' => $this->is_hidden, 'need_login' => $this->need_login, 'sort' => $this->sort, 'created' => $this->created]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'enname', $this->enname])->andFilterWhere(['like', 'desc', $this->desc])->andFilterWhere(['like', 'logo', $this->logo]); return $dataProvider; }
/** * Lists all Topic models. * @return mixed */ public function actionIndex() { $searchModel = new TopicSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); $gridColumns[] = ['class' => 'kartik\\grid\\SerialColumn', 'contentOptions' => ['class' => 'kartik-sheet-style'], 'width' => '36px', 'header' => '序号', 'headerOptions' => ['class' => 'kartik-sheet-style']]; $gridColumns[] = ['attribute' => 'user_id', 'vAlign' => 'middle', 'width' => '180px', 'value' => function ($searchModel, $key, $index, $widget) { return $searchModel->user->username; }, 'filterType' => GridView::FILTER_SELECT2, 'filter' => ArrayHelper::map(User::find()->orderBy('username')->asArray()->all(), 'id', 'username'), 'filterWidgetOptions' => ['pluginOptions' => ['allowClear' => true]], 'filterInputOptions' => ['placeholder' => '用户'], 'format' => 'raw']; $gridColumns[] = ['attribute' => 'node_id', 'vAlign' => 'middle', 'width' => '180px', 'value' => function ($searchModel, $key, $index, $widget) { return $searchModel->node->name; }, 'filterType' => GridView::FILTER_SELECT2, 'filter' => ArrayHelper::map(Node::find()->orderBy('name')->asArray()->all(), 'id', 'name'), 'filterWidgetOptions' => ['pluginOptions' => ['allowClear' => true]], 'filterInputOptions' => ['placeholder' => '节点'], 'format' => 'raw']; $gridColumns[] = ['class' => 'kartik\\grid\\BooleanColumn', 'attribute' => 'need_login', 'vAlign' => 'middle', 'trueLabel' => '是', 'falseLabel' => '否']; $gridColumns[] = 'title'; $gridColumns[] = 'click'; $gridColumns[] = 'follow'; $gridColumns[] = 'reply'; $gridColumns[] = ['mergeHeader' => true, 'attribute' => 'created', 'format' => 'datetime']; $gridColumns[] = ['class' => '\\kartik\\grid\\ActionColumn', 'template' => '{view} {update} {delete}', 'buttons' => ['view' => function ($url, $model) { return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', Yii::$app->params['domain'] . $url, ['title' => '查看', 'target' => '_blank']); }]]; return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'gridColumns' => $gridColumns]); }
<?php $HotNode = \common\models\Node::HotNode(); if (!empty($HotNode)) { ?> <section> <div class="block-header"><small>最热节点</small></div> <article class="node block-content"> <?php $HotNode = \common\models\Node::HotNode(); ?> <?php foreach ($HotNode as $n) { ?> <a href="/node/<?php echo $n['enname']; ?> "><?php echo $n['name']; ?> </a> <?php } ?> </article> </section> <?php }
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]); }
?> <section> <?php echo yii\widgets\Breadcrumbs::widget(['options' => ['class' => 'breadcrumb mb0'], 'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : []]); ?> <article class="header"> <div class="row"> <div class="col-lg-12"> <?php $form = ActiveForm::begin(['id' => 'create-form']); ?> <?php echo $form->field($model, 'title'); ?> <?php echo $form->field($model, 'node_id')->widget(Select2::classname(), ['data' => \common\models\Node::AllNode(), 'options' => ['placeholder' => '请选择一个节点'], 'pluginOptions' => ['allowClear' => true]]); ?> <?php echo $form->field($topicContent, 'content')->textarea(['rows' => 16]); ?> <div class="form-group"> <?php echo Html::submitButton('提交建议', ['class' => 'btn btn-success']); ?> <?php echo Html::button('预览正文', ['class' => 'btn btn-primary preview']); ?> </div> <?php ActiveForm::end(); ?>
static function Info($id) { if (!($nodeInfo = Yii::$app->cache->get('node' . $id))) { $nodeInfo = Node::find()->where(['id' => $id])->asArray()->one(); Yii::$app->cache->set('node' . $id, $nodeInfo, 0); } return $nodeInfo; }
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]); }
<?php $ParentNode = \common\models\Node::Info($node->parent_id); $RelatedNode = \common\models\Node::RelatedNode($node->id, $node->parent_id, false); $SubNode = \common\models\Node::SubNode($node->id, false); if (!empty($ParentNode) || !empty($RelatedNode) || !empty($SubNode)) { ?> <section> <?php if (!empty($ParentNode)) { ?> <div class="block-header"> <small><b>父节点</b></small> <div class="mt10"></div> <img src="<?php echo Yii::$app->params['nodeUrl'] . '/24/' . $ParentNode['logo']; ?> " class="img-rounded"> <a href="/node/<?php echo $ParentNode['enname']; ?> "><?php echo $ParentNode['name']; ?> </a> </div> <?php } ?> <?php
</article> <?php } ?> </section> <section> <div class="block-header"><?php echo $model->username; ?> 最近发布的主题</div> <?php foreach ($model->topicList as $t) { ?> <?php $nodeInfo = \common\models\Node::Info($t->node_id); ?> <article class="item"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tbody><tr> <td width="auto" valign="middle"> <h2><a href="/topic/<?php echo $t->id; if ($t->reply > 0) { ?> #reply<?php echo $t->reply; } ?> "><?php
/** * 获取我收藏的节点 * @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; }
/** * 热门主题 * @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(); }
/** * @return \yii\db\ActiveQuery */ public function getNode() { return $this->hasOne(Node::className(), ['id' => 'node_id']); }
/** * @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(); }
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; }
/** * 获取tab的节点id,有缓存就获取缓存,用来取tab下面的节点建议 * @return array|\yii\db\ActiveRecord[] */ static function SubNodeId($enname) { if (!($SubNodeId = Yii::$app->cache->get('subnodeid' . $enname))) { $tab = Tab::findOne(['enname' => $enname]); if (empty($tab->id)) { return []; } $SubNodeId = ArrayHelper::map(Node::find()->where(['tab_id' => $tab->id])->andWhere(['is_hidden' => 0])->all(), 'id', 'id'); Yii::$app->cache->set('subnodeid' . $enname, $SubNodeId, 0); } return $SubNodeId; }
/** * Finds the Topic model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Topic the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($name) { if (($model = Node::findOne(['enname' => $name])) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
use yii\helpers\Html; use yii\widgets\ActiveForm; /* @var $this yii\web\View */ /* @var $model common\models\Topic */ /* @var $form yii\widgets\ActiveForm */ ?> <div class="topic-form"> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model, 'node_id')->widget(\kartik\select2\Select2::classname(), ['data' => \yii\helpers\ArrayHelper::map(\common\models\Node::find()->all(), 'id', 'name'), 'options' => ['placeholder' => '节点'], 'pluginOptions' => ['allowClear' => true]]); ?> <?php echo $form->field($model, 'need_login')->dropDownList(['1' => '是', '0' => '否']); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?> </div> <?php ActiveForm::end(); ?>