start.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * Elgg notifications plugin
  4. *
  5. * @package ElggNotifications
  6. */
  7. elgg_register_event_handler('init', 'system', 'notifications_plugin_init');
  8. function notifications_plugin_init() {
  9. elgg_extend_view('css/elgg','notifications/css');
  10. elgg_register_page_handler('notifications', 'notifications_page_handler');
  11. elgg_register_event_handler('pagesetup', 'system', 'notifications_plugin_pagesetup');
  12. // Unset the default notification settings
  13. elgg_unregister_plugin_hook_handler('usersettings:save', 'user', '_elgg_save_notification_user_settings');
  14. elgg_unextend_view('forms/account/settings', 'core/settings/account/notifications');
  15. // update notifications based on relationships changing
  16. elgg_register_event_handler('delete', 'member', 'notifications_relationship_remove');
  17. elgg_register_event_handler('delete', 'friend', 'notifications_relationship_remove');
  18. // update notifications when new friend or access collection membership
  19. elgg_register_event_handler('create', 'friend', 'notifications_update_friend_notify');
  20. elgg_register_plugin_hook_handler('access:collections:add_user', 'collection', 'notifications_update_collection_notify');
  21. $actions_base = elgg_get_plugins_path() . 'notifications/actions';
  22. elgg_register_action("notificationsettings/save", "$actions_base/save.php");
  23. elgg_register_action("notificationsettings/groupsave", "$actions_base/groupsave.php");
  24. }
  25. /**
  26. * Route page requests
  27. *
  28. * @param array $page Array of url parameters
  29. * @return bool
  30. */
  31. function notifications_page_handler($page) {
  32. elgg_gatekeeper();
  33. $current_user = elgg_get_logged_in_user_entity();
  34. // default to personal notifications
  35. if (!isset($page[0])) {
  36. $page[0] = 'personal';
  37. }
  38. if (!isset($page[1])) {
  39. forward("notifications/{$page[0]}/{$current_user->username}");
  40. }
  41. $user = get_user_by_username($page[1]);
  42. if (($user->guid != $current_user->guid) && !$current_user->isAdmin()) {
  43. forward();
  44. }
  45. $base = elgg_get_plugins_path() . 'notifications';
  46. // note: $user passed in
  47. switch ($page[0]) {
  48. case 'group':
  49. require "$base/groups.php";
  50. break;
  51. case 'personal':
  52. require "$base/index.php";
  53. break;
  54. default:
  55. return false;
  56. }
  57. return true;
  58. }
  59. /**
  60. * Notification settings sidebar menu
  61. *
  62. */
  63. function notifications_plugin_pagesetup() {
  64. if (elgg_in_context("settings") && elgg_get_logged_in_user_guid()) {
  65. $user = elgg_get_page_owner_entity();
  66. if (!$user) {
  67. $user = elgg_get_logged_in_user_entity();
  68. }
  69. $params = array(
  70. 'name' => '2_a_user_notify',
  71. 'text' => elgg_echo('notifications:subscriptions:changesettings'),
  72. 'href' => "notifications/personal/{$user->username}",
  73. 'section' => "notifications",
  74. );
  75. elgg_register_menu_item('page', $params);
  76. if (elgg_is_active_plugin('groups')) {
  77. $params = array(
  78. 'name' => '2_group_notify',
  79. 'text' => elgg_echo('notifications:subscriptions:changesettings:groups'),
  80. 'href' => "notifications/group/{$user->username}",
  81. 'section' => "notifications",
  82. );
  83. elgg_register_menu_item('page', $params);
  84. }
  85. }
  86. }
  87. /**
  88. * Update notifications when a relationship is deleted
  89. *
  90. * @param string $event
  91. * @param string $object_type
  92. * @param object $relationship
  93. */
  94. function notifications_relationship_remove($event, $object_type, $relationship) {
  95. $NOTIFICATION_HANDLERS = _elgg_services()->notifications->getMethodsAsDeprecatedGlobal();
  96. $user_guid = $relationship->guid_one;
  97. $object_guid = $relationship->guid_two;
  98. // loop through all notification types
  99. foreach($NOTIFICATION_HANDLERS as $method => $foo) {
  100. remove_entity_relationship($user_guid, "notify{$method}", $object_guid);
  101. }
  102. }
  103. /**
  104. * Turn on notifications for new friends if all friend notifications is on
  105. *
  106. * @param string $event
  107. * @param string $object_type
  108. * @param object $relationship
  109. */
  110. function notifications_update_friend_notify($event, $object_type, $relationship) {
  111. $NOTIFICATION_HANDLERS = _elgg_services()->notifications->getMethodsAsDeprecatedGlobal();
  112. $user_guid = $relationship->guid_one;
  113. $friend_guid = $relationship->guid_two;
  114. $user = get_entity($user_guid);
  115. // loop through all notification types
  116. foreach ($NOTIFICATION_HANDLERS as $method => $foo) {
  117. $metaname = 'collections_notifications_preferences_' . $method;
  118. $collections_preferences = $user->$metaname;
  119. if ($collections_preferences) {
  120. if (!empty($collections_preferences) && !is_array($collections_preferences)) {
  121. $collections_preferences = array($collections_preferences);
  122. }
  123. if (is_array($collections_preferences)) {
  124. // -1 means all friends is on - should be a define
  125. if (in_array(-1, $collections_preferences)) {
  126. add_entity_relationship($user_guid, 'notify' . $method, $friend_guid);
  127. }
  128. }
  129. }
  130. }
  131. }
  132. /**
  133. * Update notifications for changes in access collection membership.
  134. *
  135. * This function assumes that only friends can belong to access collections.
  136. *
  137. * @param string $event
  138. * @param string $object_type
  139. * @param bool $returnvalue
  140. * @param array $params
  141. */
  142. function notifications_update_collection_notify($event, $object_type, $returnvalue, $params) {
  143. $NOTIFICATION_HANDLERS = _elgg_services()->notifications->getMethodsAsDeprecatedGlobal();
  144. // only update notifications for user owned collections
  145. $collection_id = $params['collection_id'];
  146. $collection = get_access_collection($collection_id);
  147. $user = get_entity($collection->owner_guid);
  148. if (!($user instanceof ElggUser)) {
  149. return $returnvalue;
  150. }
  151. $member_guid = $params['user_guid'];
  152. // loop through all notification types
  153. foreach ($NOTIFICATION_HANDLERS as $method => $foo) {
  154. $metaname = 'collections_notifications_preferences_' . $method;
  155. $collections_preferences = $user->$metaname;
  156. if (!$collections_preferences) {
  157. continue;
  158. }
  159. if (!is_array($collections_preferences)) {
  160. $collections_preferences = array($collections_preferences);
  161. }
  162. if (in_array(-1, $collections_preferences)) {
  163. // if "all friends" notify is on, we don't change any notifications
  164. // since must be a friend to be in an access collection
  165. continue;
  166. }
  167. if (in_array($collection_id, $collections_preferences)) {
  168. // notifications are on for this collection so we add/remove
  169. if ($event == 'access:collections:add_user') {
  170. add_entity_relationship($user->guid, "notify$method", $member_guid);
  171. } elseif ($event == 'access:collections:remove_user') {
  172. // removing someone from an access collection is not a guarantee
  173. // that they should be removed from notifications
  174. //remove_entity_relationship($user->guid, "notify$method", $member_guid);
  175. }
  176. }
  177. }
  178. }