start.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Notification tools
  4. */
  5. elgg_register_event_handler('init', 'system', 'notification_tools_init');
  6. /**
  7. * Initialize the plugin
  8. */
  9. function notification_tools_init () {
  10. $plugin_dir = elgg_get_plugins_path() . 'notification_tools/';
  11. elgg_register_action('notification_tools/enable_personal', "{$plugin_dir}actions/notification_tools/enable_personal.php", 'admin');
  12. elgg_register_action('notification_tools/enable_collection', "{$plugin_dir}actions/notification_tools/enable_collection.php", 'admin');
  13. elgg_register_action('notification_tools/enable_group', "{$plugin_dir}actions/notification_tools/enable_group.php", 'admin');
  14. // This overrides the default save action
  15. elgg_register_action('notification_tools/settings/save', "{$plugin_dir}actions/notification_tools/settings/save.php", 'admin');
  16. elgg_register_admin_menu_item('administer', 'notification_tools', 'administer_utilities');
  17. elgg_register_event_handler('join', 'group', 'notification_tools_enable_for_new_group_member');
  18. elgg_register_event_handler('create', 'user', 'notification_tools_enable_for_new_user');
  19. }
  20. /**
  21. * Enable personal and friend notifications for new users
  22. *
  23. * We do this using 'create, user' event instead of 'register, user' plugin
  24. * hook so that it affects also users created by an admin.
  25. *
  26. * @param string $event 'create'
  27. * @param string $type 'user'
  28. * @param ElggUser $user
  29. * @return boolean
  30. */
  31. function notification_tools_enable_for_new_user ($event, $type, $user) {
  32. $personal = elgg_get_plugin_setting('default_personal_methods', 'notification_tools');
  33. // Set methods for personal notifications
  34. if ($personal) {
  35. $personal_methods = explode(',', $personal);
  36. foreach ($personal_methods as $method) {
  37. $prefix = "notification:method:{$method}";
  38. $user->$prefix = true;
  39. }
  40. }
  41. $collection = elgg_get_plugin_setting('default_collection_methods', 'notification_tools');
  42. // Set methods for notifications about friends' activity
  43. if ($collection) {
  44. $collection_methods = explode(',', $collection);
  45. // Here we just mark the default methods. The core notification plugin
  46. // will take care of creating the actual 'notify<method>' relationships
  47. // between user and each friends.
  48. foreach ($collection_methods as $method) {
  49. $setting_name = "collections_notifications_preferences_{$method}";
  50. // The -1 seems like a weird value but that's what the core
  51. // is using for whatever reason.
  52. $user->$setting_name = '-1';
  53. }
  54. }
  55. $user->save();
  56. return true;
  57. }
  58. /**
  59. * Enable default notification methods when user joins a group
  60. *
  61. * @param string $event 'join'
  62. * @param string $type 'group'
  63. * @param array $params Array containing ElggUser and ElggGroup
  64. */
  65. function notification_tools_enable_for_new_group_member($event, $type, $params) {
  66. $group = $params['group'];
  67. $user = $params['user'];
  68. $methods = elgg_get_plugin_setting('default_group_methods', 'notification_tools');
  69. if (empty($methods)) {
  70. return true;
  71. }
  72. if (!$group instanceof ElggGroup) {
  73. return true;
  74. }
  75. if (!$user instanceof ElggUser) {
  76. return true;
  77. }
  78. $methods = explode(',', $methods);
  79. foreach ($methods as $method) {
  80. add_entity_relationship($user->guid, "notify{$method}", $group->guid);
  81. }
  82. return true;
  83. }