You are here

function node_registration_form in Node registration 7

The registration form. It takes a (mandatory) existing or empty registration object.

4 string references to 'node_registration_form'
node_registration_block_view in ./node_registration.module
Implements hook_block_view().
node_registration_entity_view in ./node_registration.module
Implements hook_entity_view().
node_registration_menu in ./node_registration.module
Implements hook_menu().
node_registration_register_page in ./node_registration.module
Page callback for adding a registration.

File

includes/node_registration.forms.inc, line 757
New registration forms. Public and admin.

Code

function node_registration_form($form, &$form_state, $registration, $user_to_register = NULL) {
  global $user;
  $account = $user_to_register ? $user_to_register : $user;
  $account_email = isset($account->mail) ? $account->mail : '';
  $form['#registration'] = $registration;
  $form['#node'] = $node = $registration->node ?: node_load($registration->nid);
  $form['#account'] = $account;
  $admin = user_access('administer node registration');
  $mail_admin = node_registration_node_access($node, 'administer');
  $can_register_others = $admin || node_registration_node_access($node, 'register others');
  $auth_type = $account->uid ? 'authenticated' : 'anonymous';
  $settings = $node->registration;
  $capacity = $settings
    ->capacity();
  $is_new = (bool) $registration->is_new;

  // Verify the event hasn't sold out.
  if ($is_new && 'GET' == $_SERVER['REQUEST_METHOD']) {
    if (!_node_registration_event_has_room($node)) {
      $waitinglist = _node_registration_waitinglist_size($node);
      drupal_set_message(t("This event is at capacity. You can register for the waiting list. There are @num people waiting...", array(
        '@num' => $waitinglist,
      )), 'warning');
    }
  }

  // Different user context
  if ($can_register_others && $is_new && !$user_to_register) {

    // Allow changing the context user (=registree).
    $form['account'] = array(
      '#type' => 'textfield',
      '#title' => t('Account'),
      '#element_validate' => array(
        'value_is_username',
      ),
      '#exists' => TRUE,
      '#autocomplete_path' => 'user/autocomplete',
      '#default_value' => $account->uid ? $account->name : '',
      '#empty_option' => t('-- Anonymous'),
      '#description' => t('Leaving this field empty, means the registree will be anonymous.'),
    );

    // Get users from a view.
    if ($options = _node_registration_assoc_users_from_view()) {
      $form['account']['#type'] = 'select';
      $form['account']['#options'] = $options;
      $form['account']['#description'] = t('Choose Anonymous to disconnect this registration from any Drupal user.');
    }
  }

  // Field: attended.
  if (!$is_new) {
    $form['attended'] = array(
      '#type' => 'checkbox',
      '#title' => t('Attended?'),
      '#default_value' => !empty($registration->attended),
      '#access' => $admin,
      '#weight' => -11,
    );
  }

  // Field: slots.
  $setting_name = 'max_slots_per_registration_' . $auth_type;
  $max_slots = min($settings->{$setting_name}, ($capacity ?: 999) - node_registration_event_count($node));
  $options = range(1, max(1, min(9, $max_slots)));
  $form['slots'] = array(
    '#type' => 'select',
    '#title' => t('Slots'),
    '#description' => t('Select the number of slots your registration will consume.'),
    '#options' => array_combine($options, $options),
    '#default_value' => $registration->slots,
    '#access' => 1 < $max_slots,
    '#real_max_slots' => $max_slots,
  );

  // Field fields.
  _node_registration_fields_callback('field_attach_form', $registration, $form, $form_state);

  // E-mail field/value.
  $default_email = !empty($registration->email) ? $registration->email : $account_email;

  // Show e-mail field.
  if (!$default_email || $settings->allow_email_change || $can_register_others || $mail_admin) {
    $form['email'] = array(
      '#type' => 'textfield',
      '#title' => t('E-mail address'),
      '#default_value' => $default_email,
      '#required' => TRUE,
      '#element_validate' => array(
        'value_is_email',
      ),
    );

    // Admins are allowed to leave this empty, so it's autofilled via the registree user.
    // If the registree user is Anonymous, this field will be required (in the validation handler).
    if ($can_register_others && $is_new) {
      $form['email']['#default_value'] = '';
      $form['email']['#required'] = FALSE;
      $form['email']['#description'] = t("You can leave this field empty to autofill it with the registree's e-mail address.");
    }
  }
  else {
    $form['#fixed_email'] = $default_email;
    $form['email'] = array(
      '#type' => 'item',
      '#title' => t('E-mail address'),
      '#markup' => '<div class="value">' . $default_email . '</div>',
      '#value' => $default_email,
    );
  }

  // Default values.
  if ($is_new) {
    $values = array();
    $context = array(
      'node' => $node,
      'registration' => $registration,
      'user' => $account,
    );
    drupal_alter('node_registration_default_values', $form, $values, $context);

    // Simple default values.
    $lang = $registration->language;
    foreach ($values as $field_name => $value) {
      if ($field_info = field_info_field($field_name)) {
        if (isset($field_info['columns']['value'])) {
          $form[$field_name][$lang][0]['value']['#default_value'] = $value;
        }
        elseif (isset($field_info['columns']['email'])) {
          $form[$field_name][$lang][0]['email']['#default_value'] = $value;
        }
      }
    }
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => $is_new ? t('Create registration') : t('Update registration'),
  );

  // Cancel registration.
  if ($registration->registration_id) {
    $uri = node_registration_uri($registration);
    $form['actions']['cancel'] = array(
      '#type' => 'link',
      '#title' => t('Cancel registration'),
      '#href' => $uri['path'] . '/cancel',
      '#access' => node_registration_access($registration, 'cancel'),
    );
  }
  else {
    $form['actions']['return'] = array(
      '#type' => 'link',
      '#title' => t('Cancel'),
      '#href' => 'node/' . $registration->nid,
    );
  }
  $form['#validate'][] = 'node_registration_form_validate';
  $form['#submit'][] = 'node_registration_form_submit';
  return $form;
}