/** * 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)); } }
/** * Get body for the notification * * Plugin can define a subtype specific body simply by providing a * translation for the string "notification:body:<action>:<type>:<subtype". * * For example in mod/blog/languages/en.php: * * 'notification:body:publish:object:blog' => ' * Hi %s! * * %s has created a new post called "%s" in the group %s. * * It says: * * "%s" * * You can comment the post here: * %s * ', * * The arguments passed into the translation are: * 1. Recipient's name * 2. Name of the user who triggered the notification * 3. Title of the content * 4. Name of the content's container * 5. The actual content (entity's 'description' field) * 6. URL to the content * * Argument swapping can be used to change the order of the parameters. * See http://php.net/manual/en/function.sprintf.php#example-5427 * * @param Event $event Notification event * @param \ElggUser $recipient Notification recipient * @return string Notification body in the recipient's language */ private function getNotificationBody(Event $event, \ElggUser $recipient) { $actor = $event->getActor(); $object = $event->getObject(); /* @var \ElggObject $object */ $language = $recipient->language; // Check custom notification body for the action/type/subtype combination $body_key = "notification:{$event->getDescription()}:body"; if ($this->translator->languageKeyExists($body_key, $language)) { $container = $object->getContainerEntity(); return $this->translator->translate($body_key, array($recipient->name, $actor->name, $object->getDisplayName(), $container->getDisplayName(), $object->description, $object->getURL()), $language); } // Fall back to default body return $this->translator->translate('notification:body', array($object->getURL()), $language); }