You are here

function og_subgroups_form_add_audience in Subgroups for Organic groups 5.3

A modified version of og_form_add_og_audience that takes into account appropriate parent types of OG homepage nodes.

1 call to og_subgroups_form_add_audience()
og_subgroups_form_alter in ./og_subgroups.module
Implementation of hook_form_alter().

File

./og_subgroups.module, line 309
Maintains a hierarchy of group/subgroup relationships.

Code

function og_subgroups_form_add_audience($form_id, &$form) {
  global $user;
  $node = $form['#node'];
  $prop_type = variable_get('og_subgroups_prop_type', 'none');
  $filtered_options = og_subgroups_selective_groups_options($node->type);
  $og_node_types = variable_get('og_node_types', array(
    'og',
  ));
  $parent_node_types = variable_get('og_subgroups_' . $node->type . '_parents', array(
    $og_node_types,
  ));

  //Avoided nesting another variable_get here.

  // determine the selected groups if fapi doesn't tell us.
  if ($_SERVER["REQUEST_METHOD"] == 'GET') {
    $gids = $_GET['gids'];
  }
  elseif ($_POST['og_groups_hidden']) {
    $gids = unserialize($_POST['og_groups_hidden']);
  }
  $required = variable_get('og_audience_required', 0) && !user_access('administer nodes');

  // Determine the list of groups that are shown.
  // Start by collecting all groups that the user is a member of.
  $subs = og_get_subscriptions($user->uid);
  $options = array();
  foreach ($subs as $key => $val) {
    if (isset($parent_node_types[$val['type']])) {
      $options[$key] = $val['title'];
    }
  }
  if (user_access('administer nodes')) {

    // Node admins see all of groups.
    $other = array_diff_assoc($filtered_options, $options);

    // Use an optgroup if admin is not a member of all groups.
    if ($other) {
      $options = array(
        t('My subscribed groups') => $options,
        t('Other groups') => $other,
      );
      $is_optgroup = TRUE;
    }
    else {
      $options = $all;
    }
  }
  else {

    // Classify those groups which the node already has but the author does not.
    if (function_exists('og_node_groups_distinguish')) {
      $current_groups = og_node_groups_distinguish($node->og_groups, $node->og_groups_names);
    }

    // Put inaccessible groups in the $form so that they can persist. See og_submit_group()
    $form['og_invisible']['og_groups_inaccessible'] = array(
      '#type' => 'value',
      '#value' => $current_groups['inaccessible'],
    );

    // Add the accessible groups that they node already belongs to.
    if ($current_groups['accessible']) {

      // Use an optgroup to distinguish between my subscriptions and additional groups in the Audience select.
      // There is code below which assumes that $options does not have optgroups but that code is within a $simple check
      // So we are OK as long as $simple does not apply to node edits.
      // NOTE: If you form_alter the audience element, beware that it can sometimes be an optgroup.
      foreach ($current_groups['accessible'] as $key => $val) {
        $other[$key] = $val['title'];
      }
      $options = array(
        t('My subscribed groups') => $options,
        t('Other groups') => $other,
      );
      $is_optgroup = TRUE;
    }
    else {
      $is_optgroup = FALSE;
    }
  }

  // show read only item if we are non-admin, and in simple mode (i.e. non-checkboxes) and at least one group is in querystring
  $simple = !user_access('administer organic groups') && !variable_get('og_audience_checkboxes', TRUE) && count($gids);

  // determine value of audience multi-select
  if (count($options) == 1 && $required) {
    $gids = array_keys($options);
    $gid = $gids[0];
    $groups = array(
      $gid,
    );

    // also show read only mode if user has 1 option and we are in required mode
    $simple = TRUE;
  }
  elseif ($gids) {

    // populate field from the querystring if sent
    $groups = $gids;
    if (!user_access('administer nodes') && $simple) {

      // filter out any groups where author is not a member. we cannot rely on fapi to do this when in simple mode.
      $groups = array_intersect($gids, array_keys($options));
    }
  }
  elseif ($node->nid || $node->og_groups) {
    $groups = $node->og_groups;
  }
  else {
    $groups = array();
  }

  // don't bother with visibility if access control is disabled. all is public.
  if (variable_get('og_enabled', FALSE)) {

    // get the visibility for normal users
    $vis = variable_get('og_visibility', 0);

    // override visibility for og admins and when author only has 1 group
    if (user_access('administer organic groups') && $vis < 2) {
      $vis = $vis == OG_VISIBLE_GROUPONLY ? OG_VISIBLE_CHOOSE_PRIVATE : OG_VISIBLE_CHOOSE_PUBLIC;
    }
    elseif (!count($options)) {

      // don't show checkbox if no subscriptions. must be public.
      $vis = OG_VISIBLE_BOTH;
    }

    // If the post is to a private group, visibility must default to one of the private options.
    // Try not to show checkbox if admin likes to reduce decisions for node authors.
    $selected_groups = isset($form['#post']['og_groups']) ? array_filter($form['#post']['og_groups']) : $groups;
    if (count($selected_groups)) {
      foreach ($selected_groups as $gid) {
        $group_node = new stdClass();
        $group_node->nid = $gid;
        og_load_group($group_node);
        if ($group_node->og_private) {
          $vis = variable_get('og_visibility', 0) == OG_VISIBLE_BOTH ? OG_VISIBLE_GROUPONLY : OG_VISIBLE_CHOOSE_PRIVATE;
          break;
        }
      }
    }
    switch ($vis) {
      case OG_VISIBLE_BOTH:
        $form['og_nodeapi']['og_public'] = array(
          '#type' => 'value',
          '#value' => 1,
        );
        break;
      case OG_VISIBLE_GROUPONLY:
        $form['og_nodeapi']['og_public'] = array(
          '#type' => 'value',
          '#value' => 0,
        );
        break;

      //user decides how public the post is.
      case OG_VISIBLE_CHOOSE_PUBLIC:
        $form['og_nodeapi']['visible']['og_public'] = array(
          '#type' => 'checkbox',
          '#title' => t('Public'),
          '#default_value' => $node->nid ? $node->og_public : 1,
          '#description' => t('Show this post to everyone, or only to subscribers of the groups checked above. Posts without any groups are always <em>Public</em>.'),
          '#weight' => 2,
        );
        break;
      case OG_VISIBLE_CHOOSE_PRIVATE:
        $form['og_nodeapi']['visible']['og_public'] = array(
          '#type' => 'checkbox',
          '#title' => t('Public'),
          '#default_value' => $node->nid ? $node->og_public : 0,
          '#description' => t('Show this post to everyone, or only to subscribers of the groups checked above. Posts without any groups are always <em>Public</em>.'),
          '#weight' => 2,
        );
        break;
    }
  }

  // Emit the audience form element.
  if ($simple) {

    // 'simple' mode. read only.
    if (count($groups)) {
      foreach ($groups as $gid) {
        $titles[] = $options[$gid];
        $item_value = implode(', ', $titles);
      }
      $form['og_nodeapi']['visible']['og_groups_visible'] = array(
        '#type' => 'item',
        '#title' => t('Audience'),
        '#value' => $item_value,
      );
      $assoc_groups = drupal_map_assoc($groups);

      // this hidden element persists audience values during a Preview cycle. avoids errors on Preview.
      $form['og_nodeapi']['invisible']['og_groups_hidden'] = array(
        '#type' => 'hidden',
        '#value' => serialize($assoc_groups),
      );

      // this 'value' element persists the audience value during submit process
      $form['og_nodeapi']['invisible']['og_groups'] = array(
        '#type' => 'value',
        '#value' => $assoc_groups,
      );
    }
  }
  elseif ($cnt = count($options, COUNT_RECURSIVE)) {
    drupal_add_js(drupal_get_path('module', 'og') . '/og.js');

    // show multi-select. if less than 20 choices, use checkboxes.
    $type = $cnt >= 20 || $is_optgroup ? 'select' : 'checkboxes';
    $form['og_nodeapi']['visible']['og_groups'] = array(
      '#type' => $type,
      '#title' => t('Audience'),
      '#attributes' => array(
        'class' => 'og-audience',
      ),
      '#options' => $options,
      '#required' => $required,
      '#description' => format_plural(count($options), 'Show this post in this group.', 'Show this post in these groups.'),
      '#default_value' => $groups,
      '#required' => $required,
      '#multiple' => TRUE,
    );
  }
  else {
    if ($required) {
      form_set_error('title', t('You must !join before posting a %type.', array(
        '!join' => l(t('join a group'), 'og'),
        '%type' => node_get_types('name', $node->type),
      )));
    }
  }
  if (count($form['og_nodeapi']['visible']) > 1) {
    $form['og_nodeapi']['#type'] = 'fieldset';
    $form['og_nodeapi']['#title'] = t('Groups');
    $form['og_nodeapi']['#collapsible'] = TRUE;
    $form['og_nodeapi']['#collapsed'] = $gids ? TRUE : FALSE;
  }
}