hooks.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * All plugin hook callback functions are bundled in this file
  4. */
  5. /**
  6. * Add a subscribe/unsubscribe link to the supported entity types
  7. *
  8. * @param string $hook "register"
  9. * @param string $type "menu:entity"
  10. * @param ElggMenuItem[] $return_value the current menu items
  11. * @param array $params supplied params
  12. *
  13. * @return ElggMenuItem[]
  14. */
  15. function content_subscriptions_register_entity_menu_hook($hook, $type, $return_value, $params) {
  16. if (!elgg_is_logged_in()) {
  17. return $return_value;
  18. }
  19. if (empty($params) || !is_array($params)) {
  20. return $return_value;
  21. }
  22. $entity = elgg_extract("entity", $params);
  23. if (empty($entity) || !content_subscriptions_can_subscribe($entity)) {
  24. return $return_value;
  25. }
  26. $subscribed = false;
  27. if (content_subscriptions_check_subscription($entity->getGUID())) {
  28. $subscribed = true;
  29. }
  30. $methods = content_subscriptions_get_notification_settings();
  31. if (!empty($methods)) {
  32. $return_value[] = ElggMenuItem::factory(array(
  33. "name" => "content_subscription_subscribe",
  34. "text" => elgg_echo("content_subscriptions:subscribe"),
  35. "href" => "action/content_subscriptions/subscribe?entity_guid=" . $entity->getGUID(),
  36. "is_action" => true,
  37. "priority" => 100,
  38. "item_class" => $subscribed ? "hidden" : ""
  39. ));
  40. }
  41. $return_value[] = ElggMenuItem::factory(array(
  42. "name" => "content_subscription_unsubscribe",
  43. "text" => elgg_echo("content_subscriptions:unsubscribe"),
  44. "href" => "action/content_subscriptions/subscribe?entity_guid=" . $entity->getGUID(),
  45. "is_action" => true,
  46. "priority" => 101,
  47. "item_class" => $subscribed ? "" : "hidden"
  48. ));
  49. return $return_value;
  50. }
  51. /**
  52. * Change the default notification message for comments
  53. *
  54. * @param string $hook the name of the hook
  55. * @param stirng $type the type of the hook
  56. * @param Elgg_Notifications_Notification $return_value the current return value
  57. * @param array $params supplied values
  58. *
  59. * @return Elgg_Notifications_Notification
  60. */
  61. function content_subscriptions_prepare_comment_notification($hook, $type, $return_value, $params) {
  62. if (empty($return_value) || !($return_value instanceof \Elgg\Notifications\Notification)) {
  63. return $return_value;
  64. }
  65. if (empty($params) || !is_array($params)) {
  66. return $return_value;
  67. }
  68. $event = elgg_extract("event", $params);
  69. if (empty($event) || !($event instanceof \Elgg\Notifications\Event)) {
  70. return $return_value;
  71. }
  72. // ignore access for now
  73. $ia = elgg_set_ignore_access(true);
  74. $comment = $event->getObject();
  75. $actor = $event->getActor();
  76. $object = $comment->getContainerEntity();
  77. $language = elgg_extract("language", $params, get_current_language());
  78. $recipient = elgg_extract("recipient", $params);
  79. $return_value->subject = elgg_echo("content_subscriptions:create:comment:subject", array($object->title), $language);
  80. $return_value->body = elgg_echo("content_subscriptions:create:comment:message", array(
  81. $recipient->name,
  82. $actor->name,
  83. $object->title,
  84. $comment->description,
  85. $object->getURL(),
  86. ), $language);
  87. $return_value->summary = elgg_echo("content_subscriptions:create:comment:summary", array($object->title), $language);
  88. // restore access
  89. elgg_set_ignore_access($ia);
  90. return $return_value;
  91. }
  92. /**
  93. * Verify that the subscribed users still have their preferences
  94. *
  95. * @param string $hook the name of the hook
  96. * @param stirng $type the type of the hook
  97. * @param array $return_value the current return value
  98. * @param array $params supplied values
  99. *
  100. * @return array
  101. */
  102. function content_subscriptions_get_subscriptions_verify_hook($hook, $type, $return_value, $params) {
  103. if (empty($params) || !is_array($params)) {
  104. return $return_value;
  105. }
  106. if (empty($return_value)) {
  107. // no subscribers to check
  108. return $return_value;
  109. }
  110. $event = elgg_extract("event", $params);
  111. if (empty($event) || !($event instanceof \Elgg\Notifications\Event)) {
  112. return $return_value;
  113. }
  114. $object = $event->getObject();
  115. if (empty($object) || (!elgg_instanceof($object, "object", "discussion_reply") && !elgg_instanceof($object, "object", "comment"))) {
  116. return $return_value;
  117. }
  118. foreach ($return_value as $user_guid => $preferences) {
  119. $settings = content_subscriptions_get_notification_settings($user_guid);
  120. if (!empty($settings)) {
  121. $return_value[$user_guid] = $settings;
  122. } else {
  123. unset($return_value[$user_guid]);
  124. }
  125. }
  126. return $return_value;
  127. }
  128. /**
  129. * Make sure unsubscribed users don't get notifications based on their group-subscriptions
  130. *
  131. * @param string $hook the name of the hook
  132. * @param stirng $type the type of the hook
  133. * @param array $return_value the current return value
  134. * @param array $params supplied values
  135. *
  136. * @return array
  137. */
  138. function content_subscriptions_get_subscriptions_group_check_hook($hook, $type, $return_value, $params) {
  139. if (empty($params) || !is_array($params)) {
  140. return $return_value;
  141. }
  142. if (empty($return_value)) {
  143. // no subscribers to check
  144. return $return_value;
  145. }
  146. $event = elgg_extract("event", $params);
  147. if (empty($event) || !($event instanceof \Elgg\Notifications\Event)) {
  148. return $return_value;
  149. }
  150. $object = $event->getObject();
  151. if (empty($object) || (!elgg_instanceof($object, "object", "discussion_reply") && !elgg_instanceof($object, "object", "comment"))) {
  152. return $return_value;
  153. }
  154. $options = array(
  155. "type" => "user",
  156. "limit" => false,
  157. "relationship" => CONTENT_SUBSCRIPTIONS_BLOCK,
  158. "relationship_guid" => $object->getContainerGUID(),
  159. "inverse_relationship" => true
  160. );
  161. $batch = new ElggBatch("elgg_get_entities_from_relationship", $options);
  162. foreach ($batch as $user) {
  163. unset($return_value[$user->getGUID()]);
  164. }
  165. return $return_value;
  166. }
  167. /**
  168. * Save the content subscriptions preferences for the user
  169. *
  170. * @param string $hook the name of the hook
  171. * @param stirng $type the type of the hook
  172. * @param array $return_value the current return value
  173. * @param array $params supplied values
  174. *
  175. * @return void
  176. */
  177. function content_subscriptions_notifications_settings_save_hook($hook, $type, $return_value, $params) {
  178. $NOTIFICATION_HANDLERS = _elgg_services()->notifications->getMethods();
  179. if (empty($NOTIFICATION_HANDLERS) || !is_array($NOTIFICATION_HANDLERS)) {
  180. return;
  181. }
  182. $user_guid = (int) get_input("guid");
  183. if (empty($user_guid)) {
  184. return;
  185. }
  186. $user = get_user($user_guid);
  187. if (empty($user) || !$user->canEdit()) {
  188. return;
  189. }
  190. $methods = array();
  191. foreach ($NOTIFICATION_HANDLERS as $method) {
  192. $setting = get_input("content_subscriptions_" . $method);
  193. if (!empty($setting)) {
  194. $methods[] = $method;
  195. }
  196. }
  197. if (!empty($methods)) {
  198. elgg_set_plugin_user_setting("notification_settings", implode(",", $methods), $user->getGUID(), "content_subscriptions");
  199. } else {
  200. elgg_unset_plugin_user_setting("notification_settings", $user->getGUID(), "content_subscriptions");
  201. }
  202. // set flag for correct fallback behaviour
  203. elgg_set_plugin_user_setting("notification_settings_saved", "1", $user->getGUID(), "content_subscriptions");
  204. }