/**
  * Updating the forums order.
  * @return string|\yii\web\Response
  */
 public function actionSortForum()
 {
     if (Yii::$app->request->isAjax) {
         if (User::can(Rbac::PERM_UPDATE_FORUM)) {
             $modelId = Yii::$app->request->post('id');
             $modelCategory = Yii::$app->request->post('category');
             $new = Yii::$app->request->post('new');
             if (is_numeric($modelId) && is_numeric($modelCategory) && is_numeric($new) && $modelId > 0 && $modelCategory > 0 && $new >= 0) {
                 $moved = Forum::findOne((int) $modelId);
                 $movedCategory = Category::findOne((int) $modelCategory);
                 if ($moved && $modelCategory && $moved->category_id == $movedCategory->id) {
                     $query = (new Query())->from(Forum::tableName())->where('id != :id AND category_id = :cid')->params([':id' => $moved->id, ':cid' => $movedCategory->id])->orderBy(['sort' => SORT_ASC, 'id' => SORT_ASC])->indexBy('id');
                     $next = 0;
                     $newSort = -1;
                     try {
                         foreach ($query->each() as $id => $forum) {
                             if ($next == (int) $new) {
                                 $newSort = $next;
                                 $next++;
                             }
                             Yii::$app->db->createCommand()->update(Forum::tableName(), ['sort' => $next], 'id = :id', [':id' => $id])->execute();
                             $next++;
                         }
                         if ($newSort == -1) {
                             $newSort = $next;
                         }
                         $moved->sort = $newSort;
                         if (!$moved->save()) {
                             return Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', "Sorry! We can not save new forums' order."), ['class' => 'text-danger']);
                         } else {
                             Log::info('Forums orded updated', $moved->id, __METHOD__);
                             return Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-ok-circle']) . ' ' . Yii::t('podium/view', "New forums' order has been saved."), ['class' => 'text-success']);
                         }
                     } catch (Exception $e) {
                         Log::error($e->getMessage(), null, __METHOD__);
                         return Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', "Sorry! We can not save new forums' order."), ['class' => 'text-danger']);
                     }
                 } else {
                     return Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Sorry! We can not find Forum with this ID.'), ['class' => 'text-danger']);
                 }
             } else {
                 return Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Sorry! Sorting parameters are wrong.'), ['class' => 'text-danger']);
             }
         } else {
             return Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'You are not allowed to perform this action.'), ['class' => 'text-danger']);
         }
     } else {
         return $this->redirect(['admin/forums']);
     }
 }
 /**
  * Moving the posts of given category ID, forum ID, thread ID and slug.
  * @param integer $cid
  * @param integer $fid
  * @param integer $id
  * @param string $slug
  * @return string|\yii\web\Response
  */
 public function actionMoveposts($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_POST, ['item' => $thread])) {
         if (Yii::$app->request->post()) {
             $posts = Yii::$app->request->post('post');
             $newthread = Yii::$app->request->post('newthread');
             $newname = Yii::$app->request->post('newname');
             $newforum = Yii::$app->request->post('newforum');
             if (empty($posts) || !is_array($posts)) {
                 $this->error(Yii::t('podium/flash', 'You have to select at least one post.'));
             } else {
                 if (!is_numeric($newthread) || $newthread < 0) {
                     $this->error(Yii::t('podium/flash', 'You have to select a thread for this posts to be moved to.'));
                 } else {
                     if ($newthread == 0 && (empty($newname) || empty($newforum) || !is_numeric($newforum) || $newforum < 1)) {
                         $this->error(Yii::t('podium/flash', 'If you want to move posts to a new thread you have to enter its name and select parent forum.'));
                     } else {
                         if ($newthread == $thread->id) {
                             $this->error(Yii::t('podium/flash', 'Are you trying to move posts from this thread to this very same thread?'));
                         } else {
                             $transaction = Thread::getDb()->beginTransaction();
                             try {
                                 if ($newthread == 0) {
                                     $parent = Forum::findOne($newforum);
                                     if (!$parent) {
                                         $this->error(Yii::t('podium/flash', 'We can not find the parent forum with this ID.'));
                                     } else {
                                         $nThread = new Thread();
                                         $nThread->name = $newname;
                                         $nThread->posts = 0;
                                         $nThread->views = 0;
                                         $nThread->category_id = $parent->category_id;
                                         $nThread->forum_id = $parent->id;
                                         $nThread->author_id = User::loggedId();
                                         $nThread->save();
                                     }
                                 } else {
                                     $nThread = Thread::findOne($newthread);
                                     if (!$nThread) {
                                         $this->error(Yii::t('podium/flash', 'We can not find the thread with this ID.'));
                                     }
                                 }
                                 if (!empty($nThread)) {
                                     $error = false;
                                     foreach ($posts as $post) {
                                         if (!is_numeric($post) || $post < 1) {
                                             $this->error(Yii::t('podium/flash', 'Incorrect post ID.'));
                                             $error = true;
                                             break;
                                         } else {
                                             $nPost = Post::find()->where(['id' => $post, 'thread_id' => $thread->id, 'forum_id' => $forum->id])->limit(1)->one();
                                             if (!$nPost) {
                                                 $this->error(Yii::t('podium/flash', 'We can not find the post with this ID.'));
                                                 $error = true;
                                                 break;
                                             } else {
                                                 $nPost->thread_id = $nThread->id;
                                                 $nPost->forum_id = $nThread->forum_id;
                                                 $nPost->save();
                                             }
                                         }
                                     }
                                     if (!$error) {
                                         $wholeThread = false;
                                         if ((new Query())->from(Post::tableName())->where(['thread_id' => $thread->id, 'forum_id' => $forum->id])->count()) {
                                             $thread->updateCounters(['posts' => -count($posts)]);
                                             $forum->updateCounters(['posts' => -count($posts)]);
                                         } else {
                                             $wholeThread = true;
                                             $thread->delete();
                                             $forum->updateCounters(['posts' => -count($posts), 'threads' => -1]);
                                         }
                                         $nThread->updateCounters(['posts' => count($posts)]);
                                         $nThread->forum->updateCounters(['posts' => count($posts)]);
                                         $transaction->commit();
                                         Cache::getInstance()->delete('forum.threadscount');
                                         Cache::getInstance()->delete('forum.postscount');
                                         Cache::getInstance()->delete('user.postscount');
                                         Cache::getInstance()->delete('forum.latestposts');
                                         Log::info('Posts moved', null, __METHOD__);
                                         $this->success(Yii::t('podium/flash', 'Posts have been moved.'));
                                         if ($wholeThread) {
                                             return $this->redirect(['default/forum', 'cid' => $forum->category_id, 'id' => $forum->id, 'slug' => $forum->slug]);
                                         } else {
                                             return $this->redirect(['default/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 posts.'));
                             }
                         }
                     }
                 }
             }
         }
         $categories = Category::find()->orderBy(['name' => SORT_ASC])->all();
         $forums = Forum::find()->orderBy(['name' => SORT_ASC])->all();
         $threads = Thread::find()->orderBy(['name' => SORT_ASC])->all();
         $list = [0 => Yii::t('podium/view', 'Create new thread')];
         $listforum = [];
         $options = [];
         foreach ($categories as $cat) {
             $catlist = [];
             foreach ($forums as $for) {
                 $forlist = [];
                 if ($for->category_id == $cat->id) {
                     $catlist[$for->id] = (User::can(Rbac::PERM_UPDATE_THREAD, ['item' => $for]) ? '* ' : '') . Html::encode($cat->name) . ' &raquo; ' . Html::encode($for->name);
                     foreach ($threads as $thr) {
                         if ($thr->category_id == $cat->id && $thr->forum_id == $for->id) {
                             $forlist[$thr->id] = (User::can(Rbac::PERM_UPDATE_THREAD, ['item' => $thr]) ? '* ' : '') . Html::encode($cat->name) . ' &raquo; ' . Html::encode($for->name) . ' &raquo; ' . Html::encode($thr->name);
                             if ($thr->id == $thread->id) {
                                 $options[$thr->id] = ['disabled' => true];
                             }
                         }
                     }
                     $list[Html::encode($cat->name) . ' > ' . Html::encode($for->name)] = $forlist;
                 }
             }
             $listforum[Html::encode($cat->name)] = $catlist;
         }
         return $this->render('moveposts', ['category' => $category, 'forum' => $forum, 'thread' => $thread, 'list' => $list, 'options' => $options, 'listforum' => $listforum, 'dataProvider' => (new Post())->search($forum->id, $thread->id)]);
     } 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']);
         }
     }
 }
 /**
  * Reporting the post of given category ID, forum ID, thread ID, own ID and slug.
  * @param integer $cid
  * @param integer $fid
  * @param integer $tid
  * @param integer $pid
  * @param string $slug
  * @return string|\yii\web\Response
  */
 public function actionReport($cid = null, $fid = null, $tid = null, $pid = null, $slug = null)
 {
     if (!Yii::$app->user->isGuest) {
         if (!is_numeric($cid) || $cid < 1 || !is_numeric($fid) || $fid < 1 || !is_numeric($tid) || $tid < 1 || !is_numeric($pid) || $pid < 1 || empty($slug)) {
             $this->error('Sorry! We can not find the post you are looking for.');
             return $this->redirect(['default/index']);
         }
         $category = Category::findOne(['id' => (int) $cid]);
         if (!$category) {
             $this->error('Sorry! We can not find the post you are looking for.');
             return $this->redirect(['default/index']);
         } else {
             $forum = Forum::findOne(['id' => (int) $fid, 'category_id' => $category->id]);
             if (!$forum) {
                 $this->error('Sorry! We can not find the post you are looking for.');
                 return $this->redirect(['default/index']);
             } else {
                 $thread = Thread::findOne(['id' => (int) $tid, 'category_id' => $category->id, 'forum_id' => $forum->id, 'slug' => $slug]);
                 if (!$thread) {
                     $this->error('Sorry! We can not find the post you are looking for.');
                     return $this->redirect(['default/index']);
                 } else {
                     $post = Post::findOne(['id' => (int) $pid, 'forum_id' => $forum->id, 'thread_id' => $thread->id]);
                     if (!$post) {
                         $this->error('Sorry! We can not find the post you are looking for.');
                         return $this->redirect(['default/index']);
                     } else {
                         if ($post->author_id == Yii::$app->user->id) {
                             $this->info('You can not report your own post. Please contact the administrator or moderators if you have got any concerns regarding your post.');
                             return $this->redirect(['default/thread', 'cid' => $category->id, 'fid' => $forum->id, 'id' => $thread->id, 'slug' => $thread->slug]);
                         } else {
                             $model = new Message();
                             $model->setScenario('report');
                             if ($model->load(Yii::$app->request->post())) {
                                 if ($model->validate()) {
                                     try {
                                         $mods = $forum->getMods();
                                         $package = [];
                                         foreach ($mods as $mod) {
                                             if ($mod != Yii::$app->user->id) {
                                                 $package[] = ['sender_id' => Yii::$app->user->id, 'receiver_id' => $mod, 'topic' => Yii::t('podium/view', 'Complaint about the post #{id}', ['id' => $post->id]), 'content' => $model->content . '<hr>' . Html::a(Yii::t('podium/view', 'Direct link to the post'), ['show', 'id' => $post->id]) . '<hr>' . '<strong>' . Yii::t('podium/view', 'Post contents') . '</strong><br><blockquote>' . $post->content . '</blockquote>', 'sender_status' => Message::STATUS_REMOVED, 'receiver_status' => Message::STATUS_NEW, 'created_at' => time(), 'updated_at' => time()];
                                             }
                                         }
                                         if (!empty($package)) {
                                             Yii::$app->db->createCommand()->batchInsert(Message::tableName(), ['sender_id', 'receiver_id', 'topic', 'content', 'sender_status', 'receiver_status', 'created_at', 'updated_at'], array_values($package))->execute();
                                             Cache::getInstance()->delete('user.newmessages');
                                             Log::info('Post reported', !empty($post->id) ? $post->id : '', __METHOD__);
                                             $this->success('Thank you for your report. The moderation team will take a look at this post.');
                                             return $this->redirect(['thread', 'cid' => $category->id, 'fid' => $forum->id, 'id' => $thread->id, 'slug' => $thread->slug]);
                                         } else {
                                             $this->warning('Apparently there is no one we can send this report to except you and you already reporting it so...');
                                         }
                                     } catch (Exception $e) {
                                         Log::error($e->getMessage(), null, __METHOD__);
                                         $this->error('Sorry! There was an error while notifying the moderation team. Contact administrator about this problem.');
                                     }
                                 }
                             }
                             return $this->render('report', ['model' => $model, 'category' => $category, 'forum' => $forum, 'thread' => $thread, 'post' => $post]);
                         }
                     }
                 }
             }
         }
     } else {
         $this->warning('Please sign in to report the post.');
         return $this->redirect(['account/login']);
     }
 }