/** * 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) . ' » ' . 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) . ' » ' . Html::encode($for->name) . ' » ' . 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']); } } }
/** * Subscribing the thread of given ID. * @param integer $id * @return \yii\web\Response */ public function actionAdd($id = null) { if (Yii::$app->request->isAjax) { $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Error while adding this subscription!'), ['class' => 'text-danger'])]; if (!Yii::$app->user->isGuest) { if (is_numeric($id) && $id > 0) { $thread = Thread::findOne((int) $id); if ($thread) { $subscription = Subscription::findOne(['thread_id' => $thread->id, 'user_id' => User::loggedId()]); if ($subscription) { $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'You are already subscribed to this thread.'), ['class' => 'text-info'])]; } else { $sub = new Subscription(); $sub->thread_id = $thread->id; $sub->user_id = User::loggedId(); $sub->post_seen = Subscription::POST_SEEN; if ($sub->save()) { $data = ['error' => 0, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-ok-circle']) . ' ' . Yii::t('podium/view', 'You have subscribed to this thread!'), ['class' => 'text-success'])]; } } } } } else { $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Please sign in to subscribe to this thread'), ['class' => 'text-info'])]; } return Json::encode($data); } else { 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']); } }