You are here

public function UcAddressesAddressBook::addAddress in Ubercart Addresses 7

Same name and namespace in other branches
  1. 6.2 class/UcAddressesAddressBook.class.php \UcAddressesAddressBook::addAddress()

Adds address to address book.

@access public

Parameters

UcAddressesAddress $address: (optional) An instance of UcAddressesAddress to add. Defaults to a new instance of UcAddressesAddress.

Return value

UcAddressesAddress The instance of UcAddressesAddress that was added.

Throws

UcAddressesInvalidParameterException

UcAddressesNameCollisionException

File

class/UcAddressesAddressBook.class.php, line 266
Contains the UcAddressesAddressBook class.

Class

UcAddressesAddressBook
The address book class

Code

public function addAddress(UcAddressesAddress $address = NULL) {

  // If we add an address, then we'll probably save it, which
  // requires loading all addresses for error checking.
  if ($this->performanceHint == self::PERF_HINT_LOAD_ONE) {
    $this->performanceHint = self::PERF_HINT_LOAD_ALL;
  }
  if ($address) {

    // In case of a new address with an address name,
    // load other addresses to do a name check comparison.
    if ($address
      ->isNew() && $address
      ->getName() && !$this->allLoaded) {
      $this
        ->loadAll();
    }

    // Check if address is already in addressbook.
    foreach ($this->addresses as $aid => $addressBookAddress) {
      if ($address === $addressBookAddress) {
        throw new UcAddressesInvalidParameterException(t('Tried to add an address already in the address book'));
      }
      if ($address
        ->getName() && $address
        ->getName() == $addressBookAddress
        ->getName()) {
        throw new UcAddressesNameCollisionException(t('Tried to add an address with a name matching that of an address already in the address book'));
      }
    }

    // Check if address belongs to this address book.
    if ($address
      ->getAddressBook() !== $this && $address
      ->isOwned()) {
      throw new UcAddressesInvalidParameterException(t('Tried to add an address already in an other address book'));
    }
  }
  if (!$address) {
    $address = new UcAddressesAddress($this);
  }
  $this->addresses[$address
    ->getId()] = $address;
  if ($address
    ->isDefault('shipping')) {
    $this->defaultAddresses['shipping'] = $address;
  }
  if ($address
    ->isDefault('billing')) {
    $this->defaultAddresses['billing'] = $address;
  }

  // Make sure this becomes one of our addresses.
  if ($address
    ->getAddressBook() !== $this) {
    $address
      ->privChangeAddressBook($this);
  }
  return $address;
}