/** * @return boolean */ public function create() { // very, so much, stupid source code :) if ($this->validate()) { $user = Yii::$app->getUser()->getIdentity(); // create post $post = new Post(); $post->topic_id = 0; $post->message = $this->message; $post->save(); if ($post->save()) { // create topic $topic = new Topic(); $topic->subject = $this->subject; $topic->post = $post; $topic->save(); // update post.topic_id $post->link('topic', $topic); $tagNames = explode(',', $this->tags); foreach ($tagNames as $tagName) { /** @var Tag $tagModel */ $tagModel = Tag::findOne($tagName); $topic->link('tags', $tagModel); } $this->topic = $topic; return true; } } return false; }
/** * @param $id * @return string */ public function actionView($id) { /** @var Post $post */ $post = Post::findOne(['id' => $id]); /** @var Topic $topic */ $topic = Topic::find()->where(['id' => $post->topic_id])->one(); if (!$topic) { throw new NotFoundHttpException(); } $topic->updateCounters(['number_views' => 1]); $topic->save(); if (!Yii::$app->getUser()->getIsGuest()) { $userMentions = UserMention::findAll(['post_id' => $id, 'mention_user_id' => Yii::$app->getUser()->getId(), 'status' => UserMention::MENTION_SATUS_UNVIEWED]); foreach ($userMentions as $userMention) { $userMention->status = UserMention::MENTION_SATUS_VIEWED; $userMention->save(); } } $dataProvider = Post::getDataProviderByTopic($topic->id); $dataProvider->pagination->route = '/topic/default/view'; $dataProvider->pagination->params = ['id' => $topic->id, 'page' => $this->getPostPage($post)]; $posts = $dataProvider->getModels(); $model = new PostForm(); return $this->render('/default/view', ['dataProvider' => $dataProvider, 'model' => $model, 'topic' => $topic, 'posts' => $posts]); }
/** * @return string */ public function actionList() { $name = Yii::$app->getRequest()->get('name'); $tagModel = ''; if (isset($name)) { /** @var Tag $tagModel */ $tagModel = Tag::findOne(['name' => $name]); if (!$tagModel) { throw new NotFoundHttpException(); } } $query = Topic::find()->select('*')->from('topic t')->with('tags', 'firstPostUser', 'lastPostUser'); if (isset($name)) { $query->innerJoin('tag_topic_assignment tta', 'tta.topic_id = t.id')->where(['tta.tag_name' => $name]); } $sort_by = Yii::$app->getRequest()->get('sort_by'); if (!$sort_by || $sort_by == 'new') { $query->orderBy(['t.last_post_created_at' => SORT_DESC]); } elseif ($sort_by == 'unanwser') { $query->andWhere(['t.number_posts' => 0])->orderBy(['t.last_post_created_at' => SORT_DESC]); } elseif ($sort_by == 'own') { $id = Yii::$app->getUser()->getId(); $query->andWhere(['t.first_post_user_id' => $id])->orderBy(['t.last_post_created_at' => SORT_DESC]); } $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['forcePageParam' => false, 'pageSizeLimit' => false, 'defaultPageSize' => Yii::$app->config->get('display_topics_count')]]); $topics = $dataProvider->getModels(); return $this->render('list', ['dataProvider' => $dataProvider, 'tagModel' => $tagModel, 'topics' => $topics]); }
/** * @param Topic $topic * @return boolean */ public function create($topic) { if ($this->validate()) { $user = Yii::$app->getUser()->getIdentity(); $post = new Post(); $post->topic_id = $topic->id; $post->message = $this->message; $post->save(); $topic->updateCounters(['number_posts' => 1]); $topic->last_post_username = $user->username; $topic->last_post_created_at = time(); $topic->last_post_id = $post->id; $topic->last_post_user_id = $user->id; $topic->save(); $this->post = $post; return true; } return false; }
/** * @return ActiveQuery */ public function getTopic() { return $this->hasOne(Topic::className(), ['id' => 'topic_id']); }
/** * @return ActiveQuery */ public function getTopic() { return $this->hasOne(Topic::className(), ['id' => 'topic_id'])->inverseOf('posts'); }
</td> </tr> <?php } ?> </tbody> </table> </div> <?php } ?> <div class="statistic"> <div class="clearfix"> <ul class="right"> <li>Тем: <strong><?php echo $formatter->asInteger(\topic\models\Topic::countAll()); ?> </strong></li> <li>Сообщений: <strong><?php echo $formatter->asInteger(\post\models\Post::find()->count()); ?> </strong></li> </ul> <ul class="left"> <li>Количество пользователей: <strong><?php echo $formatter->asInteger(User::find()->count()); ?> </strong></li> <li>Последним зарегистрировался: <a href="">X</a></li> </ul> </div>
/** * @return ActiveQuery */ public function getTopics() { return $this->hasMany(Topic::className(), ['id' => 'topic_id'])->viaTable('tag_topic_assignment', ['tag_name' => 'name']); }