Example #1
0
 /**
  * Send a notification to a subscriber
  *
  * @param \Elgg\Notifications\Event $event  The notification event
  * @param int                      $guid   The guid of the subscriber
  * @param string                   $method The notification method
  * @return bool
  * @access private
  */
 protected function sendNotification(\Elgg\Notifications\Event $event, $guid, $method)
 {
     $recipient = get_user($guid);
     if (!$recipient || $recipient->isBanned()) {
         return false;
     }
     // don't notify the creator of the content
     if ($recipient->getGUID() == $event->getActorGUID()) {
         return false;
     }
     $actor = $event->getActor();
     $object = $event->getObject();
     if (!$actor || !$object) {
         return false;
     }
     if ($object instanceof ElggEntity && !has_access_to_entity($object, $recipient)) {
         return false;
     }
     $language = $recipient->language;
     $params = array('event' => $event, 'method' => $method, 'recipient' => $recipient, 'language' => $language, 'object' => $object);
     $subject = _elgg_services()->translator->translate('notification:subject', array($actor->name), $language);
     $body = _elgg_services()->translator->translate('notification:body', array($object->getURL()), $language);
     $notification = new \Elgg\Notifications\Notification($event->getActor(), $recipient, $language, $subject, $body, '', $params);
     $type = 'notification:' . $event->getDescription();
     if ($this->hooks->hasHandler('prepare', $type)) {
         $notification = $this->hooks->trigger('prepare', $type, $params, $notification);
     } else {
         // pre Elgg 1.9 notification message generation
         $notification = $this->getDeprecatedNotificationBody($notification, $event, $method);
     }
     if ($this->hooks->hasHandler('send', "notification:{$method}")) {
         // return true to indicate the notification has been sent
         $params = array('notification' => $notification, 'event' => $event);
         return $this->hooks->trigger('send', "notification:{$method}", $params, false);
     } else {
         // pre Elgg 1.9 notification handler
         $userGuid = $notification->getRecipientGUID();
         $senderGuid = $notification->getSenderGUID();
         $subject = $notification->subject;
         $body = $notification->body;
         $params = $notification->params;
         return (bool) _elgg_notify_user($userGuid, $senderGuid, $subject, $body, $params, array($method));
     }
 }
Example #2
0
 /**
  * Send a notification to a subscriber
  *
  * @param NotificationEvent $event  The notification event
  * @param int               $guid   The guid of the subscriber
  * @param string            $method The notification method
  * @param array             $params Default notification params
  * @return bool
  * @access private
  */
 protected function sendNotification(NotificationEvent $event, $guid, $method, array $params = [])
 {
     $actor = $event->getActor();
     $object = $event->getObject();
     if ($event instanceof InstantNotificationEvent) {
         $recipient = $this->entities->get($guid);
         /* @var \ElggEntity $recipient */
         $subject = elgg_extract('subject', $params, '');
         $body = elgg_extract('body', $params, '');
         $summary = elgg_extract('summary', $params, '');
     } else {
         $recipient = $this->entities->get($guid, 'user');
         /* @var \ElggUser $recipient */
         if (!$recipient || $recipient->isBanned()) {
             return false;
         }
         if ($recipient->getGUID() == $event->getActorGUID()) {
             // Content creators should not be receiving subscription
             // notifications about their own content
             return false;
         }
         if (!$actor || !$object) {
             return false;
         }
         if ($object instanceof ElggEntity && !has_access_to_entity($object, $recipient)) {
             // Recipient does not have access to the notification object
             // The access level may have changed since the event was enqueued
             return false;
         }
         $subject = $this->getNotificationSubject($event, $recipient);
         $body = $this->getNotificationBody($event, $recipient);
         $summary = '';
         $params['origin'] = Notification::ORIGIN_SUBSCRIPTIONS;
     }
     $language = $recipient->language;
     $params['event'] = $event;
     $params['method'] = $method;
     $params['sender'] = $actor;
     $params['recipient'] = $recipient;
     $params['language'] = $language;
     $params['object'] = $object;
     $params['action'] = $event->getAction();
     $notification = new Notification($actor, $recipient, $language, $subject, $body, $summary, $params);
     $notification = $this->hooks->trigger('prepare', 'notification', $params, $notification);
     if (!$notification instanceof Notification) {
         throw new RuntimeException("'prepare','notification' hook must return an instance of " . Notification::class);
     }
     $type = 'notification:' . $event->getDescription();
     if ($this->hooks->hasHandler('prepare', $type)) {
         $notification = $this->hooks->trigger('prepare', $type, $params, $notification);
         if (!$notification instanceof Notification) {
             throw new RuntimeException("'prepare','{$type}' hook must return an instance of " . Notification::class);
         }
     } else {
         // pre Elgg 1.9 notification message generation
         $notification = $this->getDeprecatedNotificationBody($notification, $event, $method);
     }
     $notification = $this->hooks->trigger('format', "notification:{$method}", [], $notification);
     if (!$notification instanceof Notification) {
         throw new RuntimeException("'format','notification:{$method}' hook must return an instance of " . Notification::class);
     }
     if ($this->hooks->hasHandler('send', "notification:{$method}")) {
         // return true to indicate the notification has been sent
         $params = array('notification' => $notification, 'event' => $event);
         $result = $this->hooks->trigger('send', "notification:{$method}", $params, false);
         if ($this->logger->getLevel() == Logger::INFO) {
             $logger_data = print_r((array) $notification->toObject(), true);
             if ($result) {
                 $this->logger->info("Notification sent: " . $logger_data);
             } else {
                 $this->logger->info("Notification was not sent: " . $logger_data);
             }
         }
         return $result;
     } else {
         // pre Elgg 1.9 notification handler
         $userGuid = $notification->getRecipientGUID();
         $senderGuid = $notification->getSenderGUID();
         $subject = $notification->subject;
         $body = $notification->body;
         $params = $notification->params;
         return (bool) _elgg_notify_user($userGuid, $senderGuid, $subject, $body, $params, array($method));
     }
 }
