Elgg  Version 1.9
NotificationsService.php
Go to the documentation of this file.
1 <?php
2 
13 
14  const QUEUE_NAME = 'notifications';
15 
17  protected $subscriptions;
18 
20  protected $queue;
21 
23  protected $hooks;
24 
26  protected $access;
27 
29  protected $events = array();
30 
32  protected $methods = array();
33 
35  protected $deprHandlers = array();
36 
38  protected $deprSubjects = array();
39 
50  $this->subscriptions = $subscriptions;
51  $this->queue = $queue;
52  $this->hooks = $hooks;
53  $this->access = $access;
54  }
55 
60  public function registerEvent($type, $subtype, array $actions = array()) {
61 
62  if (!isset($this->events[$type])) {
63  $this->events[$type] = array();
64  }
65  if (!isset($this->events[$type][$subtype])) {
66  $this->events[$type][$subtype] = array();
67  }
68 
69  $action_list =& $this->events[$type][$subtype];
70  if ($actions) {
71  $action_list = array_unique(array_merge($action_list, $actions));
72  } elseif (!in_array('create', $action_list)) {
73  $action_list[] = 'create';
74  }
75  }
76 
81  public function unregisterEvent($type, $subtype) {
82 
83  if (!isset($this->events[$type]) || !isset($this->events[$type][$subtype])) {
84  return false;
85  }
86 
87  unset($this->events[$type][$subtype]);
88 
89  return true;
90  }
91 
95  public function getEvents() {
96  return $this->events;
97  }
98 
103  public function registerMethod($name) {
104  $this->methods[$name] = $name;
105  }
106 
111  public function unregisterMethod($name) {
112  if (isset($this->methods[$name])) {
113  unset($this->methods[$name]);
114  return true;
115  }
116  return false;
117  }
118 
122  public function getMethods() {
123  return $this->methods;
124  }
125 
135  public function enqueueEvent($action, $type, $object) {
136  if ($object instanceof ElggData) {
137  $object_type = $object->getType();
138  $object_subtype = $object->getSubtype();
139 
140  $registered = false;
141  if (isset($this->events[$object_type])
142  && isset($this->events[$object_type][$object_subtype])
143  && in_array($action, $this->events[$object_type][$object_subtype])) {
144  $registered = true;
145  }
146 
147  if ($registered) {
148  $params = array(
149  'action' => $action,
150  'object' => $object,
151  );
152  $registered = $this->hooks->trigger('enqueue', 'notification', $params, $registered);
153  }
154 
155  if ($registered) {
156  $this->queue->enqueue(new Elgg_Notifications_Event($object, $action));
157  }
158  }
159  }
160 
168  public function processQueue($stopTime) {
169 
170  $this->subscriptions->methods = $this->methods;
171 
172  $count = 0;
173 
174  // @todo grab mutex
175 
176  $ia = $this->access->setIgnoreAccess(true);
177 
178  while (time() < $stopTime) {
179  // dequeue notification event
180  $event = $this->queue->dequeue();
181  if (!$event) {
182  break;
183  }
184 
185  // test for usage of the deprecated override hook
186  if ($this->existsDeprecatedNotificationOverride($event)) {
187  continue;
188  }
189 
190  $subscriptions = $this->subscriptions->getSubscriptions($event);
191 
192  // return false to stop the default notification sender
193  $params = array('event' => $event, 'subscriptions' => $subscriptions);
194  if ($this->hooks->trigger('send:before', 'notifications', $params, true)) {
195  $this->sendNotifications($event, $subscriptions);
196  }
197  $this->hooks->trigger('send:after', 'notifications', $params);
198  $count++;
199  }
200 
201  // release mutex
202 
203  $this->access->setIgnoreAccess($ia);
204 
205  return $count;
206  }
207 
216  protected function sendNotifications($event, $subscriptions) {
217 
218  if (!$this->methods) {
219  return 0;
220  }
221 
222  $count = 0;
223  foreach ($subscriptions as $guid => $methods) {
224  foreach ($methods as $method) {
225  if (in_array($method, $this->methods)) {
226  if ($this->sendNotification($event, $guid, $method)) {
227  $count++;
228  }
229  }
230  }
231  }
232  return $count;
233  }
234 
245 
246  $recipient = get_user($guid);
247  if (!$recipient || $recipient->isBanned()) {
248  return false;
249  }
250 
251  // don't notify the creator of the content
252  if ($recipient->getGUID() == $event->getActorGUID()) {
253  return false;
254  }
255 
256  $actor = $event->getActor();
257  $object = $event->getObject();
258  if (!$actor || !$object) {
259  return false;
260  }
261 
262  if (($object instanceof ElggEntity) && !has_access_to_entity($object, $recipient)) {
263  return false;
264  }
265 
266  $language = $recipient->language;
267  $params = array(
268  'event' => $event,
269  'method' => $method,
270  'recipient' => $recipient,
271  'language' => $language,
272  'object' => $object,
273  );
274 
275  $subject = elgg_echo('notification:subject', array($actor->name), $language);
276  $body = elgg_echo('notification:body', array($object->getURL()), $language);
277  $notification = new Elgg_Notifications_Notification($event->getActor(), $recipient, $language, $subject, $body, '', $params);
278 
279  $type = 'notification:' . $event->getDescription();
280  if ($this->hooks->hasHandler('prepare', $type)) {
281  $notification = $this->hooks->trigger('prepare', $type, $params, $notification);
282  } else {
283  // pre Elgg 1.9 notification message generation
284  $notification = $this->getDeprecatedNotificationBody($notification, $event, $method);
285  }
286 
287  if ($this->hooks->hasHandler('send', "notification:$method")) {
288  // return true to indicate the notification has been sent
289  $params = array(
290  'notification' => $notification,
291  'event' => $event,
292  );
293  return $this->hooks->trigger('send', "notification:$method", $params, false);
294  } else {
295  // pre Elgg 1.9 notification handler
296  $userGuid = $notification->getRecipientGUID();
297  $senderGuid = $notification->getSenderGUID();
298  $subject = $notification->subject;
299  $body = $notification->body;
300  $params = $notification->params;
301  return (bool)_elgg_notify_user($userGuid, $senderGuid, $subject, $body, $params, array($method));
302  }
303  }
304 
313  $this->deprHandlers[$method] = $handler;
314  }
315 
322  public function getDeprecatedHandler($method) {
323  if (isset($this->deprHandlers[$method])) {
324  return $this->deprHandlers[$method];
325  } else {
326  return null;
327  }
328  }
329 
336  public function getMethodsAsDeprecatedGlobal() {
337  $data = array();
338  foreach ($this->methods as $method) {
339  $data[$method] = 'empty';
340  }
341  return $data;
342  }
343 
353  $entity = $event->getObject();
354  $params = array(
355  'entity' => $entity,
356  'to_entity' => $notification->getRecipient(),
357  'method' => $method,
358  );
359  $subject = $this->getDeprecatedNotificationSubject($entity->getType(), $entity->getSubtype());
360  $string = $subject . ": " . $entity->getURL();
361  $body = $this->hooks->trigger('notify:entity:message', $entity->getType(), $params, $string);
362 
363  if ($subject) {
364  $notification->subject = $subject;
365  $notification->body = $body;
366  }
367 
368  return $notification;
369  }
370 
380  if ($type == '') {
381  $type = '__BLANK__';
382  }
383  if ($subtype == '') {
384  $subtype = '__BLANK__';
385  }
386 
387  if (!isset($this->deprSubjects[$type])) {
388  $this->deprSubjects[$type] = array();
389  }
390 
391  $this->deprSubjects[$type][$subtype] = $subject;
392  }
393 
402  if ($type == '') {
403  $type = '__BLANK__';
404  }
405  if ($subtype == '') {
406  $subtype = '__BLANK__';
407  }
408 
409  if (!isset($this->deprSubjects[$type])) {
410  return '';
411  }
412 
413  if (!isset($this->deprSubjects[$type][$subtype])) {
414  return '';
415  }
416 
417  return $this->deprSubjects[$type][$subtype];
418  }
419 
427  $entity = $event->getObject();
428  if (!elgg_instanceof($entity)) {
429  return false;
430  }
431  $params = array(
432  'event' => $event->getAction(),
433  'object_type' => $entity->getType(),
434  'object' => $entity,
435  );
436  $hookresult = $this->hooks->trigger('object:notifications', $entity->getType(), $params, false);
437  if ($hookresult === true) {
438  elgg_deprecated_notice("Using the plugin hook 'object:notifications' has been deprecated by the hook 'send:before', 'notifications'", 1.9);
439  return true;
440  } else {
441  return false;
442  }
443  }
444 }
$subject
Definition: exceptions.php:25
getDeprecatedNotificationSubject($type, $subtype)
Get the deprecated subject.
if($guid==elgg_get_logged_in_user_guid()) $name
Definition: delete.php:21
$method
Definition: form.php:25
$object
Definition: upgrade.php:12
enqueueEvent($action, $type, $object)
Add a notification event to the queue.
getRecipient()
Get the recipient entity.
$data
Definition: opendd.php:13
getActorGUID()
Get the GUID of the actor.
Definition: Event.php:68
$ia
Definition: upgrade.php:26
$guid
Removes an admin notice.
registerEvent($type, $subtype, array $actions=array())
events($event="", $object_type="", $function="", $priority=500, $call=false, $object=null)
Deprecated events core function.
$action
getDeprecatedNotificationBody(Elgg_Notifications_Notification $notification, Elgg_Notifications_Event $event, $method)
Get the notification body using a pre-Elgg 1.9 plugin hook.
$string
getActor()
Get the actor of the event.
Definition: Event.php:59
$params
Definition: login.php:72
getObject()
Get the object of the event.
Definition: Event.php:77
existsDeprecatedNotificationOverride(Elgg_Notifications_Event $event)
Is someone using the deprecated override.
registerDeprecatedHandler($method, $handler)
Register a deprecated notification handler.
elgg_instanceof($entity, $type=null, $subtype=null, $class=null)
Checks if $entity is an ElggEntity and optionally for type and subtype.
Definition: entities.php:1886
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
get_user($guid)
Get a user object from a GUID.
Definition: users.php:222
setDeprecatedNotificationSubject($type, $subtype, $subject)
Set message subject for deprecated notification code.
__construct(Elgg_Notifications_SubscriptionsService $subscriptions, Elgg_Queue_Queue $queue, Elgg_PluginHooksService $hooks, Elgg_Access $access)
Constructor.
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Sends a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1171
$type
Definition: add.php:8
sendNotification(Elgg_Notifications_Event $event, $guid, $method)
Send a notification to a subscriber.
$count
Definition: tools.php:19
processQueue($stopTime)
Pull notification events from queue until stop time is reached.
has_access_to_entity($entity, $user=null)
Can a user access an entity.
Definition: access.php:435
$handler
Definition: add.php:10
getAction()
Get the name of the action.
Definition: Event.php:100
getMethodsAsDeprecatedGlobal()
Provides a way to incrementally wean Elgg&#39;s notifications code from the global $NOTIFICATION_HANDLERS...
$entity
Definition: delete.php:10
sendNotifications($event, $subscriptions)
Sends the notifications based on subscriptions.
$language
$vars[&#39;language&#39;] $vars[&#39;lc&#39;] if present, client will be sent long expires headers ...
Definition: languages.php:7
$subtype
Definition: river.php:10
getDeprecatedHandler($method)
Get a deprecated notification handler callback.
$actions
Provides common Elgg services.
Definition: user_hover.php:12
_elgg_notify_user($to, $from, $subject, $message, array $params=null, $methods_override="")
Notify a user via their preferences.
getDescription()
Get a description of the event.
Definition: Event.php:109