Elgg  Version 1.11
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  $prefixLength = strlen(self::RELATIONSHIP_PREFIX);
66  $records = $this->getSubscriptionRecords($object->getContainerGUID());
67  foreach ($records as $record) {
68  $deliveryMethods = explode(',', $record->methods);
69  $subscriptions[$record->guid] = substr_replace($deliveryMethods, '', 0, $prefixLength);
70  }
71 
72  $params = array('event' => $event);
73  return _elgg_services()->hooks->trigger('get', 'subscriptions', $params, $subscriptions);
74  }
75 
89 
90  $subscriptions = array();
91 
92  if (!$this->methods) {
93  return $subscriptions;
94  }
95 
96  $prefixLength = strlen(self::RELATIONSHIP_PREFIX);
97  $records = $this->getSubscriptionRecords($container_guid);
98  foreach ($records as $record) {
99  $deliveryMethods = explode(',', $record->methods);
100  $subscriptions[$record->guid] = substr_replace($deliveryMethods, '', 0, $prefixLength);
101  }
102 
103  return $subscriptions;
104  }
105 
116  public function addSubscription($userGuid, $method, $targetGuid) {
117  if (!in_array($method, $this->methods)) {
118  return false;
119  }
120  $prefix = self::RELATIONSHIP_PREFIX;
121  return add_entity_relationship($userGuid, "$prefix$method", $targetGuid);
122  }
123 
132  public function removeSubscription($userGuid, $method, $targetGuid) {
133  $prefix = self::RELATIONSHIP_PREFIX;
134  return remove_entity_relationship($userGuid, "$prefix$method", $targetGuid);
135  }
136 
147 
148  $container_guid = $this->db->sanitizeInt($container_guid);
149 
150  // create IN clause
151  $rels = $this->getMethodRelationships();
152  if (!$rels) {
153  return array();
154  }
155  array_walk($rels, array($this->db, 'sanitizeString'));
156  $methods_string = "'" . implode("','", $rels) . "'";
157 
158  $db_prefix = $this->db->getTablePrefix();
159  $query = "SELECT guid_one AS guid, GROUP_CONCAT(relationship SEPARATOR ',') AS methods
160  FROM {$db_prefix}entity_relationships
161  WHERE guid_two = $container_guid AND
162  relationship IN ($methods_string) GROUP BY guid_one";
163  return $this->db->getData($query);
164  }
165 
171  protected function getMethodRelationships() {
172  $prefix = self::RELATIONSHIP_PREFIX;
173  $names = array();
174  foreach ($this->methods as $method) {
175  $names[] = "$prefix$method";
176  }
177  return $names;
178  }
179 }
180 
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;.