/** * @param SiteModels $forum * @return boolean */ public function create($forum) { // very, so much, stupid source code :) if ($this->validate()) { $user = Yii::$app->getUser()->getIdentity(); // create post $post = new PostModels(); $post->topic_id = 0; $post->message = $this->message; $post->save(); if ($post->save()) { // create topic $topic = new TopicModels(); $topic->forum_id = $forum->id; $topic->subject = $this->subject; $topic->post = $post; $topic->save(); // update post.topic_id $post->link('topic', $topic); // update forum information $forum->updateCounters(['number_topics' => 1]); $forum->last_post_created_at = time(); $forum->last_post_user_id = $post->id; $forum->last_post_username = $user->username; $forum->save(); $this->topic = $topic; return true; } } }
/** * @inheritdoc */ public function init() { parent::init(); if (!$this->model instanceof PostModel) { throw new InvalidConfigException('The "model" property must be set.'); } if ($this->topic instanceof TopicModels && $this->model->user instanceof UserModels) { if ($this->topic->first_post_user_id == $this->model->user->id) { $this->model->setIsTopicAuthor(true); } } if (!isset($this->count)) { $this->count = 1; } $this->registerClientScript(); }
public function actionViewOwnpostTopics() { // !!! need access check if (Yii::$app->getUser()->getIsGuest()) { throw new NotFoundHttpException(); } $user = Yii::$app->getUser()->getIdentity(); $posts = PostModels::find()->select(['topic_id', 'user_id'])->where('user_id = :user_id', [':user_id' => $user->id])->asArray()->all(); $ids = ArrayHelper::getColumn($posts, 'topic_id'); $uniqueIDs = array_unique($ids); $query = TopicModels::find()->where(['IN', 'id', $uniqueIDs])->andWhere('forum_id NOT LIKE 0')->with('forum')->orderBy(['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('topic_list', ['title' => 'Темы с вашим участием', 'dataProvider' => $dataProvider, 'topics' => $topics]); }
/** * @return string */ public function actionMention() { if (Yii::$app->getRequest()->getIsAjax()) { Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $id = substr(Yii::$app->getRequest()->post('id'), 1); $posts = PostModels::find()->with('user')->where(['topic_id' => $id])->asArray()->all(); $currentUser = Yii::$app->getUser()->getIdentity(); $users = ArrayHelper::getColumn($posts, 'user'); $usernames = array_unique(ArrayHelper::getColumn($users, 'username')); $key = array_search($currentUser->username, $usernames); if (is_array($usernames) && is_numeric($key) && array_key_exists($key, $usernames)) { unset($usernames[$key]); } $usernames = array_values($usernames); return $usernames; } throw new NotFoundHttpException(); }
/** * @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 PostModels $post */ $post = PostModels::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 ActiveQuery */ public function getPosts() { return $this->hasMany(PostModels::className(), ['topic_id' => 'id'])->inverseOf('user'); }
?> <div class="statistic"> <div class="clearfix"> <ul class="right"> <li><?php echo Yii::t('forum', 'Тем:'); ?> <strong><?php echo $formatter->asInteger(TopicModels::countAll()); ?> </strong></li> <li><?php echo Yii::t('forum', 'Сообщений:'); ?> <strong><?php echo $formatter->asInteger(PostModels::find()->count()); ?> </strong></li> </ul> <ul class="left"> <li><?php echo Yii::t('forum', 'Количество пользователей:'); ?> <strong><?php echo $formatter->asInteger(UserModels::find()->count()); ?> </strong></li> <li><?php echo Yii::t('forum', 'Последним зарегистрировался:'); ?> <?php
public function getPostPage($post) { $rows = PostModels::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; }
/** * @return ActiveQuery */ public function getPost() { return $this->hasone(PostModels::className(), ['id' => 'post_id']); }