You are here

function og_menu_form_alter in Organic Groups Menu (OG Menu) 5

Same name and namespace in other branches
  1. 8 og_menu.module \og_menu_form_alter()
  2. 6.2 og_menu.module \og_menu_form_alter()
  3. 6 og_menu.module \og_menu_form_alter()
  4. 7.3 og_menu.module \og_menu_form_alter()
  5. 7.2 og_menu.module \og_menu_form_alter()

File

./og_menu.module, line 199
Modifies the menu module to support menus specific to organic groups.

Code

function og_menu_form_alter($form_id, &$form) {

  // alters the menu edit item form from menu.module so only the organic
  // groups of the current user can be the parent menu
  if ($form_id == 'menu_edit_item_form' && !user_access('administer all menus')) {
    $options = menu_parent_options($item['mid']);
    $item_mid = (int) $form['#parameters'][2];
    $item_pid = db_result(db_query('SELECT pid FROM {menu} WHERE mid = %d', $item_mid));
    if (empty($item_pid)) {
      $item_pid = NULL;
    }
    $og_menu = _generate_menu_listing();
    foreach ($og_menu as $og_mid => $array) {
      foreach ($options as $mid => $title) {
        if ($og_mid == $mid) {
          $og_options[$mid] = $title;
        }
      }
    }
    $form['pid'] = array(
      '#type' => 'select',
      '#title' => t('Parent item'),
      '#default_value' => $item_pid,
      '#options' => $og_options,
    );
  }
  elseif (isset($form['type']) && $form['type']['#value'] . '_node_form' == $form_id) {

    // modifies og_nodeapi() so that the checkboxes displaying groups become a select box
    $groups = og_menu_generate_group_listing();

    // if og audience is required for the user, and there is only one group to which the user
    // belongs, the select box is unnecessary. this checks for that condition
    if (variable_get('og_audience_required', '') == 1) {
      $gid_count = count($groups);
    }
    else {
      $gid_count = NULL;
    }
    foreach ($groups as $gid => $array) {
      $options[$gid] = $array['title'];
    }

    // taken from og.module
    $node = $form['#node'];
    if ($node->nid || $node->og_groups) {
      $groups = $node->og_groups[0];
    }
    else {
      if (isset($_GET['gids'])) {
        $groups = $_GET['gids'];
      }
      else {
        $groups = array();
      }
    }

    // following checks to see if node form is for a group type
    // used to determine whether or not to show Audience select box
    $is_group_type = og_is_group_type($form['type']['#value']);

    // makes OG checkboxes become a select box. code slightly modified from og.module
    // unset select box if og audience required and user belongs to only one group
    // or if the node form is for a content type designated as a group
    if (!empty($gid_count) && $gid_count == 1) {
      unset($form['og_nodeapi']['visible']['og_groups']);
    }
    elseif ($is_group_type === TRUE) {
    }
    else {
      $form['og_nodeapi']['visible']['og_groups'] = array(
        '#type' => 'select',
        '#title' => t('Audience'),
        '#attributes' => array(
          'class' => 'og-audience',
        ),
        '#options' => $options,
        '#description' => format_plural(count($options), 'Show this post in this group.', 'Show this post in these groups.'),
        '#default_value' => $groups,
        '#multiple' => TRUE,
      );
    }

    // limit parent menus to only those belonging to the user's groups
    // only for non-admins (users without 'administer all menus' permissions)
    if (!user_access('administer all menus')) {
      $options = menu_parent_options($item['mid']);
      $item_mid = $form['menu']['mid']['#value'];
      $item_pid = db_result(db_query('SELECT pid FROM {menu} WHERE mid = %d', $item_mid));
      if (empty($item_pid)) {
        $item_pid = NULL;
      }
      $og_menu = _generate_menu_listing();
      foreach ($og_menu as $og_mid => $array) {
        foreach ($options as $mid => $title) {
          if ($og_mid == $mid) {
            $og_options[$mid] = $title;
          }
        }
      }
      $form['menu']['pid'] = array(
        '#type' => 'select',
        '#title' => t('Parent item'),
        '#default_value' => $item_pid,
        '#options' => $og_options,
      );
    }

    /**
     * access rights for 'Publishing Options' changed from 'administer nodes'
     * to 'create (content type) content' so as to allow authenticated users
     * access to these options without being able to have broader node editing
     * abilities. Code for the fieldset and its checkboxes taken from node.module.
     */
    if (!user_access('administer nodes')) {
      $access = "create " . check_plain($form['type']['#value']) . " content";
      $form['options'] = array(
        '#type' => 'fieldset',
        '#access' => user_access($access),
        '#title' => t('Publishing options'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#weight' => 25,
      );
      $form['options']['status'] = array(
        '#type' => 'checkbox',
        '#title' => t('Published'),
        '#default_value' => $node->status,
      );
      $form['options']['promote'] = array(
        '#type' => 'checkbox',
        '#title' => t('Promoted to front page'),
        '#default_value' => $node->promote,
      );
      $form['options']['sticky'] = array(
        '#type' => 'checkbox',
        '#title' => t('Sticky at top of lists'),
        '#default_value' => $node->sticky,
      );
      $form['options']['revision'] = array(
        '#type' => 'checkbox',
        '#title' => t('Create new revision'),
        '#default_value' => $node->revision,
      );
    }
  }
}