You are here

function spaces_announce_form_alter in Spaces 5

Same name and namespace in other branches
  1. 5.2 spaces_announce/spaces_announce.module \spaces_announce_form_alter()

Implementation of hook_form_alter.

File

spaces_announce/spaces_announce.module, line 97

Code

function spaces_announce_form_alter($form_id, &$form) {

  // Add check box to nodetype forms
  if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
    $form['workflow']['spaces_announce'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable announcements'),
      '#default_value' => variable_get('spaces_announce_' . $form['#node_type']->type, 0),
      '#description' => t('Allow this content type to be broadcast from one group to another.'),
    );
  }

  // Add group checkboxes to node forms.
  if (isset($form['type']) && $form['type']['#value'] . '_node_form' && variable_get('spaces_announce_' . $form['type']['#value'], 0)) {
    global $user;
    $gid = spaces_gid();

    // Retrieve the primary_gid for exiting nodes.
    if (is_numeric($form['#node']->nid)) {
      $primary_gid = spaces_announce_primary_gid($form['#node']->nid);

      // It's possible that a post doesn't have an assigned primary gid, so if it's in the
      // current group we assume that the current group is the primary one, and set the
      // primary_gid so that it can be respected and saved on form submission.
      if (!$primary_gid && in_array($gid, $form['#node']->og_groups)) {
        $primary_gid = $gid;
      }
    }
    if (!isset($primary_gid) || $primary_gid == $gid) {

      // Retrieve groups which the current user is a member and where the announce setting is enabled.
      $gids = array();

      // If the user has admin nodes they can edit annoucements they didn't author and will need be
      // able to post posts in any groups which can recieve posts.
      if (user_access('administer nodes')) {
        $result = db_query("SELECT DISTINCT(gid), title FROM {spaces_features} i INNER JOIN {og_uid} og ON i.gid = og.nid INNER JOIN {node} n ON og.nid = n.nid WHERE i.id ='%s' AND i.type = 1 AND i.value <> 0", $form['type']['#value'] . '_announce');
      }
      else {
        $result = db_query("SELECT DISTINCT(gid), title FROM {spaces_features} i INNER JOIN {og_uid} og ON i.gid = og.nid INNER JOIN {node} n ON og.nid = n.nid WHERE i.id ='%s' AND i.type = 1 AND i.value <> 0 AND og.uid = %d", $form['type']['#value'] . '_announce', $user->uid);
      }
      while ($row = db_fetch_object($result)) {
        $gids[$row->gid] = $row->title;
      }

      // Remove the current group from broadcast options.
      unset($gids[$gid]);

      // If the node has groups, present those - minus the primary groups, otherwise use the spaces_gid.
      if (isset($primary_gid)) {
        $active_gids = array_diff($form['#node']->og_groups, array(
          $primary_gid,
        ));
      }
      else {
        $active_gids = array();
      }
      if (isset($form['parent']) && $form['parent']['#default_value'] !== null) {
        $parent = node_load($form['parent']['#default_value']);
        $active_gids = array_merge($active_gids, $parent->og_groups);
        $parent_set = true;
      }
      if (count($gids)) {
        if ($parent_set) {
          $form['announce_display'] = array(
            '#type' => 'checkboxes',
            '#title' => t('Broadcast to'),
            '#description' => t('Broadcast settings are controled by at the root of the book.'),
            // '#value' => $active_gids,
            '#default_value' => $active_gids,
            '#options' => $gids,
            '#disabled' => true,
          );
          $form['announce_hidden'] = array(
            '#type' => 'hidden',
            '#value' => serialize($active_gids),
          );
        }
        else {
          $form['announce'] = array(
            '#type' => 'checkboxes',
            '#title' => t('Broadcast to'),
            '#description' => t('Selected groups will recieve this post.'),
            '#default_value' => $active_gids,
            '#options' => $gids,
          );
        }

        // Add after_build function to save extra og relations.
        $form['#after_build'][] = 'spaces_announce_group_save';
      }
    }
    else {
      $link = cl(t('Click here'), spaces_group_path($primary_gid) . '/node/' . $form['#node']->nid, array(), null, null, false, false, true);
      drupal_set_message(t('This !type was not authored in this group and should not be edited here. !click_here to edit where it was created.', array(
        '!type' => $form['type']['#value'],
        '!click_here' => $link,
      )));
    }
  }
  elseif ($form_id == 'intranet_features_form') {
    $form['settings']['intranet_home']['#options']['announcements'] = t('Announcements');
  }
}