Exemplo n.º 1
0
 /**
  * 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;
 }
Exemplo n.º 2
0
 /**
  * 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']);
     }
 }
Exemplo n.º 4
0
 /**
  * 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;
 }
Exemplo n.º 5
0
 /**
  * Replying to the message of given ID.
  * @param integer $id
  * @return string|\yii\web\Response
  */
 public function actionReply($id = null)
 {
     $podiumUser = User::findMe();
     if (Message::tooMany($podiumUser->id)) {
         $this->warning(Yii::t('podium/flash', 'You have reached maximum {max_messages, plural, =1{ message} other{ messages}} per {max_minutes, plural, =1{ minute} other{ minutes}} limit. Wait few minutes before sending a new message.', ['max_messages' => Message::SPAM_MESSAGES, 'max_minutes' => Message::SPAM_WAIT]));
         return $this->redirect(['messages/inbox']);
     }
     $reply = Message::find()->where([Message::tableName() . '.id' => $id])->joinWith(['messageReceivers' => function ($q) use($podiumUser) {
         $q->where(['receiver_id' => $podiumUser->id]);
     }])->limit(1)->one();
     if (empty($reply)) {
         $this->error(Yii::t('podium/flash', 'Sorry! We can not find the message with the given ID.'));
         return $this->redirect(['messages/inbox']);
     }
     $model = new Message();
     $model->topic = Message::re() . ' ' . $reply->topic;
     if ($model->load(Yii::$app->request->post())) {
         if ($model->validate()) {
             if (!$podiumUser->isIgnoredBy($model->receiversId[0])) {
                 $model->replyto = $reply->id;
                 if ($model->send()) {
                     $this->success(Yii::t('podium/flash', 'Message has been sent.'));
                     return $this->redirect(['messages/inbox']);
                 } else {
                     $this->error(Yii::t('podium/flash', 'Sorry! There was some error while sending your message.'));
                 }
             } else {
                 $this->error(Yii::t('podium/flash', 'Sorry! This member ignores you so you can not send the message.'));
             }
         }
     }
     $model->receiversId = [$reply->sender_id];
     return $this->render('reply', ['model' => $model, 'reply' => $reply]);
 }