function _uc_addresses_db_add_address in Ubercart Addresses 6
Same name and namespace in other branches
- 5.2 uc_addresses.module \_uc_addresses_db_add_address()
- 5 uc_addresses.module \_uc_addresses_db_add_address()
Add a new address to the database table. If the address is already in the database (for this user), it is not added.
Parameters
$address The address to add (as an object).:
$silent boolean TRUE if no warnings should be displayed to the user.:
Return value
The id of the new address or FALSE if there was an error.
3 calls to _uc_addresses_db_add_address()
- uc_addresses_get_address_form_submit in ./
uc_addresses.module - Handle the form submit for adding a new address or editing an existing address.
- uc_addresses_uc_checkout_complete in ./
uc_addresses.module - Use hook_uc_checkout_complete to catch any new addresses.
- uc_addresses_user in ./
uc_addresses.module - Implementation of hook_user().
File
- ./
uc_addresses.module, line 1245
Code
function _uc_addresses_db_add_address($address, $silent = FALSE) {
// No user -- shouldn't happen
if ($address->uid == 0) {
trigger_error(t('The address can not be saved because it is not owned by an user.'));
watchdog('uc_addresses', 'Tried to save an unowned address.', array(), WATCHDOG_WARNING);
return FALSE;
}
// Check if at least one address field is filled in
$is_empty = TRUE;
$fields = variable_get('uc_address_fields', drupal_map_assoc(array(
'first_name',
'last_name',
'phone',
'company',
'street1',
'street2',
'city',
'zone',
'postal_code',
'country',
)));
foreach ($fields as $fieldname) {
if (isset($address->{$fieldname}) && drupal_strlen($address->{$fieldname}) > 0) {
$is_empty = FALSE;
break;
}
}
if ($is_empty) {
// No address -- may happen
return FALSE;
}
// From this point on, we have both a user and an address to add
// We need to work with systems where this module is added when
// users are already present. Find out how many addresses this user has
$num_addresses = db_result(db_query("SELECT COUNT(*) FROM {uc_addresses} WHERE uid = %d", $address->uid));
// If this is the first address the user has ever added, make it the
// default address
if ($num_addresses < 1) {
$address->is_default = 1;
}
if ($num_addresses > 0) {
// Check for problems
$result = _uc_addresses_db_check_address($address, 'add', $silent);
if (!$result) {
return FALSE;
}
}
else {
_uc_addresses_db_normalize_address($address);
}
// Add the address
db_query("INSERT INTO {uc_addresses} (" . "uid, first_name, last_name, " . "phone, company, street1, street2, city, zone, postal_code, country, " . "address_name, " . "created, modified) " . "VALUES (" . "%d, '%s', '%s', " . "'%s', '%s', '%s', '%s', '%s', %d, '%s', %d, " . "'%s', " . "%d, %d)", $address->uid, $address->first_name, $address->last_name, $address->phone, $address->company, $address->street1, $address->street2, $address->city, $address->zone, $address->postal_code, is_null($address->country) || $address->country == 0 ? variable_get('uc_store_country', 840) : $address->country, $address->address_name, time(), time());
$aid = db_last_insert_id('uc_addresses', 'aid');
// Update the default address, if necessary
if ($address->is_default) {
if ($num_addresses < 1) {
db_query("INSERT INTO {uc_addresses_defaults} (aid, uid) VALUES (%d, %d)", $aid, $address->uid);
}
else {
db_query("UPDATE {uc_addresses_defaults} SET aid = %d WHERE uid = %d", $aid, $address->uid);
}
}
return $aid;
}