You are here

function civicrm_entity_price_set_field_display_form_event in CiviCRM Entity 7.2

Form callback for event registration form

_state

Parameters

$form:

Return value

mixed

1 string reference to 'civicrm_entity_price_set_field_display_form_event'
civicrm_entity_price_set_field_field_formatter_view in modules/civicrm_entity_price_set_field/civicrm_entity_price_set_field.module
Implements hook_field_formatter_view().

File

modules/civicrm_entity_price_set_field/includes/civicrm_entity_price_set_field.event_registration.inc, line 77
CiviCRM Entity Price Set Field, Form display formatter, event registration

Code

function civicrm_entity_price_set_field_display_form_event($form, &$form_state) {
  $form = array();
  $price_set_id = $form_state['build_info']['args'][0];
  $host_entity_type = $form_state['build_info']['args'][1];
  $event = $form_state['event'] = $form_state['build_info']['args'][2];
  $form_state['field'] = $form_state['build_info']['args'][3];
  $form_state['instance'] = $form_state['build_info']['args'][4];
  $form_state['display_settings'] = $form_state['build_info']['args'][5];

  // online registration
  if (!empty($event->is_online_registration)) {
    if (user_access('register for events') && civicrm_entity_price_set_field_allow_registration($host_entity_type, $event, $form_state['field'], $form_state['instance'])) {

      // check if registration date is set and passed yet
      if (!empty($event->registration_start_date)) {
        if (time() < strtotime($event->registration_start_date)) {
          $form['message'] = array(
            '#type' => 'markup',
            '#message' => 'registration_state_date',
            '#markup' => 'Registration opens ' . $event->registration_start_date,
          );
          return $form;
        }
      }

      // check if registration end date is set and passed yet
      if (!empty($event->registration_end_date)) {
        if (time() > strtotime($event->registration_end_date)) {
          $form['message'] = array(
            '#type' => 'markup',
            '#message' => 'registration_end_date',
            '#markup' => 'Registration closed on ' . $event->registration_end_date,
          );
          return $form;
        }
      }

      // check if max_participants has been reached, and if so, close the form down, good messaging
      if (isset($event->max_participants)) {
        $registered_count = _civicrm_entity_price_set_field_get_participant_count_for_event($event->id);
        if ($registered_count >= $event->max_participants) {
          $form['message'] = array(
            '#type' => 'markup',
            '#message' => 'max_participants',
            '#markup' => !empty($event->event_full_text) ? $event->event_full_text : 'The event is currently full',
          );
          return $form;
        }
      }

      // paid event
      if (!empty($event->is_monetary)) {
        if ($price_set_id) {
          if (empty($form_state['contacts'])) {
            $form_state['price_set_data'][0] = _civicrm_entity_price_set_field_get_relevant_entities_clone($price_set_id);
          }
          else {
            foreach ($form_state['contacts'] as $count => $contact) {
              $form_state['price_set_data'][$count] = _civicrm_entity_price_set_field_get_relevant_entities_clone($price_set_id);
            }
          }
          foreach ($form_state['price_set_data'] as $count => $ps_data) {
            $context = array(
              'participant_count' => $count,
              'entity_type' => $host_entity_type,
              'entity' => $event,
              'field' => $form_state['field'],
              'instance' => $form_state['instance'],
              'display_settings' => $form_state['display_settings'],
            );
            drupal_alter('civicrm_entity_price_set_field_registration_form_price_set_data', $form_state['price_set_data'][$count], $context);
          }
        }
        else {
          $form['price_set']['message'] = array(
            '#type' => 'markup',
            '#message' => 'no_price_set',
            '#markup' => 'No price options for event.',
          );
          return $form;
        }
      }
      else {

        // free event
        if (empty($form_state['contacts'])) {
          $form_state['price_set_data'][0] = array();
        }
        else {
          foreach ($form_state['contacts'] as $count => $contact) {
            $form_state['price_set_data'][$count] = array();
          }
        }
      }

      // setup ajax multi step
      // If $form_state['step'] is not set, we set it to 1
      $form_state['step'] = isset($form_state['step']) ? $form_state['step'] : 1;

      // Add a wrapper div that will be used by the Form API to update the form using AJAX
      $form['#prefix'] = '<div id="civicrm-price-set-field-event-registration">';
      $form['#suffix'] = '</div>';
      $form['#attached']['js'][] = drupal_get_path('module', 'civicrm_entity_price_set_field') . '/js/civicrm_entity_price_set_field_display_form.js';

      // Depending on which step of the form we are on, we output different form fields
      switch ($form_state['step']) {

        // Registration Form
        case 1:
          $default_values = array();
          if (isset($form_state['input']['registration_form'])) {
            $default_values = $form_state['input']['registration_form'];
          }
          elseif (isset($form_state['values']['registration_form'])) {
            $default_values = $form_state['values']['registration_form'];
          }
          elseif (isset($form_state['storage']['registration_form'])) {
            $default_values = $form_state['storage']['registration_form'];
          }
          global $user;
          if ($user->uid && empty($default_values['register_as_another'])) {
            $contact = civicrm_entity_user_contact_get($user, array(), '', '', '');
            if ($contact) {
              $already_registered = _civicrm_entity_price_set_field_get_participant_count_for_event($event->id, $contact->id);
              if (empty($event->allow_same_participant_emails) && !empty($already_registered)) {
                drupal_set_message(t('You have already registered for this event.'), 'status', FALSE);
                $form_state['main_registrant_already_registered'] = TRUE;
              }
            }
          }

          // setup registration form
          _civicrm_entity_price_set_field_setup_event_registration_form_fapi($form, $form_state, $default_values);
          break;

        // Confirmation Page
        case 2:
          _civicrm_entity_price_set_field_setup_event_confirmation_page_fapi($form, $form_state);
          break;

        // Thank you page
        case 3:
          _civicrm_entity_price_set_field_setup_event_thank_you_page_fapi($form, $form_state);
          break;
      }

      // Create a container for our buttons
      $form['buttons'] = array(
        '#type' => 'container',
        '#attributes' => array(
          'class' => array(
            'civicrm-entity-price-set-registration-buttons',
          ),
        ),
      );
      switch ($form_state['step']) {
        case 1:
          $form['buttons']['register'] = array(
            '#type' => 'submit',
            '#value' => t('Register'),
            '#ajax' => array(
              'wrapper' => 'civicrm-price-set-field-event-registration',
              'callback' => 'civicrm_entity_price_set_field_event_registration_form_ajax_callback',
            ),
          );
          break;
        case 2:
          $form['buttons']['confirm'] = array(
            '#type' => 'submit',
            '#value' => t('Confirm'),
            '#ajax' => array(
              'wrapper' => 'civicrm-price-set-field-event-registration',
              'callback' => 'civicrm_entity_price_set_field_event_registration_form_ajax_callback',
            ),
          );
          $form['buttons']['back'] = array(
            '#type' => 'submit',
            '#value' => t('Back'),
            '#limit_validation_errors' => array(),
            '#submit' => array(
              '_civicrm_entity_price_set_field_event_confirmation_page_back_submit',
            ),
            '#ajax' => array(
              'wrapper' => 'civicrm-price-set-field-event-registration',
              'callback' => 'civicrm_entity_price_set_field_event_registration_form_ajax_callback',
            ),
          );
          break;
        case 3:
          break;
      }
    }
    else {
      $form['message'] = array(
        '#type' => 'markup',
        '#message' => 'online_registration_access_denied',
        '#markup' => 'You are not authorized to register for the event.',
      );
    }
  }
  else {
    $form['message'] = array(
      '#type' => 'markup',
      '#message' => 'online_registration_disabled',
      '#markup' => 'Online registration disabled.',
    );
    return $form;
  }
  return $form;
}