You are here

function commerce_customer_profile_pane_checkout_form in Commerce Core 7

Checkout pane callback: returns a customer profile edit form.

File

modules/customer/includes/commerce_customer.checkout_pane.inc, line 81
Checkout pane callback functions for the customer module.

Code

function commerce_customer_profile_pane_checkout_form($form, &$form_state, $checkout_pane, $order) {
  $pane_form = array(
    '#parents' => array(
      $checkout_pane['pane_id'],
    ),
  );

  // Extract the type of profile represented by this pane from its ID.
  $type = substr($checkout_pane['pane_id'], 17);

  // Removes 'customer_profile_'
  // Find the referenced profile using the related reference field...
  $wrapper = entity_metadata_wrapper('commerce_order', $order);
  $profile = NULL;

  // If the associated order field has been set...
  if ($field_name = variable_get('commerce_' . $checkout_pane['pane_id'] . '_field', '')) {

    // Check to ensure the specified profile reference field exists on the
    // current order type.
    if (!field_info_instance('commerce_order', $field_name, $order->type)) {
      return array();
    }
    $profile = $wrapper->{$field_name}
      ->value();
  }
  else {

    // Or try the association stored in the order's data array if no field is set.
    if (!empty($order->data['profiles'][$checkout_pane['pane_id']])) {
      $profile = commerce_customer_profile_load($order->data['profiles'][$checkout_pane['pane_id']]);
    }
  }

  // Create a new profile of the specified type if it hasn't already been made.
  if (empty($profile)) {
    $profile = commerce_customer_profile_new($type, $order->uid);
  }

  // Add the entity context of the current cart order.
  $profile->entity_context = array(
    'entity_type' => 'commerce_order',
    'entity_id' => $order->order_id,
  );
  $pane_form['customer_profile'] = array(
    '#type' => 'value',
    '#value' => $profile,
  );

  // Add the field widgets for the profile.
  field_attach_form('commerce_customer_profile', $profile, $pane_form, $form_state);

  // If this checkout pane is configured to use values from a different
  // customer profile, add a checkbox to allow users to toggle this.
  if (variable_get('commerce_' . $checkout_pane['pane_id'] . '_profile_copy', FALSE) && ($source_profile_type_name = variable_get('commerce_' . $checkout_pane['pane_id'] . '_profile_copy_source', NULL))) {

    // Load the default profile copy option from settings.
    $profile_copy_default = variable_get('commerce_' . $checkout_pane['pane_id'] . '_profile_copy_default', FALSE);

    // Make sure our profile type still exists..
    if ($source_profile_type = commerce_customer_profile_type_load($source_profile_type_name)) {
      $target_profile_type = commerce_customer_profile_type_load($profile->type);
      $pane_form['#prefix'] = '<div id="' . strtr($checkout_pane['pane_id'], '_', '-') . '-ajax-wrapper">';
      $pane_form['#suffix'] = '</div>';
      $pane_form['commerce_customer_profile_copy'] = array(
        '#type' => 'checkbox',
        '#title' => t('My %target is the same as my %source.', array(
          '%target' => $target_profile_type['name'],
          '%source' => $source_profile_type['name'],
        )),
        '#element_validate' => array(
          'commerce_customer_profile_copy_validate',
        ),
        '#default_value' => isset($order->data['profile_copy'][$checkout_pane['pane_id']]['status']) ? $order->data['profile_copy'][$checkout_pane['pane_id']]['status'] : $profile_copy_default,
        '#weight' => -30,
        '#ajax' => array(
          'callback' => 'commerce_customer_profile_copy_refresh',
          'wrapper' => strtr($checkout_pane['pane_id'], '_', '-') . '-ajax-wrapper',
        ),
        '#attached' => array(
          'css' => array(
            drupal_get_path('module', 'commerce_customer') . '/theme/commerce_customer.theme.css',
          ),
        ),
        '#prefix' => '<div class="commerce-customer-profile-copy">',
        '#suffix' => '</div>',
      );

      // If the order data has reference to fields that were copied over, hide
      // them so we don't confuse the user by still allowing them to edit values.
      if (!empty($order->data['profile_copy'][$checkout_pane['pane_id']]['status']) && isset($order->data['profile_copy'][$checkout_pane['pane_id']]['elements'])) {
        foreach ($order->data['profile_copy'][$checkout_pane['pane_id']]['elements'] as $field_name => $field) {
          foreach ($field as $langcode => $items) {
            if (is_array($items)) {
              foreach ($items as $delta => $item) {
                if (!empty($pane_form[$field_name][$langcode][$delta])) {
                  $pane_form[$field_name][$langcode][$delta]['#access'] = FALSE;
                }
                elseif (!empty($pane_form[$field_name][$langcode])) {
                  $pane_form[$field_name][$langcode]['#access'] = FALSE;
                }
              }
            }
            else {
              $pane_form[$field_name][$langcode]['#access'] = FALSE;
            }
          }
        }
      }
      elseif (empty($order->data['profile_copy']) && $profile_copy_default) {

        // Get field names that will be copied from the source profile.
        foreach (field_info_instances('commerce_customer_profile', $source_profile_type['type']) as $field_name => $field) {

          // If the field exists on the destination profile then disable it.
          if (!empty($pane_form[$field_name])) {
            $langcode = $pane_form[$field_name]['#language'];
            $pane_form[$field_name][$langcode]['#access'] = FALSE;
          }
        }
      }
    }
  }

  // Tweak the form to remove the fieldset from the address field if there
  // is only one on this profile.
  $addressfields = array();
  foreach (commerce_info_fields('addressfield', 'commerce_customer_profile') as $field_name => $field) {
    if (!empty($pane_form[$field_name]['#language'])) {
      $langcode = $pane_form[$field_name]['#language'];

      // Only consider this addressfield if it's represented on the form.
      if (!empty($pane_form[$field_name][$langcode])) {
        $addressfields[] = array(
          $field_name,
          $langcode,
        );
      }
    }
  }

  // Check to ensure only one addressfield was found on the form.
  if (count($addressfields) == 1) {
    list($field_name, $langcode) = array_shift($addressfields);
    foreach (element_children($pane_form[$field_name][$langcode]) as $delta) {

      // Don't mess with the "Add another item" button that could be present.
      if ($pane_form[$field_name][$langcode][$delta]['#type'] != 'submit') {
        $pane_form[$field_name][$langcode][$delta]['#type'] = 'container';
      }
    }
  }
  return $pane_form;
}