Elgg  Version 2.3
SubscriptionsService.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Notifications;
3 
5 
16 
20  const RELATIONSHIP_PREFIX = 'notify';
21 
26  public $methods;
27 
29  protected $db;
30 
37  public function __construct(Database $db, array $methods = array()) {
38  $this->db = $db;
39  $this->methods = $methods;
40  }
41 
54  public function getSubscriptions(NotificationEvent $event) {
55 
56  $subscriptions = array();
57 
58  if (!$this->methods) {
59  return $subscriptions;
60  }
61 
62  $object = $event->getObject();
63  if (!$object) {
64  return $subscriptions;
65  }
66 
67  if ($object instanceof \ElggEntity) {
68  $prefixLength = strlen(self::RELATIONSHIP_PREFIX);
69  $records = $this->getSubscriptionRecords($object->getContainerGUID());
70  foreach ($records as $record) {
71  $deliveryMethods = explode(',', $record->methods);
72  $subscriptions[$record->guid] = substr_replace($deliveryMethods, '', 0, $prefixLength);
73  }
74  }
75 
76  $params = array('event' => $event, 'origin' => Notification::ORIGIN_SUBSCRIPTIONS);
77  return _elgg_services()->hooks->trigger('get', 'subscriptions', $params, $subscriptions);
78  }
79 
93 
94  $subscriptions = array();
95 
96  if (!$this->methods) {
97  return $subscriptions;
98  }
99 
100  $prefixLength = strlen(self::RELATIONSHIP_PREFIX);
101  $records = $this->getSubscriptionRecords($container_guid);
102  foreach ($records as $record) {
103  $deliveryMethods = explode(',', $record->methods);
104  $subscriptions[$record->guid] = substr_replace($deliveryMethods, '', 0, $prefixLength);
105  }
106 
107  return $subscriptions;
108  }
109 
120  public function addSubscription($userGuid, $method, $targetGuid) {
121  if (!in_array($method, $this->methods)) {
122  return false;
123  }
124  $prefix = self::RELATIONSHIP_PREFIX;
125  return add_entity_relationship($userGuid, "$prefix$method", $targetGuid);
126  }
127 
136  public function removeSubscription($userGuid, $method, $targetGuid) {
137  $prefix = self::RELATIONSHIP_PREFIX;
138  return remove_entity_relationship($userGuid, "$prefix$method", $targetGuid);
139  }
140 
151 
152  $container_guid = $this->db->sanitizeInt($container_guid);
153 
154  // create IN clause
155  $rels = $this->getMethodRelationships();
156  if (!$rels) {
157  return array();
158  }
159  array_walk($rels, array($this->db, 'sanitizeString'));
160  $methods_string = "'" . implode("','", $rels) . "'";
161 
162  $db_prefix = $this->db->prefix;
163  $query = "SELECT guid_one AS guid, GROUP_CONCAT(relationship SEPARATOR ',') AS methods
164  FROM {$db_prefix}entity_relationships
165  WHERE guid_two = $container_guid AND
166  relationship IN ($methods_string) GROUP BY guid_one";
167  return $this->db->getData($query);
168  }
169 
175  protected function getMethodRelationships() {
176  $prefix = self::RELATIONSHIP_PREFIX;
177  $names = array();
178  foreach ($this->methods as $method) {
179  $names[] = "$prefix$method";
180  }
181  return $names;
182  }
183 }
184 
getObject()
Get the object of the event.
$object
These two snippets demonstrates triggering an event and how to register for that event.
Definition: trigger.php:7
getSubscriptionsForContainer($container_guid)
Get the subscriptions for the content created inside this container.
add_entity_relationship($guid_one, $relationship, $guid_two)
Create a relationship between two entities.
remove_entity_relationship($guid_one, $relationship, $guid_two)
Delete a relationship between two entities.
$method
Definition: form.php:25
The Elgg database.
Definition: Database.php:17
removeSubscription($userGuid, $method, $targetGuid)
Unsubscribe a user to notifications about a target entity.
__construct(Database $db, array $methods=array())
Constructor.
$params
Definition: login.php:72
getSubscriptionRecords($container_guid)
Get subscription records from the database.
_elgg_services(\Elgg\Di\ServiceProvider $services=null)
Get the global service provider.
Definition: autoloader.php:17
addSubscription($userGuid, $method, $targetGuid)
Subscribe a user to notifications about a target entity.
getMethodRelationships()
Get the relationship names for notifications.
$container_guid
getSubscriptions(NotificationEvent $event)
Get the subscriptions for this notification event.
http free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:5
if(!$num_display) $db_prefix
Definition: content.php:13
const RELATIONSHIP_PREFIX
Elgg has historically stored subscriptions as relationships with the prefix &#39;notify&#39;.