/** * Creating the thread of given category ID and forum ID. * @param integer $cid * @param integer $fid * @return string|\yii\web\Response */ public function actionNewThread($cid = null, $fid = null) { if (!User::can(Rbac::PERM_CREATE_THREAD)) { if (Yii::$app->user->isGuest) { $this->warning(Yii::t('podium/flash', 'Please sign in to create a new thread.')); return $this->redirect(['account/login']); } else { $this->error(Yii::t('podium/flash', 'Sorry! You do not have the required permission to perform this action.')); return $this->redirect(['default/index']); } } else { if (!is_numeric($cid) || $cid < 1 || !is_numeric($fid) || $fid < 1) { $this->error(Yii::t('podium/flash', 'Sorry! We can not find the forum you are looking for.')); return $this->redirect(['default/index']); } $category = Category::findOne((int) $cid); if (!$category) { $this->error(Yii::t('podium/flash', 'Sorry! We can not find the forum you are looking for.')); return $this->redirect(['default/index']); } else { $forum = Forum::find()->where(['id' => (int) $fid, 'category_id' => $category->id])->limit(1)->one(); if (!$forum) { $this->error(Yii::t('podium/flash', 'Sorry! We can not find the forum you are looking for.')); return $this->redirect(['default/index']); } else { $model = new Thread(); $model->setScenario('new'); $postData = Yii::$app->request->post(); $preview = ''; $model->subscribe = 1; if ($model->load($postData)) { $model->posts = 0; $model->views = 0; $model->category_id = $category->id; $model->forum_id = $forum->id; $model->author_id = User::loggedId(); if ($model->validate()) { if (isset($postData['preview-button'])) { $preview = $model->post; } else { $transaction = Thread::getDb()->beginTransaction(); try { if ($model->save()) { $forum->updateCounters(['threads' => 1]); $post = new Post(); $post->content = $model->post; $post->thread_id = $model->id; $post->forum_id = $model->forum_id; $post->author_id = User::loggedId(); $post->likes = 0; $post->dislikes = 0; if ($post->save()) { $post->markSeen(); $forum->updateCounters(['posts' => 1]); $model->updateCounters(['posts' => 1]); $model->touch('new_post_at'); $model->touch('edited_post_at'); if ($model->subscribe) { $subscription = new Subscription(); $subscription->user_id = User::loggedId(); $subscription->thread_id = $model->id; $subscription->post_seen = Subscription::POST_SEEN; $subscription->save(); } } } $transaction->commit(); Cache::getInstance()->delete('forum.threadscount'); Cache::getInstance()->delete('forum.postscount'); Cache::getInstance()->deleteElement('user.threadscount', User::loggedId()); Cache::getInstance()->deleteElement('user.postscount', User::loggedId()); Cache::getInstance()->delete('forum.latestposts'); Log::info('Thread added', $model->id, __METHOD__); $this->success(Yii::t('podium/flash', 'New thread has been created.')); return $this->redirect(['thread', 'cid' => $category->id, 'fid' => $forum->id, 'id' => $model->id, 'slug' => $model->slug]); } catch (Exception $e) { $transaction->rollBack(); Log::error($e->getMessage(), null, __METHOD__); $this->error(Yii::t('podium/flash', 'Sorry! There was an error while creating the thread. Contact administrator about this problem.')); } } } } } } return $this->render('new-thread', ['preview' => $preview, 'model' => $model, 'category' => $category, 'forum' => $forum]); } }
/** * Performs thread posts move with counters update. * @param integer $target new parent thread's ID * @param array $posts IDs of posts to move * @param string $name new thread's name if $target = 0 * @param type $forum new thread's parent forum if $target = 0 * @return boolean * @throws Exception * @since 0.2 */ public function podiumMovePostsTo($target = null, $posts = [], $name = null, $forum = null) { $transaction = static::getDb()->beginTransaction(); try { if ($target == 0) { $parent = Forum::find()->where(['id' => $forum])->limit(1)->one(); if (empty($parent)) { throw new Exception('No parent forum of given ID found'); } $newThread = new Thread(); $newThread->name = $name; $newThread->posts = 0; $newThread->views = 0; $newThread->category_id = $parent->category_id; $newThread->forum_id = $parent->id; $newThread->author_id = User::loggedId(); $newThread->save(); } else { $newThread = Thread::find()->where(['id' => $target])->limit(1)->one(); if (empty($newThread)) { throw new Exception('No thread of given ID found'); } } if (!empty($newThread)) { foreach ($posts as $post) { if (!is_numeric($post) || $post < 1) { throw new Exception('Incorrect post ID'); } $newPost = Post::find()->where(['id' => $post, 'thread_id' => $this->id, 'forum_id' => $this->forum->id])->limit(1)->one(); if (empty($newPost)) { throw new Exception('No post of given ID found'); } $newPost->thread_id = $newThread->id; $newPost->forum_id = $newThread->forum_id; $newPost->save(); } $wholeThread = false; if ($this->postCount) { $this->updateCounters(['posts' => -count($posts)]); $this->forum->updateCounters(['posts' => -count($posts)]); } else { $wholeThread = true; $this->delete(); $this->forum->updateCounters(['posts' => -count($posts), 'threads' => -1]); } $newThread->updateCounters(['posts' => count($posts)]); $newThread->forum->updateCounters(['posts' => count($posts)]); $transaction->commit(); Cache::clearAfter('postMove'); Log::info('Posts moved', null, __METHOD__); return true; } } catch (Exception $e) { $transaction->rollBack(); Log::error($e->getMessage(), null, __METHOD__); } return false; }