You are here

function _uc_addresses_db_check_address in Ubercart Addresses 5.2

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

Before adding or updating an address, check it for errors.

Parameters

$address The address we are about to add or update.:

$op Either 'add' or 'update'.:

A boolean which, if TRUE, tells us not to display warnings: to the user.

Return value

TRUE if the address is valid, FALSE otherwise.

2 calls to _uc_addresses_db_check_address()
_uc_addresses_db_add_address in ./uc_addresses.module
Add a new address to the database table. If the address is already in the database (for this user), it is not added.
_uc_addresses_db_update_address in ./uc_addresses.module
Update an address in the database table.

File

./uc_addresses.module, line 1395

Code

function _uc_addresses_db_check_address($address, $op, $silent) {
  _uc_addresses_db_normalize_address($address);
  $result = db_query("SELECT aid FROM {uc_addresses} WHERE " . "uid = %d AND " . "first_name = '%s' AND " . "last_name = '%s' AND " . "phone = '%s' AND " . "company = '%s' AND " . "street1 = '%s' AND " . "street2 = '%s' AND " . "city = '%s' AND " . "zone = '%s' AND " . "postal_code = '%s' AND " . "country = '%s'", $address->uid, $address->first_name, $address->last_name, $address->phone, $address->company, $address->street1, $address->street2, $address->city, $address->zone, $address->postal_code, $address->country);
  $num_rows = 0;
  while ($db_address = db_fetch_object($result)) {
    $num_rows++;

    // If the address appears more than once, the database table is
    // corrupted. The fix, however, is simple: delete the extra
    // address
    if ($num_rows > 1) {
      drupal_set_message(t('This address appears more than once in your address book. ' . 'Please delete the duplicates and file a bug report.'), 'error');
      return FALSE;
    }

    // If we get one address, it's OK only if it is the address we're
    // trying to add or update
    if (!isset($address->aid) || $db_address->aid != $address->aid) {
      if (!$silent) {
        if ($op == 'add') {
          drupal_set_message(t('This address already appears in your address book. ' . 'A new address was not added.'), 'warning');
        }
        else {
          drupal_set_message(t('The revised address is already in your address book. ' . 'Your change was not made.'), 'warning');
        }
      }
      return FALSE;
    }
  }

  // Now check to make sure the address_name is not already in use
  // Unless its for the address we are trying to update
  if ($address->address_name) {
    $result = db_query("SELECT aid FROM {uc_addresses} WHERE " . "uid = %d AND " . "address_name = '%s'", $address->uid, $address->address_name);
    while ($db_address = db_fetch_object($result)) {
      if ($db_address->aid != $address->aid && !$silent) {
        drupal_set_message(t('The short name you selected for this address is already in your address book. ' . 'Please select a different name.'), 'error');
        return FALSE;
      }
    }
  }
  return TRUE;
}