View source
<?php
require_once 'uc_addresses_address_pane.inc';
define('UC_ADDRESSES_ACCESS_VIEW_DEFAULT', 'view default addresses');
define('UC_ADDRESSES_ACCESS_VIEW_ALL', 'view all addresses');
define('UC_ADDRESSES_ACCESS_ADD_EDIT', 'add/edit addresses');
function uc_addresses_menu($may_cache) {
global $user;
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/store/settings/addresses',
'title' => t('Address settings'),
'description' => t('Configure the address settings.'),
'callback' => 'uc_addresses_settings_overview',
'access' => user_access('administer store'),
'type' => MENU_NORMAL_ITEM,
);
$items[] = array(
'path' => 'admin/store/settings/addresses/overview',
'title' => t('Overview'),
'access' => user_access('administer store'),
'description' => t('View the address settings.'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items[] = array(
'path' => 'admin/store/settings/addresses/edit',
'title' => t('Edit'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'uc_addresses_settings_form',
),
'access' => user_access('administer store'),
'description' => t('Edit the address settings.'),
'type' => MENU_LOCAL_TASK,
'weight' => -5,
);
}
else {
drupal_add_css(drupal_get_path('module', 'uc_order') . '/uc_order.css');
if (is_numeric(arg(1)) && intval(arg(1)) == arg(1)) {
$items[] = array(
'path' => 'user/' . arg(1) . '/addresses',
'title' => t('Addresses'),
'description' => t('Manage your addresses'),
'callback' => 'uc_addresses_list_addresses',
'callback arguments' => array(
arg(1),
),
'access' => user_access(UC_ADDRESSES_ACCESS_VIEW_DEFAULT) || user_access(UC_ADDRESSES_ACCESS_VIEW_ALL) || user_access(UC_ADDRESSES_ACCESS_ADD_EDIT) || $user->uid == arg(1),
'type' => MENU_LOCAL_TASK,
'weight' => 1,
);
if (!(is_numeric(arg(3)) && intval(arg(3)) == arg(3))) {
$items[] = array(
'path' => 'user/' . arg(1) . '/addresses/list',
'title' => t('Address list'),
'description' => t('Manage your addresses'),
'access' => user_access(UC_ADDRESSES_ACCESS_VIEW_DEFAULT) || user_access(UC_ADDRESSES_ACCESS_VIEW_ALL) || user_access(UC_ADDRESSES_ACCESS_ADD_EDIT) || $user->uid == arg(1),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 0,
);
$items[] = array(
'path' => 'user/' . arg(1) . '/addresses/add',
'title' => t('Add address'),
'description' => t('Add a new address.'),
'callback' => 'uc_addresses_get_address',
'callback arguments' => array(
arg(1),
NULL,
'add',
),
'access' => user_access(UC_ADDRESSES_ACCESS_ADD_EDIT) || $user->uid == arg(1),
'type' => MENU_LOCAL_TASK,
'weight' => 3,
);
}
else {
$items[] = array(
'path' => 'user/' . arg(1) . '/addresses/' . arg(3),
'title' => t('View Address'),
'description' => t('View one saved address'),
'callback' => 'uc_addresses_list_addresses',
'callback arguments' => array(
arg(1),
arg(3),
),
'access' => user_access(UC_ADDRESSES_ACCESS_VIEW_DEFAULT) || user_access(UC_ADDRESSES_ACCESS_VIEW_ALL) || user_access(UC_ADDRESSES_ACCESS_ADD_EDIT) || $user->uid == arg(1),
'type' => MENU_CALLBACK,
);
$items[] = array(
'path' => 'user/' . arg(1) . '/addresses/' . arg(3) . '/edit',
'title' => t('Edit address'),
'callback' => 'uc_addresses_get_address',
'callback arguments' => array(
arg(1),
arg(3),
'edit',
),
'access' => user_access(UC_ADDRESSES_ACCESS_ADD_EDIT) || $user->uid == arg(1),
'type' => MENU_CALLBACK,
);
$items[] = array(
'path' => 'user/' . arg(1) . '/addresses/' . arg(3) . '/delete',
'title' => t('Delete address'),
'callback' => 'uc_addresses_delete_address_confirm',
'callback arguments' => array(
arg(1),
arg(3),
),
'access' => user_access(UC_ADDRESSES_ACCESS_ADD_EDIT) || $user->uid == arg(1),
'type' => MENU_CALLBACK,
);
}
}
}
return $items;
}
function uc_addresses_perm() {
return array(
UC_ADDRESSES_ACCESS_VIEW_DEFAULT,
UC_ADDRESSES_ACCESS_VIEW_ALL,
UC_ADDRESSES_ACCESS_ADD_EDIT,
);
}
function uc_addresses_user($op, &$edit, &$account, $category = NULL) {
global $user;
switch ($op) {
case 'view':
if ($user->uid == $account->uid) {
$items = array();
$items['addresses'] = array(
'value' => l(t('Click here to manage your addresses.'), 'user/' . $account->uid . '/addresses'),
'class' => 'member',
);
return array(
t('Addresses') => $items,
);
}
else {
return NULL;
}
case 'register':
if (variable_get('uc_addresses_require_address', TRUE)) {
$form = uc_addresses_pane_address('new', $arg1, $arg2);
$form = array(
$form['contents'],
);
$form[0]['#title'] = t('Address');
return $form;
}
case 'insert':
$address = (object) $edit;
$address->is_default = 0;
_uc_addresses_db_add_address($address);
return;
case 'delete':
db_query("DELETE FROM {uc_addresses} WHERE uid = %d", $account->uid);
db_query("DELETE FROM {uc_addresses_defaults} WHERE uid = %d", $account->uid);
return;
}
}
function uc_addresses_form_alter($form_id, &$form) {
global $user;
if ($form_id == 'uc_cart_checkout_form') {
$options = NULL;
if ($addresses = _uc_addresses_db_get_address($user->uid)) {
$options = array(
'0' => t('Select one...'),
);
foreach ($addresses as $address) {
$address = (array) $address;
if ($address['address_name']) {
$options[drupal_to_js($address)] = $address['address_name'];
}
else {
$options[drupal_to_js($address)] = preg_replace('/<.*?>/', ', ', uc_address_format($address['first_name'], $address['last_name'], $address['company'], $address['street1'], $address['street2'], $address['city'], $address['zone'], $address['postal_code'], $address['country']));
}
}
}
$address_book_icon = l(uc_store_get_icon('file:address_book', FALSE, 'address-book-icon'), 'user/' . $user->uid . '/addresses', array(), NULL, NULL, FALSE, TRUE);
if ($options) {
if ($form['panes']['delivery']) {
if (variable_get('uc_pane_delivery_enabled', TRUE)) {
$form['panes']['delivery']['#description'] = (variable_get('uc_addresses_default_delivery_address', TRUE) ? t('Edit the address below or select an address from the list. ') : t('Enter an address below or select an address from the list. ')) . t('Click !here to manage your saved addresses.', array(
'!here' => l('here', 'user/' . $user->uid . '/addresses'),
));
$form['panes']['delivery']['delivery_address_select'] = array(
'#type' => 'select',
'#title' => t('Saved addresses'),
'#options' => $options,
'#attributes' => array(
'onchange' => 'apply_address(\'delivery\', this.value);',
),
'#suffix' => $address_book_icon,
'#weight' => -10,
);
}
}
if (variable_get('uc_pane_billing_enabled', TRUE)) {
$form['panes']['billing']['#description'] = (variable_get('uc_addresses_default_billing_address', TRUE) ? t('Edit the address below or select an address from the list. ') : t('Enter an address below or select an address from the list. ')) . t('Click !here to manage your saved addresses.', array(
'!here' => l(t('here'), 'user/' . $user->uid . '/addresses'),
));
$form['panes']['billing']['billing_address_select'] = array(
'#type' => 'select',
'#title' => t('Saved addresses'),
'#options' => $options,
'#attributes' => array(
'onchange' => 'apply_address(\'billing\', this.value);',
),
'#suffix' => $address_book_icon,
'#weight' => -10,
);
}
if (variable_get('uc_addresses_default_delivery_address', TRUE) && variable_get('uc_pane_delivery_enabled', TRUE) && _uc_addresses_address_fields_empty($form['panes']['delivery'], 'delivery')) {
foreach ($addresses as $address) {
if ($address->is_default) {
if (uc_address_field_enabled('first_name')) {
$form['panes']['delivery']['delivery_first_name']['#default_value'] = $address->first_name;
}
if (uc_address_field_enabled('last_name')) {
$form['panes']['delivery']['delivery_last_name']['#default_value'] = $address->last_name;
}
if (uc_address_field_enabled('phone')) {
$form['panes']['delivery']['delivery_phone']['#default_value'] = $address->phone;
}
if (uc_address_field_enabled('company')) {
$form['panes']['delivery']['delivery_company']['#default_value'] = $address->company;
}
if (uc_address_field_enabled('street1')) {
$form['panes']['delivery']['delivery_street1']['#default_value'] = $address->street1;
}
if (uc_address_field_enabled('street2')) {
$form['panes']['delivery']['delivery_street2']['#default_value'] = $address->street2;
}
if (uc_address_field_enabled('city')) {
$form['panes']['delivery']['delivery_city']['#default_value'] = $address->city;
}
if (uc_address_field_enabled('country')) {
$form['panes']['delivery']['delivery_country']['#default_value'] = $address->country;
}
if (uc_address_field_enabled('zone')) {
$form['panes']['delivery']['delivery_zone'] = uc_zone_select(uc_get_field_name('zone'), $address->zone, NULL, $address->country, 'name', uc_address_field_required('zone'));
}
if (uc_address_field_enabled('postal_code')) {
$form['panes']['delivery']['delivery_postal_code']['#default_value'] = $address->postal_code;
}
}
}
}
if (variable_get('uc_addresses_default_billing_address', TRUE) && variable_get('uc_pane_billing_enabled', TRUE) && _uc_addresses_address_fields_empty($form['panes']['billing'], 'billing')) {
foreach ($addresses as $address) {
if ($address->is_default) {
if (uc_address_field_enabled('first_name')) {
$form['panes']['billing']['billing_first_name']['#default_value'] = $address->first_name;
}
if (uc_address_field_enabled('last_name')) {
$form['panes']['billing']['billing_last_name']['#default_value'] = $address->last_name;
}
if (uc_address_field_enabled('phone')) {
$form['panes']['billing']['billing_phone']['#default_value'] = $address->phone;
}
if (uc_address_field_enabled('company')) {
$form['panes']['billing']['billing_company']['#default_value'] = $address->company;
}
if (uc_address_field_enabled('street1')) {
$form['panes']['billing']['billing_street1']['#default_value'] = $address->street1;
}
if (uc_address_field_enabled('street2')) {
$form['panes']['billing']['billing_street2']['#default_value'] = $address->street2;
}
if (uc_address_field_enabled('city')) {
$form['panes']['billing']['billing_city']['#default_value'] = $address->city;
}
if (uc_address_field_enabled('country')) {
$form['panes']['billing']['billing_country']['#default_value'] = $address->country;
}
if (uc_address_field_enabled('zone')) {
$form['panes']['billing']['billing_zone'] = uc_zone_select(uc_get_field_name('zone'), $address->zone, NULL, $address->country, 'name', uc_address_field_required('zone'));
}
if (uc_address_field_enabled('postal_code')) {
$form['panes']['billing']['billing_postal_code']['#default_value'] = $address->postal_code;
}
}
}
}
}
else {
unset($form['panes']['delivery']['delivery_address_select']);
unset($form['panes']['billing']['billing_address_select']);
}
}
}
function _uc_addresses_address_fields_empty($fields, $type) {
if (uc_address_field_enabled('first_name') && $fields[$type . '_first_name']['#default_value']) {
return FALSE;
}
else {
if (uc_address_field_enabled('last_name') && $fields[$type . '_last_name']['#default_value']) {
return FALSE;
}
else {
if (uc_address_field_enabled('phone') && $fields[$type . '_phone']['#default_value']) {
return FALSE;
}
else {
if (uc_address_field_enabled('company') && $fields[$type . '_company']['#default_value']) {
return FALSE;
}
else {
if (uc_address_field_enabled('street1') && $fields[$type . '_street1']['#default_value']) {
return FALSE;
}
else {
if (uc_address_field_enabled('street2') && $fields[$type . '_street2']['#default_value']) {
return FALSE;
}
else {
if (uc_address_field_enabled('city') && $fields[$type . '_city']['#default_value']) {
return FALSE;
}
else {
if (uc_address_field_enabled('postal_code') && $fields[$type . '_postal_code']['#default_value']) {
return FALSE;
}
}
}
}
}
}
}
}
return TRUE;
}
function uc_addresses_address_pane() {
$panes[] = array(
'id' => 'address',
'callback' => 'uc_addresses_pane_address',
'title' => t('Address'),
'desc' => t("Manage the user's addresses and contact information."),
'class' => 'pos-left',
'weight' => 1,
'show' => array(
'view',
'add',
'edit',
),
);
return $panes;
}
function uc_addresses_order($op, &$arg1, $arg2) {
if ($op == 'submit' && $arg1->order_status == 'in_checkout') {
$order = $arg1;
$address = new stdClass();
if (variable_get('uc_pane_billing_enabled', TRUE)) {
$address->address_name = '';
$address->uid = $order->uid;
$address->first_name = $order->billing_first_name;
$address->last_name = $order->billing_last_name;
$address->company = $order->billing_company;
$address->street1 = $order->billing_street1;
$address->street2 = $order->billing_street2;
$address->city = $order->billing_city;
$address->zone = $order->billing_zone;
$address->postal_code = $order->billing_postal_code;
$address->country = $order->billing_country;
$address->phone = $order->billing_phone;
$address->is_default = 0;
_uc_addresses_db_add_address($address, TRUE);
}
if (variable_get('uc_pane_delivery_enabled', TRUE)) {
$address->address_name = '';
$address->uid = $order->uid;
$address->first_name = $order->delivery_first_name;
$address->last_name = $order->delivery_last_name;
$address->company = $order->delivery_company;
$address->street1 = $order->delivery_street1;
$address->street2 = $order->delivery_street2;
$address->city = $order->delivery_city;
$address->zone = $order->delivery_zone;
$address->postal_code = $order->delivery_postal_code;
$address->country = $order->delivery_country;
$address->phone = $order->delivery_phone;
$address->is_default = 0;
_uc_addresses_db_add_address($address, TRUE);
}
}
}
function uc_addresses_settings_overview() {
$items = array(
variable_get('uc_addresses_default_delivery_address', TRUE) ? t('Automatically filling in the delivery address with the user\'s default address.') : t('The customer must enter or choose a delivery address.'),
variable_get('uc_addresses_default_billing_address', TRUE) ? t('Automatically filling in the billing address with the user\'s default address.') : t('The customer must enter or choose a billing address.'),
variable_get('uc_addresses_require_address', TRUE) ? t('The customer must enter an address when registering') : t('The customer does not enter an address when registering.'),
);
$sections[] = array(
'edit' => 'admin/store/settings/addresses/edit',
'title' => t('Address settings '),
'items' => $items,
);
$output = theme('uc_settings_overview', $sections);
return $output;
}
function uc_addresses_settings_form() {
$form['user'] = array(
'#type' => 'fieldset',
'#title' => t('User settings'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$form['user']['uc_addresses_default_billing_address'] = array(
'#type' => 'checkbox',
'#title' => t('Automatically fill in the billing address with the user\'s default address.'),
'#default_value' => variable_get('uc_addresses_default_billing_address', TRUE),
);
$form['user']['uc_addresses_default_delivery_address'] = array(
'#type' => 'checkbox',
'#title' => t('Automatically fill in the delivery address with the user\'s default address.'),
'#default_value' => variable_get('uc_addresses_default_delivery_address', TRUE),
);
$form['user']['uc_addresses_require_address'] = array(
'#type' => 'checkbox',
'#title' => t('Require that an address be entered during registration'),
'#default_value' => variable_get('uc_addresses_require_address', TRUE),
);
return system_settings_form($form);
}
function uc_addresses_list_addresses($uid, $aid = NULL) {
global $user;
$saved_aid = $aid;
$can_view_all_addresses = $user->uid == $uid || user_access(UC_ADDRESSES_ACCESS_VIEW_ALL) || user_access(UC_ADDRESSES_ACCESS_ADD_EDIT);
$allowed_to_view_address = true;
if ($can_view_all_addresses) {
$addresses = _uc_addresses_db_get_address($uid, $aid);
}
else {
if ($aid === NULL) {
$aid = _uc_addresses_get_default_address_id($uid);
if ($aid === NULL) {
$aid = 0;
}
}
$addresses = _uc_addresses_db_get_address($uid, $aid);
if (is_object($addresses) && $addresses->is_default === false) {
$addresses = FALSE;
$allowed_to_view_address = FALSE;
}
}
$output = '';
if (is_array($addresses)) {
foreach ($addresses as $address) {
$output .= _uc_addresses_list_one_address($uid, $address);
}
}
elseif (is_object($addresses)) {
$output .= _uc_addresses_list_one_address($uid, $addresses);
}
else {
if ($saved_aid === NULL) {
$output .= t('No addresses have been saved.<br />');
}
elseif ($allowed_to_view_address) {
$output .= t('This address does not exist.<br />');
}
else {
$output .= t('You are not allowed to view this address.<br />');
}
}
if (user_access(UC_ADDRESSES_ACCESS_ADD_EDIT) || $user->uid == $uid) {
$link = l(t('Add a new address'), 'user/' . $uid . '/addresses/add');
$output .= $link;
}
return $output;
}
function _uc_addresses_list_one_address($uid, $address) {
$panes = _address_pane_list();
foreach ($panes as $pane) {
if (variable_get('uc_addresses_pane_' . $pane['id'] . '_enabled', TRUE)) {
$func = $pane['callback'];
if (function_exists($func)) {
$return = $func('view', $address, NULL);
if (!is_NULL($return)) {
$data[$pane['title']] = $return;
}
}
}
}
$output .= '<div class="list_address">';
$output .= theme('uc_addresses_list_address', $address, $data);
$output .= '</div>';
return $output;
}
function theme_uc_addresses_list_address($address, $panes) {
global $user;
$uid = $address->uid;
$aid = $address->aid;
drupal_add_css(drupal_get_path('module', 'uc_addresses') . '/uc_addresses.css');
if ($address->is_default) {
$output = '<table class="address-preview-table addresses-default-address">';
}
else {
$output = '<table class="address-preview-table">';
}
foreach ($panes as $title => $data) {
if (user_access(UC_ADDRESSES_ACCESS_ADD_EDIT) || $user->uid == $uid) {
$output .= '<tr class="pane-title-row"><td colspan="2">' . l(t('Edit this address'), 'user/' . $uid . '/addresses/' . $aid . '/edit') . ($address->is_default ? '' : ' | ' . l(t('Delete this address'), 'user/' . $uid . '/addresses/' . $aid . '/delete')) . '</td></tr>';
}
if ($address->is_default) {
$output .= '<tr><td colspan="2" class="addresses-default-address-label"> ' . t('Default address') . '</td></tr>';
}
if ($address->address_name) {
$output .= '<tr class="pane-data-row"' . $border . '><td class="title-col" ' . '>' . t('Name') . ':</td><td class="data-col">' . $address->address_name . '</td></tr>';
}
if (is_array($data)) {
foreach ($data as $row) {
if (is_array($row)) {
if (isset($row['border'])) {
$border = ' class="row-border-' . $row['border'] . '"';
}
else {
$border = '';
}
$output .= '<tr class="pane-data-row"' . $border . '><td class="title-col" ' . '>' . $row['title'] . ':</td><td class="data-col">' . $row['data'] . '</td></tr>';
}
else {
$output .= '<tr class="pane-data-row"><td colspan="2">' . $row . '</td></tr>';
}
}
}
else {
$output .= '<tr class="pane-data-row"><td colspan="2">' . $data . '</td></tr>';
}
}
$output .= '</table><br />';
return $output;
}
function uc_addresses_get_address($uid, $aid, $view) {
global $user;
$output = drupal_get_form('uc_addresses_get_address_form', $uid, $aid, $view);
return $output;
}
function uc_addresses_get_address_form($uid, $aid, $view) {
if ($_POST['op'] == t('Cancel')) {
drupal_goto('user/' . $uid . '/addresses');
}
if ($_POST['op'] == variable_get('uc_addresses_delete_button', t('Delete address'))) {
cache_clear_all();
drupal_goto('user/' . $uid . '/addresses/' . $aid . '/delete');
}
$form['panes'] = array(
'#tree' => TRUE,
);
$panes = _address_pane_list($view);
foreach ($panes as $pane) {
if (in_array($view, $pane['show']) && variable_get('uc_addresses_pane_' . $pane['id'] . '_show_' . $view, TRUE)) {
if ($aid != NULL) {
$address = _uc_addresses_db_get_address($uid, $aid);
$return = $pane['callback']($view, $address, NULL);
}
else {
$return = $pane['callback']($view, NULL, NULL);
}
if (is_array($return) && (!empty($return['description']) || !empty($return['contents']))) {
$form['panes'][$pane['id']] = array(
'#type' => 'fieldset',
'#title' => $pane['title'],
'#description' => !empty($return['description']) ? $return['description'] : NULL,
'#collapsible' => $pane['collapsible'],
'#collapsed' => FALSE,
'#attributes' => array(
'id' => $pane['id'] . '-pane',
),
'#theme' => $return['theme'],
);
if (!empty($return['contents'])) {
$form['panes'][$pane['id']] = array_merge($form['panes'][$pane['id']], $return['contents']);
}
}
}
}
$form['cancel'] = array(
'#type' => 'submit',
'#submit' => FALSE,
'#value' => t('Cancel'),
);
if (arg(4) == 'edit') {
$form['delete'] = array(
'#type' => 'submit',
'#value' => variable_get('uc_addresses_delete_button', t('Delete address')),
'#disabled' => $address->is_default,
);
$form['continue'] = array(
'#type' => 'submit',
'#value' => variable_get('uc_addresses_update_button', t('Update address')),
);
}
else {
$form['continue'] = array(
'#type' => 'submit',
'#value' => variable_get('uc_addresses_update_button', t('Add address')),
);
}
$form['#submit'] = array(
'uc_addresses_get_address_form_submit' => array(
$uid,
$aid,
$view,
),
);
return $form;
}
function theme_uc_addresses_get_address_form($form) {
drupal_add_css(drupal_get_path('module', 'uc_addresses') . '/uc_addresses.css');
$output = '<p>';
foreach (element_children($form['panes']) as $pane_id) {
if (function_exists($func = _address_pane_data($pane_id, 'callback'))) {
$result = $func('theme', $form['panes'][$pane_id], NULL);
if (!empty($result)) {
$output .= $result;
$form['panes'][$pane_id] = array();
}
else {
$output .= drupal_render($form['panes'][$pane_id]);
}
}
else {
$output .= drupal_render($form['panes'][$pane_id]);
}
}
$output .= '<div id="checkout-form-bottom">' . drupal_render($form) . '</div>';
return $output;
}
function uc_addresses_get_address_form_submit($form_id, $form_values, $uid, $aid, $view) {
global $user;
$address = new stdClass();
$address->uid = $uid;
$valid = TRUE;
foreach (element_children($form_values['panes']) as $pane_id) {
$func = _address_pane_data($pane_id, 'callback');
$isvalid = $func('process', $address, $form_values['panes'][$pane_id]);
if ($isvalid === FALSE) {
$_SESSION['expanded_panes'][] = $key;
$valid = FALSE;
}
}
if ($view == 'edit') {
_uc_addresses_db_update_address($address);
}
elseif ($view == 'new' || $view == 'add') {
_uc_addresses_db_add_address($address);
}
return 'user/' . $address->uid . '/addresses';
}
function uc_addresses_address($items) {
$address = uc_address_format($items->first_name, $items->last_name, $items->company, $items->street1, $items->street2, $items->city, $items->zone, $items->postal_code, $items->country);
return $address;
}
function uc_addresses_delete_address_confirm($uid, $aid) {
if ($_POST['op'] == t('Cancel')) {
cache_clear_all();
drupal_goto('user/' . $uid . '/addresses/');
}
$form = drupal_get_form('uc_addresses_delete_address_confirm_form', $uid, $aid);
$address = _uc_addresses_db_get_address($uid, $aid);
if ($address === FALSE) {
$_SESSION['add_address'] = NULL;
unset($_SESSION['add_address']);
drupal_goto('user/' . $uid . '/addresses/');
}
if (_uc_addresses_db_check_if_default($aid)) {
drupal_set_message(t('You cannot delete your default address'), 'warning');
drupal_goto('user/' . $uid . '/addresses');
}
$help = variable_get('uc_addresses_delete_instructions', t('Are you are sure you want to Delete this address? ' . 'Once deleted, the address cannot be recovered.', array(
'!delete' => variable_get('uc_addresses_delete_button', t('Delete address')),
)));
$panes = _address_pane_list();
foreach ($panes as $pane) {
if (variable_get('uc_addresses_pane_' . $pane['id'] . '_enabled', TRUE)) {
$func = $pane['callback'];
if (function_exists($func)) {
$return = $func('view', $address, NULL);
if (!is_NULL($return)) {
$data[$pane['title']] = $return;
}
}
}
}
$output = theme('uc_addresses_address_delete_confirm', $help, $data, $form);
return $output;
}
function theme_uc_addresses_address_delete_confirm($help, $panes, $form) {
drupal_add_css(drupal_get_path('module', 'uc_addresses') . '/uc_addresses.css');
$output .= '<p>' . $help . '</p>';
$output .= '<div class="list_address"><table class="address-preview-table">';
foreach ($panes as $title => $data) {
$output .= '<tr class="pane-title-row"><td colspan="2">' . t('Address') . '</td></tr>';
if (is_array($data)) {
foreach ($data as $row) {
if (is_array($row)) {
if (isset($row['border'])) {
$border = ' class="row-border-' . $row['border'] . '"';
}
else {
$border = '';
}
$output .= '<tr class="pane-data-row"' . $border . '><td class="title-col" ' . '>' . $row['title'] . ':</td><td class="data-col">' . $row['data'] . '</td></tr>';
}
else {
$output .= '<tr class="pane-data-row"><td colspan="2">' . $row . '</td></tr>';
}
}
}
else {
$output .= '<tr class="pane-data-row"><td colspan="2">' . $data . '</td></tr>';
}
}
$output .= '<tr class="preview-button-row"><td colspan="2">' . $form . '</td></tr></table>';
$output .= '</div>';
return $output;
}
function uc_addresses_delete_address_confirm_form($uid, $aid) {
$form['cancel'] = array(
'#type' => 'button',
'#value' => t('Cancel'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => variable_get('uc_addresses_delete_button', t('Delete address')),
);
$form['#submit'] = array(
'uc_addresses_delete_address_confirm_form_submit' => array(
$uid,
$aid,
),
);
return $form;
}
function uc_addresses_delete_address_confirm_form_submit($form_id, $form_values, $uid, $aid) {
switch ($form_values['op']) {
case variable_get('uc_addresses_delete_button', t('Delete address')):
$_SESSION['do_complete'] = TRUE;
if (_uc_addresses_db_delete_address($aid)) {
drupal_set_message(t('The address has been deleted.'));
}
return 'user/' . $uid . '/addresses';
}
}
function _uc_addresses_db_add_address($address, $silent = FALSE) {
global $user;
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 ($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']);
}
return TRUE;
}
if ($address->uid == 0) {
return TRUE;
}
$result = db_query("SELECT COUNT(*) FROM {uc_addresses} WHERE uid = %d", $address->uid);
$num_addresses = 0;
if (db_num_rows($result) == 1) {
$a = db_fetch_array($result);
$num_addresses = $a['COUNT(*)'];
}
if ($num_addresses < 1) {
$address->is_default = 1;
}
if ($num_addresses > 0) {
$result = _uc_addresses_db_check_address($address, 'add', $silent);
if (!$result) {
return FALSE;
}
}
else {
_uc_addresses_db_normalize_address($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());
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;
}
function _uc_addresses_db_update_address($address) {
$result = _uc_addresses_db_check_address($address, 'update', false);
if (!$result) {
return FALSE;
}
db_query("UPDATE {uc_addresses} SET " . "first_name = '%s', " . "last_name = '%s', " . "phone = '%s', " . "company = '%s', " . "street1 = '%s', " . "street2 = '%s', " . "city = '%s', " . "zone = %d, " . "postal_code = '%s', " . "country = %d, " . "address_name = '%s', " . "modified = %d " . "WHERE aid = %d", $address->first_name, $address->last_name, $address->phone, $address->company, $address->street1, $address->street2, $address->city, $address->zone, $address->postal_code, $address->country, $address->address_name, time(), $address->aid);
if ($address->is_default) {
db_query("UPDATE {uc_addresses_defaults} SET aid = %d WHERE uid = %d", $address->aid, $address->uid);
}
drupal_set_message(t('The address was updated.'));
return TRUE;
}
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);
if (db_num_rows($result) > 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 (db_num_rows($result) == 1) {
$db_address = db_fetch_object($result);
if ($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;
}
}
if ($address->address_name) {
$result = db_query("SELECT aid FROM {uc_addresses} WHERE " . "uid = %d AND " . "address_name = '%s'", $address->uid, $address->address_name);
if (db_num_rows($result) > 0) {
$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;
}
function _uc_addresses_db_normalize_address(&$address) {
if (!$address->first_name) {
$address->first_name = '';
}
if (!$address->last_name) {
$address->last_name = '';
}
if (!$address->phone) {
$address->phone = '';
}
if (!$address->company) {
$address->company = '';
}
if (!$address->street1) {
$address->street1 = '';
}
if (!$address->street2) {
$address->street2 = '';
}
if (!$address->city) {
$address->city = '';
}
if (!$address->zone) {
$address->zone = 0;
}
if (!$address->postal_code) {
$address->postal_code = '';
}
if (!$address->country) {
$address->country = variable_get('uc_store_country', 840);
}
if (!$address->address_name) {
$address->address_name = '';
}
}
function _uc_addresses_db_get_address($uid, $aid = NULL) {
if ($aid) {
$result = db_query("SELECT * FROM {uc_addresses} WHERE aid = %d ", $aid);
if (db_num_rows($result) == 0) {
return FALSE;
}
$address = db_fetch_object($result);
if ($address->uid != $uid) {
return FALSE;
}
$address->is_default = 0;
$def = db_query("SELECT aid FROM {uc_addresses_defaults} WHERE uid = %d", $uid);
if (db_num_rows($def) == 1) {
$default_aid = db_fetch_object($def);
$address->is_default = $default_aid->aid == $address->aid;
}
return $address;
}
$result = db_query("SELECT * FROM {uc_addresses} WHERE uid = %d ORDER BY created", $uid);
if (db_num_rows($result) == 0) {
return FALSE;
}
$default_aid = _uc_addresses_get_default_address_id($uid);
while ($address = db_fetch_object($result)) {
$address->is_default = $default_aid == $address->aid;
$addresses[] = $address;
}
return $addresses;
}
function _uc_addresses_get_default_address_id($uid) {
$def = db_query("SELECT aid FROM {uc_addresses_defaults} WHERE uid = %d", $uid);
if (db_num_rows($def) != 1) {
$aid = NULL;
}
else {
$obj = db_fetch_object($def);
$aid = $obj->aid;
}
return $aid;
}
function _uc_addresses_db_delete_address($aid) {
if (_uc_addresses_db_check_if_default($aid)) {
drupal_set_message(t('You cannot delete your default address'), 'warning');
return FALSE;
}
else {
db_query("DELETE FROM {uc_addresses} WHERE aid = %d", $aid);
return TRUE;
}
}
function _uc_addresses_db_check_if_default($aid) {
$count = db_result(db_query("SELECT COUNT(*) FROM {uc_addresses_defaults} WHERE " . "aid = %d", $aid));
if ($count > 0) {
return TRUE;
}
return FALSE;
}