Elgg  Version 1.12
SubscriptionsService.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Notifications;
3 
14 
18  const RELATIONSHIP_PREFIX = 'notify';
19 
24  public $methods;
25 
27  protected $db;
28 
35  public function __construct(\Elgg\Database $db, array $methods = array()) {
36  $this->db = $db;
37  $this->methods = $methods;
38  }
39 
52  public function getSubscriptions(\Elgg\Notifications\Event $event) {
53 
54  $subscriptions = array();
55 
56  if (!$this->methods) {
57  return $subscriptions;
58  }
59 
60  $object = $event->getObject();
61  if (!$object) {
62  return $subscriptions;
63  }
64 
65  if ($object instanceof \ElggEntity) {
66  $prefixLength = strlen(self::RELATIONSHIP_PREFIX);
67  $records = $this->getSubscriptionRecords($object->getContainerGUID());
68  foreach ($records as $record) {
69  $deliveryMethods = explode(',', $record->methods);
70  $subscriptions[$record->guid] = substr_replace($deliveryMethods, '', 0, $prefixLength);
71  }
72  }
73 
74  $params = array('event' => $event);
75  return _elgg_services()->hooks->trigger('get', 'subscriptions', $params, $subscriptions);
76  }
77 
91 
92  $subscriptions = array();
93 
94  if (!$this->methods) {
95  return $subscriptions;
96  }
97 
98  $prefixLength = strlen(self::RELATIONSHIP_PREFIX);
99  $records = $this->getSubscriptionRecords($container_guid);
100  foreach ($records as $record) {
101  $deliveryMethods = explode(',', $record->methods);
102  $subscriptions[$record->guid] = substr_replace($deliveryMethods, '', 0, $prefixLength);
103  }
104 
105  return $subscriptions;
106  }
107 
118  public function addSubscription($userGuid, $method, $targetGuid) {
119  if (!in_array($method, $this->methods)) {
120  return false;
121  }
122  $prefix = self::RELATIONSHIP_PREFIX;
123  return add_entity_relationship($userGuid, "$prefix$method", $targetGuid);
124  }
125 
134  public function removeSubscription($userGuid, $method, $targetGuid) {
135  $prefix = self::RELATIONSHIP_PREFIX;
136  return remove_entity_relationship($userGuid, "$prefix$method", $targetGuid);
137  }
138 
149 
150  $container_guid = $this->db->sanitizeInt($container_guid);
151 
152  // create IN clause
153  $rels = $this->getMethodRelationships();
154  if (!$rels) {
155  return array();
156  }
157  array_walk($rels, array($this->db, 'sanitizeString'));
158  $methods_string = "'" . implode("','", $rels) . "'";
159 
160  $db_prefix = $this->db->getTablePrefix();
161  $query = "SELECT guid_one AS guid, GROUP_CONCAT(relationship SEPARATOR ',') AS methods
162  FROM {$db_prefix}entity_relationships
163  WHERE guid_two = $container_guid AND
164  relationship IN ($methods_string) GROUP BY guid_one";
165  return $this->db->getData($query);
166  }
167 
173  protected function getMethodRelationships() {
174  $prefix = self::RELATIONSHIP_PREFIX;
175  $names = array();
176  foreach ($this->methods as $method) {
177  $names[] = "$prefix$method";
178  }
179  return $names;
180  }
181 }
182 
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
$object
Definition: upgrade.php:12
removeSubscription($userGuid, $method, $targetGuid)
Unsubscribe a user to notifications about a target entity.
getSubscriptions(\Elgg\Notifications\Event $event)
Get the subscriptions for this notification event.
$params
Definition: login.php:72
__construct(\Elgg\Database $db, array $methods=array())
Constructor.
Save menu items.
_elgg_services()
Definition: autoloader.php:14
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.
$container_guid
if(!$num_display) $db_prefix
Definition: content.php:12
const RELATIONSHIP_PREFIX
Elgg has historically stored subscriptions as relationships with the prefix &#39;notify&#39;.