You are here

function commerce_registration_information_checkout_form_submit in Commerce Registration 7.3

Same name and namespace in other branches
  1. 7 includes/commerce_registration.checkout_pane.inc \commerce_registration_information_checkout_form_submit()
  2. 7.2 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 403
Checkout pane callback functions.

Code

function commerce_registration_information_checkout_form_submit($form, &$form_state, $checkout_pane, $order) {
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  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;
    }
    $product_id = (int) $line_item_wrapper->commerce_product->product_id
      ->value();
    if (registration_status('commerce_product', $product_id, TRUE) == 1) {
      $settings = registration_entity_settings('commerce_product', $product_id);

      // Registration objects are stored as a serialized string in the database,
      // so we need to unserialize it to get the full registration object.
      $registrations = $line_item_wrapper->registrations
        ->value();
      if (!is_array($registrations)) {
        $registrations = unserialize($registrations);
      }
      $submit = array();
      $prodkey = 'lineitem-' . $line_item_wrapper->line_item_id
        ->value();
      switch ($settings['settings']['limit_registrations']) {
        case 'registrations':
          foreach ($form_state['values']['registration_information'][$prodkey] as $i => $quantity_reg) {
            foreach ($quantity_reg as $j => $registration) {
              $registration['form_index'] = "{$i}][{$j}";
              $registration['entity'] =& $registrations[$i][$j];
              $registration['form_structure'] = $form['registration_information'][$prodkey][$i][$j];
              $submit[] = $registration;
            }
          }
          break;
        case 'slots':
          foreach ($form_state['values']['registration_information'][$prodkey] as $i => $registration) {
            $registration['form_index'] = "{$i}";
            $registration['entity'] =& $registrations[$i];
            $registration['form_structure'] = $form['registration_information'][$prodkey][$i];
            $submit[] = $registration;
          }
          break;
      }
      foreach ($submit as $registration_data) {
        if (isset($registration_data['no_reg_permission'])) {
          continue;
        }
        $entity =& $registration_data['entity'];
        $entity->count = $registration_data['slots'];
        $type = $registration_data['who_is_registering'];
        switch ($type) {
          case REGISTRATION_REGISTRANT_TYPE_ANON:
            $entity->anon_mail = $registration_data['anon_mail'];
            break;
          case REGISTRATION_REGISTRANT_TYPE_USER:
            $account = user_load_by_name($registration_data['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, $registration_data['form_structure'], $form_state);
        $entity->updated = REQUEST_TIME;
        registration_save($entity);
        $registrations[$registration_data['form_index']] = $entity;
      }
      $line_item_wrapper->registrations = $registrations;
      $line_item_wrapper
        ->save();
    }
  }
}