/** * Moving the thread of given category ID, forum ID, own ID and slug. * @param integer $cid * @param integer $fid * @param integer $id * @param string $slug * @return string|\yii\web\Response */ public function actionMove($cid = null, $fid = null, $id = null, $slug = null) { $verify = $this->_verifyThread($cid, $fid, $id, $slug); if ($verify === false) { $this->error(Yii::t('podium/flash', 'Sorry! We can not find the thread you are looking for.')); return $this->redirect(['default/index']); } list($category, $forum, $thread) = $verify; if (User::can(Rbac::PERM_MOVE_THREAD, ['item' => $thread])) { $postData = Yii::$app->request->post(); if ($postData) { $moveTo = $postData['forum']; if (is_numeric($moveTo) && $moveTo > 0 && $moveTo != $forum->id) { $newParent = Forum::findOne($moveTo); if ($newParent) { $postsCount = $thread->posts; $oldParent = Forum::findOne($thread->forum_id); if ($oldParent) { $transaction = Forum::getDb()->beginTransaction(); try { $oldParent->updateCounters(['threads' => -1, 'posts' => -$postsCount]); $newParent->updateCounters(['threads' => 1, 'posts' => $postsCount]); $thread->forum_id = $newParent->id; $thread->category_id = $newParent->category_id; if ($thread->save()) { Post::updateAll(['forum_id' => $newParent->id], 'thread_id = :tid', [':tid' => $thread->id]); } $transaction->commit(); Log::info('Thread moved', $thread->id, __METHOD__); $this->success(Yii::t('podium/flash', 'Thread has been moved.')); return $this->redirect(['thread', 'cid' => $thread->category_id, 'fid' => $thread->forum_id, 'id' => $thread->id, 'slug' => $thread->slug]); } catch (Exception $e) { $transaction->rollBack(); Log::error($e->getMessage(), null, __METHOD__); $this->error(Yii::t('podium/flash', 'Sorry! There was an error while moving the thread.')); } } else { $this->error(Yii::t('podium/flash', 'Sorry! There was an error while moving the thread.')); } } else { $this->error(Yii::t('podium/flash', 'Sorry! We can not find the forum you want to move this thread to.')); } } else { $this->error(Yii::t('podium/flash', 'Incorrect forum ID.')); } } $categories = Category::find()->orderBy(['name' => SORT_ASC])->all(); $forums = Forum::find()->orderBy(['name' => SORT_ASC])->all(); $list = []; $options = []; foreach ($categories as $cat) { $catlist = []; foreach ($forums as $for) { if ($for->category_id == $cat->id) { $catlist[$for->id] = (User::can(Rbac::PERM_UPDATE_THREAD, ['item' => $for]) ? '* ' : '') . Html::encode($cat->name) . ' » ' . Html::encode($for->name); if ($for->id == $forum->id) { $options[$for->id] = ['disabled' => true]; } } } $list[Html::encode($cat->name)] = $catlist; } return $this->render('move', ['category' => $category, 'forum' => $forum, 'thread' => $thread, 'list' => $list, 'options' => $options]); } else { if (Yii::$app->user->isGuest) { $this->warning(Yii::t('podium/flash', 'Please sign in to update the 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']); } } }
/** * Performs thread move with counters update. * @param integer $target new parent forum's ID * @return boolean * @since 0.2 */ public function podiumMoveTo($target = null) { $newParent = Forum::find()->where(['id' => $target])->limit(1)->one(); if ($newParent) { $postsCount = $this->postsCount; $transaction = Forum::getDb()->beginTransaction(); try { $this->forum->updateCounters(['threads' => -1, 'posts' => -$postsCount]); $newParent->updateCounters(['threads' => 1, 'posts' => $postsCount]); $this->forum_id = $newParent->id; $this->category_id = $newParent->category_id; if ($this->save()) { Post::updateAll(['forum_id' => $newParent->id], 'thread_id = :tid', [':tid' => $this->id]); } $transaction->commit(); Cache::clearAfter('threadMove'); Log::info('Thread moved', $this->id, __METHOD__); return true; } catch (Exception $e) { $transaction->rollBack(); Log::error($e->getMessage(), null, __METHOD__); } } else { Log::error('No parent forum of given ID found', $this->id, __METHOD__); } return false; }