Relationships.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace ColdTrick\FriendRequest;
  3. class Relationships {
  4. /**
  5. * Send a notification on a friend request
  6. *
  7. * @param string $event the name of the event
  8. * @param string $type the type of the event
  9. * @param \ElggRelationship $relationship supplied param
  10. *
  11. * @return void
  12. */
  13. public static function createFriendRequest($event, $type, $relationship) {
  14. if (!($relationship instanceof \ElggRelationship)) {
  15. return;
  16. }
  17. if ($relationship->relationship !== 'friendrequest') {
  18. return;
  19. }
  20. $requester = get_user($relationship->guid_one);
  21. $new_friend = get_user($relationship->guid_two);
  22. if (empty($requester) || empty($new_friend)) {
  23. return;
  24. }
  25. $view_friends_url = elgg_normalize_url("friend_request/{$new_friend->username}");
  26. // Notify target user
  27. $subject = elgg_echo('friend_request:newfriend:subject', [$requester->name]);
  28. $message = elgg_echo('friend_request:newfriend:body', [
  29. $requester->name,
  30. $view_friends_url
  31. ]);
  32. $params = [
  33. 'action' => 'friend_request',
  34. 'object' => $requester,
  35. ];
  36. notify_user($new_friend->getGUID(), $requester->getGUID(), $subject, $message, $params);
  37. }
  38. }