/**
  * Check if the user can delete addresses of this user.
  * Default addresses can never be deleted.
  *
  * @param object $address_user
  *   The owner of the address.
  * @param UcAddressesAddress
  *   (optional) The address object.
  * @param object $account
  *   (optional) The account to check access for.
  *   Defaults to the current active user.
  *
  * @access public
  * @static
  * @return boolean
  *   TRUE if the given user has permission to delete the address.
  *   FALSE otherwise.
  */
 public static function canDeleteAddress($address_user, UcAddressesAddress $address = NULL, $account = NULL)
 {
     $account = self::getAccount($account);
     if ($address instanceof UcAddressesAddress) {
         // Check if the address is a default address. If so, the address may not be deleted.
         if ($address->isDefault('shipping') || $address->isDefault('billing')) {
             return FALSE;
         }
     }
     if ($address_user->uid == $account->uid && self::canDeleteOwn($account)) {
         // Ask other modules if the address may be deleted.
         return self::invoke('uc_addresses_may_delete', $address_user, $address, $account);
     }
     if ($address_user->uid != $account->uid && self::canDeleteAll($account)) {
         // Ask other modules if the address may be deleted.
         return self::invoke('uc_addresses_may_delete', $address_user, $address, $account);
     }
     // No other cases are permitted.
     return FALSE;
 }
 /**
  * Creates UcAddressesAddress objects from a database resource.
  *
  * @param resource $result
  *   Database result.
  *
  * @access private
  * @return void
  */
 private function dbResultToAddresses($result)
 {
     // Create each UcAddressesAddress object from the database record.
     $loaded_addresses = array();
     foreach ($result as $obj) {
         // Skip addresses that have already been loaded (and perhaps modified).
         if (!isset($this->addresses[$obj->aid])) {
             $address = new UcAddressesAddress($this, $obj);
             if ($address->isDefault('shipping')) {
                 $this->defaultAddresses['shipping'] = $address;
             }
             if ($address->isDefault('billing')) {
                 $this->defaultAddresses['billing'] = $address;
             }
             // Give other modules a chance to add their fields.
             module_invoke_all('uc_addresses_address_load', $address, $obj);
             $loaded_addresses[$obj->aid] = $address;
         }
     }
     if (count($loaded_addresses) > 0) {
         // Invoke entity load hook.
         entity_get_controller('uc_addresses')->invokeLoad($loaded_addresses);
     }
 }
/**
 * This hook allows you to prevent a certain address from being edited.
 *
 * Don't use this hook if you want to prevent editing addresses for users
 * with a certain role. You can use the permission settings for that.
 *
 * If you want the address not to be edited return FALSE.
 * Return TRUE in all other cases.
 * WARNING: If you don't return TRUE, then no address may be edited.
 *
 * Note that this hook is only invoked when permissions are checked and not
 * when changes to an address are done programmatically.
 *
 * @param object $address_user
 *   The owner of the address.
 * @param UcAddressesAddress $address
 *   (optional) Address object.
 * @param object $account
 *   The account to check access for.
 *
 * @return boolean
 *   FALSE if the account may not edit the address or any address from
 *   the address user if no address is passed.
 *   TRUE otherwise.
 */
function hook_uc_addresses_may_edit($address_user, $address, $account)
{
    // Example: don't allow editing of default addresses.
    if ($address instanceof UcAddressesAddress) {
        if ($address->isDefault('shipping') || $address->isDefault('billing')) {
            return FALSE;
        }
    }
    // In all other cases, the address may be edited.
    return TRUE;
}