start.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Site notifications
  4. *
  5. * @todo check for notifications when setting topbar icon
  6. * @todo add a remove visible and all notifications button
  7. */
  8. elgg_register_event_handler('init', 'system', 'site_notifications_init');
  9. function site_notifications_init() {
  10. // register as a notification type
  11. elgg_register_notification_method('site');
  12. elgg_register_plugin_hook_handler('send', 'notification:site', 'site_notifications_send');
  13. elgg_register_page_handler('site_notifications', 'site_notifications_page_handler');
  14. elgg_extend_view('css/elgg', 'site_notifications/css');
  15. $js = elgg_get_simplecache_url('js', 'site_notifications');
  16. elgg_register_js('elgg.site_notifications', $js, 'footer');
  17. site_notifications_set_topbar();
  18. $actions_base = elgg_get_plugins_path() . 'site_notifications/actions/site_notifications';
  19. elgg_register_action('site_notifications/delete', "$actions_base/delete.php");
  20. elgg_register_action('site_notifications/process', "$actions_base/process.php");
  21. }
  22. /**
  23. * Page handler
  24. *
  25. * /site_notifications/view/<username>
  26. *
  27. * @param array $segments URL segments
  28. * @return boolean
  29. */
  30. function site_notifications_page_handler($segments) {
  31. $base = elgg_get_plugins_path() . 'site_notifications/pages/site_notifications';
  32. elgg_gatekeeper();
  33. if (!isset($segments[1])) {
  34. $segments[1] = elgg_get_logged_in_user_entity()->username;
  35. }
  36. $user = get_user_by_username($segments[1]);
  37. if (!$user) {
  38. return false;
  39. }
  40. elgg_set_page_owner_guid($user->guid);
  41. elgg_load_js('elgg.site_notifications');
  42. require "$base/view.php";
  43. return true;
  44. }
  45. /**
  46. * Sets the topbar notify icon and text
  47. */
  48. function site_notifications_set_topbar() {
  49. if (elgg_is_logged_in()) {
  50. elgg_register_menu_item('topbar', array(
  51. 'name' => 'site_notifications',
  52. 'href' => 'site_notifications/view/' . elgg_get_logged_in_user_entity()->username,
  53. 'text' => elgg_view_icon('info') . elgg_echo('site_notifications:topbar'),
  54. 'priority' => 150,
  55. 'section' => 'alt',
  56. ));
  57. }
  58. }
  59. /**
  60. * Create a site notification
  61. *
  62. * @param string $hook Hook name
  63. * @param string $type Hook type
  64. * @param bool $result Has the notification been sent
  65. * @param array $params Hook parameters
  66. */
  67. function site_notifications_send($hook, $type, $result, $params) {
  68. /* @var Elgg\Notifications\Notification */
  69. $notification = $params['notification'];
  70. if ($notification->summary) {
  71. $message = $notification->summary;
  72. } else {
  73. $message = $notification->subject;
  74. }
  75. if (isset($params['event'])) {
  76. $event = $params['event'];
  77. $object = $event->getObject();
  78. } else {
  79. $object = null;
  80. }
  81. $actor = $notification->getSender();
  82. $recipient = $notification->getRecipient();
  83. $ia = elgg_set_ignore_access(true);
  84. $note = SiteNotificationFactory::create($recipient, $message, $actor, $object);
  85. elgg_set_ignore_access($ia);
  86. if ($note) {
  87. return true;
  88. }
  89. }