View source
<?php
function merci_cpm_menu() {
$items['address/autocomplete'] = array(
'title' => 'Autocomplete for addresses',
'page callback' => '_merci_cpm_autocomplete',
'access arguments' => array(
'use address autocomplete',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function merci_cpm_permission() {
return array(
'use address autocomplete' => array(
'title' => t('Use address autocomplete'),
'description' => t('Allow autocomplete of addresses via the commerce_profile_manager field widget.'),
),
);
}
function _merci_cpm_autocomplete($bundle, $string) {
$address_table = 'field_data_field_full_name';
$search_field = 'field_full_name_value';
$profile_table = 'commerce_customer_profile';
$matches = array();
$query = db_select($address_table, 'f');
$query
->addField('f', $search_field, 'name');
$query
->condition($search_field, '%' . db_like($string) . '%', 'LIKE');
$query
->condition('bundle', $bundle);
$query
->join($profile_table, 'p', 'p.profile_id = f.entity_id');
$query
->addField('p', 'profile_id');
$result = $query
->execute();
foreach ($result as $row) {
$matches[$row->name . " [{$row->profile_id}]"] = check_plain($row->name);
}
$matches_erp = array();
switch ($bundle) {
case 'billing':
$matches_erp = _evergreen_erp_autocomplete($string);
break;
case 'organization':
$matches_erp = _evergreen_erp_budget_autocomplete($string);
break;
}
if (!empty($matches)) {
$matches = array_merge($matches, $matches_erp);
}
else {
$matches = $matches_erp;
}
drupal_json_output($matches);
}
function merci_cpm_validate($element, &$form_state, $form) {
if (array_key_exists('autocomplete_profile_id', $form_state)) {
$value[] = array(
'profile_id' => $form_state['autocomplete_profile_id'],
);
form_set_value($element, $value, $form_state);
unset($form_state['input'][$element['#field_name']]);
unset($form_state['addressfield']);
unset($form_state['autocomplete_profile_id']);
}
else {
unset($form_state['triggering_element']['#limit_validation_errors']);
if (!empty($form_state['triggering_element']) && in_array('customer_profile_ui_checkboxes', $form_state['triggering_element']['#parents'], TRUE)) {
form_set_value($element, array(), $form_state);
}
else {
commerce_customer_profile_manager_validate($element, $form_state, $form);
}
}
}
function merci_cpm_autocomplete_validate($element, &$form_state, $form) {
evergreen_erp_autocomplete_validate($element, $form_state, $form);
if (!array_key_exists('autocomplete_profile_id', $form_state) or empty($form_state['autocomplete_profile_id'])) {
$name = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
$matches = array();
$result = preg_match('/\\[([0-9]+)\\]$/', $name, $matches);
if ($result > 0) {
$form_state['autocomplete_profile_id'] = $matches[$result];
}
}
}
function merci_cpm_field_widget_commerce_customer_profile_type_ui_manager_form_alter(&$element, &$form_state, $context) {
$langcode = $context['langcode'];
if (user_access('use address autocomplete') and $element['#entity_type'] == 'merci_reservation') {
foreach (element_children($element['profiles']) as $key) {
$child =& $element['profiles'][$key];
$child['#prefix'] = '<div id="merci-cpm-wrapper-' . $key . '">';
$child['#suffix'] = '</div>';
if (array_key_exists('#parents', $child)) {
unset($child['#parents']);
}
$lookup_field = array(
'#type' => 'textfield',
'#title' => 'Lookup',
'#autocomplete_path' => 'address/autocomplete/' . $child['#bundle'],
'#ajax' => array(
'callback' => 'merci_cpm_update_name',
'wrapper' => 'merci-cpm-wrapper-' . $key,
),
'#element_validate' => array(
'merci_cpm_autocomplete_validate',
),
'#weight' => -100,
);
if (!commerce_customer_ui_customer_profile_add_any_access()) {
foreach (element_children($child) as $key2) {
$child[$key2]['#access'] = FALSE;
}
if (isset($child['#entity'])) {
$element['profiles'][$key]['entity_view'] = entity_view('commerce_customer_profile', array(
$child['#entity'],
));
}
}
$child['name_lookup_field'] = $lookup_field;
}
$element['#element_validate'] = array(
'merci_cpm_validate',
);
}
}
function merci_cpm_update_name($form, &$form_state) {
$parents = array_slice($form_state['triggering_element']['#array_parents'], 0, -1);
return drupal_array_get_nested_value($form, $parents);
}