Elgg  Version 3.0
SubscriptionsService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Notifications;
4 
5 use Elgg\Database;
6 
17 
21  const RELATIONSHIP_PREFIX = 'notify';
22 
27  public $methods;
28 
30  protected $db;
31 
38  public function __construct(Database $db, array $methods = []) {
39  $this->db = $db;
40  $this->methods = $methods;
41  }
42 
55  public function getSubscriptions(NotificationEvent $event) {
56 
57  $subscriptions = [];
58 
59  if (empty($this->methods)) {
60  return [];
61  }
62 
63  $object = $event->getObject();
64  if (!$object instanceof \ElggData) {
65  return [];
66  }
67 
68  // get subscribers only for \ElggEntity if it isn't private
69  if (($object instanceof \ElggEntity) && ($object->access_id !== ACCESS_PRIVATE)) {
70  $prefixLength = strlen(self::RELATIONSHIP_PREFIX);
71  $records = $this->getSubscriptionRecords($object->getContainerGUID());
72  foreach ($records as $record) {
73  $deliveryMethods = explode(',', $record->methods);
74  $subscriptions[$record->guid] = substr_replace($deliveryMethods, '', 0, $prefixLength);
75  }
76  }
77 
78  $params = [
79  'event' => $event,
81  ];
82  return _elgg_services()->hooks->trigger('get', 'subscriptions', $params, $subscriptions);
83  }
84 
98 
99  $subscriptions = [];
100 
101  if (empty($this->methods)) {
102  return $subscriptions;
103  }
104 
105  $prefixLength = strlen(self::RELATIONSHIP_PREFIX);
106  $records = $this->getSubscriptionRecords($container_guid);
107  foreach ($records as $record) {
108  $deliveryMethods = explode(',', $record->methods);
109  $subscriptions[$record->guid] = substr_replace($deliveryMethods, '', 0, $prefixLength);
110  }
111 
112  return $subscriptions;
113  }
114 
125  public function addSubscription($userGuid, $method, $targetGuid) {
126  if (!in_array($method, $this->methods)) {
127  return false;
128  }
129  $prefix = self::RELATIONSHIP_PREFIX;
130  return add_entity_relationship($userGuid, "$prefix$method", $targetGuid);
131  }
132 
141  public function removeSubscription($userGuid, $method, $targetGuid) {
142  $prefix = self::RELATIONSHIP_PREFIX;
143  return remove_entity_relationship($userGuid, "$prefix$method", $targetGuid);
144  }
145 
156 
158 
159  // create IN clause
160  $rels = $this->getMethodRelationships();
161  if (!$rels) {
162  return [];
163  }
164  array_walk($rels, [$this->db, 'sanitizeString']);
165  $methods_string = "'" . implode("','", $rels) . "'";
166 
167  $db_prefix = $this->db->prefix;
168  $query = "SELECT guid_one AS guid, GROUP_CONCAT(relationship SEPARATOR ',') AS methods
169  FROM {$db_prefix}entity_relationships
170  WHERE guid_two = $container_guid AND
171  relationship IN ($methods_string) GROUP BY guid_one";
172  return $this->db->getData($query);
173  }
174 
180  protected function getMethodRelationships() {
181  $prefix = self::RELATIONSHIP_PREFIX;
182  $names = [];
183  foreach ($this->methods as $method) {
184  $names[] = "$prefix$method";
185  }
186  return $names;
187  }
188 }
getObject()
Get the object of the event.
$query
Definition: groups.php:8
if(!$item instanceof ElggRiverItem) $object
Definition: responses.php:23
$params
Saves global plugin settings.
Definition: save.php:13
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.
removeSubscription($userGuid, $method, $targetGuid)
Unsubscribe a user to notifications about a target entity.
__construct(Database $db, array $methods=[])
Constructor.
const ACCESS_PRIVATE
Definition: constants.php:12
getSubscriptionRecords($container_guid)
Get subscription records from the database.
addSubscription($userGuid, $method, $targetGuid)
Subscribe a user to notifications about a target entity.
getMethodRelationships()
Get the relationship names for notifications.
_elgg_services()
Get the global service provider.
Definition: elgglib.php:1292
$container_guid
getSubscriptions(NotificationEvent $event)
Get the subscriptions for this notification event.
const RELATIONSHIP_PREFIX
Elgg has historically stored subscriptions as relationships with the prefix &#39;notify&#39;.