public function mentionHandler($post) { $usernames = MentionHelper::find($post->message); if (!empty($usernames)) { foreach ($usernames as $username) { /** @var UserModels $mentioned */ $mentioned = UserModels::findByUsername($username); if (!$mentioned instanceof UserModels) { continue; } $exist = UserMention::find()->where(['post_id' => $post->id, 'mention_user_id' => $mentioned->id, 'status' => UserMention::MENTION_SATUS_UNVIEWED])->exists(); if ($exist) { continue; } $currentUser = Yii::$app->getUser()->getIdentity(); $model = new UserMention(); $model->user_id = $currentUser->id; $model->mention_user_id = $mentioned->id; $model->post_id = $post->id; $model->topic_id = $post->topic->id; $model->status = UserMention::MENTION_SATUS_UNVIEWED; if ($mentioned->notify_mention_web == 1) { $model->save(); } if ($mentioned->notify_mention_email == 1) { \Yii::$app->mailer->compose(['text' => 'mention'], ['model' => $model, 'topic' => $post->topic])->setFrom([Yii::$app->config->get('support_email') => Yii::$app->config->get('site_title')])->setTo([$model->mentionUser->email => $model->mentionUser->username])->setSubject('#' . $post->id . ' ' . $post->topic->subject)->send(); } } return true; } return false; }
public function actionView($id) { /* @var TopicModels $topic */ $topic = TopicModels::find()->where(['id' => $id])->with('forum')->one(); if (!$topic) { throw new NotFoundHttpException(); } $topic->updateCounters(['number_views' => 1]); $topic->save(); $dataProvider = PostModels::getDataProviderByTopic($topic->id); $posts = $dataProvider->getModels(); if (!Yii::$app->getUser()->getIsGuest()) { $userMentions = UserMention::findAll(['topic_id' => $id, 'mention_user_id' => Yii::$app->getUser()->getId(), 'status' => UserMention::MENTION_STATUS_UNVIEWED]); /* user mention update */ foreach ($userMentions as $userMention) { $userMention->status = UserMention::MENTION_STATUS_VIEWED; $userMention->save(); } $model = new PostForm(); if ($model->load(Yii::$app->getRequest()->post()) && $model->create($topic)) { $page = $model->post->getPostPage($model->post); if ($page > 1) { $this->redirect(['view', 'id' => $model->getPost()->topic->id, 'page' => $page, '#' => 'p' . $model->getPost()->id]); } else { $this->redirect(['view', 'id' => $model->getPost()->topic->id, '#' => 'p' . $model->getPost()->id]); } } return $this->render('view', ['dataProvider' => $dataProvider, 'model' => $model, 'topic' => $topic, 'posts' => $posts]); } else { return $this->render('view', ['dataProvider' => $dataProvider, 'topic' => $topic, 'posts' => $posts]); } }
/** * @inheritdoc */ public static function notifications($user) { $userMentions = UserMention::find()->with(['post' => function ($query) { $query->andWhere(['status' => UserMention::MENTION_STATUS_UNVIEWED]); }, 'topic'])->where(['mention_user_id' => $user->id])->andWhere(['status' => UserMention::MENTION_STATUS_UNVIEWED])->all(); return $userMentions; }