getGUID(), 'friend', $user->getGUID())) { try { $user->addFriend($friend->getGUID()); system_message(elgg_echo('friends:add:successful', [$friend->name])); forward(REFERER); } catch (Exception $e) { register_error(elgg_echo('friends:add:failure', [$friend->name])); forward(REFERER); } } elseif (check_entity_relationship($friend->getGUID(), 'friendrequest', $user->getGUID())) { // Check if your potential friend already invited you, if so make friends if (remove_entity_relationship($friend->getGUID(), 'friendrequest', $user->getGUID())) { // Friends mean reciprical... $user->addFriend($friend->getGUID()); $friend->addFriend($user->getGUID()); system_message(elgg_echo('friend_request:approve:successful', [$friend->name])); // add to river friend_request_create_river_events($user->getGUID(), $friend->getGUID()); forward(REFERER); } else { register_error(elgg_echo('friend_request:approve:fail', [$friend->name])); forward(REFERER); } } else { try { if (!add_entity_relationship($user->getGUID(), 'friendrequest', $friend->getGUID())) { register_error(elgg_echo('friend_request:add:exists', [$friend->name])); forward(REFERER); } } catch (Exception $e) { // add_entity_relationship calls insert_data which CAN raise Exceptions. register_error(elgg_echo('friend_request:add:exists', [$friend->name])); forward(REFERER); } } system_message(elgg_echo('friend_request:add:successful', array($friend->name))); forward(REFERER);