You are here

function uc_addresses_order_pane_address in Ubercart Addresses 7

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

Callback for an address order pane.

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 order edit form.

array $form_state: The form state of the order edit form.

Return value

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

2 calls to uc_addresses_order_pane_address()
uc_addresses_order_pane_bill_to in ./uc_addresses.ubercart.inc
Callback for the "bill to" pane.
uc_addresses_order_pane_ship_to in ./uc_addresses.ubercart.inc
Callback for the "ship to" pane.

File

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

Code

function uc_addresses_order_pane_address($type, $op, $order, &$form = NULL, &$form_state = NULL) {
  if ($type == 'shipping') {
    $pane = 'ship_to';
    $other_pane = 'bill_to';
    $order_fieldname_prefix = 'delivery';
    $other_order_fieldname_prefix = 'billing';
    $copy_text = t('Copy billing information');
  }
  else {
    $pane = 'bill_to';
    $other_pane = 'ship_to';
    $order_fieldname_prefix = 'billing';
    $other_order_fieldname_prefix = 'delivery';
    $copy_text = t('Copy shipping information');
  }
  switch ($op) {
    case 'customer':
    case 'view':
      drupal_add_css(drupal_get_path('module', 'uc_addresses') . '/uc_addresses.css');
      $lines = array();
      $values = uc_addresses_preprocess_address($order->uc_addresses[$type], 'order_view');
      foreach ($values as $fieldname => $value) {
        if (isset($value['title']) && $value['title'] != '') {
          $lines[] = '<strong>' . $value['title'] . '</strong>: ' . $value['data'];
        }
        else {
          $lines[] = $value['data'] . '<br />';
        }
      }
      $build = array(
        '#markup' => implode('<br />', $lines),
      );
      return $build;
    case 'edit-form':

      // Tell address book we want to get multiple addresses.
      UcAddressesAddressBook::get($order->uid)
        ->setPerformanceHint(UcAddressesAddressBook::PERF_HINT_LOAD_ALL);
      $form[$pane] = array(
        '#tree' => TRUE,
      );

      // Get address for order if set.
      if (isset($order->uc_addresses[$type])) {
        $address = $order->uc_addresses[$type];
      }
      if (!$address) {
        $address = UcAddressesAddress::newAddress();
      }
      $select = uc_addresses_select_addresses($order->uid, 'order_form', $order_fieldname_prefix);
      if ($select) {
        $form[$pane]['select_address'] = $select + array(
          '#ajax' => array(
            'callback' => 'uc_addresses_address_render',
            'wrapper' => $pane . '-address-pane',
            'progress' => array(
              'type' => 'throbber',
            ),
            'event' => 'change',
          ),
          '#uc_addresses_address_element' => array(
            'panes',
            $pane,
            'address',
          ),
          '#uc_addresses_address_input' => array(
            'panes',
            $pane,
            'address',
          ),
          '#default_value' => $address,
        );
      }

      // Show copy address button if other pane is enabled.
      $panes = _uc_order_pane_list('edit');
      if (in_array('edit', $panes[$other_pane]['show']) && variable_get('uc_order_pane_' . $other_pane . '_show_edit', $panes[$other_pane]['enabled'])) {
        $form[$pane]['copy'] = array(
          '#type' => 'button',
          '#name' => $pane . '_copy',
          '#value' => $copy_text,
          '#ajax' => array(
            'callback' => 'uc_checkout_pane_address_render',
            'wrapper' => $pane . '-address-pane',
            'progress' => array(
              'type' => 'throbber',
            ),
          ),
          '#attributes' => array(
            'class' => array(
              'rules-switch-button',
              'uc-addresses-button',
              'uc-addresses-copy-button',
            ),
            'title' => $copy_text,
          ),
        );
      }
      if (isset($form_state['triggering_element'])) {
        $element =& $form_state['triggering_element'];
        switch ($element['#name']) {
          case $pane . '[select_address]':

            // An address is selected.
            $address_id = $element['#value'];
            $address_source = UcAddressesAddressBook::get($order->uid)
              ->getAddressById($address_id);
            if ($address_source instanceof UcAddressesAddress) {
              $address = $address_source
                ->copyAddress(UcAddressesAddressBook::get(0));
              $address_data = $address
                ->getRawFieldData();
              foreach ($address_data as $field => $value) {
                if (isset($order->{$order_fieldname_prefix . '_' . $field})) {
                  $form_state['input'][$pane]['address'][$order_fieldname_prefix . '_' . $field] = $value;
                  $order->{$order_fieldname_prefix . '_' . $field} = $value;
                }
              }
            }
            break;
          case $pane . '_copy':

            // Copy over address information from the other address pane
            // if the other address pane exists.
            if (isset($form_state['input'][$other_pane])) {
              foreach ($form_state['input'][$other_pane]['address'] as $source_fieldname => $value) {

                // Substract prefix from fieldname.
                $fieldname = substr($source_fieldname, strlen($other_order_fieldname_prefix) + 1);
                $target_fieldname = $order_fieldname_prefix . '_' . $fieldname;

                // Copy over value to current address pane if the fieldname is not 'aid'.
                if ($fieldname != 'aid' && isset($order->{$target_fieldname})) {

                  // Copy.
                  $value = $form_state['input'][$other_pane]['address'][$source_fieldname];
                  if ($address
                    ->fieldExists($fieldname)) {
                    $address
                      ->setField($fieldname, $value);
                  }
                  $order->{$target_fieldname} = $value;
                  $form_state['input'][$pane]['address'][$target_fieldname] = $value;
                }
              }
            }
            break;
        }

        // Forget any previous Ajax submissions, as we send new default values.
        unset($form_state['uc_addresses_address']);
      }

      // View the address form.
      $form[$pane]['address'] = array(
        '#type' => 'uc_addresses_address',
        '#uc_addresses_address' => $address,
        '#uc_addresses_context' => 'order_form',
        '#prefix' => '<div id="' . $pane . '-address-pane">',
        '#suffix' => '</div>',
        '#key_prefix' => $order_fieldname_prefix,
      );

      /*
            // Form element for debug purposes, displays the address that is taken.
            $form[$pane]['address']['display'] = array(
              '#type' => 'item',
              '#title' => t('Address'),
              '#markup' => (string) $address,
              '#weight' => 100,
            );
      //*/
      return $form;
    case 'edit-theme':
      return '<div id="' . $pane . '-pane">' . drupal_render($form[$pane]) . '</div>';
    case 'edit-process':
      $order_values = $form_state['values'];
      $address = $order->uc_addresses[$type];
      $changes = array();
      foreach ($order_values[$pane]['address'] as $key => $value) {

        // Check if this is an address field.
        if (strpos($key, $order_fieldname_prefix . '_') !== 0) {
          continue;
        }

        // Check if the value was changed.
        $fieldname = substr($key, strlen($order_fieldname_prefix) + 1);
        try {
          if ($address
            ->getField($fieldname) != $value && isset($order->{$key})) {
            $changes[$key] = $value;
          }
        } catch (UcAddressesException $e) {

          // Ignore any Ubercart Addresses exceptions.
        }
      }
      return $changes;
  }
}