You are here

function uc_checkout_pane_address in Ubercart 7.3

Generic address pane handler.

2 calls to uc_checkout_pane_address()
uc_checkout_pane_billing in uc_cart/uc_cart_checkout_pane.inc
Gets the billing information.
uc_checkout_pane_delivery in uc_cart/uc_cart_checkout_pane.inc
Gets the delivery information.

File

uc_cart/uc_cart_checkout_pane.inc, line 198
Callbacks for the default Ubercart checkout panes plus helper functions.

Code

function uc_checkout_pane_address($pane, $op, $order, &$form_state, $description, $copy) {
  global $user;

  // Source pane for "copy address" checkbox.
  static $source;
  if (!isset($source)) {
    $source = $pane;
  }
  switch ($op) {
    case 'view':
      if ($source != $pane) {
        $contents['copy_address'] = array(
          '#type' => 'checkbox',
          '#title' => $copy,
          '#default_value' => variable_get('uc_cart_default_same_address', FALSE),
          '#ajax' => array(
            'callback' => 'uc_checkout_pane_address_render',
            'wrapper' => $pane . '-address-pane',
            'progress' => array(
              'type' => 'throbber',
            ),
          ),
        );
      }
      if ($user->uid && ($addresses = uc_select_addresses($user->uid, $pane))) {
        $contents['select_address'] = array(
          '#type' => 'select',
          '#title' => t('Saved addresses'),
          '#options' => $addresses['#options'],
          '#ajax' => array(
            'callback' => 'uc_checkout_pane_address_render',
            'wrapper' => $pane . '-address-pane',
            'progress' => array(
              'type' => 'throbber',
            ),
          ),
          '#states' => array(
            'invisible' => array(
              'input[name="panes[' . $pane . '][copy_address]"]' => array(
                'checked' => TRUE,
              ),
            ),
          ),
        );
      }
      $contents['address'] = array(
        '#type' => 'uc_address',
        '#default_value' => $order,
        '#key_prefix' => $pane,
        '#prefix' => '<div id="' . $pane . '-address-pane">',
        '#suffix' => '</div>',
      );
      if (isset($form_state['values']['panes'][$pane]['copy_address'])) {
        $contents['address']['#hidden'] = !empty($form_state['values']['panes'][$pane]['copy_address']);
      }
      elseif (isset($contents['copy_address'])) {
        $contents['address']['#hidden'] = variable_get('uc_cart_default_same_address', FALSE);
      }

      // If this was an Ajax request, update form input values for the
      // copy and select address features.
      if (isset($form_state['triggering_element'])) {
        $element =& $form_state['triggering_element'];
        if ($element['#name'] == "panes[{$pane}][copy_address]") {
          $address =& $form_state['values']['panes'][$source];
          foreach ($address as $field => $value) {
            if (substr($field, 0, strlen($source)) == $source) {
              $field = str_replace($source, $pane, $field);
              $form_state['input']['panes'][$pane][$field] = $value;
            }
          }
        }
        if ($element['#name'] == "panes[{$pane}][select_address]" && isset($addresses[$element['#value']])) {
          $address = $addresses[$element['#value']];
          foreach ($address as $field => $value) {
            $form_state['input']['panes'][$pane][$pane . '_' . $field] = $value;
          }
        }

        // Forget any previous Ajax submissions, as we send new default values.
        unset($form_state['uc_address']);
      }
      return array(
        'description' => $description,
        'contents' => $contents,
      );
    case 'process':
      $panes =& $form_state['values']['panes'];
      foreach ($panes[$pane] as $field => $value) {
        if (substr($field, 0, strlen($pane)) == $pane) {
          if (!empty($panes[$pane]['copy_address'])) {
            $value = $panes[$source][str_replace($pane, $source, $field)];
          }
          $order->{$field} = $value;
        }
      }
      if (isset($panes[$pane]['select_address']) && $panes[$pane]['select_address'] >= 0) {
        $addresses = uc_select_addresses($user->uid, $pane);
        $address = $addresses[$panes[$pane]['select_address']];
        foreach ($address as $field => $value) {
          $order->{$pane . '_' . $field} = $value;
        }
      }
      return TRUE;
    case 'review':
      $review[] = array(
        'title' => t('Address'),
        'data' => uc_order_address($order, $pane, FALSE),
      );
      if (uc_address_field_enabled('phone') && !empty($order->{$pane . '_phone'})) {
        $review[] = array(
          'title' => t('Phone'),
          'data' => check_plain($order->{$pane . '_phone'}),
        );
      }
      return $review;
  }
}