You are here

function _spaces_og_form_alter_node in Spaces 6.2

Same name and namespace in other branches
  1. 5.2 spaces_og.module \_spaces_og_form_alter_node()
  2. 6.3 spaces_og/spaces_og.pages.inc \_spaces_og_form_alter_node()
  3. 6 spaces_og/spaces_og.module \_spaces_og_form_alter_node()
  4. 7.3 spaces_og/spaces_og.pages.inc \_spaces_og_form_alter_node()
  5. 7 spaces_og/spaces_og.pages.inc \_spaces_og_form_alter_node()

Group-enabled node form_alter()

1 call to _spaces_og_form_alter_node()
spaces_og_form_alter in spaces_og/spaces_og.module

File

spaces_og/spaces_og.module, line 853

Code

function _spaces_og_form_alter_node(&$form, $form_state) {
  global $user;
  $space = spaces_get_space();

  // Retrieve the content type label
  $types = node_get_types();
  if ($group_type = array_filter(array_keys($types), 'og_is_group_type')) {
    $type = array_pop($group_type);
    $typename = $types[$type]->name;
  }

  // Collect groups for which this feature is enabled
  $options = array(
    0 => '--' . t('Select a !typename', array(
      '!typename' => $typename,
    )) . '--',
  );
  $valid_groups = _spaces_og_group_options($form['#node']->type);
  $user_groups = array();
  foreach (og_get_subscriptions($user->uid) as $node) {
    if (!empty($valid_groups[$node['nid']])) {
      $user_groups[$node['nid']] = $node['title'];
    }
  }

  // Give users access to only their groups
  $options[t('My !typenames', array(
    '!typename' => $typename,
  ))] = $user_groups;

  // Give admins access to all group options
  if (user_access('administer organic groups')) {
    $options[t('All !typenames', array(
      '!typename' => $typename,
    ))] = array_diff_key($valid_groups, $user_groups);
  }

  // Only show the dialogue if we have at least 1 group to target
  if (count($options > 1)) {

    // Node preview handling
    if (!empty($form['#node']->spaces_og['gid'])) {
      $default_gid = $form['#node']->spaces_og['gid'];
    }
    else {
      if (is_array($form['#node']->og_groups) && count($form['#node']->og_groups)) {
        reset($form['#node']->og_groups);
        $default_gid = key($form['#node']->og_groups);
      }
      else {
        if ($space) {
          $default_gid = $space->sid;
        }
        else {
          $default_gid = 0;

          // The invalid group
        }
      }
    }

    // If the current user doesn't have this group as an option,
    // they probably aren't a member here but have admin perms.
    if (!isset($valid_groups[$default_gid])) {
      $disabled = TRUE;
    }
    else {
      if (!empty($default_gid) && !user_access('administer spaces') && !user_access('administer organic groups')) {
        $disabled = TRUE;
      }
    }
    if ($disabled) {
      $form['spaces_og'] = array(
        '#tree' => TRUE,
        'gid' => array(
          '#type' => 'value',
          '#value' => $default_gid,
        ),
      );
    }
    else {
      $form['spaces_og'] = array(
        '#type' => 'fieldset',
        '#tree' => true,
        '#title' => $typename,
      );
      $message = $form['#node']->nid ? t('Please select a !typename to move this post to.', array(
        '!typename' => strtolower($typename),
      )) : t('Please select a !typename to add this post to.', array(
        '!typename' => strtolower($typename),
      ));
      $form['spaces_og']['gid'] = array(
        '#required' => TRUE,
        '#type' => 'select',
        '#options' => $options,
        '#default_value' => $default_gid,
        '#description' => $message,
        '#element_validate' => array(
          'spaces_og_nodeform_validate',
        ),
      );
    }
  }

  // Recurse into og_options hiding all of them.
  _spaces_og_make_hidden($form['og_nodeapi']);

  // We can only determine the privacy of this node if currently in
  // a group space. Otherwise, it will be determined by the feature
  // setting of the group targeted by the selector above.
  if ($space->type == 'og') {
    $form['spaces'] = array(
      '#title' => t('Privacy'),
      '#type' => 'fieldset',
      '#weight' => 100,
    );
    switch ($form['#node']->og_public) {
      case OG_VISIBLE_GROUPONLY:
        $form['spaces']['#description'] = t('A post of this type is <strong>private</strong>. Only members of this !typename will be able to see it.', array(
          '!typename' => strtolower($typename),
        ));
        break;
      case OG_VISIBLE_BOTH:
        $form['spaces']['#description'] = t('A post of this type is <strong>public</strong>. All visitors will be able to see it.');
        break;
    }
  }
}