function _uc_addresses_db_add_address in Ubercart Addresses 5.2
Same name and namespace in other branches
- 5 uc_addresses.module \_uc_addresses_db_add_address()
- 6 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).:
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. If $view is 'edit' run update database function, if 'new' or 'add', run insert database function.
- uc_addresses_order in ./
uc_addresses.module - Use hook_order to add an address or two to the user's address list. For the moment, we save all addresses. I'd like to add an option to let the user select which addresses get saved.
- uc_addresses_user in ./
uc_addresses.module - Implementation of hook_user().
File
- ./
uc_addresses.module, line 1206
Code
function _uc_addresses_db_add_address($address, $silent = FALSE) {
global $user;
// If this is an anonymous user, save the address for later
if ($address->uid == 0 && isset($address->first_name)) {
$saved_addresses = NULL;
if (isset($_SESSION['uc_addresses_saved_addresses'])) {
$saved_addresses = $_SESSION['uc_addresses_saved_addresses'];
}
if (!is_array($saved_addresses)) {
$saved_addresses = array();
}
$saved_addresses[] = drupal_clone($address);
$_SESSION['uc_addresses_saved_addresses'] = $saved_addresses;
return;
}
// If we have a logged in user but no address information, then some
// module created a user without going through the normal
// registration procedure. Let's see if we have any address
// information saved for this user
if ($address->uid != 0 && !isset($address->first_name)) {
if (isset($_SESSION['uc_addresses_saved_addresses'])) {
$saved_addresses = $_SESSION['uc_addresses_saved_addresses'];
if (is_array($saved_addresses)) {
foreach ($saved_addresses as $addr) {
$addr->uid = $address->uid;
$status = _uc_addresses_db_add_address($addr, true);
if ($status === FALSE) {
return FALSE;
}
}
}
$_SESSION['uc_addresses_saved_addresses'] = null;
unset($_SESSION['uc_addresses_saved_addresses']);
}
// Let this user get created without an address
return TRUE;
}
// Final special case: no logged in user and no address information!
if ($address->uid == 0) {
return TRUE;
}
// 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
$aid = db_next_id('{uc_addresses}_aid');
db_query("INSERT INTO {uc_addresses} (aid, uid, first_name, last_name, " . "phone, company, street1, street2, city, zone, postal_code, country, " . "address_name, " . "created, modified) VALUES (%d, %d, " . "'%s', '%s', '%s', " . "'%s', '%s', '%s', " . "'%s', %d, '%s', %d, " . "'%s', " . "%d, %d)", $aid, $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());
// 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;
}