/**
  * Returns a List of all notifications for the session user
  */
 public function actionIndex()
 {
     $pageSize = 10;
     $notifications = [];
     $filterForm = new FilterForm();
     $filterForm->initFilter();
     $filterForm->load(Yii::$app->request->get());
     $query = Notification::findGrouped();
     $query->andWhere(['user_id' => Yii::$app->user->id]);
     if ($filterForm->isExcludeFilter()) {
         $query->andFilterWhere(['not in', 'class', $filterForm->getExcludeClassFilter()]);
     } else {
         if ($filterForm->isActive()) {
             $query->andFilterWhere(['in', 'class', $filterForm->getIncludeClassFilter()]);
         } else {
             return $this->render('index', ['notificationEntries' => [], 'filterForm' => $filterForm, 'pagination' => null, 'notifications' => $notifications]);
         }
     }
     $countQuery = clone $query;
     $pagination = new \yii\data\Pagination(['totalCount' => $countQuery->count(), 'pageSize' => $pageSize]);
     //Reset pagegination after new filter set
     if (Yii::$app->request->post()) {
         $pagination->setPage(0);
     }
     $query->offset($pagination->offset)->limit($pagination->limit);
     foreach ($query->all() as $notificationRecord) {
         $notifications[] = $notificationRecord->getClass();
     }
     return $this->render('index', array('notifications' => $notifications, 'filterForm' => $filterForm, 'pagination' => $pagination));
 }
Example #2
0
 public function getMailUpdate(User $user, $interval)
 {
     $output = "";
     foreach (Session::find()->where(['<', 'expire', time()])->all() as $session) {
         $session->delete();
     }
     $receive_email_notifications = $user->getSetting("receive_email_notifications", 'core', Setting::Get('receive_email_notifications', 'mailing'));
     // Never receive notifications
     if ($receive_email_notifications == User::RECEIVE_EMAIL_NEVER) {
         return "";
     }
     // We are in hourly mode and user wants daily
     if ($interval == CronController::EVENT_ON_HOURLY_RUN && $receive_email_notifications == User::RECEIVE_EMAIL_DAILY_SUMMARY) {
         return "";
     }
     // We are in daily mode and user dont wants daily reports
     if ($interval == CronController::EVENT_ON_DAILY_RUN && $receive_email_notifications != User::RECEIVE_EMAIL_DAILY_SUMMARY) {
         return "";
     }
     // User wants only when offline and is online
     if ($interval == CronController::EVENT_ON_HOURLY_RUN) {
         $isOnline = count($user->httpSessions) > 0;
         if ($receive_email_notifications == User::RECEIVE_EMAIL_WHEN_OFFLINE && $isOnline) {
             return "";
         }
     }
     $query = Notification::find()->where(['user_id' => $user->id])->andWhere(['!=', 'seen', 1])->andWhere(['!=', 'emailed', 1]);
     foreach ($query->all() as $notification) {
         $output .= $notification->getClass()->render(BaseNotification::OUTPUT_MAIL);
         $notification->emailed = 1;
         $notification->save();
     }
     return $output;
 }
Example #3
0
 /**
  * @inheritdoc
  */
 public function send(User $user)
 {
     // Check if there is also a mention notification, so skip this notification
     if (\humhub\modules\notification\models\Notification::find()->where(['class' => \humhub\modules\user\notifications\Mentioned::className(), 'user_id' => $user->id, 'source_class' => $this->source->className(), 'source_pk' => $this->source->getPrimaryKey()])->count() > 0) {
         return;
     }
     return parent::send($user);
 }
 public function up()
 {
     $this->renameClass('Notification', Notification::className());
     $this->update('notification', ['class' => 'humhub\\modules\\comment\\notifications\\NewComment'], ['class' => 'NewCommentNotification']);
     $this->update('notification', ['class' => 'humhub\\modules\\like\\notifications\\NewLike'], ['class' => 'NewLikeNotification']);
     $this->update('notification', ['class' => 'humhub\\modules\\user\\notifications\\Followed'], ['class' => 'FollowNotification']);
     $this->update('notification', ['class' => 'humhub\\modules\\user\\notifications\\Mentioned'], ['class' => 'MentionedNotification']);
     $this->update('notification', ['class' => 'humhub\\modules\\content\\notifications\\ContentCreated'], ['class' => 'ContentCreatedNotification']);
     // Fixes
     $this->update('notification', ['source_class' => new Expression("NULL"), 'source_pk' => new Expression("NULL")], ['class' => "humhub\\modules\\user\notifications\\Followed"]);
     Yii::$app->db->createCommand("UPDATE notification SET originator_user_id = created_by")->execute();
 }
