You are here

function og_ui_subscribe_link in Organic groups 7

Return a subscribe link according to user's permissions.

Parameters

$entity_type:

$entity:

$account:

Return value

unknown_type

1 call to og_ui_subscribe_link()
og_ui_field_formatter_view in og_ui/og_ui.module
Implements hook_field_formatter_view().

File

og_ui/og_ui.module, line 317
Organic groups UI.

Code

function og_ui_subscribe_link($entity_type, $entity, $account = NULL) {
  $return = array();
  if (empty($account)) {
    global $user;
    $account = clone $user;
  }
  list($id) = entity_extract_ids($entity_type, $entity);
  if ($group = og_get_group($entity_type, $id)) {
    if (!empty($entity->uid) && $entity->uid == $user->uid) {

      // User is the group manager.
      $return = array(
        '#type' => 'item',
        '#markup' => t('You are the group manager'),
      );
    }
    else {

      // Check if the user is a group member.
      if (og_is_member($group->gid, 'user', $account, array(
        OG_STATE_ACTIVE,
        OG_STATE_PENDING,
      ))) {
        if (og_user_access($group->gid, 'unsubscribe', $account)) {
          $links['title'] = t('Unsubscribe from group');
          $links['href'] = "group/{$entity_type}/{$id}/unsubscribe";
        }
      }
      else {
        if (og_is_member($group->gid, 'user', $account, array(
          OG_STATE_BLOCKED,
        ))) {

          // If user is blocked, they should not be able to apply for
          // membership.
          return;
        }
        if (og_user_access($group->gid, 'subscribe without approval', $account)) {
          $links['title'] = t('Subscribe to group');
          $url = "group/{$entity_type}/{$id}/subscribe";
          if ($account->uid) {
            $links['href'] = $url;
          }
          else {
            $links['href'] = 'user/login';
            $links['options'] = array(
              'query' => array(
                'destination' => $url,
              ),
            );
          }
        }
        elseif (og_user_access($group->gid, 'subscribe')) {
          $links['title'] = t('Request group membership');
          $url = "group/{$entity_type}/{$id}/subscribe";
          if ($account->uid) {
            $links['href'] = $url;
          }
          else {
            $links['href'] = 'user/login';
            $links['options'] = array(
              'query' => array(
                'destination' => $url,
              ),
            );
          }
        }
        else {
          $return = array(
            '#type' => 'item',
            '#markup' => t('This is a closed group. Only a group administrator can add you.'),
          );
        }
      }
      if (!empty($links['title'])) {
        $links += array(
          'options' => array(),
        );
        $return = array(
          '#type' => 'link',
          '#title' => $links['title'],
          '#href' => $links['href'],
          '#options' => $links['options'],
        );
      }
    }
  }
  return $return;
}