Elgg  Version 1.9
SubscriptionsService.php
Go to the documentation of this file.
1 <?php
2 
13 
17  const RELATIONSHIP_PREFIX = 'notify';
18 
23  public $methods;
24 
26  protected $db;
27 
34  public function __construct(Elgg_Database $db, array $methods = array()) {
35  $this->db = $db;
36  $this->methods = $methods;
37  }
38 
51  public function getSubscriptions(Elgg_Notifications_Event $event) {
52 
53  $subscriptions = array();
54 
55  if (!$this->methods) {
56  return $subscriptions;
57  }
58 
59  $object = $event->getObject();
60  if (!$object) {
61  return $subscriptions;
62  }
63 
64  $prefixLength = strlen(self::RELATIONSHIP_PREFIX);
65  $records = $this->getSubscriptionRecords($object->getContainerGUID());
66  foreach ($records as $record) {
67  $deliveryMethods = explode(',', $record->methods);
68  $subscriptions[$record->guid] = substr_replace($deliveryMethods, '', 0, $prefixLength);
69  }
70 
71  $params = array('event' => $event);
72  return elgg_trigger_plugin_hook('get', 'subscriptions', $params, $subscriptions);
73  }
74 
87  public function getSubscriptionsForContainer($container_guid) {
88 
89  $subscriptions = array();
90 
91  if (!$this->methods) {
92  return $subscriptions;
93  }
94 
95  $prefixLength = strlen(self::RELATIONSHIP_PREFIX);
96  $records = $this->getSubscriptionRecords($container_guid);
97  foreach ($records as $record) {
98  $deliveryMethods = explode(',', $record->methods);
99  $subscriptions[$record->guid] = substr_replace($deliveryMethods, '', 0, $prefixLength);
100  }
101 
102  return $subscriptions;
103  }
104 
115  public function addSubscription($userGuid, $method, $targetGuid) {
116  if (!in_array($method, $this->methods)) {
117  return false;
118  }
119  $prefix = self::RELATIONSHIP_PREFIX;
120  return add_entity_relationship($userGuid, "$prefix$method", $targetGuid);
121  }
122 
131  public function removeSubscription($userGuid, $method, $targetGuid) {
132  $prefix = self::RELATIONSHIP_PREFIX;
133  return remove_entity_relationship($userGuid, "$prefix$method", $targetGuid);
134  }
135 
145  protected function getSubscriptionRecords($container_guid) {
146 
147  $container_guid = $this->db->sanitizeInt($container_guid);
148 
149  // create IN clause
150  $rels = $this->getMethodRelationships();
151  if (!$rels) {
152  return array();
153  }
154  array_walk($rels, array($this->db, 'sanitizeString'));
155  $methods_string = "'" . implode("','", $rels) . "'";
156 
157  $db_prefix = $this->db->getTablePrefix();
158  $query = "SELECT guid_one AS guid, GROUP_CONCAT(relationship SEPARATOR ',') AS methods
159  FROM {$db_prefix}entity_relationships
160  WHERE guid_two = $container_guid AND
161  relationship IN ($methods_string) GROUP BY guid_one";
162  return $this->db->getData($query);
163  }
164 
170  protected function getMethodRelationships() {
171  $prefix = self::RELATIONSHIP_PREFIX;
172  $names = array();
173  foreach ($this->methods as $method) {
174  $names[] = "$prefix$method";
175  }
176  return $names;
177  }
178 }
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.
addSubscription($userGuid, $method, $targetGuid)
Subscribe a user to notifications about a target entity.
getSubscriptionRecords($container_guid)
Get subscription records from the database.
$params
Definition: login.php:72
getMethodRelationships()
Get the relationship names for notifications.
getSubscriptions(Elgg_Notifications_Event $event)
Get the subscriptions for this notification event.
getObject()
Get the object of the event.
Definition: Event.php:77
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Trigger a Plugin Hook and run all handler callbacks registered to that hook:type. ...
Definition: elgglib.php:925
getSubscriptionsForContainer($container_guid)
Get the subscriptions for the content created inside this container.
const RELATIONSHIP_PREFIX
Elgg has historically stored subscriptions as relationships with the prefix &#39;notify&#39;.
if(!$num_display) $db_prefix
Definition: content.php:12
__construct(Elgg_Database $db, array $methods=array())
Constructor.