You are here

function uc_addresses_checkout_pane_address in Ubercart Addresses 7

Same name and namespace in other branches
  1. 6.2 uc_addresses.ubercart.inc \uc_addresses_checkout_pane_address()

Generic address pane handler.

Parameters

string $type: The address type: billing or shipping.

string $op: The operation that is being performed.

object $order: The Ubercart order.

array $form: The checkout form.

array $form_state: The form state of the checkout form.

string $description: The help text shown above the form.

string $copy: The text shown for the "copy address" checkbox.

Return value

mixed Depending on the operation, different results may be returned.

2 calls to uc_addresses_checkout_pane_address()
uc_addresses_checkout_pane_billing in ./uc_addresses.ubercart.inc
Gets the billing information.
uc_addresses_checkout_pane_shipping in ./uc_addresses.ubercart.inc
Gets the delivery information.

File

./uc_addresses.ubercart.inc, line 60
Ubercart callbacks for the checkout- and order panes.

Code

function uc_addresses_checkout_pane_address($type, $op, $order, $form, &$form_state, $description, $copy) {
  global $user;
  if ($type == 'shipping') {
    $pane = 'delivery';
    $other_pane = 'billing';
    $order_fieldname_prefix = 'delivery';
    $other_order_fieldname_prefix = 'billing';
  }
  else {
    $pane = 'billing';
    $other_pane = 'delivery';
    $order_fieldname_prefix = 'billing';
    $other_order_fieldname_prefix = 'delivery';
  }

  // Source pane for "copy address" checkbox.
  $source =& drupal_static('uc_addresses_checkout_pane_address_source_pane');
  if (!isset($source)) {
    $source = $pane;
  }
  switch ($op) {
    case 'view':

      // Tell address book we want to get multiple addresses.
      UcAddressesAddressBook::get($user->uid)
        ->setPerformanceHint(UcAddressesAddressBook::PERF_HINT_LOAD_ALL);

      // Initialize address variable.
      $address = NULL;

      // Get address for order if set.
      if (isset($order->uc_addresses[$type])) {
        $address = $order->uc_addresses[$type];
      }
      if (!$address) {
        $address = UcAddressesAddress::newAddress();
      }

      // Check if address is copied from source pane
      // (if the "copy address" checkbox was enabled).
      if (isset($address->copy_address) && $address->copy_address || !empty($form_state['values']['panes'][$pane]['copy_address'])) {
        $copy_address_checked = TRUE;
      }
      elseif (!isset($form_state['values']['panes'][$pane]['copy_address']) && !isset($address->copy_address) && variable_get('uc_cart_default_same_address', FALSE) && $source != $pane) {
        $copy_address_checked = TRUE;
      }
      else {
        $copy_address_checked = FALSE;
      }
      if ($source != $pane) {
        $contents['copy_address'] = array(
          '#type' => 'checkbox',
          '#title' => $copy,
          '#ajax' => array(
            'callback' => 'uc_checkout_pane_address_render',
            'wrapper' => $pane . '-address-pane',
            'progress' => array(
              'type' => 'throbber',
            ),
          ),
          '#default_value' => $copy_address_checked,
        );
      }
      if ($user->uid) {
        $select = uc_addresses_select_addresses($user->uid, 'checkout_form', $pane);
        if ($select) {
          $contents['select_address'] = $select + array(
            '#ajax' => array(
              'callback' => 'uc_checkout_pane_address_render',
              'wrapper' => $pane . '-address-pane',
              'progress' => array(
                'type' => 'throbber',
              ),
              'event' => 'change',
            ),
            '#states' => array(
              'invisible' => array(
                'input[name="panes[' . $pane . '][copy_address]"]' => array(
                  'checked' => TRUE,
                ),
              ),
            ),
            '#uc_addresses_address_element' => array(
              'panes',
              $pane,
              'address',
            ),
            '#uc_addresses_address_input' => array(
              'panes',
              $pane,
              'address',
            ),
            '#default_value' => $address,
          );
        }
      }
      if (isset($form_state['triggering_element'])) {
        $element =& $form_state['triggering_element'];
        if ($element['#name'] == "panes[{$pane}][copy_address]") {
          $address_data =& $form_state['values']['panes'][$source];
          foreach ($address_data as $source_fieldname => $value) {
            if (substr($source_fieldname, 0, strlen($source)) == $source) {
              $fieldname = substr($source_fieldname, strlen($source . '_'));
              $target_fieldname = str_replace($source, $pane, $source_fieldname);
              $form_state['input']['panes'][$pane]['address'][$target_fieldname] = $value;
              $order->{$target_fieldname} = $value;
              if (UcAddressesSchemaAddress::fieldExists($fieldname)) {
                $address
                  ->setField($fieldname, $value);
              }
            }
          }
        }

        // Forget any previous Ajax submissions, as we send new default values.
        unset($form_state['uc_addresses_address']);
      }
      $contents['address'] = array(
        '#type' => 'uc_addresses_address',
        '#uc_addresses_context' => 'checkout_form',
        '#uc_addresses_address' => $address,
        '#key_prefix' => $pane,
        '#hidden' => $copy_address_checked && $source != $pane,
        '#prefix' => '<div id="' . $pane . '-address-pane">',
        '#suffix' => '</div>',
      );

      /*
            // Form element for debug purposes, displays the address that is taken.
            $contents['address']['display'] = array(
              '#type' => 'item',
              '#title' => t('Address'),
              '#markup' => (string) $address,
              '#weight' => 100,
            );
      //*/
      return array(
        'description' => $description,
        'contents' => $contents,
      );
    case 'process':
      $address = $form['panes'][$pane]['address']['#uc_addresses_address'];
      $address->op = $op;
      $panes =& $form_state['values']['panes'];
      if ($address
        ->isNew() && !(isset($panes[$pane]['copy_address']) && $panes[$pane]['copy_address'])) {

        // Set flag that this address may be saved to the addressbook.
        $address->save_address = TRUE;
      }

      // Set flag for if this address is "copied" or not.
      if (isset($panes[$pane]['copy_address'])) {
        if (!empty($panes[$pane]['copy_address'])) {
          $address->copy_address = TRUE;
        }
        else {
          $address->copy_address = FALSE;
        }
      }
      foreach ($panes[$pane]['address'] as $target_fieldname => $value) {
        if (substr($target_fieldname, 0, strlen($pane)) == $pane) {
          if (!empty($panes[$pane]['copy_address'])) {
            $fieldname = substr($target_fieldname, strlen($pane . '_'));
            $source_fieldname = $source . '_' . $fieldname;
            if (isset($panes[$source]['address'][$source_fieldname])) {
              $value = $panes[$source]['address'][$source_fieldname];
              if (UcAddressesSchemaAddress::fieldExists($fieldname)) {
                $address
                  ->setField($fieldname, $value);
              }
            }
          }
          $order->{$target_fieldname} = $value;
        }
      }

      // Temporary save address with order.
      $order->uc_addresses[$type] = $address;

      // Save address into session.
      $_SESSION['uc_addresses_order'][$order->order_id][$type] = serialize($address);
      return TRUE;
    case 'review':
      drupal_add_css(drupal_get_path('module', 'uc_addresses') . '/uc_addresses.css');
      return uc_addresses_preprocess_address($order->uc_addresses[$type], 'checkout_review');
  }
}