You are here

function commerce_registration_line_item_registrations_validate in Commerce Registration 7.2

Validation callback for line item registration elements on the order edit ui.

1 string reference to 'commerce_registration_line_item_registrations_validate'
commerce_registration_form_commerce_order_ui_order_form_alter in ./commerce_registration.module
Implements hook_form_FORM_ID_alter().

File

./commerce_registration.module, line 1047
Commerce Registration module code.

Code

function commerce_registration_line_item_registrations_validate($element, &$form_state, $form) {

  // If the user is editing an order, load a fresh copy, but don't mess with the
  // form_state version because other modules like commerce_line_item depend on
  // staged changes they have made to it until it is saved.
  if ($form_state['commerce_order']->order_id) {
    $order = commerce_order_load($form_state['commerce_order']->order_id);
  }
  else {
    $order = $form_state['commerce_order'];
  }
  $prodkey = $element['registrations']['#prodkey'];
  $line_item = $element['registrations']['#line_item'];
  $sync = $element['sync_registrations']['#value'];

  // If the the form has been instructed to add a new registration for this
  // line item then validate the registration form.
  if (!empty($form_state['line_item_registration_add']) && isset($form_state['line_item_registration_add'][$line_item->line_item_id])) {

    // Use the existing registration module form validation function.
    $form_state['registration'] = $form_state['line_item_registration_add'][$line_item->line_item_id];
    registration_form_validate($element['registration_control']['form'], $form_state);
    unset($form_state['registration']);
  }
  else {
    if ($sync && (!isset($order->data) || empty($order->data['register_entities'][$prodkey]))) {
      $line_item_title = commerce_line_item_title($line_item);
      form_set_error(implode('][', $element['sync_registrations']['#array_parents']), t('Currently the line item quantity for @line_item_title cannot be synchronized as there are no registrations associated with the line item yet.', array(
        '@line_item_title' => $line_item_title,
      )));
    }
  }

  // Set up a new registration if the AJAXified "Add registration" button was
  // pressed. No need for a submit function since nothing needs to be validated
  // and this can only be done over AJAX really.
  if (!empty($form_state['triggering_element'])) {
    $trigger_parents = array_reverse($form_state['triggering_element']['#array_parents']);
    $triggering_element = $trigger_parents[0];
    if ($triggering_element == 'registration_control_add' && isset($trigger_parents[2]) && (int) $trigger_parents[2] && $trigger_parents[2] == $line_item->line_item_id) {
      $entity_type = 'commerce_product';
      $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
      $product_id = (int) $line_item_wrapper->commerce_product->product_id
        ->value();
      if (registration_status($entity_type, $product_id)) {
        $product = commerce_product_load($product_id);
        $registration_type = registration_get_entity_registration_type($entity_type, $product);
        $registration = entity_get_controller('registration')
          ->create(array(
          'entity_type' => $entity_type,
          'entity_id' => $product_id,
          'type' => $registration_type,
        ));
        $form_state['line_item_registration_add'][$line_item->line_item_id] = $registration;
      }
      else {

        // We don't use form_set_error() here since we limit the validation for
        // the add registration button and the triggering element will not be
        // there after rebuild. So just go ahead and display the error message.
        drupal_set_message(t('It is not possible to add registrations at this time. Check the registration settings and spaces available for this product.'), 'error');
      }
      $form_state['rebuild'] = TRUE;
    }
  }
}