Advertisement
Guest User

Untitled

a guest
Mar 6th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  /**
  2.          * Send the actual notification email
  3.          * @param \Users\Model\Entity\User $user
  4.          * @param \NotificationCenter\Model\Entity\Notification $notification
  5.          * @return bool
  6.          */
  7.         protected function _sendEmail(\Users\Model\Entity\User $user, \NotificationCenter\Model\Entity\Notification $notification) {
  8.  
  9.             //see if this user has any outstanding notifications that have not been viewed yet
  10.             $unreadNotifications = $this->NotificationsUsers->find()
  11.                                                             ->where([
  12.                                                                 'NotificationsUsers.user_id' => $user->id,
  13.                                                                 'NotificationsUsers.sent' => 1,
  14.                                                                 'NotificationsUsers.displayed' => 0
  15.                                                             ])
  16.                                                             ->contain([
  17.                                                                 'Notifications'
  18.                                                             ])
  19.                                                             ->order([
  20.                                                                 'NotificationsUsers.sent_at' => 'desc'
  21.                                                             ]);
  22.  
  23.  
  24.             //switch locale based on user's preferred language
  25.             \Cake\I18n\I18n::locale($this->localelist[$user->last_language_id]);
  26.  
  27.  
  28.             //pick the right subject line
  29.             if ($unreadNotifications->count() == 0) {
  30.                 $subject = __d('notification_center', 'You have received a new alert');
  31.             } else {
  32.                 $subject = __d('notification_center', 'You have {0} unread alerts waiting', ($unreadNotifications->count() + 1));
  33.             }
  34.  
  35.             //do the mailing
  36.             $email = new \Cake\Mailer\Email();
  37.  
  38.             $email
  39.                 ->subject($subject)
  40.                 ->to([$user->email => sprintf('%s %s', $user->forename, $user->surname)])
  41.                 ->template('NotificationCenter.new_notification')
  42.                 ->viewVars([
  43.                     'user' => $user,
  44.                     'notification' => $notification,
  45.                     'unreadNotifications' => $unreadNotifications
  46.                 ]);
  47.  
  48.             if ($email->send()) {
  49.  
  50.                 //mark as sent
  51.                 $notificationsUser = $this->NotificationsUsers
  52.                     ->find()
  53.                     ->where([
  54.                         'NotificationsUsers.user_id' => $user->id,
  55.                         'NotificationsUsers.notification_id' => $notification->id
  56.                     ])
  57.                     ->first();
  58.  
  59.                 if ($notificationsUser) {
  60.  
  61.                     $notificationsUser->sent = 1;
  62.                     $notificationsUser->sent_at = new \DateTime();
  63.  
  64.                     $this->NotificationsUsers->save($notificationsUser);
  65.  
  66.                     $this->out('Successfully sent notification to ' . $user->email);
  67.  
  68.                     return true;
  69.                 }
  70.  
  71.             }
  72.  
  73.             return false;
  74.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement