friends.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * Elgg friends library.
  4. * Provides the UI for friends. Includes access collections since they are based
  5. * on friends relationships.
  6. *
  7. * @package Elgg.Core
  8. * @subpackage Friends
  9. */
  10. /**
  11. * Init friends library
  12. *
  13. * @access private
  14. */
  15. function _elgg_friends_init() {
  16. elgg_register_action('friends/add');
  17. elgg_register_action('friends/remove');
  18. elgg_register_action('friends/collections/add');
  19. elgg_register_action('friends/collections/delete');
  20. elgg_register_action('friends/collections/edit');
  21. elgg_register_page_handler('friends', '_elgg_friends_page_handler');
  22. elgg_register_page_handler('friendsof', '_elgg_friends_page_handler');
  23. elgg_register_page_handler('collections', '_elgg_collections_page_handler');
  24. elgg_register_widget_type('friends', elgg_echo('friends'), elgg_echo('friends:widget:description'));
  25. elgg_register_event_handler('pagesetup', 'system', '_elgg_friends_page_setup');
  26. elgg_register_event_handler('pagesetup', 'system', '_elgg_setup_collections_menu');
  27. elgg_register_plugin_hook_handler('register', 'menu:user_hover', '_elgg_friends_setup_user_hover_menu');
  28. elgg_register_event_handler('create', 'friend', '_elgg_send_friend_notification');
  29. }
  30. /**
  31. * Register some menu items for friends UI
  32. * @access private
  33. */
  34. function _elgg_friends_page_setup() {
  35. $owner = elgg_get_page_owner_entity();
  36. $viewer = elgg_get_logged_in_user_entity();
  37. if ($owner) {
  38. $params = array(
  39. 'name' => 'friends',
  40. 'text' => elgg_echo('friends'),
  41. 'href' => 'friends/' . $owner->username,
  42. 'contexts' => array('friends')
  43. );
  44. elgg_register_menu_item('page', $params);
  45. $params = array(
  46. 'name' => 'friends:of',
  47. 'text' => elgg_echo('friends:of'),
  48. 'href' => 'friendsof/' . $owner->username,
  49. 'contexts' => array('friends')
  50. );
  51. elgg_register_menu_item('page', $params);
  52. }
  53. // topbar
  54. if ($viewer) {
  55. elgg_register_menu_item('topbar', array(
  56. 'name' => 'friends',
  57. 'href' => "friends/{$viewer->username}",
  58. 'text' => elgg_view_icon('users'),
  59. 'title' => elgg_echo('friends'),
  60. 'priority' => 300,
  61. ));
  62. }
  63. }
  64. /**
  65. * Adds friending to user hover menu
  66. *
  67. * @access private
  68. */
  69. function _elgg_friends_setup_user_hover_menu($hook, $type, $return, $params) {
  70. $user = $params['entity'];
  71. /* @var \ElggUser $user */
  72. if (elgg_is_logged_in()) {
  73. if (elgg_get_logged_in_user_guid() != $user->guid) {
  74. $isFriend = $user->isFriend();
  75. // Always emit both to make it super easy to toggle with ajax
  76. $return[] = \ElggMenuItem::factory(array(
  77. 'name' => 'remove_friend',
  78. 'href' => elgg_add_action_tokens_to_url("action/friends/remove?friend={$user->guid}"),
  79. 'text' => elgg_echo('friend:remove'),
  80. 'section' => 'action',
  81. 'item_class' => $isFriend ? '' : 'hidden',
  82. ));
  83. $return[] = \ElggMenuItem::factory(array(
  84. 'name' => 'add_friend',
  85. 'href' => elgg_add_action_tokens_to_url("action/friends/add?friend={$user->guid}"),
  86. 'text' => elgg_echo('friend:add'),
  87. 'section' => 'action',
  88. 'item_class' => $isFriend ? 'hidden' : '',
  89. ));
  90. }
  91. }
  92. return $return;
  93. }
  94. /**
  95. * Page handler for friends-related pages
  96. *
  97. * @param array $segments URL segments
  98. * @param string $handler The first segment in URL used for routing
  99. *
  100. * @return bool
  101. * @access private
  102. */
  103. function _elgg_friends_page_handler($segments, $handler) {
  104. elgg_set_context('friends');
  105. if (isset($segments[0]) && $user = get_user_by_username($segments[0])) {
  106. elgg_set_page_owner_guid($user->getGUID());
  107. }
  108. if (!elgg_get_page_owner_guid()) {
  109. return false;
  110. }
  111. switch ($handler) {
  112. case 'friends':
  113. require_once(dirname(dirname(dirname(__FILE__))) . "/pages/friends/index.php");
  114. break;
  115. case 'friendsof':
  116. require_once(dirname(dirname(dirname(__FILE__))) . "/pages/friends/of.php");
  117. break;
  118. default:
  119. return false;
  120. }
  121. return true;
  122. }
  123. /**
  124. * Page handler for friends collections
  125. *
  126. * @param array $page_elements Page elements
  127. *
  128. * @return bool
  129. * @access private
  130. */
  131. function _elgg_collections_page_handler($page_elements) {
  132. elgg_set_context('friends');
  133. $base = elgg_get_config('path');
  134. if (isset($page_elements[0])) {
  135. switch ($page_elements[0]) {
  136. case 'add':
  137. elgg_set_page_owner_guid(elgg_get_logged_in_user_guid());
  138. require_once "{$base}pages/friends/collections/add.php";
  139. return true;
  140. break;
  141. case 'owner':
  142. $user = get_user_by_username($page_elements[1]);
  143. if ($user) {
  144. elgg_set_page_owner_guid($user->getGUID());
  145. require_once "{$base}pages/friends/collections/view.php";
  146. return true;
  147. }
  148. break;
  149. }
  150. }
  151. return false;
  152. }
  153. /**
  154. * Adds collection sidebar menu items
  155. *
  156. * @return void
  157. * @access private
  158. */
  159. function _elgg_setup_collections_menu() {
  160. if (elgg_get_logged_in_user_guid() == elgg_get_page_owner_guid()) {
  161. $user = elgg_get_page_owner_entity();
  162. elgg_register_menu_item('page', array(
  163. 'name' => 'friends:view:collections',
  164. 'text' => elgg_echo('friends:collections'),
  165. 'href' => "collections/owner/$user->username",
  166. 'contexts' => array('friends')
  167. ));
  168. }
  169. }
  170. /**
  171. * Notify user that someone has friended them
  172. *
  173. * @param string $event Event name
  174. * @param string $type Object type
  175. * @param \ElggRelationship $object Object
  176. *
  177. * @return bool
  178. * @access private
  179. */
  180. function _elgg_send_friend_notification($event, $type, $object) {
  181. $user_one = get_entity($object->guid_one);
  182. /* @var \ElggUser $user_one */
  183. $user_two = get_entity($object->guid_two);
  184. /* @var ElggUser $user_two */
  185. // Notification subject
  186. $subject = elgg_echo('friend:newfriend:subject', array(
  187. $user_one->name
  188. ), $user_two->language);
  189. // Notification body
  190. $body = elgg_echo("friend:newfriend:body", array(
  191. $user_one->name,
  192. $user_one->getURL()
  193. ), $user_two->language);
  194. // Notification params
  195. $params = [
  196. 'action' => 'add_friend',
  197. 'object' => $user_one,
  198. ];
  199. return notify_user($user_two->guid, $object->guid_one, $subject, $body, $params);
  200. }
  201. return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
  202. $events->registerHandler('init', 'system', '_elgg_friends_init');
  203. };