Example #3
0
/**
 * Notify a user via their preferences.
 *
 * @param mixed  $to               Either a guid or an array of guid's to notify.
 * @param int    $from             GUID of the sender, which may be a user, site or object.
 * @param string $subject          Message subject.
 * @param string $message          Message body.
 * @param array  $params           Misc additional parameters specific to various methods.
 *
 *                                 By default Elgg core supports three parameters, which give
 *                                 notification plugins more control over the notifications:
 *
 *                                 object => null|\ElggEntity|\ElggAnnotation The object that
 *                                           is triggering the notification.
 *
 *                                 action => null|string Word that describes the action that
 *                                           is triggering the notification (e.g. "create"
 *                                           or "update").
 *
 *                                 summary => null|string Summary that notification plugins
 *                                            can use alongside the notification title and body.
 *
 * @param mixed  $methods_override A string, or an array of strings specifying the delivery
 *                                 methods to use - or leave blank for delivery using the
 *                                 user's chosen delivery methods.
 *
 * @return array Compound array of each delivery user/delivery method's success or failure.
 * @throws NotificationException
 */
function notify_user($to, $from, $subject, $message, array $params = array(), $methods_override = "")
{
    if (!is_array($to)) {
        $to = array((int) $to);
    }
    $from = (int) $from;
    $from = get_entity($from) ? $from : elgg_get_site_entity()->guid;
    $sender = get_entity($from);
    $summary = elgg_extract('summary', $params, '');
    // Get notification methods
    if ($methods_override && !is_array($methods_override)) {
        $methods_override = array($methods_override);
    }
    // temporary backward compatibility for 1.8 and earlier notifications
    $event = null;
    if (isset($params['object']) && isset($params['action'])) {
        $event = new \Elgg\Notifications\Event($params['object'], $params['action'], $sender);
    }
    $params['event'] = $event;
    $result = array();
    foreach ($to as $guid) {
        // Results for a user are...
        $result[$guid] = array();
        if ($guid) {
            // Is the guid > 0?
            // Are we overriding delivery?
            $methods = $methods_override;
            if (!$methods) {
                $tmp = (array) get_user_notification_settings($guid);
                $methods = array();
                foreach ($tmp as $k => $v) {
                    // Add method if method is turned on for user!
                    if ($v) {
                        $methods[] = $k;
                    }
                }
            }
            if ($methods) {
                // Deliver
                foreach ($methods as $method) {
                    if (_elgg_services()->hooks->hasHandler('send', "notification:{$method}")) {
                        // 1.9 style notification handler
                        $recipient = get_entity($guid);
                        if (!$recipient) {
                            continue;
                        }
                        $language = $recipient->language;
                        $notification = new \Elgg\Notifications\Notification($sender, $recipient, $language, $subject, $message, $summary, $params);
                        $params['notification'] = $notification;
                        $result[$guid][$method] = _elgg_services()->hooks->trigger('send', "notification:{$method}", $params, false);
                    } else {
                        $result[$guid][$method] = _elgg_notify_user($guid, $from, $subject, $message, $params, array($method));
                    }
                }
            }
        }
    }
    return $result;
}
Example #4
0
/**
 * Notify a user via their preferences.
 *
 * @param mixed  $to               Either a guid or an array of guid's to notify.
 * @param int    $from             GUID of the sender, which may be a user, site or object.
 * @param string $subject          Message subject.
 * @param string $message          Message body.
 * @param array  $params           Misc additional parameters specific to various methods.
 * @param mixed  $methods_override A string, or an array of strings specifying the delivery
 *                                 methods to use - or leave blank for delivery using the
 *                                 user's chosen delivery methods.
 *
 * @return array Compound array of each delivery user/delivery method's success or failure.
 * @throws NotificationException
 */
function notify_user($to, $from, $subject, $message, array $params = array(), $methods_override = "")
{
    if (!is_array($to)) {
        $to = array((int) $to);
    }
    $from = (int) $from;
    $from = get_entity($from) ? $from : elgg_get_site_entity()->guid;
    // Get notification methods
    if ($methods_override && !is_array($methods_override)) {
        $methods_override = array($methods_override);
    }
    $result = array();
    foreach ($to as $guid) {
        // Results for a user are...
        $result[$guid] = array();
        if ($guid) {
            // Is the guid > 0?
            // Are we overriding delivery?
            $methods = $methods_override;
            if (!$methods) {
                $tmp = (array) get_user_notification_settings($guid);
                $methods = array();
                foreach ($tmp as $k => $v) {
                    // Add method if method is turned on for user!
                    if ($v) {
                        $methods[] = $k;
                    }
                }
            }
            if ($methods) {
                // Deliver
                foreach ($methods as $method) {
                    if (_elgg_services()->hooks->hasHandler('send', "notification:{$method}")) {
                        // 1.9 style notification handler
                        $sender = get_entity($from);
                        $recipient = get_entity($guid);
                        if (!$recipient) {
                            continue;
                        }
                        $language = $recipient->language;
                        $notification = new Elgg_Notifications_Notification($sender, $recipient, $language, $subject, $message, '', $params);
                        $params = array('notification' => $notification, 'event' => null);
                        $result[$guid][$method] = _elgg_services()->hooks->trigger('send', "notification:{$method}", $params, false);
                    } else {
                        $result[$guid][$method] = _elgg_notify_user($guid, $from, $subject, $message, $params, array($method));
                    }
                }
            }
        }
    }
    return $result;
}