You are here

function commerce_customer_profile_copy_validate in Commerce Core 7

Element validate callback: Pertaining to the "copy profile" checkbox.

1 string reference to 'commerce_customer_profile_copy_validate'
commerce_customer_profile_pane_checkout_form in modules/customer/includes/commerce_customer.checkout_pane.inc
Checkout pane callback: returns a customer profile edit form.

File

modules/customer/commerce_customer.module, line 1395
Defines the customer profile entity and API functions to manage customers and interact with them.

Code

function commerce_customer_profile_copy_validate($element, &$form_state, $form) {
  $triggering_element = end($form_state['triggering_element']['#array_parents']);
  $pane_id = reset($element['#array_parents']);

  // If this profile copying checkbox was unchecked, update and save the order.
  if ($triggering_element == 'commerce_customer_profile_copy' && $form_state['triggering_element']['#id'] == $element['#id'] && empty($element['#value'])) {
    $form_state['order']->data['profile_copy'][$pane_id]['status'] = FALSE;
    unset($form_state['order']->data['profile_copy'][$pane_id]['elements']);
    commerce_order_save($form_state['order']);
  }
  elseif (!empty($element['#value'])) {
    $type = substr($pane_id, 17);

    // Removes 'customer_profile_'
    $source_id = 'customer_profile_' . variable_get('commerce_' . $pane_id . '_profile_copy_source', '');
    $info = array(
      'commerce_customer_profile',
      $type,
      $pane_id,
    );

    // Try getting the source profile from the form_state values, if it is present on the form..
    if (isset($form_state['values'][$source_id])) {

      // If there were errors in the source profile pane, there's no need
      // to copy the profile fields, we should therefore stop here.
      if ($errors = form_get_errors()) {
        foreach ($errors as $field => $error) {
          if (strpos($field, $source_id . '][') === 0) {
            return;
          }
        }
      }
      commerce_customer_profile_copy_fields($info, $form_state['input'][$pane_id], $form_state['input'][$source_id], $form_state);
      commerce_customer_profile_copy_fields($info, $form_state['values'][$pane_id], $form_state['values'][$source_id], $form_state);
    }
    else {

      // Check for source profile via order wrapper.
      $wrapper = entity_metadata_wrapper('commerce_order', $form_state['order']);
      $profile = NULL;
      if ($source_field_name = variable_get('commerce_' . $source_id . '_field', '')) {
        $profile = $wrapper->{$source_field_name}
          ->value();
      }
      elseif (!empty($form_state['order']->data['profiles'][$source_id])) {
        $profile = commerce_customer_profile_load($form_state['order']->data['profiles'][$source_id]);
      }
      if (!empty($profile)) {
        commerce_customer_profile_copy_fields($info, $form_state['input'][$pane_id], $profile, $form_state);
        commerce_customer_profile_copy_fields($info, $form_state['values'][$pane_id], $profile, $form_state);
      }
    }
    $form_state['order']->data['profile_copy'][$pane_id]['status'] = TRUE;
    commerce_order_save($form_state['order']);

    // Unset any cached addressfield data for this customer profile.
    if (!empty($form_state['addressfield'])) {
      foreach ($form_state['addressfield'] as $key => $value) {
        if (strpos($key, 'commerce_customer_profile|' . $type) === 0) {
          unset($form_state['addressfield'][$key]);
        }
      }
    }
  }
}