You are here

function og_ui_subscribe in Organic groups 7

Same name and namespace in other branches
  1. 7.2 og_ui/og_ui.pages.inc \og_ui_subscribe()

Manage user subscription to a group.

Parameters

$node: The group node entity.

$uid: Optional; The user ID of the subscribing user. If empty the current user will be used.

1 string reference to 'og_ui_subscribe'
og_ui_menu in og_ui/og_ui.module
Implements hook_menu().

File

og_ui/og_ui.pages.inc, line 18
Page callbacks for Group module.

Code

function og_ui_subscribe($entity_type, $etid, $uid = NULL) {
  global $user;
  if ($group = og_get_group($entity_type, $etid)) {
    if (empty($uid)) {
      $account = clone $user;
      if (!$account->uid) {
        $dest = drupal_get_destination();
        if (variable_get('user_register', 1)) {
          drupal_set_message(t('In order to join any group, you must <a href="!login">login</a>. After you have successfully done so, you will need to request membership again.', array(
            '!login' => url("user/login", array(
              'query' => $dest,
            )),
          )));
        }
        else {
          drupal_set_message(t('In order to join any group, you must <a href="!login">login</a> or <a href="!register">register</a> a new account. After you have successfully done so, you will need to request membership again.', array(
            '!register' => url("user/register", array(
              'query' => $dest,
            )),
            '!login' => url("user/login", array(
              'query' => $dest,
            )),
          )));
        }
        drupal_goto('user');
      }
    }
    else {
      $account = user_load($uid);
    }
    $entity = entity_load($entity_type, array(
      $etid,
    ));
    $entity = reset($entity);
    $redirect = FALSE;
    $params = array(
      '@user' => format_username($account),
      // TODO: check user has permissions to see label (e.g. node title).
      '@group' => og_entity_label($entity_type, $entity),
    );
    if (og_is_member($group->gid, 'user', $account, array(
      OG_STATE_BLOCKED,
    ))) {

      // User is blocked, give them access denied.
      drupal_access_denied();
    }
    if (og_is_member($group->gid, 'user', $account, array(
      OG_STATE_PENDING,
    ))) {

      // User is pending, return them back.
      $message = $account->uid == $user->uid ? t('You already have a pending membership for the group @group.', $params) : t('@user already has a pending membership for the  the group @group.', $params);
      $redirect = TRUE;
    }
    if (og_is_member($group->gid, 'user', $account, array(
      OG_STATE_ACTIVE,
    ))) {

      // User is already a member, return them back.
      $message = $account->uid == $user->uid ? t('You are already a member of the group @group.', $params) : t('@user is already a member of the group @group.', $params);
      $redirect = TRUE;
    }
    if (!empty($message)) {
      drupal_set_message($message);
    }
    if ($redirect) {
      _group_ui_redirect($entity_type, $entity);
    }
    if (og_user_access($group->gid, 'subscribe', $account) || og_user_access($group->gid, 'subscribe without approval', $account)) {

      // Show the user a subscription confirmation.
      return drupal_get_form('og_ui_confirm_subscribe', $group, $account);
    }
    drupal_access_denied();
  }

  // Not a valid group node.
  drupal_not_found();
}