Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Send the actual notification email
- * @param \Users\Model\Entity\User $user
- * @param \NotificationCenter\Model\Entity\Notification $notification
- * @return bool
- */
- protected function _sendEmail(\Users\Model\Entity\User $user, \NotificationCenter\Model\Entity\Notification $notification) {
- //see if this user has any outstanding notifications that have not been viewed yet
- $unreadNotifications = $this->NotificationsUsers->find()
- ->where([
- 'NotificationsUsers.user_id' => $user->id,
- 'NotificationsUsers.sent' => 1,
- 'NotificationsUsers.displayed' => 0
- ])
- ->contain([
- 'Notifications'
- ])
- ->order([
- 'NotificationsUsers.sent_at' => 'desc'
- ]);
- //switch locale based on user's preferred language
- \Cake\I18n\I18n::locale($this->localelist[$user->last_language_id]);
- //pick the right subject line
- if ($unreadNotifications->count() == 0) {
- $subject = __d('notification_center', 'You have received a new alert');
- } else {
- $subject = __d('notification_center', 'You have {0} unread alerts waiting', ($unreadNotifications->count() + 1));
- }
- //do the mailing
- $email = new \Cake\Mailer\Email();
- $email
- ->subject($subject)
- ->to([$user->email => sprintf('%s %s', $user->forename, $user->surname)])
- ->template('NotificationCenter.new_notification')
- ->viewVars([
- 'user' => $user,
- 'notification' => $notification,
- 'unreadNotifications' => $unreadNotifications
- ]);
- if ($email->send()) {
- //mark as sent
- $notificationsUser = $this->NotificationsUsers
- ->find()
- ->where([
- 'NotificationsUsers.user_id' => $user->id,
- 'NotificationsUsers.notification_id' => $notification->id
- ])
- ->first();
- if ($notificationsUser) {
- $notificationsUser->sent = 1;
- $notificationsUser->sent_at = new \DateTime();
- $this->NotificationsUsers->save($notificationsUser);
- $this->out('Successfully sent notification to ' . $user->email);
- return true;
- }
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement