/** * 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(Yii::t('podium/flash', 'Sorry! We can not find the post 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 post 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 post you are looking for.')); return $this->redirect(['default/index']); } else { $thread = Thread::find()->where(['id' => (int) $tid, 'category_id' => $category->id, 'forum_id' => $forum->id, 'slug' => $slug])->limit(1)->one(); if (!$thread) { $this->error(Yii::t('podium/flash', 'Sorry! We can not find the post you are looking for.')); return $this->redirect(['default/index']); } else { $post = Post::find()->where(['id' => (int) $pid, 'forum_id' => $forum->id, 'thread_id' => $thread->id])->limit(1)->one(); if (!$post) { $this->error(Yii::t('podium/flash', 'Sorry! We can not find the post you are looking for.')); return $this->redirect(['default/index']); } else { if ($post->author_id == User::loggedId()) { $this->info(Yii::t('podium/flash', '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 != User::loggedId()) { $package[] = ['sender_id' => User::loggedId(), '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 this post'), ['default/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', $post->id, __METHOD__); $this->success(Yii::t('podium/flash', 'Thank you for your report. The moderation team will take a look at this post.')); return $this->redirect(['default/thread', 'cid' => $category->id, 'fid' => $forum->id, 'id' => $thread->id, 'slug' => $thread->slug]); } else { $this->warning(Yii::t('podium/flash', 'Apparently there is no one we can send this report to except you and you are already reporting it so...')); } } catch (Exception $e) { Log::error($e->getMessage(), null, __METHOD__); $this->error(Yii::t('podium/flash', '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(Yii::t('podium/flash', 'Please sign in to report the post.')); return $this->redirect(['account/login']); } }
/** * Dashboard. * @return string */ public function actionIndex() { return $this->render('index', ['members' => User::find()->orderBy(['id' => SORT_DESC])->limit(10)->all(), 'posts' => Post::find()->orderBy(['id' => SORT_DESC])->limit(10)->all()]); }
/** * Counts number of created posts. * @return integer */ public static function totalPosts() { $posts = Cache::getInstance()->get('forum.postscount'); if ($posts === false) { $posts = Post::find()->count(); Cache::getInstance()->set('forum.postscount', $posts); } return $posts; }
/** * Creating the post of given category ID, forum ID, thread ID and slug. * This can be reply to selected post. * @param integer $cid * @param integer $fid * @param integer $tid * @param integer $pid * @return string|\yii\web\Response */ public function actionPost($cid = null, $fid = null, $tid = null, $pid = null) { if (!Yii::$app->user->can('createPodiumPost')) { if (Yii::$app->user->isGuest) { $this->warning('Please sign in to post a reply.'); return $this->redirect(['account/login']); } else { $this->error('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 || !is_numeric($tid) || $tid < 1) { $this->error('Sorry! We can not find the thread 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 thread 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 thread you are looking for.'); return $this->redirect(['default/index']); } else { $thread = Thread::findOne(['id' => (int) $tid, 'category_id' => $category->id, 'forum_id' => $forum->id]); if (!$thread) { $this->error('Sorry! We can not find the thread you are looking for.'); return $this->redirect(['default/index']); } else { if ($thread->locked == 0 || $thread->locked == 1 && Yii::$app->user->can('updatePodiumThread', ['item' => $thread])) { $model = new Post(); $model->subscribe = 1; $postData = Yii::$app->request->post(); $replyFor = null; if (is_numeric($pid) && $pid > 0) { $replyFor = Post::findOne((int) $pid); if ($replyFor) { if (isset($postData['quote']) && !empty($postData['quote'])) { $model->content = Helper::prepareQuote($replyFor, $postData['quote']); } else { $model->content = Helper::prepareQuote($replyFor); } } } $preview = ''; $previous = Post::find()->where(['thread_id' => $thread->id])->orderBy(['id' => SORT_DESC])->one(); if ($model->load($postData)) { $model->thread_id = $thread->id; $model->forum_id = $forum->id; $model->author_id = Yii::$app->user->id; if ($model->validate()) { if (isset($postData['preview-button'])) { $preview = $model->content; } else { $transaction = Post::getDb()->beginTransaction(); try { $id = null; if ($previous->author_id == Yii::$app->user->id) { $previous->content .= '<hr>' . $model->content; $previous->edited = 1; $previous->edited_at = time(); if ($previous->save()) { $previous->markSeen(); $thread->touch('edited_post_at'); $id = $previous->id; } } else { if ($model->save()) { $model->markSeen(); $forum->updateCounters(['posts' => 1]); $thread->updateCounters(['posts' => 1]); $thread->touch('new_post_at'); $thread->touch('edited_post_at'); $id = $model->id; } } if ($id !== null) { Subscription::notify($thread->id); if ($model->subscribe && !$model->thread->subscription) { $subscription = new Subscription(); $subscription->user_id = Yii::$app->user->id; $subscription->thread_id = $model->thread->id; $subscription->post_seen = Subscription::POST_SEEN; $subscription->save(); } $transaction->commit(); Cache::getInstance()->delete('forum.postscount'); Cache::getInstance()->deleteElement('user.postscount', Yii::$app->user->id); Cache::getInstance()->delete('forum.latestposts'); Log::info('Post added', !empty($model->id) ? $model->id : '', __METHOD__); $this->success('New reply has been added.'); return $this->redirect(['default/show', 'id' => $id]); } else { throw new Exception('Saved Post ID missing.'); } } catch (Exception $e) { $transaction->rollBack(); Log::error($e->getMessage(), null, __METHOD__); $this->error('Sorry! There was an error while adding the reply. Contact administrator about this problem.'); } } } } return $this->render('post', ['replyFor' => $replyFor, 'preview' => $preview, 'model' => $model, 'category' => $category, 'forum' => $forum, 'thread' => $thread, 'previous' => $previous]); } else { $this->info('This thread is locked.'); return $this->redirect(['default/thread', 'cid' => $category->id, 'fid' => $forum->id, 'thread' => $thread->id, 'slug' => $thread->slug]); } } } } } }
/** * Voting on the post. * @return string|\yii\web\Response */ public function actionThumb() { if (!Yii::$app->request->isAjax) { return $this->redirect(['default/index']); } $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Error while voting on this post!'), ['class' => 'text-danger'])]; if (!Yii::$app->user->isGuest) { $data['msg'] = Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Please sign in to vote on this post'), ['class' => 'text-info']); return Json::encode($data); } $postId = Yii::$app->request->post('post'); $thumb = Yii::$app->request->post('thumb'); if (is_numeric($postId) && $postId > 0 && in_array($thumb, ['up', 'down'])) { $post = Post::find()->where(['id' => $postId])->limit(1)->one(); if ($post) { if ($post->thread->locked) { $data['msg'] = Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'This thread is locked.'), ['class' => 'text-info']); return Json::encode($data); } if ($post->author_id == User::loggedId()) { $data['msg'] = Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'You can not vote on your own post!'), ['class' => 'text-info']); return Json::encode($data); } $count = 0; $votes = Cache::getInstance()->get('user.votes.' . User::loggedId()); if ($votes !== false) { if ($votes['expire'] < time()) { $votes = false; } elseif ($votes['count'] >= 10) { $data['msg'] = Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', '{max} votes per hour limit reached!', ['max' => 10]), ['class' => 'text-danger']); return Json::encode($data); } else { $count = $votes['count']; } } if ($post->podiumThumb($thumb == 'up', $count)) { $data = ['error' => 0, 'likes' => '+' . $post->likes, 'dislikes' => '-' . $post->dislikes, 'summ' => $post->likes - $post->dislikes, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-ok-circle']) . ' ' . Yii::t('podium/view', 'Your vote has been saved!'), ['class' => 'text-success'])]; } } } return Json::encode($data); }
/** * 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; }