View source
<?php
module_load_include('inc', 'uc_addresses', 'uc_addresses_address_pane');
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_init() {
drupal_add_css(drupal_get_path('module', 'uc_order') . '/uc_order.css');
}
function uc_addresses_address_load($aid, $uid) {
if (!isset($aid)) {
return FALSE;
}
return _uc_addresses_db_get_address($uid, $aid);
}
function uc_addresses_menu() {
$items = array();
$items['admin/store/settings/addresses'] = array(
'title' => 'Address settings',
'description' => 'Configure the address settings.',
'page callback' => 'uc_addresses_settings_overview',
'access arguments' => array(
'administer store',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'uc_addresses.admin.inc',
);
$items['admin/store/settings/addresses/overview'] = array(
'title' => 'Overview',
'access arguments' => array(
'administer store',
),
'description' => 'View the address settings.',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/store/settings/addresses/edit'] = array(
'title' => 'Edit',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'uc_addresses_settings_form',
),
'access arguments' => array(
'administer store',
),
'description' => 'Edit the address settings.',
'type' => MENU_LOCAL_TASK,
'weight' => -5,
'file' => 'uc_addresses.admin.inc',
);
$items['user/%user_uid_optional/addresses'] = array(
'title' => 'Addresses',
'description' => 'Manage your addresses',
'page callback' => 'uc_addresses_list_addresses',
'page arguments' => array(
1,
NULL,
),
'access callback' => 'uc_addresses_can_view_addresses',
'access arguments' => array(
1,
),
'type' => MENU_LOCAL_TASK,
);
$items['user/%user_uid_optional/addresses/list'] = array(
'title' => 'Address list',
'description' => 'Manage your addresses',
'access callback' => 'uc_addresses_can_view_addresses',
'access arguments' => array(
1,
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 0,
);
$items['user/%user_uid_optional/addresses/add'] = array(
'title' => 'Add address',
'description' => 'Add a new address.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'uc_addresses_get_address_form',
1,
NULL,
'add',
),
'access callback' => 'uc_addresses_can_add_edit_address',
'access arguments' => array(
1,
),
'type' => MENU_LOCAL_TASK,
'weight' => 3,
);
$items['user/%user_uid_optional/addresses/%uc_addresses_address'] = array(
'title' => 'View Address',
'description' => 'View one saved address',
'load arguments' => array(
1,
),
'page callback' => 'uc_addresses_list_addresses',
'page arguments' => array(
1,
arg(3),
),
'access callback' => 'uc_addresses_can_view_addresses',
'access arguments' => array(
1,
),
'type' => MENU_CALLBACK,
);
$items['user/%user_uid_optional/addresses/%uc_addresses_address/edit'] = array(
'title' => 'Edit address',
'load arguments' => array(
1,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'uc_addresses_get_address_form',
1,
3,
'edit',
),
'access callback' => 'uc_addresses_can_add_edit_address',
'access arguments' => array(
1,
),
'type' => MENU_CALLBACK,
);
$items['user/%user_uid_optional/addresses/%uc_addresses_address/delete'] = array(
'title' => 'Delete address',
'load arguments' => array(
1,
),
'page callback' => 'uc_addresses_delete_address_confirm',
'page arguments' => array(
1,
3,
),
'access callback' => 'uc_addresses_can_add_edit_address',
'access arguments' => array(
1,
),
'type' => MENU_CALLBACK,
);
return $items;
}
function uc_addresses_theme() {
return array(
'uc_addresses_list_address' => array(
'arguments' => array(
'address' => NULL,
'panes' => array(),
),
),
'uc_addresses_get_address_form' => array(
'arguments' => array(
'form' => NULL,
),
),
'uc_addresses_address_delete_confirm' => array(
'arguments' => array(
'help' => '',
'panes' => array(),
'form' => NULL,
),
),
'uc_addresses_pane' => array(
'arguments' => array(
'form' => NULL,
),
'file' => 'uc_addresses_address_pane.inc',
),
);
}
function uc_addresses_perm() {
return array(
'view default addresses',
'view all addresses',
'add/edit addresses',
);
}
function uc_addresses_token_list($type = 'all') {
$tokens = array();
if ($type === 'user' || $type === 'all') {
$schema = drupal_get_schema('uc_addresses');
$exclude = array(
'aid',
'uid',
'created',
'modified',
);
foreach ($schema['fields'] as $key => $field) {
if ($key === 'created' || $key === 'modified') {
$tokens[t('Ubercart Addresses') . ' - ' . t('Default address')] += token_get_date_token_info($field['description'], 'uc-addresses-default-' . $key . '-');
}
else {
$tokens[t('Ubercart Addresses') . ' - ' . t('Default address')]['uc-addresses-default-' . $key] = $field['description'];
}
if (!in_array($key, $exclude)) {
$tokens[t('Ubercart Addresses') . ' - ' . t('Default address')]['uc-addresses-default-' . $key . '-raw'] = $field['description'];
}
}
}
return $tokens;
}
function uc_addresses_token_values($type, $object = NULL, $options = array()) {
$values = array();
if ($type === 'user') {
$user = $object;
if (isset($user->uid) && $user->uid) {
$aid = _uc_addresses_get_default_address_id($user->uid);
if ($aid) {
$address = (array) uc_addresses_address_load($aid, $user->uid);
$exclude = array(
'aid',
'uid',
'created',
'modified',
);
foreach ($address as $key => $value) {
if ($key === 'country') {
$values['uc-addresses-default-' . $key . '-raw'] = uc_country_get_by_id($value);
$values['uc-addresses-default-' . $key] = check_plain(uc_country_get_by_id($value));
}
elseif ($key === 'zone') {
$values['uc-addresses-default-' . $key . '-raw'] = uc_zone_get_by_id($value);
$values['uc-addresses-default-' . $key] = check_plain(uc_zone_get_by_id($value));
}
elseif ($key === 'created' || $key === 'modified') {
$values += token_get_date_token_values($address[$key], 'uc-addresses-default-' . $key . '-');
}
else {
if (!in_array($key, $exclude)) {
$values['uc-addresses-default-' . $key . '-raw'] = trim($value);
}
$values['uc-addresses-default-' . $key] = trim(check_plain($value));
}
}
}
}
}
return $values;
}
function uc_addresses_can_view_addresses($address_user) {
global $user;
$access = user_access(UC_ADDRESSES_ACCESS_VIEW_DEFAULT) || user_access(UC_ADDRESSES_ACCESS_VIEW_ALL) || user_access(UC_ADDRESSES_ACCESS_ADD_EDIT) || $user->uid == $address_user->uid;
return $access;
}
function uc_addresses_can_add_edit_address($address_user) {
global $user;
$access = user_access(UC_ADDRESSES_ACCESS_ADD_EDIT) || $user->uid == $address_user->uid;
return $access;
}
function uc_addresses_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'register':
if (variable_get('uc_addresses_require_address', TRUE)) {
$form = uc_addresses_pane_address('new', NULL, $edit);
$form = array(
$form['contents'],
);
$form[0]['#title'] = t('Address');
return $form;
}
return;
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_uc_cart_checkout_form_alter(&$form, &$form_state) {
global $user;
if (isset($_SESSION['uc_addresses_saved_addresses'])) {
$_SESSION['uc_addresses_saved_addresses'] = NULL;
unset($_SESSION['uc_addresses_saved_addresses']);
}
$options = NULL;
if ($addresses = _uc_addresses_db_get_address($user->uid)) {
$options = array(
'0' => t('Select one...'),
);
foreach ($addresses as $address) {
if ($address->is_default) {
$default_address = $address;
}
$address = (array) $address;
if ($address['is_default']) {
$default = drupal_to_js($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(
'html' => TRUE,
));
if ($options) {
$default_country = uc_store_default_country();
$js_zone = FALSE;
if ($form['panes']['delivery'] && (uc_cart_is_shippable() || !variable_get('uc_cart_delivery_not_shippable', TRUE))) {
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('Go to your <a href="@address-book">address book</a> to manage your saved addresses.', array(
'@address-book' => url('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_addresses_default_delivery_address', TRUE) && _uc_addresses_address_fields_empty($form['panes']['delivery'], 'delivery')) {
$form['panes']['delivery']['delivery_address_select']['#default_value'] = $default;
if (uc_address_field_enabled('first_name')) {
$form['panes']['delivery']['delivery_first_name']['#default_value'] = $default_address->first_name;
}
if (uc_address_field_enabled('last_name')) {
$form['panes']['delivery']['delivery_last_name']['#default_value'] = $default_address->last_name;
}
if (uc_address_field_enabled('phone')) {
$form['panes']['delivery']['delivery_phone']['#default_value'] = $default_address->phone;
}
if (uc_address_field_enabled('company')) {
$form['panes']['delivery']['delivery_company']['#default_value'] = $default_address->company;
}
if (uc_address_field_enabled('street1')) {
$form['panes']['delivery']['delivery_street1']['#default_value'] = $default_address->street1;
}
if (uc_address_field_enabled('street2')) {
$form['panes']['delivery']['delivery_street2']['#default_value'] = $default_address->street2;
}
if (uc_address_field_enabled('city')) {
$form['panes']['delivery']['delivery_city']['#default_value'] = $default_address->city;
}
if (uc_address_field_enabled('country')) {
$form['panes']['delivery']['delivery_country']['#default_value'] = $default_address->country;
}
if (uc_address_field_enabled('zone')) {
if ($default_country == $default_address->country) {
$form['panes']['delivery']['delivery_zone']['#default_value'] = $default_address->zone;
}
else {
$js_zone = TRUE;
}
}
if (uc_address_field_enabled('postal_code')) {
$form['panes']['delivery']['delivery_postal_code']['#default_value'] = $default_address->postal_code;
}
}
}
}
if (variable_get('uc_pane_billing_enabled', TRUE)) {
$form['panes']['billing']['#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('Go to your <a href="@address-book">address book</a> to manage your saved addresses.', array(
'@address-book' => url('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_billing_address', TRUE) && _uc_addresses_address_fields_empty($form['panes']['billing'], 'billing')) {
$form['panes']['billing']['billing_address_select']['#default_value'] = $default;
if (uc_address_field_enabled('first_name')) {
$form['panes']['billing']['billing_first_name']['#default_value'] = $default_address->first_name;
}
if (uc_address_field_enabled('last_name')) {
$form['panes']['billing']['billing_last_name']['#default_value'] = $default_address->last_name;
}
if (uc_address_field_enabled('phone')) {
$form['panes']['billing']['billing_phone']['#default_value'] = $default_address->phone;
}
if (uc_address_field_enabled('company')) {
$form['panes']['billing']['billing_company']['#default_value'] = $default_address->company;
}
if (uc_address_field_enabled('street1')) {
$form['panes']['billing']['billing_street1']['#default_value'] = $default_address->street1;
}
if (uc_address_field_enabled('street2')) {
$form['panes']['billing']['billing_street2']['#default_value'] = $default_address->street2;
}
if (uc_address_field_enabled('city')) {
$form['panes']['billing']['billing_city']['#default_value'] = $default_address->city;
}
if (uc_address_field_enabled('country')) {
$form['panes']['billing']['billing_country']['#default_value'] = $default_address->country;
}
if (uc_address_field_enabled('zone')) {
if ($default_country == $default_address->country) {
$form['panes']['billing']['billing_zone']['#default_value'] = $default_address->zone;
}
else {
$js_zone = TRUE;
}
}
if (uc_address_field_enabled('postal_code')) {
$form['panes']['billing']['billing_postal_code']['#default_value'] = $default_address->postal_code;
}
}
}
if ($js_zone) {
drupal_add_js(drupal_get_path('module', 'uc_addresses') . '/uc_addresses.js');
drupal_add_js(array(
'uc_address_default' => array(
'country' => $default_address->country,
'zone' => $default_address->zone,
),
), 'setting');
}
}
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;
}
elseif (uc_address_field_enabled('last_name') && $fields[$type . '_last_name']['#default_value']) {
return FALSE;
}
elseif (uc_address_field_enabled('phone') && $fields[$type . '_phone']['#default_value']) {
return FALSE;
}
elseif (uc_address_field_enabled('company') && $fields[$type . '_company']['#default_value']) {
return FALSE;
}
elseif (uc_address_field_enabled('street1') && $fields[$type . '_street1']['#default_value']) {
return FALSE;
}
elseif (uc_address_field_enabled('street2') && $fields[$type . '_street2']['#default_value']) {
return FALSE;
}
elseif (uc_address_field_enabled('city') && $fields[$type . '_city']['#default_value']) {
return FALSE;
}
elseif (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_uc_checkout_complete($order, $user) {
$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 (uc_order_is_shippable($order)) {
$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_list_addresses($address_user, $address = NULL) {
global $user;
$uid = $address_user->uid;
$saved_address = $address;
$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) {
if ($address) {
$addresses = $address;
}
else {
$addresses = _uc_addresses_db_get_address($uid, NULL);
}
}
else {
if ($address) {
if ($address->is_default === FALSE) {
$addresses = FALSE;
$allowed_to_view_address = FALSE;
}
}
else {
$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($address);
}
}
elseif (is_object($addresses)) {
$output .= _uc_addresses_list_one_address($addresses);
}
else {
if ($saved_address === 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($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');
$output = '';
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"><td class="title-col" ' . '>' . t('Name') . ':</td><td class="data-col">' . check_plain($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_form(&$form_state, $address_user, $address, $view) {
$uid = $address_user->uid;
$aid = $address ? $address->aid : 0;
$form['stored_values'] = array(
'#type' => 'value',
'#value' => array(
'user' => $address_user,
'address' => $address,
'view' => $view,
),
);
$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)) {
$return = $pane['callback']($view, $address, 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' => !empty($pane['collapsible']) ? $pane['collapsible'] : FALSE,
'#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']);
}
}
}
}
if ($view == 'edit') {
$form['submit'] = array(
'#type' => 'submit',
'#value' => variable_get('uc_addresses_update_button', t('Update address')),
);
$form['delete'] = array(
'#type' => 'submit',
'#value' => variable_get('uc_addresses_delete_button', t('Delete address')),
'#suffix' => l(t('Cancel'), 'user/' . $uid . '/addresses/'),
'#disabled' => $address->is_default,
);
}
else {
$form['submit'] = array(
'#type' => 'submit',
'#value' => variable_get('uc_addresses_update_button', t('Add address')),
'#suffix' => l(t('Cancel'), 'user/' . $uid . '/addresses/'),
);
}
return $form;
}
function theme_uc_addresses_get_address_form($form) {
drupal_add_css(drupal_get_path('module', 'uc_addresses') . '/uc_addresses.css');
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, &$form_state) {
global $user;
$address_user = $form['stored_values']['#value']['user'];
$address = $form['stored_values']['#value']['address'];
$view = $form['stored_values']['#value']['view'];
if ($form_state['clicked_button']['#value'] == t('Delete address')) {
cache_clear_all();
$form_state['redirect'] = array(
'user/' . $address_user->uid . '/addresses/' . $address->aid . '/delete',
);
}
else {
if (!$address) {
$address = new stdClass();
$address->uid = $address_user->uid;
}
$valid = TRUE;
foreach (element_children($form_state['values']['panes']) as $pane_id) {
$func = _address_pane_data($pane_id, 'callback');
$isvalid = $func('process', $address, $form_state['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);
}
$form_state['redirect'] = array(
'user/' . $address_user->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($address_user, $address) {
$uid = $address_user->uid;
$aid = $address->aid;
$form = drupal_get_form('uc_addresses_delete_address_confirm_form', $address_user, $address);
$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(&$form_state, $address_user, $address) {
$form['stored_values'] = array(
'#type' => 'value',
'#value' => array(
'user' => $address_user,
'address' => $address,
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => variable_get('uc_addresses_delete_button', t('Delete address')),
'#suffix' => l(t('Cancel'), 'user/' . $address_user->uid . '/addresses/'),
);
return $form;
}
function uc_addresses_delete_address_confirm_form_submit($form, &$form_state) {
$address_user = $form['stored_values']['#value']['user'];
$address = $form['stored_values']['#value']['address'];
_uc_addresses_db_delete_address($address->aid);
drupal_set_message(t('The address has been deleted.'));
$form_state['redirect'] = array(
'user/' . $address_user->uid . '/addresses',
);
}
function _uc_addresses_db_add_address($address, $silent = FALSE) {
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;
}
$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) {
return FALSE;
}
$num_addresses = db_result(db_query("SELECT COUNT(*) FROM {uc_addresses} WHERE uid = %d", $address->uid));
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);
}
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');
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_have_saved_addresses() {
return isset($_SESSION['uc_addresses_saved_addresses']) && is_array($_SESSION['uc_addresses_saved_addresses']);
}
function _uc_addresses_db_update_address($address, $silent = FALSE) {
$result = _uc_addresses_db_check_address($address, 'update', $silent);
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);
}
if (!$silent) {
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 = %d AND " . "postal_code = '%s' AND " . "country = %d", $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);
$num_rows = 0;
while ($db_address = db_fetch_object($result)) {
$num_rows++;
if ($num_rows > 1) {
if (!$silent) {
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 (!isset($address->aid) || $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);
while ($db_address = db_fetch_object($result)) {
if ($db_address->aid != $address->aid) {
if (!$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 (!isset($address->address_name) || !$address->address_name) {
$address->address_name = '';
}
}
function _uc_addresses_db_get_address($uid, $aid = NULL) {
if ($aid === 0) {
return FALSE;
}
if ($aid) {
$result = db_query("SELECT * FROM {uc_addresses} WHERE aid = %d ", $aid);
if (($address = db_fetch_object($result)) == FALSE) {
return FALSE;
}
if ($address->uid != $uid) {
return FALSE;
}
$address->is_default = 0;
$def = db_query("SELECT aid FROM {uc_addresses_defaults} WHERE uid = %d", $uid);
if ($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);
$default_aid = _uc_addresses_get_default_address_id($uid);
$num_rows = 0;
while ($address = db_fetch_object($result)) {
$num_rows++;
$address->is_default = $default_aid == $address->aid;
$addresses[] = $address;
}
if ($num_rows == 0) {
return FALSE;
}
return $addresses;
}
function _uc_addresses_get_default_address_id($uid) {
$def = db_query("SELECT aid FROM {uc_addresses_defaults} WHERE uid = %d", $uid);
$aid = NULL;
while ($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;
}