You are here

function uc_addresses_checkout_pane_address in Ubercart Addresses 6.2

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

Callback for an address checkout pane.

Parameters

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

string $op: The operation that is being performed.

array/object $order: The Ubercart order.

array $values: The values from the checkout form.

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 70
Ubercart callbacks for the checkout- and order panes.

Code

function uc_addresses_checkout_pane_address($type, $op, &$order, $values = array()) {
  global $user;
  drupal_add_js(drupal_get_path('module', 'uc_addresses') . '/uc_addresses.js');
  if ($type == 'shipping') {
    $other_type = 'billing';
    $uc_type = 'delivery';
  }
  else {
    $other_type = 'delivery';
    $uc_type = 'billing';
  }
  $copy_address_title = $type == 'shipping' ? t('My delivery information is the same as my billing information.') : t('My billing information is the same as my delivery information.');
  switch ($op) {
    case 'view':

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

      // Initialize address variable.
      $address = NULL;

      // Initialize pane description.
      switch ($type) {
        case 'shipping':
          $description = t('Enter your delivery address and information here.');
          break;
        case 'billing':
          $description = t('Enter your billing address and information here.');
          break;
      }

      // Check if address may be automatically filled in.
      $auto_fill_address = variable_get('uc_addresses_default_' . $type . '_address', TRUE) && variable_get('uc_addresses_use_default_' . $type, TRUE);

      // Get address for order if set.
      if (isset($order->uc_addresses[$type])) {
        $address = $order->uc_addresses[$type];
      }
      elseif ($auto_fill_address) {

        // Get default address of type $type.
        $address = UcAddressesAddressBook::get($user->uid)
          ->getDefaultAddress($type);
        if ($address) {
          $address = $address
            ->copyAddress(UcAddressesAddressBook::get(0));
        }
      }

      // View the address form.
      $form[$uc_type] = array(
        '#type' => 'uc_addresses_address',
        '#uc_addresses_context' => 'checkout_form',
        '#key_prefix' => $uc_type,
      );
      if (!$address) {
        $address = UcAddressesAddress::newAddress();
      }

      // Add address to the form.
      $form[$uc_type]['#uc_addresses_address'] = $address;
      $form['uc_addresses_address'] = array(
        '#type' => 'value',
        '#value' => $address,
      );

      // Copy address checkbox.
      if ((uc_cart_is_shippable() || !variable_get('uc_cart_delivery_not_shippable', TRUE)) && _checkout_pane_data($other_type, 'weight') < _checkout_pane_data($uc_type, 'weight') && _checkout_pane_data($other_type, 'enabled')) {
        $form['copy_address'] = array(
          '#type' => 'checkbox',
          '#title' => $copy_address_title,
          '#attributes' => array(
            'class' => 'uc_addresses_copy_address',
            'onclick' => "uc_addresses_copy_address_checkout(this.checked, '{$other_type}', '{$uc_type}');",
          ),
          '#weight' => -5,
          '#default_value' => !empty($address->copy_address) ? TRUE : FALSE,
        );
      }

      // Address book selecting.
      if ($select = uc_addresses_select_address($user->uid, 'checkout_form', $uc_type, $form['uc_addresses_address']['#value'])) {
        $description = ($auto_fill_address ? t('Edit the address below or select an address from the list.') : t('Enter an address below or select an address from the list.')) . ' ' . t('Go to your <a href="@address-book">address book</a> to manage your saved addresses.', array(
          '@address-book' => url('user/' . $user->uid . '/addresses'),
        ));
        $form['addresses']['#title'] = t('Saved addresses');
        $form[$uc_type]['addressbook'] = $select;
      }
      return array(
        'description' => $description,
        'contents' => $form,
      );
      break;
    case 'process':
      $uc_type = $type;
      if ($type == 'shipping') {
        $uc_type = 'delivery';
      }
      foreach ($values[$uc_type] as $fieldname => $value) {
        $order->{$fieldname} = $value;
      }
      $address = $values['uc_addresses_address'];
      if ($address
        ->isNew() && !(isset($values['copy_address']) && $values['copy_address'])) {

        // Set flag that this address may be saved to the addressbook.
        $address->save_address = TRUE;
      }
      if (isset($values['copy_address']) && $values['copy_address']) {
        $address->copy_address = TRUE;
      }
      else {
        $address->copy_address = FALSE;
      }
      $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');
  }
}