diff --git a/commerce_addressbook.module b/commerce_addressbook.module index ed708f3..777ada6 100644 --- a/commerce_addressbook.module +++ b/commerce_addressbook.module @@ -302,81 +302,9 @@ function commerce_addressbook_form_alter(&$form, &$form_state, $form_id) { // If this pane is a customer profile based pane build a list of previous // profiles from which to pick that are of the same bundle. if (substr($pane_id, 0, 16) == 'customer_profile' && isset($form[$pane_id]) && variable_get('commerce_' . $pane_id . '_addressbook', FALSE)) { - $field = field_info_field(variable_get('commerce_' . $pane_id . '_field', '')); - $profiles = commerce_customer_profile_load_multiple(array(), array('type' => $field['settings']['profile_type'], 'uid' => $user->uid, 'status' => TRUE)); - - if ($profiles) { - // Prepare the options. - $options = array(); - foreach ($profiles as $id => $profile) { - $field_values = field_get_items('commerce_customer_profile', $profile, 'commerce_customer_address'); - $options[$id] = $field_values[0]['thoroughfare']; - } - drupal_alter('commerce_addressbook_labels', $options, $profiles); - // Prepare the default value. - $reference_field_name = variable_get('commerce_' . $pane_id . '_field', ''); - $order_wrapper = entity_metadata_wrapper('commerce_order', $form_state['order']); - $profile_reference = $order_wrapper->{$reference_field_name}->value(); - $default_value = 'none'; - if (!empty($form_state['values'][$pane_id]['addressbook'])) { - $default_value = $form_state['values'][$pane_id]['addressbook']; - } - elseif (!empty($profile_reference->profile_id)) { - $default_value = $profile_reference->profile_id; - } - // Check if the default profile is in the enabled profiles array. - if (!isset($profiles[$default_value])) { - $default_value = 'none'; - } - if ($default_value != 'none') { - // Make sure our profile type still exists.. - if (!empty($form[$pane_id]['commerce_customer_profile_copy'])) { - if (($source_profile_type_name = variable_get('commerce_' . $pane_id . '_profile_copy_source', NULL)) - && $source_profile_type = commerce_customer_profile_type_load($source_profile_type_name)) { - // Disable the profile copy checkbox. - $form[$pane_id]['commerce_customer_profile_copy']['#default_value'] = FALSE; - $form[$pane_id]['commerce_customer_profile_copy']['#access'] = FALSE; - // Loop over source profile fields and enable previously disabled - // fields. - foreach (field_info_instances('commerce_customer_profile', $source_profile_type['type']) as $field_name => $field) { - if (!empty($form[$pane_id][$field_name])){ - $langcode = $form[$pane_id][$field_name]['#language']; - $form[$pane_id][$field_name][$langcode]['#access'] = TRUE; - foreach (element_children($form[$pane_id][$field_name][$langcode]) as $key) { - $form[$pane_id][$field_name][$langcode][$key]['#access'] = TRUE; - // Disable the editing of the profile if the user doesn't - // have the right to edit customer profiles. - if (!commerce_customer_profile_access('update', $profiles[$default_value])) { - $form[$pane_id][$field_name][$langcode][$key]['#disabled'] = TRUE; - } - } - } - } - } - } - } - $form[$pane_id]['#prefix'] = '
'; - $form[$pane_id]['#suffix'] = '
'; - $form[$pane_id]['addressbook_entries'] = array( - '#type' => 'value', - '#value' => $profiles, - ); - $form[$pane_id]['addressbook'] = array( - '#addressbook' => TRUE, - '#type' => 'select', - '#title' => t('Addresses on File'), - '#description' => t('You may select a pre-existing address on file.'), - '#options' => $options, - '#empty_option' => t('-- Choose --'), - '#empty_value' => 'none', - '#ajax' => array( - 'callback' => 'commerce_addressbook_checkout_form_callback', - 'wrapper' => strtr($pane_id, '_', '-') . '-ajax-wrapper', - ), - '#element_validate' => array('commerce_addressbook_saved_addresses_validate'), - '#weight' => -100, - '#default_value' => $default_value, - ); + if ($form_element = commerce_addressbook_profile_field_form_alter($pane_id, $form, $form_state)) { + $form[$pane_id]['#field_name'] = variable_get('commerce_' . $pane_id . '_field', ''); + $form[$pane_id] += $form_element; } } } @@ -391,6 +319,18 @@ function commerce_addressbook_form_alter(&$form, &$form_state, $form_id) { ); } + // If we're dealing with a commerce order form. + if ($form_id == 'commerce_order_ui_order_form') { + $profile_types = commerce_customer_profile_types(); + foreach ($profile_types as $profile_name => $profile_type) { + if (isset($form['commerce_customer_' . $profile_name])) { + $profile_element = 'commerce_customer_' . $profile_name; + $form[$profile_element]['#field_name'] = variable_get('commerce_customer_profile_' . $profile_name . '_field', ''); + $form[$profile_element][$form[$profile_element]['#language']]['profiles'][0] += commerce_addressbook_profile_field_form_alter($profile_element, $form, $form_state); + } + } + } + if ($form_id == 'commerce_addressbook_customer_profile_form') { // Make sure the submit handlers run. rsort($form['actions']['submit']['#submit']); @@ -408,11 +348,11 @@ function commerce_addressbook_form_alter(&$form, &$form_state, $form_id) { * Ajax callback for replacing the appropriate commerce customer checkout pane. */ function commerce_addressbook_checkout_form_callback($form, $form_state) { - $pane_id = $form_state['triggering_element']['#parents'][0]; + $profile_element = $form_state['triggering_element']['#parents'][0]; // Add replace element as default AJAX command. $commands = array(); - $commands[] = ajax_command_replace(NULL, drupal_render($form[$pane_id])); + $commands[] = ajax_command_replace(NULL, drupal_render($form[$profile_element])); // Allow other modules to add arbitrary AJAX commands on the refresh. drupal_alter('commerce_addressbook_callback', $commands, $form, $form_state); @@ -425,14 +365,15 @@ function commerce_addressbook_checkout_form_callback($form, $form_state) { */ function commerce_addressbook_saved_addresses_validate($element, &$form_state, $form) { if (in_array('addressbook', $form_state['triggering_element']['#parents']) && $form_state['triggering_element']['#id'] == $element['#id']) { - $pane_id = $element['#parents'][0]; - $field_name = variable_get('commerce_' . $pane_id . '_field', ''); - $order_wrapper = entity_metadata_wrapper('commerce_order', $form_state['order']); + $profile_element = $element['#parents'][0]; + $field_name = $form[$profile_element]['#field_name']; + $order_wrapper = entity_metadata_wrapper('commerce_order', isset($form_state['commerce_order']) ? $form_state['commerce_order'] : $form_state['order']); + //$order_wrapper = entity_metadata_wrapper('commerce_order', $form_state['commerce_order']); if (is_numeric($element['#value'])) { global $user; $profile = commerce_customer_profile_load($element['#value']); // Validate that the profile being selected is owned by this user. - if ($profile->uid != $user->uid) { + if ($profile->uid != $user->uid && !user_access('administer commerce_customer_profile entities')) { drupal_set_message(t('You must own the profile you are choosing.'), 'error'); return; } @@ -451,8 +392,18 @@ function commerce_addressbook_saved_addresses_validate($element, &$form_state, $ else { $order_wrapper->{$field_name} = NULL; } - unset($form_state['input'][$pane_id]); - $element_key = $form[$pane_id]['commerce_customer_address'][$form[$pane_id]['commerce_customer_address']['#language']][0]['element_key']['#value']; + if (isset($form_state['commerce_order'])) { + $form_state['values'][$profile_element]['addressbook'] = $form_state['input'][$profile_element][$form[$profile_element]['#language']]['profiles'][0]['addressbook']; + foreach($form_state['values'][$profile_element][$form[$profile_element]['#language']]['profiles'][0]['addressbook_entries'][$form_state['values'][$profile_element]['addressbook']] as $addressbook_field_name => $addressbook_field){ + if(isset($form_state['values'][$profile_element][$form[$profile_element]['#language']]['profiles'][0][$addressbook_field_name])){ + $form_state['values'][$profile_element][$form[$profile_element]['#language']]['profiles'][0][$addressbook_field_name] = $addressbook_field; + } + } + $element_key = $form[$profile_element][$form[$profile_element]['#language']]['profiles'][0]['commerce_customer_address'][$form[$profile_element][$form[$profile_element]['#language']]['profiles'][0]['commerce_customer_address']['#language']][0]['element_key']['#value']; + } else { + $element_key = $form[$profile_element]['commerce_customer_address'][$form[$profile_element]['commerce_customer_address']['#language']][0]['element_key']['#value']; + } + unset($form_state['input'][$profile_element]); unset($form_state['addressfield'][$element_key]); } } @@ -588,3 +539,124 @@ function commerce_addressbook_retrieve_view($view_id, $display_id, $arguments, $ // Return the view. return $view; } + +/** + * Element validate callback: processes input of the user autocomplete. + */ +function commerce_addressbook_profile_addressbook_user_validate($element, &$form_state, $form) { + if (isset($form_state['commerce_order'])) { + $form_state['values'][$element['#parents'][0]]['addressbook_user'] = $form_state['input'][$element['#parents'][0]][$form[$element['#parents'][0]]['#language']]['profiles'][0]['addressbook_user']; + } +} + +/** + * Helper function . Alter the customer profile field form + * with user/customer data. + */ +function commerce_addressbook_profile_field_form_alter($profile_element, &$form, &$form_state) { + $profile_field_name = $form[$profile_element]['#field_name']; + $commerce_order = isset($form_state['commerce_order']) ? $form_state['commerce_order'] : $form_state['order']; + $field = field_info_field($profile_field_name); + $form[$profile_element]['#prefix'] = '
'; + $form[$profile_element]['#suffix'] = '
'; + // Check if the user can access all addresses + if (user_access('administer commerce_customer_profile entities')) { + $form_addressbook['addressbook_user'] = array( + '#type' => 'textfield', + '#autocomplete_path' => 'user/autocomplete', + '#title' => t('User address lookup'), + '#description' => t('You can search for a user to select an address to use'), + '#ajax' => array( + 'callback' => 'commerce_addressbook_checkout_form_callback', + 'wrapper' => strtr($profile_element, '_', '-') . '-ajax-wrapper', + ), + '#element_validate' => array('commerce_addressbook_profile_addressbook_user_validate'), + '#weight' => -101, + '#default_value' => isset($form_state['values'][$profile_element]['addressbook_user']) ? $form_state['values'][$profile_element]['addressbook_user'] : '', // TODO: Check for form state value + ); + } + $profiles = FALSE; + if (user_access('administer commerce_customer_profile entities') && isset($form_state['values'][$profile_element]['addressbook_user'])) { + $account = user_load_by_name(check_plain($form_state['values'][$profile_element]['addressbook_user'])); + $profiles = commerce_customer_profile_load_multiple(array(), array('type' => $field['settings']['profile_type'], 'uid' => $account->uid, 'status' => TRUE)); + if (empty($profiles)) { + $form_addressbook['addressbook_user']['#suffix'] = '

' . t('The user chosen does not have any stored addresses.') . '

'; + } + } + elseif (!user_access('administer commerce_customer_profile entities')) { + global $user; + $profiles = commerce_customer_profile_load_multiple(array(), array('type' => $field['settings']['profile_type'], 'uid' => $user->uid, 'status' => TRUE)); + } + if ($profiles) { + // Prepare the options. + $options = array(); + foreach ($profiles as $id => $profile) { + $field_values = field_get_items('commerce_customer_profile', $profile, 'commerce_customer_address'); + $options[$id] = $field_values[0]['thoroughfare']; + } + drupal_alter('commerce_addressbook_labels', $options, $profiles); + // Prepare the default value. + $order_wrapper = entity_metadata_wrapper('commerce_order', $commerce_order); + $profile_reference = $order_wrapper->{$profile_field_name}->value(); + $default_value = 'none'; + if (!empty($form_state['values'][$profile_element]['addressbook'])) { + $default_value = $form_state['values'][$profile_element]['addressbook']; + } + elseif (!empty($profile_reference->profile_id)) { + $default_value = $profile_reference->profile_id; + } + // Check if the default profile is in the enabled profiles array. + if (!isset($profiles[$default_value])) { + $default_value = 'none'; + } + if ($default_value != 'none') { + // Make sure our profile type still exists.. + // Only on checkout. + if (!empty($form[$profile_element]['commerce_customer_profile_copy'])) { + if (($source_profile_type_name = variable_get($profile_field_name . '_profile_copy_source', NULL)) + && $source_profile_type = commerce_customer_profile_type_load($source_profile_type_name)) { + // Disable the profile copy checkbox. + $form[$profile_element]['commerce_customer_profile_copy']['#default_value'] = FALSE; + $form[$profile_element]['commerce_customer_profile_copy']['#access'] = FALSE; + // Loop over source profile fields and enable previously disabled + // fields. + foreach (field_info_instances('commerce_customer_profile', $source_profile_type['type']) as $field_name => $field) { + if (!empty($form[$profile_element][$field_name])){ + $langcode = $form[$profile_element][$field_name]['#language']; + $form[$profile_element][$field_name][$langcode]['#access'] = TRUE; + foreach (element_children($form[$profile_element][$field_name][$langcode]) as $key) { + $form[$profile_element][$field_name][$langcode][$key]['#access'] = TRUE; + // Disable the editing of the profile if the user doesn't + // have the right to edit customer profiles. + if (!commerce_customer_profile_access('update', $profiles[$default_value])) { + $form[$profile_element][$field_name][$langcode][$key]['#disabled'] = TRUE; + } + } + } + } + } + } + } + $form_addressbook['addressbook_entries'] = array( + '#type' => 'value', + '#value' => $profiles, + ); + $form_addressbook['addressbook'] = array( + '#addressbook' => TRUE, + '#type' => 'select', + '#title' => t('Addresses on File'), + '#description' => t('You may select a pre-existing address on file.'), + '#options' => $options, + '#empty_option' => t('-- Choose --'), + '#empty_value' => 'none', + '#ajax' => array( + 'callback' => 'commerce_addressbook_checkout_form_callback', + 'wrapper' => strtr($profile_element, '_', '-') . '-ajax-wrapper', + ), + '#element_validate' => array('commerce_addressbook_saved_addresses_validate'), + '#weight' => -100, + '#default_value' => $default_value, + ); + } + return $form_addressbook; +}