start.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * Elgg Bookmarks plugin
  4. *
  5. * @package ElggBookmarks
  6. */
  7. elgg_register_event_handler('init', 'system', 'bookmarks_init');
  8. /**
  9. * Bookmark init
  10. */
  11. function bookmarks_init() {
  12. $root = dirname(__FILE__);
  13. elgg_register_library('elgg:bookmarks', "$root/lib/bookmarks.php");
  14. // actions
  15. $action_path = "$root/actions/bookmarks";
  16. elgg_register_action('bookmarks/save', "$action_path/save.php");
  17. elgg_register_action('bookmarks/delete', "$action_path/delete.php");
  18. elgg_register_action('bookmarks/share', "$action_path/share.php");
  19. // menus
  20. elgg_register_menu_item('site', array(
  21. 'name' => 'bookmarks',
  22. 'text' => elgg_echo('bookmarks'),
  23. 'href' => 'bookmarks/all'
  24. ));
  25. elgg_register_plugin_hook_handler('register', 'menu:page', 'bookmarks_page_menu');
  26. elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'bookmarks_owner_block_menu');
  27. elgg_register_page_handler('bookmarks', 'bookmarks_page_handler');
  28. elgg_extend_view('css/elgg', 'bookmarks/css');
  29. elgg_extend_view('js/elgg', 'bookmarks/js');
  30. elgg_register_widget_type('bookmarks', elgg_echo('bookmarks'), elgg_echo('bookmarks:widget:description'));
  31. if (elgg_is_logged_in()) {
  32. $user_guid = elgg_get_logged_in_user_guid();
  33. $address = urlencode(current_page_url());
  34. elgg_register_menu_item('extras', array(
  35. 'name' => 'bookmark',
  36. 'text' => elgg_view_icon('push-pin-alt'),
  37. 'href' => "bookmarks/add/$user_guid?address=$address",
  38. 'title' => elgg_echo('bookmarks:this'),
  39. 'rel' => 'nofollow',
  40. ));
  41. }
  42. // Register for notifications
  43. elgg_register_notification_event('object', 'bookmarks', array('create'));
  44. elgg_register_plugin_hook_handler('prepare', 'notification:create:object:bookmarks', 'bookmarks_prepare_notification');
  45. // Register bookmarks view for ecml parsing
  46. elgg_register_plugin_hook_handler('get_views', 'ecml', 'bookmarks_ecml_views_hook');
  47. // Register a URL handler for bookmarks
  48. elgg_register_plugin_hook_handler('entity:url', 'object', 'bookmark_set_url');
  49. // Register entity type for search
  50. elgg_register_entity_type('object', 'bookmarks');
  51. // Groups
  52. add_group_tool_option('bookmarks', elgg_echo('bookmarks:enablebookmarks'), true);
  53. elgg_extend_view('groups/tool_latest', 'bookmarks/group_module');
  54. }
  55. /**
  56. * Dispatcher for bookmarks.
  57. *
  58. * URLs take the form of
  59. * All bookmarks: bookmarks/all
  60. * User's bookmarks: bookmarks/owner/<username>
  61. * Friends' bookmarks: bookmarks/friends/<username>
  62. * View bookmark: bookmarks/view/<guid>/<title>
  63. * New bookmark: bookmarks/add/<guid> (container: user, group, parent)
  64. * Edit bookmark: bookmarks/edit/<guid>
  65. * Group bookmarks: bookmarks/group/<guid>/all
  66. * Bookmarklet: bookmarks/bookmarklet/<guid> (user)
  67. *
  68. * Title is ignored
  69. *
  70. * @param array $page
  71. * @return bool
  72. */
  73. function bookmarks_page_handler($page) {
  74. elgg_load_library('elgg:bookmarks');
  75. if (!isset($page[0])) {
  76. $page[0] = 'all';
  77. }
  78. elgg_push_breadcrumb(elgg_echo('bookmarks'), 'bookmarks/all');
  79. $pages = dirname(__FILE__) . '/pages/bookmarks';
  80. switch ($page[0]) {
  81. case "all":
  82. include "$pages/all.php";
  83. break;
  84. case "owner":
  85. include "$pages/owner.php";
  86. break;
  87. case "friends":
  88. include "$pages/friends.php";
  89. break;
  90. case "view":
  91. set_input('guid', $page[1]);
  92. include "$pages/view.php";
  93. break;
  94. case "add":
  95. elgg_gatekeeper();
  96. include "$pages/add.php";
  97. break;
  98. case "edit":
  99. elgg_gatekeeper();
  100. set_input('guid', $page[1]);
  101. include "$pages/edit.php";
  102. break;
  103. case 'group':
  104. elgg_group_gatekeeper();
  105. include "$pages/owner.php";
  106. break;
  107. case "bookmarklet":
  108. set_input('container_guid', $page[1]);
  109. include "$pages/bookmarklet.php";
  110. break;
  111. default:
  112. return false;
  113. }
  114. elgg_pop_context();
  115. return true;
  116. }
  117. /**
  118. * Populates the ->getUrl() method for bookmarked objects
  119. *
  120. * @param string $hook
  121. * @param string $type
  122. * @param string $url
  123. * @param array $params
  124. * @return string bookmarked item URL
  125. */
  126. function bookmark_set_url($hook, $type, $url, $params) {
  127. $entity = $params['entity'];
  128. if (elgg_instanceof($entity, 'object', 'bookmarks')) {
  129. $title = elgg_get_friendly_title($entity->title);
  130. return "bookmarks/view/" . $entity->getGUID() . "/" . $title;
  131. }
  132. }
  133. /**
  134. * Add a menu item to an ownerblock
  135. *
  136. * @param string $hook
  137. * @param string $type
  138. * @param array $return
  139. * @param array $params
  140. */
  141. function bookmarks_owner_block_menu($hook, $type, $return, $params) {
  142. if (elgg_instanceof($params['entity'], 'user')) {
  143. $url = "bookmarks/owner/{$params['entity']->username}";
  144. $item = new ElggMenuItem('bookmarks', elgg_echo('bookmarks'), $url);
  145. $return[] = $item;
  146. } else {
  147. if ($params['entity']->bookmarks_enable != 'no') {
  148. $url = "bookmarks/group/{$params['entity']->guid}/all";
  149. $item = new ElggMenuItem('bookmarks', elgg_echo('bookmarks:group'), $url);
  150. $return[] = $item;
  151. }
  152. }
  153. return $return;
  154. }
  155. /**
  156. * Prepare a notification message about a new bookmark
  157. *
  158. * @param string $hook Hook name
  159. * @param string $type Hook type
  160. * @param Elgg\Notifications\Notification $notification The notification to prepare
  161. * @param array $params Hook parameters
  162. * @return Elgg\Notifications\Notification
  163. */
  164. function bookmarks_prepare_notification($hook, $type, $notification, $params) {
  165. $entity = $params['event']->getObject();
  166. $owner = $params['event']->getActor();
  167. $recipient = $params['recipient'];
  168. $language = $params['language'];
  169. $method = $params['method'];
  170. $descr = $entity->description;
  171. $title = $entity->title;
  172. $notification->subject = elgg_echo('bookmarks:notify:subject', array($title), $language);
  173. $notification->body = elgg_echo('bookmarks:notify:body', array(
  174. $owner->name,
  175. $title,
  176. $entity->address,
  177. $descr,
  178. $entity->getURL()
  179. ), $language);
  180. $notification->summary = elgg_echo('bookmarks:notify:summary', array($entity->title), $language);
  181. return $notification;
  182. }
  183. /**
  184. * Add a page menu menu.
  185. *
  186. * @param string $hook
  187. * @param string $type
  188. * @param array $return
  189. * @param array $params
  190. */
  191. function bookmarks_page_menu($hook, $type, $return, $params) {
  192. if (elgg_is_logged_in()) {
  193. // only show bookmarklet in bookmark pages
  194. if (elgg_in_context('bookmarks')) {
  195. $page_owner = elgg_get_page_owner_entity();
  196. if (!$page_owner) {
  197. $page_owner = elgg_get_logged_in_user_entity();
  198. }
  199. if ($page_owner instanceof ElggGroup) {
  200. $title = elgg_echo('bookmarks:bookmarklet:group');
  201. } else {
  202. $title = elgg_echo('bookmarks:bookmarklet');
  203. }
  204. $return[] = new ElggMenuItem('bookmarklet', $title, 'bookmarks/bookmarklet/' . $page_owner->getGUID());
  205. }
  206. }
  207. return $return;
  208. }
  209. /**
  210. * Return bookmarks views to parse for ecml
  211. *
  212. * @param string $hook
  213. * @param string $type
  214. * @param array $return
  215. * @param array $params
  216. */
  217. function bookmarks_ecml_views_hook($hook, $type, $return, $params) {
  218. $return['object/bookmarks'] = elgg_echo('item:object:bookmarks');
  219. return $return;
  220. }