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; }
/** * @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); }
/** * 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; }
/** * 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()); }
/** * 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; }
/** * 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); }