start.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Elgg Message board
  4. * This plugin allows users and groups to attach a message board to their profile for other users
  5. * to post comments.
  6. *
  7. * @package MessageBoard
  8. */
  9. /**
  10. * MessageBoard initialisation
  11. */
  12. function messageboard_init() {
  13. // js
  14. elgg_extend_view('js/elgg', 'messageboard/js');
  15. // css
  16. elgg_extend_view('css/elgg', 'messageboard/css');
  17. elgg_register_page_handler('messageboard', 'messageboard_page_handler');
  18. // messageboard widget - only for profile for now
  19. elgg_register_widget_type('messageboard', elgg_echo("messageboard:board"), elgg_echo("messageboard:desc"), array("profile"));
  20. // actions
  21. $action_path = dirname(__FILE__) . '/actions';
  22. elgg_register_action("messageboard/add", "$action_path/add.php");
  23. elgg_register_action("messageboard/delete", "$action_path/delete.php");
  24. // delete annotations for posts
  25. elgg_register_plugin_hook_handler('register', 'menu:annotation', 'messageboard_annotation_menu_setup');
  26. }
  27. /**
  28. * Messageboard dispatcher for flat message board.
  29. * Profile (and eventually group) widgets handle their own.
  30. *
  31. * URLs take the form of
  32. * User's messageboard: messageboard/owner/<username>
  33. * Y's history of posts on X's board: messageboard/owner/<X>/history/<Y>
  34. * New post: messageboard/add/<guid> (container: user or group)
  35. * Group messageboard: messageboard/group/<guid>/all (not implemented)
  36. *
  37. * @param array $page Array of page elements
  38. * @return bool
  39. */
  40. function messageboard_page_handler($page) {
  41. $pages = dirname(__FILE__) . '/pages/messageboard';
  42. switch ($page[0]) {
  43. case 'owner':
  44. //@todo if they have the widget disabled, don't allow this.
  45. $owner_name = elgg_extract(1, $page);
  46. $owner = get_user_by_username($owner_name);
  47. set_input('page_owner_guid', $owner->guid);
  48. $history = elgg_extract(2, $page);
  49. $username = elgg_extract(3, $page);
  50. if ($history && $username) {
  51. set_input('history_username', $username);
  52. }
  53. include "$pages/owner.php";
  54. break;
  55. case 'add':
  56. $container_guid = elgg_extract(1, $page);
  57. set_input('container_guid', $container_guid);
  58. include "$pages/add.php";
  59. break;
  60. case 'group':
  61. elgg_group_gatekeeper();
  62. $owner_guid = elgg_extract(1, $page);
  63. set_input('page_owner_guid', $owner_guid);
  64. include "$pages/owner.php";
  65. break;
  66. default:
  67. return false;
  68. }
  69. return true;
  70. }
  71. /**
  72. * Add messageboard post
  73. *
  74. * @param ElggUser $poster User posting the message
  75. * @param ElggUser $owner User who owns the message board
  76. * @param string $message The posted message
  77. * @param int $access_id Access level (see defines in elgglib.php)
  78. * @return bool
  79. */
  80. function messageboard_add($poster, $owner, $message, $access_id = ACCESS_PUBLIC) {
  81. $result_id = $owner->annotate('messageboard', $message, $access_id, $poster->guid);
  82. if (!$result_id) {
  83. return false;
  84. }
  85. elgg_create_river_item(array(
  86. 'view' => 'river/object/messageboard/create',
  87. 'action_type' => 'messageboard',
  88. 'subject_guid' => $poster->guid,
  89. 'object_guid' => $owner->guid,
  90. 'access_id' => $access_id,
  91. 'annotation_id' => $result_id,
  92. ));
  93. // Send notification only if poster isn't the owner
  94. if ($poster->guid != $owner->guid) {
  95. $subject = elgg_echo('messageboard:email:subject', array(), $owner->language);
  96. $body = elgg_echo('messageboard:email:body', array(
  97. $poster->name,
  98. $message,
  99. elgg_get_site_url() . "messageboard/owner/" . $owner->username,
  100. $poster->name,
  101. $poster->getURL()
  102. ), $owner->language);
  103. notify_user($owner->guid, $poster->guid, $subject, $body);
  104. }
  105. return $result_id;
  106. }
  107. /**
  108. * Add edit and delete links for forum replies
  109. */
  110. function messageboard_annotation_menu_setup($hook, $type, $return, $params) {
  111. $annotation = $params['annotation'];
  112. if ($annotation->name != 'messageboard') {
  113. return $return;
  114. }
  115. if ($annotation->canEdit()) {
  116. $url = elgg_http_add_url_query_elements('action/messageboard/delete', array(
  117. 'annotation_id' => $annotation->id,
  118. ));
  119. $options = array(
  120. 'name' => 'delete',
  121. 'href' => $url,
  122. 'text' => elgg_view_icon('delete'),
  123. 'confirm' => elgg_echo('deleteconfirm'),
  124. 'encode_text' => false
  125. );
  126. $return[] = ElggMenuItem::factory($options);
  127. }
  128. return $return;
  129. }
  130. elgg_register_event_handler('init', 'system', 'messageboard_init');