add.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. //Get our data
  3. $friend_guid = (int) get_input('friend');
  4. $friend = get_user($friend_guid);
  5. $user = elgg_get_logged_in_user_entity();
  6. // Now we need to attempt to create the relationship
  7. if (empty($user) || empty($friend)) {
  8. register_error(elgg_echo('friend_request:add:failure'));
  9. forward(REFERER);
  10. }
  11. // New for v1.1 - If the other user is already a friend (fan) of this user we should auto-approve the friend request...
  12. if (check_entity_relationship($friend->getGUID(), 'friend', $user->getGUID())) {
  13. try {
  14. $user->addFriend($friend->getGUID());
  15. system_message(elgg_echo('friends:add:successful', [$friend->name]));
  16. forward(REFERER);
  17. } catch (Exception $e) {
  18. register_error(elgg_echo('friends:add:failure', [$friend->name]));
  19. forward(REFERER);
  20. }
  21. } elseif (check_entity_relationship($friend->getGUID(), 'friendrequest', $user->getGUID())) {
  22. // Check if your potential friend already invited you, if so make friends
  23. if (remove_entity_relationship($friend->getGUID(), 'friendrequest', $user->getGUID())) {
  24. // Friends mean reciprical...
  25. $user->addFriend($friend->getGUID());
  26. $friend->addFriend($user->getGUID());
  27. system_message(elgg_echo('friend_request:approve:successful', [$friend->name]));
  28. // add to river
  29. friend_request_create_river_events($user->getGUID(), $friend->getGUID());
  30. forward(REFERER);
  31. } else {
  32. register_error(elgg_echo('friend_request:approve:fail', [$friend->name]));
  33. forward(REFERER);
  34. }
  35. } else {
  36. try {
  37. if (!add_entity_relationship($user->getGUID(), 'friendrequest', $friend->getGUID())) {
  38. register_error(elgg_echo('friend_request:add:exists', [$friend->name]));
  39. forward(REFERER);
  40. }
  41. } catch (Exception $e) {
  42. // add_entity_relationship calls insert_data which CAN raise Exceptions.
  43. register_error(elgg_echo('friend_request:add:exists', [$friend->name]));
  44. forward(REFERER);
  45. }
  46. }
  47. system_message(elgg_echo('friend_request:add:successful', array($friend->name)));
  48. forward(REFERER);