You are here

function og_ui_membership_links_content_type_render in Organic groups 7.2

Render callback.

File

og_ui/plugins/content_types/membership_links/membership_links.inc, line 20

Code

function og_ui_membership_links_content_type_render($subtype, $conf, $args, $context) {
  global $user;
  if (empty($context->data)) {
    return FALSE;
  }
  $group = clone $context->data;
  $entity = $group
    ->getEntity();
  if (!og_get_membership($group->gid, 'user', $user->uid)) {
    if (!og_user_access($group->gid, 'subscribe') && !og_user_access($group->gid, 'subscribe without approval')) {

      // User doesn't have access.
      return;
    }
    $subscribe_access = og_user_access($group->gid, 'subscribe');
    $types = array();
    foreach (og_ui_subscribe_get_types($conf) as $type => $description) {
      $types[] = array(
        'type' => $type,
        'description' => $description,
        'approval needed' => !$subscribe_access,
      );
    }
    $items = array();
    $options = array();
    $count = count($types);
    foreach ($types as $type) {
      $subscribe_path = 'group/' . $group->entity_type . '/' . $group->etid . '/subscribe/' . $type['type'];
      if ($user->uid) {
        $path = $subscribe_path;
      }
      else {

        // User is anonymous, so redirect to subscribe after login.
        $path = 'user/login';
        $options['query']['destination'] = $subscribe_path;
      }
      if ($count > 1 || $conf['include_name']) {
        $params = array(
          '@type' => $type['description'],
        );
        if ($type['approval needed']) {
          $items[] = array(
            'data' => l(t('Request "@type" membership to group', $params), $path, $options),
          );
        }
        else {
          $items[] = array(
            'data' => l(t('Subscribe to group using "@type" membership', $params), $path, $options),
          );
        }
      }
      else {
        if ($type['approval needed']) {
          $items[] = array(
            'data' => l(t('Request group membership'), $path, $options),
          );
        }
        else {
          $items[] = array(
            'data' => l(t('Subscribe to group'), $path, $options),
          );
        }
      }
    }
    $title = format_plural($count, 'Membership link', 'Membership links');
  }
  else {

    // User already has membership.
    if (!og_user_access($group->gid, 'unsubscribe')) {

      // User can't unsubscribe.
      return;
    }
    if (!empty($entity->uid) && $entity->uid == $user->uid) {

      // User is the group manager.
      return;
    }
    $items[] = array(
      'data' => l(t('Unsubscribe from group'), 'group/' . $group->entity_type . '/' . $group->etid . '/unsubscribe'),
    );
    $title = t('Unsubscribe link');
  }
  $block = new stdClass();
  $block->module = 'og_ui';
  $block->title = $title;
  $block->content = array(
    '#theme' => 'item_list',
    '#items' => $items,
  );
  return $block;
}