You are here

function registration_form in Entity Registration 7

Same name and namespace in other branches
  1. 8.2 includes/registration.forms.inc \registration_form()
  2. 8 includes/registration.forms.inc \registration_form()
  3. 7.2 includes/registration.forms.inc \registration_form()

Form callback: create or edit a registration.

Parameters

Registration $registration: The registration object to edit or for a create form an empty registration object with a node defined.

4 string references to 'registration_form'
RegistrationTestCase::setUpEntity in tests/registration.test
registration_field_formatter_view in includes/registration.field.inc
Implements hook_field_formatter_view().
registration_menu in ./registration.module
Implements hook_menu().
registration_register_page in ./registration.module
Page callback: Add a new registration to a host entity.

File

includes/registration.forms.inc, line 15
Form definitions and callbacks for Registration.

Code

function registration_form($form, &$form_state, Registration $registration) {
  $wrapper = entity_metadata_wrapper('registration', $registration);
  $user = $wrapper->user
    ->value();
  $host = $wrapper->entity
    ->value();
  $state = $wrapper->state
    ->value();
  $type = $wrapper
    ->getBundle();
  $form_state['registration'] = $registration;
  $settings = registration_entity_settings($registration->entity_type, $registration->entity_id);
  if (!registration_has_room($registration->entity_type, $registration->entity_id)) {
    if ($settings['settings']['registration_waitlist_enable'] == 1 && $settings['settings']['registration_waitlist_message_enable'] == 1) {
      drupal_set_message(t($settings['settings']['registration_waitlist_message']), 'warning');
    }
  }
  $who_options = registration_access_people($registration);

  // Default value for who is registering.
  $who_default = NULL;
  if (isset($registration->registration_id)) {
    $who_default = $registration
      ->registrant_type($GLOBALS['user']);
  }
  elseif (count($who_options) == 1) {
    $keys = array_keys($who_options);
    $who_default = reset($keys);
  }

  // Show a message if there's only one option as we're going to hide the form.
  if (count($who_options) == 1 && !user_is_anonymous()) {
    $form['who_message'] = array(
      '#markup' => '<div class="registration-who-msg">' . t('You are registering: %who', array(
        '%who' => current($who_options),
      )) . '</div>',
    );
  }
  $form['who_is_registering'] = array(
    '#type' => 'select',
    '#title' => t('This registration is for:'),
    '#options' => $who_options,
    '#default_value' => $who_default,
    '#required' => TRUE,
    '#access' => count($who_options) > 1,
  );
  $form['user'] = array(
    '#type' => 'textfield',
    '#title' => t('User'),
    '#default_value' => $user ? $user->name : '',
    '#maxlength' => 60,
    '#size' => 30,
    '#description' => t('Select a user by typing their username to get a list of matches.'),
    '#autocomplete_path' => 'user/autocomplete',
    '#access' => isset($who_options[REGISTRATION_REGISTRANT_TYPE_USER]),
    '#states' => array(
      'visible' => array(
        ':input[name="who_is_registering"]' => array(
          'value' => REGISTRATION_REGISTRANT_TYPE_USER,
        ),
      ),
      'required' => array(
        ':input[name="who_is_registering"]' => array(
          'value' => REGISTRATION_REGISTRANT_TYPE_USER,
        ),
      ),
    ),
  );
  $form['anon_mail'] = array(
    '#type' => 'textfield',
    '#title' => t('Email'),
    '#description' => t('The email to associate with this registration.'),
    '#default_value' => isset($registration->anon_mail) ? $registration->anon_mail : '',
    '#size' => 40,
    '#maxlength' => 255,
    '#access' => isset($who_options[REGISTRATION_REGISTRANT_TYPE_ANON]),
    '#required' => user_is_anonymous() ? TRUE : FALSE,
    '#states' => array(
      'visible' => array(
        ':input[name="who_is_registering"]' => array(
          'value' => REGISTRATION_REGISTRANT_TYPE_ANON,
        ),
      ),
      'required' => array(
        ':input[name="who_is_registering"]' => array(
          'value' => REGISTRATION_REGISTRANT_TYPE_ANON,
        ),
      ),
    ),
  );
  $settings = registration_entity_settings($registration->entity_type, $registration->entity_id);
  $capacity = $settings['capacity'];
  $limit = isset($settings['settings']['maximum_spaces']) ? $settings['settings']['maximum_spaces'] : 1;

  // Just in case it was unset:
  $settings['settings']['maximum_spaces'] = $limit;
  $remaining = $capacity - registration_event_count($registration->entity_type, $registration->entity_id);
  if ($capacity && $limit) {
    $description = t('The number of spaces you wish to reserve. @spaces_remaining spaces remaining. You may register up to @max spaces.', array(
      '@spaces_remaining' => $remaining,
      '@max' => min($limit, $remaining),
    ));
  }
  elseif ($capacity) {
    $description = t('The number of spaces you wish to reserve. @spaces_remaining spaces remaining.', array(
      '@spaces_remaining' => $remaining,
    ));
  }
  elseif ($limit) {
    $description = t('The number of spaces you wish to reserve. You may register up to @max spaces.', array(
      '@max' => $limit,
    ));
  }
  else {
    $description = t('The number of spaces you wish to reserve.');
  }
  $form['count'] = array(
    '#type' => 'textfield',
    '#title' => t('Spaces'),
    '#description' => $description,
    '#default_value' => isset($registration->count) ? $registration->count : 1,
    '#size' => drupal_strlen($remaining),
    '#access' => isset($settings['settings']['maximum_spaces']) && $settings['settings']['maximum_spaces'] == 1 ? FALSE : TRUE,
    '#element_validate' => array(
      'element_validate_integer_positive',
      '_registration_validate_space_request',
    ),
  );
  $default_state = registration_get_default_state($type);
  $states = registration_get_states_options(array(
    'show_on_form' => TRUE,
  ));

  // Ensure default state is in options or it won't be set.
  if (!isset($states[$default_state->name])) {
    $states[$default_state->name] = t('@state', array(
      '@state' => entity_label('registration_state', $default_state),
    ));
  }
  $form['state'] = array(
    '#type' => 'select',
    '#title' => t('State'),
    '#description' => t('State of this registration'),
    '#default_value' => $state ? $state
      ->identifier() : $default_state
      ->identifier(),
    '#options' => $states,
    '#access' => !empty($states) && user_access('edit ' . $registration->type . ' registration state'),
  );
  field_attach_form('registration', $registration, $form, $form_state);
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Registration'),
  );

  // Add a delete button for existing registration:
  if (isset($registration->registration_id) && entity_access('delete', 'registration', $registration)) {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#submit' => array(
        'registration_form_delete_submit',
      ),
      "#limit_validation_errors" => array(),
    );
  }
  if ($host && entity_access('view', $registration->entity_type, $host)) {
    $uri = entity_uri($registration->entity_type, $host);
    if (isset($uri['path'])) {
      $form['actions']['cancel'] = array(
        '#type' => 'link',
        '#title' => t('Cancel'),
        '#href' => $uri['path'],
      );
    }
  }
  $form['#registration_settings'] = $settings;
  return $form;
}