You are here

function uc_addresses_get_select_addresses in Ubercart Addresses 7

Same name and namespace in other branches
  1. 6.2 uc_addresses.module \uc_addresses_get_select_addresses()

Returns an array of addresses to be used for the selecting address widget.

Parameters

int $uid: The user ID to select addresses for.

string $context: The context in which the addresses are used:

  • checkout_form
  • order_form

string $type: The type of address to select addresses for (shipping or billing).

Return value

array An array of addresses.

1 call to uc_addresses_get_select_addresses()
uc_addresses_select_addresses in ./uc_addresses.module
Widget for selecting an address.

File

./uc_addresses.module, line 1683
Adds user profile address support to Ubercart.

Code

function uc_addresses_get_select_addresses($uid, $context = 'default', $type = 'billing') {
  ctools_include('plugins');
  ctools_plugin_api_include('uc_addresses', 'uc_addresses_fields', 2, 2);

  // Ask each module for addresses.
  $addresses = array();
  foreach (module_implements('uc_addresses_select_addresses') as $module) {
    $new_addresses = module_invoke($module, 'uc_addresses_select_addresses', $uid, $context, $type);
    if (is_array($new_addresses) && count($new_addresses) > 0) {

      // Save source of each address and convert addresses arrays to UcAddressesAddress.
      foreach ($new_addresses as $index => $address) {
        $new_address = $address;
        if (is_object($address) && !$address instanceof UcAddressesAddress) {
          $address = (array) $address;
        }
        if (is_array($address)) {

          // Convert to UcAddressesAddress.
          $new_address = UcAddressesAddressBook::newAddress();
          $new_address
            ->setMultipleFields($address);
          $new_addresses[$index] = $new_address;

          // Check if the source module has been manually set.
          if (isset($address['module'])) {
            $new_address->module = $address['module'];
          }
        }
        if ($new_address instanceof UcAddressesAddress) {

          // Save the source the address came from (if not already set).
          if (!isset($new_address->module)) {
            $new_address->module = $module;
          }
        }
        else {

          // No valid address. Delete it from array.
          // @todo throw an exception here?
          unset($new_addresses[$index]);
        }
      }

      // Add to addresses array.
      $addresses = array_merge($addresses, $new_addresses);
    }
  }
  if (count($addresses) < 1) {

    // No addresses found.
    return array();
  }

  // Allow modules to alter the addresses (this hook is only invoked if there are addresses).
  drupal_alter('uc_addresses_select_addresses', $addresses, $uid, $context, $type);
  return $addresses;
}