You are here

function uc_addresses_form_alter in Ubercart Addresses 5.2

Same name and namespace in other branches
  1. 5 uc_addresses.module \uc_addresses_form_alter()

Implementation of hook_form_alter().

Here we're going to override the saved address options on the checkout form.

Parameters

$form_id The name of the form.:

$form The form array.:

File

./uc_addresses.module, line 250

Code

function uc_addresses_form_alter($form_id, &$form) {
  global $user;

  // This is a good place to clear any addresses we might have
  // leftover from a previous checkout
  if (isset($_SESSION['uc_addresses_saved_addresses'])) {
    $_SESSION['uc_addresses_saved_addresses'] = null;
    unset($_SESSION['uc_addresses_saved_addresses']);
  }
  if ($form_id == 'uc_cart_checkout_form') {

    // Create the list of addresses the user can select from
    // Each address is just line a full address, but on one line
    $options = NULL;
    if ($addresses = _uc_addresses_db_get_address($user->uid)) {
      $options = array(
        '0' => t('Select one...'),
      );
      foreach ($addresses as $address) {
        $address = (array) $address;
        if ($address['address_name']) {
          $options[drupal_to_js($address)] = $address['address_name'];
        }
        else {

          // Not happy about this--if uc_address_format() ever changes
          // it's output, we're hosed
          $options[drupal_to_js($address)] = preg_replace('/<.*?>/', ', ', uc_address_format($address['first_name'], $address['last_name'], $address['company'], $address['street1'], $address['street2'], $address['city'], $address['zone'], $address['postal_code'], $address['country']));
        }
      }
    }
    $address_book_icon = l(uc_store_get_icon('file:address_book', FALSE, 'address-book-icon'), 'user/' . $user->uid . '/addresses', array(), NULL, NULL, FALSE, TRUE);

    // If we have some addresses saved (almost always true), revise
    // the delivery/billing address selection
    if ($options) {

      // Might not have any shippable products so make sure the
      // delivery address exists before mucking with it
      if ($form['panes']['delivery'] && (uc_cart_is_shippable() || !variable_get('uc_cart_delivery_not_shippable', TRUE))) {
        if (variable_get('uc_pane_delivery_enabled', TRUE)) {
          $form['panes']['delivery']['#description'] = (variable_get('uc_addresses_default_delivery_address', TRUE) ? 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('Click !here to manage your saved addresses.', array(
            '!here' => l(t('here'), 'user/' . $user->uid . '/addresses'),
          ));
          $form['panes']['delivery']['delivery_address_select'] = array(
            '#type' => 'select',
            '#title' => t('Saved addresses'),
            '#options' => $options,
            '#attributes' => array(
              'onchange' => 'apply_address(\'delivery\', this.value);',
            ),
            '#suffix' => $address_book_icon,
            '#weight' => -10,
          );
        }
      }
      if (variable_get('uc_pane_billing_enabled', TRUE)) {
        $form['panes']['billing']['#description'] = (variable_get('uc_addresses_default_billing_address', TRUE) ? 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('Click !here to manage your saved addresses.', array(
          '!here' => l(t('here'), 'user/' . $user->uid . '/addresses'),
        ));
        $form['panes']['billing']['billing_address_select'] = array(
          '#type' => 'select',
          '#title' => t('Saved addresses'),
          '#options' => $options,
          '#attributes' => array(
            'onchange' => 'apply_address(\'billing\', this.value);',
          ),
          '#suffix' => $address_book_icon,
          '#weight' => -10,
        );
      }

      // Copy the default address to the delivery address fields if
      // the option is enabled and if the fields are empty
      if ($form['panes']['delivery'] && (uc_cart_is_shippable() || !variable_get('uc_cart_delivery_not_shippable', TRUE))) {
        if (variable_get('uc_addresses_default_delivery_address', TRUE) && variable_get('uc_pane_delivery_enabled', TRUE) && _uc_addresses_address_fields_empty($form['panes']['delivery'], 'delivery')) {
          foreach ($addresses as $address) {
            if ($address->is_default) {
              if (uc_address_field_enabled('first_name')) {
                $form['panes']['delivery']['delivery_first_name']['#default_value'] = $address->first_name;
              }
              if (uc_address_field_enabled('last_name')) {
                $form['panes']['delivery']['delivery_last_name']['#default_value'] = $address->last_name;
              }
              if (uc_address_field_enabled('phone')) {
                $form['panes']['delivery']['delivery_phone']['#default_value'] = $address->phone;
              }
              if (uc_address_field_enabled('company')) {
                $form['panes']['delivery']['delivery_company']['#default_value'] = $address->company;
              }
              if (uc_address_field_enabled('street1')) {
                $form['panes']['delivery']['delivery_street1']['#default_value'] = $address->street1;
              }
              if (uc_address_field_enabled('street2')) {
                $form['panes']['delivery']['delivery_street2']['#default_value'] = $address->street2;
              }
              if (uc_address_field_enabled('city')) {
                $form['panes']['delivery']['delivery_city']['#default_value'] = $address->city;
              }
              if (uc_address_field_enabled('country')) {
                $form['panes']['delivery']['delivery_country']['#default_value'] = $address->country;
              }
              if (uc_address_field_enabled('zone')) {
                $form['panes']['delivery']['delivery_zone'] = uc_zone_select(uc_get_field_name('zone'), $address->zone, NULL, $address->country, 'name', uc_address_field_required('zone'));
              }
              if (uc_address_field_enabled('postal_code')) {
                $form['panes']['delivery']['delivery_postal_code']['#default_value'] = $address->postal_code;
              }
            }
          }
        }
      }

      // Copy the default address to the billing address fields if the
      // option is enabled and if the fields are empty
      if (variable_get('uc_addresses_default_billing_address', TRUE) && variable_get('uc_pane_billing_enabled', TRUE) && _uc_addresses_address_fields_empty($form['panes']['billing'], 'billing')) {
        foreach ($addresses as $address) {
          if ($address->is_default) {
            if (uc_address_field_enabled('first_name')) {
              $form['panes']['billing']['billing_first_name']['#default_value'] = $address->first_name;
            }
            if (uc_address_field_enabled('last_name')) {
              $form['panes']['billing']['billing_last_name']['#default_value'] = $address->last_name;
            }
            if (uc_address_field_enabled('phone')) {
              $form['panes']['billing']['billing_phone']['#default_value'] = $address->phone;
            }
            if (uc_address_field_enabled('company')) {
              $form['panes']['billing']['billing_company']['#default_value'] = $address->company;
            }
            if (uc_address_field_enabled('street1')) {
              $form['panes']['billing']['billing_street1']['#default_value'] = $address->street1;
            }
            if (uc_address_field_enabled('street2')) {
              $form['panes']['billing']['billing_street2']['#default_value'] = $address->street2;
            }
            if (uc_address_field_enabled('city')) {
              $form['panes']['billing']['billing_city']['#default_value'] = $address->city;
            }
            if (uc_address_field_enabled('country')) {
              $form['panes']['billing']['billing_country']['#default_value'] = $address->country;
            }
            if (uc_address_field_enabled('zone')) {
              $form['panes']['billing']['billing_zone'] = uc_zone_select(uc_get_field_name('zone'), $address->zone, NULL, $address->country, 'name', uc_address_field_required('zone'));
            }
            if (uc_address_field_enabled('postal_code')) {
              $form['panes']['billing']['billing_postal_code']['#default_value'] = $address->postal_code;
            }
          }
        }
      }
    }
    else {
      unset($form['panes']['delivery']['delivery_address_select']);
      unset($form['panes']['billing']['billing_address_select']);
    }

    // Add the "Save address" checkbox
    // TODO: (Tony) I can add these, but don't know how to find out if
    // the checkbox was checked. For now, all order addresses are
    // automatically saved.
    //     $form['panes']['billing']['billing_address_save_address'] = array(
    //       '#title' => t('Save this address'),
    //       '#type' => 'checkbox',
    //       '#default_value' => 1,
    //       '#weight' => 10
    //       );
    //     $form['panes']['delivery']['delivery_address_save_address'] = array(
    //       '#title' => t('Save this address'),
    //       '#type' => 'checkbox',
    //       '#default_value' => 1,
    //       '#weight' => 10
    //       );
  }
}