/** * @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 topic identificator. * @return string */ public function actionView($id) { /** @var Topic $topic */ $topic = Topic::find()->where(['id' => $id])->one(); if (!$topic) { throw new NotFoundHttpException(); } $topic->updateCounters(['number_views' => 1]); $topic->save(); $dataProvider = Post::getDataProviderByTopic($topic->id); $posts = $dataProvider->getModels(); if (!Yii::$app->getUser()->getIsGuest()) { $userMentions = UserMention::findAll(['topic_id' => $id, 'mention_user_id' => Yii::$app->getUser()->getId(), 'status' => UserMention::MENTION_SATUS_UNVIEWED]); // user mention update foreach ($userMentions as $userMention) { $userMention->status = UserMention::MENTION_SATUS_VIEWED; $userMention->save(); } $model = new PostForm(); if ($model->load(Yii::$app->getRequest()->post()) && $model->create($topic)) { $this->redirect(['/topic/post/view', 'id' => $model->post->id, '#' => 'p' . $model->post->id]); } return $this->render('view', ['dataProvider' => $dataProvider, 'model' => $model, 'topic' => $topic, 'posts' => $posts]); } else { return $this->render('view', ['dataProvider' => $dataProvider, 'topic' => $topic, 'posts' => $posts]); } }
/** * @inheritdoc */ public function init() { parent::init(); if (!$this->model instanceof PostModel) { throw new InvalidConfigException('The "model" property must be set.'); } if ($this->topic instanceof Topic && $this->model->user instanceof User) { if ($this->topic->first_post_user_id == $this->model->user->id) { $this->model->setIsTopicAuthor(true); } } if (!isset($this->count)) { $this->count = 1; } $this->registerClientScript(); }
/** * @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; }
/** * Returns page number in topic by post. * @param Post $post post model. * @return integer */ protected function getPostPage($post) { $rows = Post::find()->select('id')->where(['topic_id' => $post->topic_id])->asArray()->all(); $index = 1; foreach ($rows as $row) { if ($row['id'] == $post->id) { break; } $index++; } $page = ceil($index / Yii::$app->config->get('display_posts_count')); return $page; }
/** * @param integer $id * @return boolean */ public function update($id) { /** @var Post $post */ $post = Post::findOne(['id' => $id]); if (!$post && !Yii::$app->getUser()->can('updatePost', ['post' => $post])) { return false; } $post->message = $this->message; $post->edited_at = time(); $post->edited_by = Yii::$app->getUser()->getIdentity()->getId(); if ($post->save()) { $this->_post = $post; return true; } return false; }
/** * @param integer $id * @param integer $page * @return string */ public function actionView($id, $page = 1) { /** @var Topic $topic */ $topic = Topic::find()->with('tags')->where(['id' => $id])->one(); if (!$topic) { throw new NotFoundHttpException(); } $topic->updateCounters(['number_views' => 1]); $topic->save(); if (!Yii::$app->getUser()->getIsGuest()) { UserMention::markAsViewedByTopicID($id); } $dataProvider = Post::getDataProvider(['topic_id' => $topic->id, 'page' => $page]); $model = new CreatePostForm(); if ($model->load(Yii::$app->getRequest()->post()) && $model->validate()) { if ($model->create($topic->id)) { $this->redirect(''); } } return $this->render('view', ['dataProvider' => $dataProvider, 'model' => $model]); }
/** * @return string */ public function actionUpdate() { if (Yii::$app->getRequest()->getIsAjax()) { Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $text = Yii::$app->getRequest()->post('text'); $id = substr(Yii::$app->getRequest()->post('id'), 1); /** @var Post $post */ $post = Post::findOne(['id' => $id]); if (!$post || !Yii::$app->getUser()->can('updatePost', ['post' => $post])) { throw new NotFoundHttpException(); } $model = new PostForm(); $model->message = $text; if ($model->validate()) { $post->message = $text; $post->edited_at = time(); $post->edited_by = Yii::$app->getUser()->getIdentity()->getId(); $post->save(); } return $post->displayMessage; } throw new NotFoundHttpException(); }
/** * @return string */ public function actionMention() { if (Yii::$app->getRequest()->getIsAjax()) { Yii::$app->response->format = Response::FORMAT_JSON; $id = substr(Yii::$app->getRequest()->get('id'), 1); $query = Yii::$app->getRequest()->get('query'); if (is_numeric($id)) { $posts = Post::find()->with(['user' => function ($query) { /** @var \yii\db\Query $query */ $query->andWhere(['not in', 'id', Yii::$app->getUser()->getId()]); }])->where(['topic_id' => $id])->orderBy(['created_at' => SORT_DESC])->asArray()->all(); $users = ArrayHelper::getColumn($posts, 'user'); $usernames = array_unique(ArrayHelper::getColumn($users, 'username')); $usernames = array_diff($usernames, ['']); } else { $usernames = User::find()->where(['like', 'username', $query . '%', false])->orderBy(['number_posts' => SORT_DESC])->limit(5)->asArray()->all(); $usernames = ArrayHelper::getColumn($usernames, 'username'); } $usernames = array_values($usernames); return $usernames; } throw new NotFoundHttpException(); }
public function setPost(\post\models\Post $post) { $post->addComment($this); $this->post = $post; }
/** * This action render the markdown helper page. * @return string */ public function actionMarkdown() { $post = Post::findOne(['id' => 450994]); return $this->render('markdown', ['post' => $post]); }
/** * @return ActiveQuery */ public function getPosts() { return $this->hasMany(Post::className(), ['topic_id' => 'id'])->inverseOf('user'); }
/** * @param $params * @return ActiveDataProvider * @throws \yii\base\InvalidConfigException */ public static function getDataProvider($params) { $query = Post::find()->with('user', 'topic')->orderBy(['created_at' => SORT_ASC]); if ($params['topic_id']) { $query->andWhere(['topic_id' => $params['topic_id']]); } $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['route' => '/topic/default/view', 'params' => ['id' => $params['topic_id'], 'page' => $params['page']], 'forcePageParam' => false, 'pageSizeLimit' => false, 'defaultPageSize' => Yii::$app->config->get('display_posts_count')]]); $dataProvider->prepare(); return $dataProvider; }
?> </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> <div class="onlinelist"> <span><strong>Сейчас на форуме: </strong> <?php echo UserOnline::countGuests(); ?>