You are here

function commerce_registration_information_checkout_form_submit in Commerce Registration 7.2

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

Commerce checkout pane form submit callback.

File

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

Code

function commerce_registration_information_checkout_form_submit($form, &$form_state, $checkout_pane, $order) {
  $order->data['register_entities'] = array();
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $i = 0;

  // Main counter for all the registrations
  foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
    if (!in_array($line_item_wrapper->type
      ->value(), commerce_product_line_item_types())) {
      continue;
    }
    $id = (int) $line_item_wrapper->commerce_product->product_id
      ->value();
    if (commerce_registration_product_has_registration_field($id) && registration_status('commerce_product', $id, TRUE) == 1) {
      $product = $line_item_wrapper->commerce_product;
      $line_item_id = $line_item_wrapper->line_item_id
        ->value();
      $quantity = (int) $line_item_wrapper->quantity
        ->value();
      for ($k = 0; $k < $quantity; $k++) {
        $prodkey = $line_item_id . 'prod-' . $product->sku
          ->value();
        if (isset($form_state['values']['registration_information'][$prodkey][$prodkey . '-reg-' . $i]['no_reg_permission'])) {
          continue;
        }
        $entity =& $form_state['values'][$checkout_pane['pane_id']]['register_entities'][$prodkey][$i];
        $type = $form_state['values']['registration_information'][$prodkey][$prodkey . '-reg-' . $i]['register_for'];
        $is_registered = FALSE;
        switch ($type) {
          case REGISTRATION_REGISTRANT_TYPE_ANON:
            $entity->anon_mail = $form_state['values']['registration_information'][$prodkey][$prodkey . '-reg-' . $i]['anon_mail'];
            break;
          case REGISTRATION_REGISTRANT_TYPE_USER:
            $account = user_load_by_name($form_state['values']['registration_information'][$prodkey][$prodkey . '-reg-' . $i]['user']);
            $entity->user_uid = $account->uid;
            break;
          case REGISTRATION_REGISTRANT_TYPE_ME:
            global $user;
            $entity->user_uid = $user->uid;
            break;
        }

        // Notify the field API that values were submitted.
        field_attach_submit('registration', $entity, $form['registration_information'][$prodkey][$prodkey . '-reg-' . $i], $form_state);
        registration_save($entity);
        $order->data['register_entities'][$prodkey][$i] = $entity;

        // Update the total registration counter
        $i++;
      }
    }
  }

  // delete any previous registrations for this order not now in the data array
  $rids = array();
  foreach ($order->data['register_entities'] as $prod) {
    foreach ($prod as $entity) {
      $rids[] = $entity->registration_id;
    }
  }
  if ($rids) {

    // not sure this test is needed but it prevents a PDO exception
    db_delete('registration')
      ->condition('order_id', (int) $order->order_id)
      ->condition('registration_id', $rids, 'NOT IN')
      ->execute();
  }

  // Save the order since we added registration entities to the data array.
  commerce_order_save($order);
}