You are here

function _uc_addresses_db_get_address in Ubercart Addresses 5.2

Same name and namespace in other branches
  1. 5 uc_addresses.module \_uc_addresses_db_get_address()
  2. 6 uc_addresses.module \_uc_addresses_db_get_address()

Get an address or list of addresses from the database.

Parameters

$address_user The id of the user who "owns" the address.:

$aid The id of the address to fetch. If NULL, fetch: all addresses owned by the user.

Return value

FALSE on error. An object if $aid was not NULL. An array of objects if $aid was NULL.

4 calls to _uc_addresses_db_get_address()
uc_addresses_delete_address_confirm in ./uc_addresses.module
Display a confirmation page before deleting an address.
uc_addresses_form_alter in ./uc_addresses.module
Implementation of hook_form_alter().
uc_addresses_get_address_form in ./uc_addresses.module
Create a form used to add a new address or edit an existing address.
uc_addresses_list_addresses in ./uc_addresses.module
Generate a list of one or all addresses defined by one user and then theme the list for display.

File

./uc_addresses.module, line 1522

Code

function _uc_addresses_db_get_address($address_user, $aid = NULL) {

  // If $aid is present, return just the one address
  if ($aid) {
    $result = db_query("SELECT * FROM {uc_addresses} WHERE aid = %d ", $aid);

    // Check to make sure there is data
    if (($address = db_fetch_object($result)) == FALSE) {
      return FALSE;
    }

    // Check to make sure it's from the right user
    if ($address->uid != $address_user) {
      return FALSE;
    }

    // Find out if this is the default address
    $address->is_default = 0;
    $def = db_query("SELECT aid FROM {uc_addresses_defaults} WHERE uid = %d", $address_user);
    if ($default_aid = db_fetch_object($def)) {
      $address->is_default = $default_aid->aid == $address->aid;
    }

    // All OK
    return $address;
  }

  // If $aid is NULL, return all addresses for that user
  $result = db_query("SELECT * FROM {uc_addresses} WHERE uid = %d ORDER BY created", $address_user);
  $default_aid = _uc_addresses_get_default_address_id($address_user);

  // Gather up everything
  $num_rows = 0;
  while ($address = db_fetch_object($result)) {
    $num_rows++;
    $address->is_default = $default_aid == $address->aid;
    $addresses[] = $address;
  }
  if ($num_rows == 0) {
    return FALSE;
  }
  return $addresses;
}