/** * Get the Notifications that have not yet been sent yet. * Ordered by "template" and "title". * @param RecipientInterface $recipient * @return array of Notification */ protected function getNotificationsFor(RecipientInterface $recipient) { // get the notification mode $notificationMode = $recipient->getNotificationMode(); // get the date/time of the last notification $qb = $this->em->createQueryBuilder()->select("max(n.sent)")->from("Azine\\EmailBundle\\Entity\\Notification", "n")->andWhere("n.recipient_id = :recipientId")->setParameter('recipientId', $recipient->getId()); $results = $qb->getQuery()->execute(); if ($results[0][1] == null) { // the user has not received any notifications yet ever $lastNotification = new \DateTime("@0"); } else { $lastNotification = new \DateTime($results[0][1]); } $sendNotifications = false; $timeDelta = time() - $lastNotification->getTimestamp(); if ($notificationMode == RecipientInterface::NOTIFICATION_MODE_IMMEDIATELY) { $sendNotifications = true; } elseif ($notificationMode == RecipientInterface::NOTIFICATION_MODE_HOURLY) { $sendNotifications = $timeDelta > $this->getHourInterval(); } elseif ($notificationMode == RecipientInterface::NOTIFICATION_MODE_DAYLY) { $sendNotifications = $timeDelta > $this->getDayInterval(); } elseif ($notificationMode == RecipientInterface::NOTIFICATION_MODE_NEVER) { $this->markAllNotificationsAsSentFarInThePast($recipient); return array(); } $userNotificationTypes = array(); foreach ($recipient->getNotificationTypes() as $notificationType) { $userNotificationTypes[] = $notificationType->getId(); } // regularly sent notifications now if ($sendNotifications) { $qb = $this->em->createQueryBuilder()->select("n")->from("Azine\\EmailBundle\\Entity\\Notification", "n")->andWhere("n.sent is null")->andWhere("n.recipient_id = :recipientId")->setParameter('recipientId', $recipient->getId())->andWhere("n.type in (:recipientNotificationTypes)")->setParameter('recipientNotificationTypes', $userNotificationTypes)->orderBy("n.importance", "desc")->orderBy("n.type", "asc")->orderBy("n.title", "asc"); $notifications = $qb->getQuery()->execute(); // if notifications exist, that should be sent immediately, then send those now disregarding the users mailing-preferences. } else { $qb = $this->em->createQueryBuilder()->select("n")->from("Azine\\EmailBundle\\Entity\\Notification", "n")->andWhere("n.sent is null")->andWhere("n.send_immediately = true")->andWhere("n.recipient_id = :recipientId")->setParameter('recipientId', $recipient->getId())->andWhere("n.type in (:recipientNotificationTypes)")->setParameter('recipientNotificationTypes', $userNotificationTypes)->orderBy("n.importance", "desc")->orderBy("n.type", "asc")->orderBy("n.title", "asc"); $notifications = $qb->getQuery()->execute(); } return $notifications; }
/** * Get the Notifications that have not yet been sent yet. * Ordered by "template" and "title". * @param RecipientInterface $recipient * @return array of Notification */ protected function getNotificationsFor(RecipientInterface $recipient) { // get the notification mode $notificationMode = $recipient->getNotificationMode(); // get the date/time of the last notification $lastNotificationDate = $this->getNotificationRepository()->getLastNotificationDate($recipient->getId()); $sendNotifications = false; $timeDelta = time() - $lastNotificationDate->getTimestamp(); if ($notificationMode == RecipientInterface::NOTIFICATION_MODE_IMMEDIATELY) { $sendNotifications = true; } elseif ($notificationMode == RecipientInterface::NOTIFICATION_MODE_HOURLY) { $sendNotifications = $timeDelta > $this->getHourInterval(); } elseif ($notificationMode == RecipientInterface::NOTIFICATION_MODE_DAYLY) { $sendNotifications = $timeDelta > $this->getDayInterval(); } elseif ($notificationMode == RecipientInterface::NOTIFICATION_MODE_NEVER) { $this->markAllNotificationsAsSentFarInThePast($recipient); return array(); } // regularly sent notifications now if ($sendNotifications) { $notifications = $this->getNotificationRepository()->getNotificationsToSend($recipient->getId()); // if notifications exist, that should be sent immediately, then send those now disregarding the users mailing-preferences. } else { $notifications = $this->getNotificationRepository()->getNotificationsToSendImmediately($recipient->getId()); } return $notifications; }