Example #5
0
 /**
  * Redirects to the target URL of the given notification
  */
 public function actionIndex()
 {
     $notificationModel = Notification::findOne(['id' => Yii::$app->request->get('id'), 'user_id' => Yii::$app->user->id]);
     if ($notificationModel === null) {
         throw new \yii\web\HttpException(404, 'Could not find requested notification!');
     }
     $notification = $notificationModel->getClass();
     if ($notification->markAsSeenOnClick) {
         $notification->markAsSeen();
     }
     // Redirect to notification URL
     return $this->redirect($notification->getUrl());
 }
Example #6
0
 /**
  * Returns a JSON which contains
  * - Number of new / unread notification
  * - Notification Output for new HTML5 Notifications
  *
  * @return string JSON String
  */
 public static function getUpdates()
 {
     $user = Yii::$app->user->getIdentity();
     $query = Notification::find()->where(['seen' => 0])->orWhere(['IS', 'seen', new \yii\db\Expression('NULL')])->andWhere(['user_id' => $user->id]);
     $update['newNotifications'] = $query->count();
     $query->andWhere(['desktop_notified' => 0]);
     $update['notifications'] = array();
     foreach ($query->all() as $notification) {
         if ($user->getSetting("enable_html5_desktop_notifications", 'core', Setting::Get('enable_html5_desktop_notifications', 'notification'))) {
             $update['notifications'][] = $notification->getClass()->render(BaseNotification::OUTPUT_TEXT);
         }
         $notification->desktop_notified = 1;
         $notification->update();
     }
     return $update;
 }
Example #7
0
 /**
  * Redirects to the target URL of the given notification
  */
 public function actionIndex()
 {
     $notificationModel = Notification::findOne(['id' => Yii::$app->request->get('id'), 'user_id' => Yii::$app->user->id]);
     if ($notificationModel === null) {
         throw new \yii\web\HttpException(404, 'Could not find requested notification!');
     }
     $notification = $notificationModel->getClass();
     if ($notification->markAsSeenOnClick) {
         $notification->markAsSeen();
         // Automatically mark similar notification as seen
         $similarNotifications = Notification::find()->where(['source_class' => $notificationModel->source_class, 'source_pk' => $notificationModel->source_pk, 'user_id' => Yii::$app->user->id])->andWhere(['!=', 'seen', '1']);
         foreach ($similarNotifications->all() as $n) {
             $n->getClass()->markAsSeen();
         }
     }
     // Redirect to notification URL
     $this->redirect($notification->getUrl());
 }
Example #8
0
 /**
  * Returns all notifications which should be send by e-mail to the given user
  * in the given interval
  *
  * @see \humhub\modules\content\components\MailUpdateSender
  * @param User $user
  * @param int $interval
  * @return components\BaseNotification[]
  */
 public function getMailNotifications(User $user, $interval)
 {
     $notifications = [];
     $receive_email_notifications = Yii::$app->getModule('notification')->settings->contentContainer($user)->get('receive_email_notifications');
     if ($receive_email_notifications === null) {
         // Use Default
         $receive_email_notifications = Yii::$app->getModule('notification')->settings->get('receive_email_notifications');
     }
     // Never receive notifications
     if ($receive_email_notifications == User::RECEIVE_EMAIL_NEVER) {
         return [];
     }
     // We are in hourly mode and user wants daily
     if ($interval == MailUpdateSender::INTERVAL_HOURY && $receive_email_notifications == User::RECEIVE_EMAIL_DAILY_SUMMARY) {
         return [];
     }
     // We are in daily mode and user dont wants daily reports
     if ($interval == MailUpdateSender::INTERVAL_DAILY && $receive_email_notifications != User::RECEIVE_EMAIL_DAILY_SUMMARY) {
         return [];
     }
     // User wants only when offline and is online
     if ($interval == MailUpdateSender::INTERVAL_HOURY) {
         $isOnline = count($user->httpSessions) > 0;
         if ($receive_email_notifications == User::RECEIVE_EMAIL_WHEN_OFFLINE && $isOnline) {
             return [];
         }
     }
     $query = Notification::findGrouped()->andWhere(['user_id' => $user->id])->andWhere(['!=', 'seen', 1])->andWhere(['!=', 'emailed', 1]);
     foreach ($query->all() as $notification) {
         $notifications[] = $notification->getClass();
         // Mark notifications as mailed
         $notification->emailed = 1;
         $notification->save();
     }
     return $notifications;
 }
Example #9
0
 public function getModuleNotifications()
 {
     if ($this->moduleNotifications == null) {
         $this->moduleNotifications = Notification::getModuleNotifications();
     }
     return $this->moduleNotifications;
 }
Example #10
0
 /**
  * Returns the last users of a grouped notification
  * 
  * @param int $limit users to return
  * @return User[] the number of user
  */
 public function getGroupLastUsers($limit = 2)
 {
     $users = [];
     $query = Notification::find()->where(['notification.user_id' => $this->record->user_id, 'notification.class' => $this->record->class, 'notification.group_key' => $this->record->group_key])->joinWith(['originator', 'originator.profile'])->orderBy(['notification.created_at' => SORT_DESC])->groupBy(['notification.originator_user_id'])->andWhere(['IS NOT', 'user.id', new \yii\db\Expression('NULL')])->limit($limit);
     foreach ($query->all() as $notification) {
         $users[] = $notification->originator;
     }
     return $users;
 }
 /**
  * Deletes this notification
  */
 public function delete(\humhub\modules\user\models\User $user = null)
 {
     $condition = [];
     $condition['class'] = $this->className();
     if ($user !== null) {
         $condition['user_id'] = $user->id;
     }
     if ($this->originator !== null) {
         $condition['originator_user_id'] = $this->originator->id;
     }
     if ($this->source !== null) {
         $condition['source_pk'] = $this->source->getPrimaryKey();
         $condition['source_class'] = $this->source->className();
     }
     Notification::deleteAll($condition);
 }
Example #12
0
 /**
  * On run of the cron, do some cleanup stuff.
  * We delete all notifications which are older than 2 month and are seen.
  *
  * @param type $event
  */
 public static function onCronDailyRun($event)
 {
     $controller = $event->sender;
     $controller->stdout("Deleting old notifications... ");
     /**
      * Delete seen notifications which are older than 2 months
      */
     $deleteTime = time() - 60 * 60 * 24 * 31 * 2;
     // Notifcations which are older as ~ 2 Months
     foreach (Notification::find()->where(['seen' => 1])->andWhere(['<', 'created_at', date('Y-m-d', $deleteTime)])->all() as $notification) {
         $notification->delete();
     }
     $controller->stdout('done.' . PHP_EOL, \yii\helpers\Console::FG_GREEN);
 }
Example #13
0
 public static function onActiveRecordDelete($event)
 {
     models\Notification::deleteAll(['source_class' => $event->sender->className(), 'source_pk' => $event->sender->getPrimaryKey()]);
 }
Example #14
0
 /**
  * Marks notification as seen
  */
 public function markAsSeen()
 {
     $this->record->seen = 1;
     $this->record->save();
 }