123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace ColdTrick\FriendRequest;
- class Users {
-
- /**
- * Add menu items to the entity menu
- *
- * @param string $hook the name of the hook
- * @param string $type the type of the hook
- * @param \ElggMenuItem[] $return_value the current return value
- * @param array $params supplied params
- *
- * @return void|\ElggMenuItem[]
- */
- public static function registerEntityMenu($hook, $type, $return_value, $params) {
- if (empty($params) || !is_array($params)) {
- return;
- }
-
- $user = elgg_get_logged_in_user_entity();
- if (empty($user)) {
- return;
- }
-
- $entity = elgg_extract('entity', $params);
- if (!($entity instanceof \ElggUser)) {
- return;
- }
-
- if ($entity->getGUID() === $user->getGUID()) {
- // looking at yourself
- return;
- }
-
- // are we friends
- if (!$entity->isFriendOf($user->getGUID())) {
- // no, check if pending request
- if (check_entity_relationship($user->getGUID(), 'friendrequest', $entity->getGUID())) {
- // pending request
- $return_value[] = \ElggMenuItem::factory([
- 'name' => 'friend_request',
- 'text' => elgg_echo('friend_request:friend:add:pending'),
- 'href' => "friend_request/{$user->username}#friend_request_sent_listing",
- 'priority' => 500,
- ]);
- } else {
- // add as friend
- $return_value[] = \ElggMenuItem::factory([
- 'name' => 'add_friend',
- 'text' => elgg_echo('friend:add'),
- 'href' => "action/friends/add?friend={$entity->getGUID()}",
- 'is_action' => true,
- 'priority' => 500,
- ]);
- }
- } else {
- // is friend, se remove friend link
- $return_value[] = \ElggMenuItem::factory([
- 'name' => 'remove_friend',
- 'text' => elgg_echo('friend:remove'),
- 'href' => "action/friends/remove?friend={$entity->getGUID()}",
- 'is_action' => true,
- 'priority' => 500,
- ]);
- }
-
- return $return_value;
- }
-
- /**
- * Add menu items to the hover menu
- *
- * @param string $hook the name of the hook
- * @param string $type the type of the hook
- * @param \ElggMenuItem[] $return_value the current return value
- * @param array $params supplied params
- *
- * @return void|\ElggMenuItem[]
- */
- public static function registerUserHoverMenu($hook, $type, $return_value, $params) {
-
- if (empty($params) || !is_array($params)) {
- return;
- }
-
- $user = elgg_get_logged_in_user_entity();
- if (empty($user)) {
- return;
- }
-
- $entity = elgg_extract('entity', $params);
- if (!($entity instanceof \ElggUser)) {
- return;
- }
-
- if ($entity->getGUID() === $user->getGUID()) {
- // looking at yourself
- return;
- }
-
- $requested = check_entity_relationship($user->getGUID(), 'friendrequest', $entity->getGUID());
- $is_friend = $entity->isFriend($user->getGUID());
-
- foreach ($return_value as $index => $item) {
- // change the text of the button to tell you already requested a friendship
- switch ($item->getName()) {
- case 'add_friend':
- if ($requested) {
- $item->setItemClass('hidden');
- }
-
- break;
- case 'remove_friend':
- if (!$requested && !$is_friend) {
- unset($return_value[$index]);
- }
- break;
- }
- }
-
- if ($requested) {
- $return_value[] = \ElggMenuItem::factory([
- 'name' => 'friend_request',
- 'text' => elgg_echo('friend_request:friend:add:pending'),
- 'href' => "friend_request/{$user->username}#friend_request_sent_listing",
- 'section' => 'action',
- ]);
- }
-
- return $return_value;
- }
- }
|