You are here

function _civicrm_entity_price_set_field_setup_event_registration_form_fapi in CiviCRM Entity 7.2

Helper function to setup event registration form FAPI

_state

Parameters

$form:

$default_values:

1 call to _civicrm_entity_price_set_field_setup_event_registration_form_fapi()
civicrm_entity_price_set_field_display_form_event in modules/civicrm_entity_price_set_field/includes/civicrm_entity_price_set_field.event_registration.inc
Form callback for event registration form

File

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

Code

function _civicrm_entity_price_set_field_setup_event_registration_form_fapi(&$form, &$form_state, $default_values) {
  $event = $form_state['event'];
  $form['registration_form'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'civicrm-entity-price-set-field-registration-form',
      ),
    ),
    '#tree' => TRUE,
  );

  // get contacts and set form state with contacts
  global $user;
  if (empty($default_values['register_as_another'])) {
    if ($user->uid && empty($form_state['main_registrant_already_registered'])) {
      $contact = civicrm_entity_user_contact_get($user, array(), '', '', '');
      if ($contact) {
        $form_state['contacts'][0] = $contact;
        $form_state['contacts'][0]->uid = $user->uid;
      }
    }
    else {
      $form_state['contacts'][0] = new CivicrmEntity(array(
        'is_new' => 1,
      ), 'civicrm_contact');
    }
  }
  else {
    if (empty($form_state['contacts'][0]->id)) {
      $form_state['contacts'][0] = new CivicrmEntity(array(
        'is_new' => 1,
      ), 'civicrm_contact');
      if ($user->uid && empty($form_state['main_registrant_already_registered'])) {
        $form_state['contacts'][0]->uid = $user->uid;
      }
    }
  }
  $form['registration_form']['register_as_another'] = array(
    '#type' => 'checkbox',
    '#title' => t('Register as new contact.'),
    '#return_value' => 1,
    '#ajax' => array(
      'wrapper' => 'civicrm-price-set-field-event-registration',
      'callback' => 'civicrm_entity_price_set_field_event_registration_form_register_as_another_ajax_callback',
      'event' => 'click',
      'method' => 'replace',
    ),
  );
  if (!empty($form_state['contacts'][0]->uid)) {
    $form['registration_form']['register_as_another']['#default_value'] = !empty($default_values['register_as_another']) ? $default_values['register_as_another'] : 0;
    $form['registration_form']['register_as_another']['#description'] = 'You are logged in.  Leaving this box unchecked will use your contact information for this registration.';
  }
  else {
    if (empty($form_state['main_registrant_already_registered'])) {
      $form['registration_form']['register_as_another']['#description'] = '<a href="/user/login">Login</a> if you already have an account.';
    }
    else {
      $form['registration_form']['register_as_another']['#description'] = 'You are already registered, you can only register for another.';
    }
    $form['registration_form']['register_as_another']['#default_value'] = 1;
    $form['registration_form']['register_as_another']['#disabled'] = TRUE;
  }
  $form['registration_form']['contacts'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'civicrm-entity-price-set-field-registration-form-contacts',
      ),
    ),
  );

  //Fetch event profiles
  $main_profiles = $add_profiles = array();
  if (module_exists('civicrm_entity_profile')) {
    $main_profiles = _civicrm_entity_profile_get_profiles('civicrm_event', $form_state['event']->id, 'CiviEvent');
    if (!empty($form_state['event']->is_multiple_registrations)) {
      $add_profiles = _civicrm_entity_profile_get_profiles('civicrm_event', $form_state['event']->id, 'CiviEvent_Additional');
    }
  }
  foreach ($form_state['contacts'] as $count => $contact) {
    if ($count) {
      $form['registration_form']['contacts'][$count]['heading'] = array(
        '#type' => 'markup',
        '#markup' => '<h2>' . 'Participant ' . ($count + 1) . '</h2>',
        '#weight' => -10,
      );
    }
    if (!empty($form_state['price_set_data'][$count])) {
      _civicrm_entity_price_set_field_setup_price_set_fapi($form, $form_state, $default_values, 'registration_form', $count);
    }

    // setup profiles
    $form['registration_form']['contacts'][$count]['contact_info'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'civicrm-entity-price-set-field-participant-form',
        ),
      ),
      '#weight' => -4,
    );
    if ($count == 0) {
      $profile_list = $main_profiles;
    }
    else {
      $profile_list = $add_profiles;
    }

    // need at least one contact profile, if not, don't display form (should also not be able to add additional participant if this is the case!)
    $profile_list_has_contact_entity = 0;
    if (count($profile_list)) {
      foreach ($profile_list as $index => $profile_id) {

        // first check for 'mixed' profiles, if mixed, then don't render
        $profile_entity_type = _civicrm_entity_profile_determine_profile_entity($profile_id);
        if (!$profile_entity_type) {
          unset($profile_list[$index]);
        }
        elseif ($profile_entity_type == 'contact') {
          $profile_list_has_contact_entity = 1;
        }
      }
    }
    $additional_participant_profiles_has_contact_entity = 0;
    if (count($add_profiles)) {
      foreach ($add_profiles as $index => $profile_id) {
        $profile_entity_type = _civicrm_entity_profile_determine_profile_entity($profile_id);
        if (!$profile_entity_type) {
          unset($profile_list[$index]);
        }
        elseif ($profile_entity_type == 'contact') {
          $additional_participant_profiles_has_contact_entity = 1;
        }
      }
    }

    // render profiles
    if (count($profile_list)) {
      if (!$profile_list_has_contact_entity) {
        $form['message'] = array(
          '#type' => 'markup',
          '#message' => 'online_registration_no_profiles',
          '#markup' => 'Event must be configured with at least one contact profile',
        );
        return;
      }
      foreach ($profile_list as $profile_id) {
        $profile_title = _civicrm_entity_profile_get_profile_title($profile_id);

        // setup profile container
        $form['registration_form']['contacts'][$count]['contact_info']['profile-' . $profile_id] = array(
          '#type' => 'container',
          '#attributes' => array(
            'class' => array(
              'civicrm-entity-price-set-field-profile-fapi',
            ),
          ),
          '#prefix' => '<div class="civicrm-entity-price-set-field-profile">',
          '#suffix' => '</div>',
        );

        // Title for the form set
        if (!empty($profile_title)) {
          $form['registration_form']['contacts'][$count]['contact_info']['profile-' . $profile_id]['#prefix'] = '<div class="civicrm-entity-price-set-field-profile">' . '<h3>' . $profile_title . '</h3>';
        }

        // Save profile metadata for future use
        $form_state['profiles']['profile-' . $profile_id] = _civicrm_entity_profile_get_profile_metadata($profile_id, 'event');

        // Get profile FAPI
        $profile_fapi = _civicrm_entity_profile_generate_profile_fapi($profile_id, 'event');
        if (!empty($profile_fapi)) {

          // add profile FAPI to main form
          $form['registration_form']['contacts'][$count]['contact_info']['profile-' . $profile_id]['profile'] = $profile_fapi;

          // Setup default values
          foreach ($form['registration_form']['contacts'][$count]['contact_info']['profile-' . $profile_id]['profile'] as $name => $field_fapi) {
            if (!empty($default_values['contacts'][$count]['contact_info']['profile-' . $profile_id]['profile'][$name])) {
              $form['registration_form']['contacts'][$count]['contact_info']['profile-' . $profile_id]['profile'][$name]['#default_value'] = $default_values['contacts'][$count]['contact_info']['profile-' . $profile_id]['profile'][$name];
            }
            elseif (!empty($form_state['contacts'][$count]->{$name})) {
              $form['registration_form']['contacts'][$count]['contact_info']['profile-' . $profile_id]['profile'][$name]['#default_value'] = $form_state['contacts'][$count]->{$name};
            }
            else {
              $form['registration_form']['contacts'][$count]['contact_info']['profile-' . $profile_id]['profile'][$name]['#default_value'] = '';
            }
          }
        }
      }
    }
    else {
      $form['message'] = array(
        '#type' => 'markup',
        '#message' => 'online_registration_no_profiles',
        '#markup' => 'Event must be configured with at least one contact profile',
      );
      return;
    }
    if ($count) {
      $form['registration_form']['contacts'][$count]['remove_participant'] = array(
        '#contact_count' => $count,
        '#type' => 'submit',
        '#value' => t('Remove participant ' . ($count + 1)),
        '#limit_validation_errors' => array(),
        '#submit' => array(
          '_civicrm_entity_price_set_field_event_registration_remove_participant_submit',
        ),
        '#ajax' => array(
          'wrapper' => 'civicrm-price-set-field-event-registration',
          'callback' => 'civicrm_entity_price_set_field_event_registration_form_ajax_callback',
        ),
        '#weight' => -3,
      );
    }
  }

  // if event is configured to allow multiple registrations then add an "Add Participant" button
  // to ajax load another contact price set and information container to the form
  if (!empty($form_state['event']->is_multiple_registrations) && !empty($additional_participant_profiles_has_contact_entity)) {

    // if max_participants has been reached disable add participant button, will be queried total of participants for event + number of participants being registered already by form
    $registered_count = _civicrm_entity_price_set_field_get_participant_count_for_event($event->id);
    if (!isset($event->max_participants) || isset($event->max_participants) && $registered_count + count($form_state['contacts']) < $event->max_participants) {
      $form['registration_form']['add_participant'] = array(
        '#type' => 'submit',
        '#value' => t('Add a participant'),
        '#limit_validation_errors' => array(),
        '#submit' => array(
          '_civicrm_entity_price_set_field_event_registration_add_participant_submit',
        ),
        '#ajax' => array(
          'wrapper' => 'civicrm-price-set-field-event-registration',
          'callback' => 'civicrm_entity_price_set_field_event_registration_form_ajax_callback',
        ),
      );
    }
  }

  // setup pay options
  if (!empty($form_state['event']->is_monetary)) {
    if (!empty($form_state['event']->is_pay_later)) {
      $form['registration_form']['pay_later'] = array(
        '#type' => 'checkbox',
        '#title' => !empty($form_state['event']->pay_later_text) ? $form_state['event']->pay_later_text : 'Pay by check',
        '#return_value' => 1,
      );
    }
    if (!empty($default_values['pay_later'])) {
      $form['registration_form']['pay_later']['#default_value'] = $default_values['pay_later'];
    }
    else {
      $form['registration_form']['pay_later']['#default_value'] = 0;
    }

    // if cc is selected, show billing block
    $form['registration_form']['registration_cc_block'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'civicrm-entity-price-set-field-cc-block',
        ),
      ),
    );
    $payment_processor_options = array();
    if (!empty($form_state['event']->payment_processor)) {
      $pp_default = '';
      if (is_array($form_state['event']->payment_processor)) {
        foreach ($form_state['event']->payment_processor as $pp_delta => $pp_id) {
          $pp = entity_load_single('civicrm_payment_processor', $pp_id);
          $payment_processor_options[$pp_id] = empty($pp->title) ? $pp->name : $pp->title;
          if (!empty($pp->is_default)) {
            $pp_default = $pp_id;
          }
        }
      }
      else {
        $pp = entity_load_single('civicrm_payment_processor', $form_state['event']->payment_processor);
        $payment_processor_options[$form_state['event']->payment_processor] = empty($pp->title) ? $pp->name : $pp->title;
        $pp_default = $form_state['event']->payment_processor;
      }
      $form['registration_form']['registration_cc_block']['payment_processor_selection'] = array(
        '#title' => t('Payment method'),
        '#type' => 'radios',
        '#options' => $payment_processor_options,
        '#default_value' => $pp_default,
      );
    }
    else {
      $form['registration_form']['registration_cc_block']['#access'] = FALSE;
    }
    $form['registration_form']['registration_cc_block']['first_name_on_card'] = array(
      '#type' => 'textfield',
      '#title' => 'First Name',
      '#description' => 'First name as it appears on card. Required.',
      '#default_value' => '',
      '#size' => 36,
      '#maxlength' => 100,
      '#prefix' => '<h3>Credit Card Information</h3><div class="name-and-number-section">',
    );
    $form['registration_form']['registration_cc_block']['last_name_on_card'] = array(
      '#type' => 'textfield',
      '#title' => 'Last Name',
      '#description' => 'Last name as it appears on card. Required.',
      '#default_value' => '',
      '#size' => 36,
      '#maxlength' => 100,
    );
    $form['registration_form']['registration_cc_block']['cc_number'] = array(
      '#type' => 'textfield',
      '#title' => 'Credit Card Number',
      '#description' => 'Enter numbers only, no dashes or spaces. Required.',
      '#default_value' => '',
      '#size' => 24,
      '#maxlength' => 24,
      '#suffix' => '</div>',
    );
    $form['registration_form']['registration_cc_block']['cc_exp'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'civicrm-entity-price-set-field-cc-exp-block',
        ),
      ),
      '#prefix' => '<div class="expiration-and-cvv-section">',
    );
    $form['registration_form']['registration_cc_block']['cc_exp']['month'] = array(
      '#prefix' => '<div class="form-item"><label>Credit Card Expiration Date</label></div>',
      '#type' => 'select',
      '#title' => 'Month',
      '#default_value' => format_date(time(), 'custom', 'n'),
      '#options' => array(
        1 => 1,
        2 => 2,
        3 => 3,
        4 => 4,
        5 => 5,
        6 => 6,
        7 => 7,
        8 => 8,
        9 => 9,
        10 => 10,
        11 => 11,
        12 => 12,
      ),
    );
    $form['registration_form']['registration_cc_block']['cc_exp']['year'] = array(
      '#type' => 'select',
      '#title' => 'Year',
      '#default_value' => format_date(time(), 'custom', 'Y'),
      '#options' => array_combine(range(date('Y'), date('Y') + 15), range(date('Y'), date('Y') + 15)),
    );
    $form['registration_form']['registration_cc_block']['cvv'] = array(
      '#type' => 'textfield',
      '#title' => 'CVV',
      '#default_value' => '',
      '#size' => 4,
      '#maxlength' => 4,
      '#description' => '3-4 digit code from back of card. Required.',
      '#suffix' => '</div>',
    );

    // add some javascript to hide/reveal cc block depending on pay later click
    // add some css too
    $form['#attached']['css'][] = drupal_get_path('module', 'civicrm_entity_price_set_field') . '/css/billing_block.css';
    $form['#attached']['js'][] = drupal_get_path('module', 'civicrm_entity_price_set_field') . '/js/billing_block.js';

    // jquery validation of billing block?
  }
}