start.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Elgg invite friends
  4. *
  5. * @package ElggInviteFriends
  6. */
  7. elgg_register_event_handler('init', 'system', 'invitefriends_init');
  8. function invitefriends_init() {
  9. elgg_register_page_handler('invite', 'invitefriends_page_handler');
  10. elgg_register_action('invitefriends/invite', elgg_get_plugins_path() . 'invitefriends/actions/invite.php');
  11. elgg_register_plugin_hook_handler('register', 'user', 'invitefriends_add_friends');
  12. if (elgg_is_logged_in() && elgg_get_config('allow_registration')) {
  13. $params = array(
  14. 'name' => 'invite',
  15. 'text' => elgg_echo('friends:invite'),
  16. 'href' => "invite",
  17. 'contexts' => array('friends'),
  18. );
  19. elgg_register_menu_item('page', $params);
  20. }
  21. }
  22. /**
  23. * Page handler function
  24. *
  25. * @param array $page Page URL segments
  26. * @return bool
  27. */
  28. function invitefriends_page_handler($page) {
  29. elgg_gatekeeper();
  30. if (!elgg_get_config('allow_registration')) {
  31. return false;
  32. }
  33. elgg_set_context('friends');
  34. elgg_set_page_owner_guid(elgg_get_logged_in_user_guid());
  35. $title = elgg_echo('friends:invite');
  36. $body = elgg_view('invitefriends/form');
  37. $params = array(
  38. 'content' => $body,
  39. 'title' => $title,
  40. );
  41. $body = elgg_view_layout('one_sidebar', $params);
  42. echo elgg_view_page($title, $body);
  43. return true;
  44. }
  45. /**
  46. * Add friends if invite code was set
  47. *
  48. * @param string $hook Hook name
  49. * @param string $type Hook type
  50. * @param bool $result Whether to allow registration
  51. * @param array $params Hook params
  52. * @return void
  53. */
  54. function invitefriends_add_friends($hook, $type, $result, $params) {
  55. $user = $params['user'];
  56. /* @var ElggUser $user */
  57. $friend_guid = $params['friend_guid'];
  58. $invite_code = $params['invitecode'];
  59. // If $friend_guid has been set, make mutual friends
  60. if ($friend_guid) {
  61. if ($friend_user = get_user($friend_guid)) {
  62. if (elgg_validate_invite_code($friend_user->username, $invite_code)) {
  63. $user->addFriend($friend_guid, true);
  64. $friend_user->addFriend($user->guid, true);
  65. }
  66. }
  67. }
  68. }