You are here

function commerce_registration_information_checkout_form in Commerce Registration 7

Same name and namespace in other branches
  1. 7.3 includes/commerce_registration.checkout_pane.inc \commerce_registration_information_checkout_form()
  2. 7.2 includes/commerce_registration.checkout_pane.inc \commerce_registration_information_checkout_form()

Commerce checkout pane form builder callback.

File

includes/commerce_registration.checkout_pane.inc, line 13
Checkout pane callback functions.

Code

function commerce_registration_information_checkout_form($form, &$form_state, $checkout_pane, $order) {
  global $user;
  $show_form = FALSE;
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  if (empty($form)) {
    $form = array();
  }
  $find_values = !empty($order->data['register_entities']);
  if (!$find_values) {
    $order->data['register_entities'] = array();
  }
  foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
    $type = $line_item_wrapper->commerce_product->type
      ->value();
    $can_register = registration_entity_registration_status(array(
      'id' => $line_item_wrapper->commerce_product->product_id
        ->value(),
      'type' => 'commerce_product',
      'bundle' => $type,
    ));
    if ($can_register == 1) {
      $show_form = TRUE;
      $product = $line_item_wrapper->commerce_product;
      $quantity = (int) $line_item_wrapper->quantity
        ->value();
      if (!$find_values) {

        // order data has not been set, so we initialize an empty array for our
        // entities to be stored
        $order->data['register_entities']['prod-' . $product->sku
          ->value()] = array();
      }
      for ($i = 0; $i < $quantity; $i++) {
        $label = $i + 1;
        $form['prod-' . $product->sku
          ->value()]['prod-' . $product->sku
          ->value() . '-reg-' . $i] = array(
          '#type' => 'fieldset',
          '#title' => check_plain($product->title
            ->value() . ' - Registrant #' . $label),
          '#collapsible' => TRUE,
          '#tree' => TRUE,
        );
        $entity = NULL;
        if ($find_values && !empty($order->data['register_entities']['prod-' . $product->sku
          ->value()][$i])) {

          // the entity exists already, so let's just load it from the order
          $entity = $order->data['register_entities']['prod-' . $product->sku
            ->value()][$i];
        }
        else {

          // entity doesn't exist, so we need to create a new registration
          $bundle = registration_entity_registration_bundle(array(
            'id' => $line_item_wrapper->commerce_product->product_id
              ->value(),
            'type' => 'commerce_product',
            'bundle' => $type,
          ));
          $entity = entity_create('registration', array(
            'type' => $bundle,
          ));
          $entity->bundle = $entity->type;
          $entity->entity_type = 'commerce_product';
          $entity->entity_bundle = $product->type
            ->value();
          $entity->eid = $product->product_id
            ->value();
          $entity->created = REQUEST_TIME;
          $entity->updated = REQUEST_TIME;
          $entity->author_uid = $user->uid;
          $entity->status = 'pending';
          $entity->order_id = $order->order_id;
          if ($user->uid != 0) {
            $entity->mail = $user->mail;
          }
          else {
            $entity->mail = '';
          }
        }
        $form['prod-' . $product->sku
          ->value()]['prod-' . $product->sku
          ->value() . '-reg-' . $i]['mail'] = array(
          '#weight' => -50,
          '#type' => 'textfield',
          '#title' => t('Email Address'),
          '#default_value' => $entity->mail,
          '#required' => TRUE,
          '#parents' => array(
            'registration_information',
            'prod-' . $product->sku
              ->value(),
            'prod-' . $product->sku
              ->value() . '-reg-' . $i,
            'email',
          ),
        );

        // attach the entity's fields to the form
        field_attach_form('registration', $entity, $form['prod-' . $product->sku
          ->value()]['prod-' . $product->sku
          ->value() . '-reg-' . $i], $form_state);
        $fields =& $form['prod-' . $product->sku
          ->value()]['prod-' . $product->sku
          ->value() . '-reg-' . $i];
        foreach ($fields as $key => $data) {
          if (is_array($data) && substr($key, 0, 1) != '#') {

            // we set the parents on just fields so we can find them in
            // form_state later; fields in the form array don't start with a #
            $fields[$key]['#parents'] = array(
              'registration_information',
              'prod-' . $product->sku
                ->value(),
              'prod-' . $product->sku
                ->value() . '-reg-' . $i,
              $key,
            );
          }
        }

        // store entities in order's data array for use in validation, submission
        // and review callbacks
        $order->data['register_entities']['prod-' . $product->sku
          ->value()][$i] = $entity;
      }
    }
  }
  if (!$show_form) {

    // no register enabled products, so go to next step of checkout process
    $order_status = commerce_order_status_load($order->status);
    if ($order_status['state'] == 'checkout' && $order_status['checkout_page'] == 'registration') {
      $this_page = commerce_checkout_page_load($order_status['checkout_page']);
      $next_page = $this_page['next_page'];
      if ($order->log == "Customer returned to the previous checkout page via a submit button.") {

        // not sure how else to check to see if they went back instead of forwards
        $next_page = $this_page['prev_page'];
      }
      $order = commerce_order_status_update($order, 'checkout_' . $next_page);

      // Inform modules of checkout completion if the next page is Completed.
      if ($next_page == 'complete') {
        commerce_checkout_complete($order);
      }
      drupal_goto('checkout/' . $order->order_id . '/' . $next_page);
    }
  }
  return $form;
}