You are here

function signup_alter_node_form in Signup 7

Same name and namespace in other branches
  1. 5.2 signup.module \signup_alter_node_form()
  2. 5 signup.module \signup_alter_node_form()
  3. 6.2 includes/node_form.inc \signup_alter_node_form()
  4. 6 includes/node_form.inc \signup_alter_node_form()

Alters the node form to inject the appropriate per-node signup settings.

1 call to signup_alter_node_form()
signup_form_alter in ./signup.module
Implements hook_form_alter().

File

includes/node_form.inc, line 106
Signup-related code needed while editing a node.

Code

function signup_alter_node_form(&$form, &$form_state, $form_id) {
  global $user;

  // Node is not saved but previewed (nid is empty).
  if (isset($form['#node']->in_preview) && $form['#node']->in_preview) {
    $node = $form['#node'];
  }
  elseif (!empty($form['nid']['#value'])) {
    $node = node_load($form['nid']['#value']);
  }
  else {
    $node = NULL;
  }
  $node_type = $form['type']['#value'];
  $signup_type_default = variable_get('signup_node_default_state_' . $node_type, 'disabled');

  // If signups are possible, and the current user either has the global
  // 'administer all signups' permission or has the 'administer signups
  // for own content' permission and is creating new content or editing
  // their own content, add a fieldset for signup-related settings.
  // Signups are possible if they're not explicitly disallowed for this
  // node type, or if this node is already signup-enabled (in case an
  // admin erroneously marks a node-type to disallow signups when there
  // are already nodes of that type with signups enabled).
  $signups_possible = $signup_type_default != 'disabled' || !empty($node) && !empty($node->nid) && !empty($node->signup);
  $admin_all = user_access('administer all signups');
  $admin_own = user_access('administer signups for own content') && (empty($node) || $node->uid == $user->uid);
  if ($signups_possible && ($admin_all || $admin_own)) {
    $form['signup'] = array(
      '#type' => 'fieldset',
      '#title' => t('Signup settings'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#weight' => 30,
      '#group' => 'additional_settings',
    );

    // Figure out what the options should be.  If there are already
    // people signed-up for this node, we need a 3rd choice: disable
    // signups and remove all signup data.
    $has_signups = !empty($node) && (bool) db_query_range("SELECT 1 from {signup_log} WHERE nid = :nid", 0, 1, array(
      ':nid' => $node->nid,
    ))
      ->fetchField();
    $radio_options[1] = t('Enabled');
    if ($has_signups) {
      $radio_options[0] = t('Disabled, but save existing signup information');
      $radio_options[2] = t('Disabled, and remove all signup information') . ' <strong>(' . t('This can not be undone, use with extreme caution!') . ')</strong>';
    }
    else {
      $radio_options[0] = t('Disabled');
    }

    // Figure out what the default selection for signups should be.
    if (isset($node->signup)) {
      $default_option = $node->signup;
    }
    else {
      $default_option = $signup_type_default == 'enabled_on' ? 1 : 0;
    }

    // Add the form element to toggle if signups are allowed.
    $form['signup']['signup_enabled'] = array(
      '#type' => 'radios',
      '#options' => $radio_options,
      '#default_value' => $default_option,
      '#description' => t('If enabled, you can control whether users may sign up by visiting the !signup_admin tab and toggling if signups are %open or %closed for this %node_type. Other signup-related settings can be defined at the !signup_settings tab.', array(
        '!signup_admin' => !empty($node->signup) ? l(t('Signups: Administer'), 'node/' . $node->nid . '/signups/admin') : drupal_placeholder(t('Signups: Administer')),
        '!signup_settings' => !empty($node->signup) ? l(t('Signups: Settings'), 'node/' . $node->nid . '/signups/settings') : drupal_placeholder(t('Signups: Settings')),
        '%open' => t('open'),
        '%closed' => t('closed'),
        '%node_type' => node_type_get_name($node_type),
      )),
    );
    $form['signup']['signup_user_reg'] = array(
      '#type' => 'checkbox',
      '#title' => t('Users can sign up for this event from the user registration form.'),
      '#description' => t('If selected, new users will be able to sign up for this event when they register.'),
      '#default_value' => isset($node->signup_user_reg_form) ? $node->signup_user_reg_form : NULL,
    );
  }
}