public function searchDeleted($params) { $dataProvider = $this->search(); $dataProvider->query->where(['or', ['and', ['sender_id' => Yii::$app->user->id], ['sender_status' => Message::getDeletedStatuses()]], ['and', ['receiver_id' => Yii::$app->user->id], ['receiver_status' => Message::getDeletedStatuses()]]]); if (!($this->load($params) && $this->validate())) { $dataProvider->query->joinWith(['receiverUser' => function ($q) { $q->from(User::tableName() . ' pdu_receiver'); }, 'senderUser' => function ($q) { $q->from(User::tableName() . ' pdu_sender'); }]); return $dataProvider; } $dataProvider->query->andFilterWhere(['like', 'topic', $this->topic]); $dataProvider->query->joinWith(['receiverUser' => function ($q) { $q->from(User::tableName() . ' pdu_receiver')->where(['like', 'pdu_receiver.username', $this->receiverName]); }, 'senderUser' => function ($q) { $q->from(User::tableName() . ' pdu_sender')->where(['like', 'pdu_sender.username', $this->senderName]); }]); return $dataProvider; }
/** * Searches for sent messages. * @param array $params * @return ActiveDataProvider */ public function search($params) { // not very proud of this query - slow for sure // let me know if it can be done better. $subquery = (new Query())->select(['m2.replyto'])->from(['m1' => Message::tableName()])->leftJoin(['m2' => Message::tableName()], '`m1`.`replyto` = `m2`.`id`')->where(['is not', 'm2.replyto', null]); $query = self::find()->where(['and', ['sender_id' => User::loggedId(), 'sender_status' => Message::getSentStatuses()], ['not in', Message::tableName() . '.id', $subquery]]); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['attributes' => ['id', 'topic', 'created_at']]]); $dataProvider->sort->defaultOrder = ['id' => SORT_DESC]; $dataProvider->pagination->pageSize = Yii::$app->session->get('per-page', 20); if (!($this->load($params) && $this->validate())) { $dataProvider->query->joinWith(['messageReceivers' => function ($q) { $q->joinWith(['receiver']); }]); return $dataProvider; } $dataProvider->query->andFilterWhere(['like', 'topic', $this->topic]); if (preg_match('/^(forum|orum|rum|um|m)?#([0-9]+)$/', strtolower($this->receiverName), $matches)) { $dataProvider->query->joinWith(['messageReceivers' => function ($q) use($matches) { $q->joinWith(['receiver' => function ($q) use($matches) { $q->andFilterWhere(['username' => ['', null], User::tableName() . '.id' => $matches[2]]); }]); }]); } elseif (preg_match('/^([0-9]+)$/', $this->receiverName, $matches)) { $dataProvider->query->joinWith(['messageReceivers' => function ($q) use($matches) { $q->joinWith(['receiver' => function ($q) use($matches) { $q->andFilterWhere(['or', ['like', 'username', $this->receiverName], ['username' => ['', null], 'id' => $matches[1]]]); }]); }]); } else { $dataProvider->query->joinWith(['messageReceivers' => function ($q) { $q->joinWith(['receiver' => function ($q) { $q->andFilterWhere(['like', User::tableName() . '.username', $this->receiverName]); }]); }]); } return $dataProvider; }
/** * Returns number of new user messages. * @return integer */ public function getNewMessagesCount() { $cache = Cache::getInstance()->getElement('user.newmessages', $this->id); if ($cache === false) { $cache = (new Query())->from(Message::tableName())->where(['receiver_id' => $this->id, 'receiver_status' => Message::STATUS_NEW])->count(); Cache::getInstance()->setElement('user.newmessages', $this->id, $cache); } return $cache; }
/** * 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']); } }
/** * Performs post report sending to moderators. * @param Post $post reported post * @return boolean * @since 0.2 */ public function podiumReport($post = null) { try { if (empty($post)) { throw new Exception('Reported post missing'); } $package = []; $mods = $post->forum->mods; foreach ($mods as $mod) { if ($mod != User::loggedId()) { $package[] = [User::loggedId(), $mod, Yii::t('podium/view', 'Complaint about the post #{id}', ['id' => $post->id]), $this->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><div class="blockquote">' . $post->content . '</div>', Message::STATUS_REMOVED, Message::STATUS_NEW, time(), time()]; } } if (empty($package)) { throw new Exception('No one to send report to'); } 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__); return true; } catch (Exception $e) { Log::error($e->getMessage(), null, __METHOD__); } return false; }
/** * Reporting the post of given category ID, forum ID, thread ID, own ID and slug. * @param integer $cid category's ID * @param integer $fid forum's ID * @param integer $tid thread's ID * @param integer $pid post's ID * @return string|\yii\web\Response */ public function actionReport($cid = null, $fid = null, $tid = null, $pid = null) { if (Yii::$app->user->isGuest) { $this->warning(Yii::t('podium/flash', 'Please sign in to report the post.')); return $this->redirect(['account/login']); } $post = Post::verify($cid, $fid, $tid, $pid); if (empty($post)) { $this->error(Yii::t('podium/flash', 'Sorry! We can not find the post you are looking for.')); return $this->redirect(['default/index']); } 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' => $post->forum->category->id, 'fid' => $post->forum->id, 'id' => $post->thread->id, 'slug' => $post->thread->slug]); } $model = new Message(); $model->scenario = 'report'; if ($model->load(Yii::$app->request->post()) && $model->validate()) { if ($model->podiumReport($post)) { $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' => $post->forum->category->id, 'fid' => $post->forum->id, 'id' => $post->thread->id, 'slug' => $post->thread->slug]); } else { $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, 'post' => $post]); }
/** * Viewing the message of given ID. * @param integer $id * @return string|\yii\web\Response */ public function actionView($id = null) { $model = Message::find()->where(['and', ['id' => $id], ['or', 'receiver_id' => User::loggedId(), 'sender_id' => User::loggedId()]])->limit(1)->one(); if ($model) { if ($model->receiver_id == User::loggedId() && $model->receiver_status == Message::STATUS_NEW) { $model->receiver_status = Message::STATUS_READ; if ($model->save()) { Cache::getInstance()->deleteElement('user.newmessages', User::loggedId()); } } return $this->render('view', ['model' => $model]); } else { $this->error(Yii::t('podium/flash', 'Sorry! We can not find the message with the given ID.')); return $this->redirect(['messages/inbox']); } }
/** * Viewing the message of given ID. * @param integer $id * @return string|\yii\web\Response */ public function actionView($id = null) { $model = Message::findOne(['and', ['id' => $id], ['or', 'receiver_id' => Yii::$app->user->id, 'sender_id' => Yii::$app->user->id]]); if ($model) { if ($model->receiver_id == Yii::$app->user->id && $model->receiver_status == Message::STATUS_NEW) { $model->receiver_status = Message::STATUS_READ; if ($model->save()) { Cache::getInstance()->deleteElement('user.newmessages', Yii::$app->user->id); } } return $this->render('view', ['model' => $model]); } else { $this->error('Sorry! We can not find the message with the given ID.'); return $this->redirect(['inbox']); } }
/** * Loads older messages in thread. * @return string */ public function actionLoad() { if (!Yii::$app->request->isAjax) { return $this->redirect(['default/index']); } $result = ['messages' => '', 'more' => 0]; if (!Yii::$app->user->isGuest) { $loggedId = User::loggedId(); $id = Yii::$app->request->post('message'); $message = Message::find()->where(['id' => $id])->limit(1)->one(); if ($message && ($message->sender_id == $loggedId || $message->isMessageReceiver($loggedId))) { $stack = 0; $reply = clone $message; while ($reply->reply && $stack < 5) { $result['more'] = 0; if ($reply->reply->sender_id == $loggedId && $reply->reply->sender_status == Message::STATUS_DELETED) { $reply = $reply->reply; continue; } $result['messages'] .= $this->renderPartial('load', ['reply' => $reply]); $reply = $reply->reply; if ($reply) { $result['more'] = $reply->id; } $stack++; } } } return Json::encode($result); }