Subscriptions.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace ColdTrick\ContentSubscriptions;
  3. class Subscriptions {
  4. /**
  5. * Add a discussion owner to the notified users
  6. *
  7. * @param string $hook the name of the hook
  8. * @param stirng $type the type of the hook
  9. * @param array $return_value the current return value
  10. * @param array $params supplied values
  11. *
  12. * @return void|array
  13. */
  14. public static function addDiscussionOwner($hook, $type, $return_value, $params) {
  15. if (empty($params) || !is_array($params)) {
  16. return;
  17. }
  18. $event = elgg_extract('event', $params);
  19. if (!($event instanceof \Elgg\Notifications\Event)) {
  20. return;
  21. }
  22. $discussion_reply = $event->getObject();
  23. if (!($discussion_reply instanceof \ElggDiscussionReply)) {
  24. return;
  25. }
  26. $discussion = $discussion_reply->getContainerEntity();
  27. if (!elgg_instanceof($discussion, 'object', 'groupforumtopic')) {
  28. return;
  29. }
  30. $owner = $discussion->getOwnerEntity();
  31. if (!($owner instanceof \ElggUser)) {
  32. return;
  33. }
  34. $user_notification_settings = get_user_notification_settings($owner->getGUID());
  35. if (empty($user_notification_settings)) {
  36. // user has no settings, so no notification
  37. return;
  38. }
  39. $temp = [];
  40. foreach ($user_notification_settings as $method => $enabled) {
  41. if (empty($enabled)) {
  42. // notification method not enabled
  43. continue;
  44. }
  45. $temp[] = $method;
  46. }
  47. if (empty($temp)) {
  48. // no enabled notification methods
  49. return;
  50. }
  51. $return_value[$owner->getGUID()] = $temp;
  52. return $return_value;
  53. }
  54. /**
  55. * Make sure unsubscribed users don't get notifications based on their group-subscriptions
  56. *
  57. * @param string $hook the name of the hook
  58. * @param stirng $type the type of the hook
  59. * @param array $return_value the current return value
  60. * @param array $params supplied values
  61. *
  62. * @return void|array
  63. */
  64. public static function removeUnsubscribedGroupMembers($hook, $type, $return_value, $params) {
  65. if (empty($params) || !is_array($params)) {
  66. return;
  67. }
  68. if (empty($return_value)) {
  69. // no subscribers to check
  70. return;
  71. }
  72. $event = elgg_extract('event', $params);
  73. if (!($event instanceof \Elgg\Notifications\Event)) {
  74. return;
  75. }
  76. $object = $event->getObject();
  77. if (!($object instanceof \ElggComment)) {
  78. return;
  79. }
  80. $options = [
  81. 'type' => 'user',
  82. 'limit' => false,
  83. 'relationship' => CONTENT_SUBSCRIPTIONS_BLOCK,
  84. 'relationship_guid' => $object->getContainerGUID(),
  85. 'inverse_relationship' => true,
  86. ];
  87. $batch = new \ElggBatch('elgg_get_entities_from_relationship', $options);
  88. foreach ($batch as $user) {
  89. if (!isset($return_value[$user->getGUID()])) {
  90. continue;
  91. }
  92. unset($return_value[$user->getGUID()]);
  93. }
  94. return $return_value;
  95. }
  96. /**
  97. * Verify that the subscribed users still have their preferences
  98. *
  99. * @param string $hook the name of the hook
  100. * @param stirng $type the type of the hook
  101. * @param array $return_value the current return value
  102. * @param array $params supplied values
  103. *
  104. * @return void|array
  105. */
  106. public static function verifySubscribersSettings($hook, $type, $return_value, $params) {
  107. if (empty($params) || !is_array($params)) {
  108. return;
  109. }
  110. if (empty($return_value)) {
  111. // no subscribers to check
  112. return;
  113. }
  114. $event = elgg_extract("event", $params);
  115. if (!($event instanceof \Elgg\Notifications\Event)) {
  116. return;
  117. }
  118. $object = $event->getObject();
  119. if (!($object instanceof \ElggComment)) {
  120. return;
  121. }
  122. foreach ($return_value as $user_guid => $preferences) {
  123. $settings = content_subscriptions_get_notification_settings($user_guid);
  124. if (!empty($settings)) {
  125. $return_value[$user_guid] = $settings;
  126. } else {
  127. unset($return_value[$user_guid]);
  128. }
  129. }
  130. return $return_value;
  131. }
  132. }