You are here

function _merci_cpm_autocomplete in MERCI (Manage Equipment Reservations, Checkout and Inventory) 7.3

1 string reference to '_merci_cpm_autocomplete'
merci_cpm_menu in merci_cpm/merci_cpm.module

File

merci_cpm/merci_cpm.module, line 31

Code

function _merci_cpm_autocomplete($bundle, $string) {

  // TODO don't hard code.
  $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();

  // save the query to matches
  foreach ($result as $row) {
    $matches[$row->name . " [{$row->profile_id}]"] = check_plain($row->name);
  }
  $matches_erp = array();

  // Allow multiple types of autocomplete
  switch ($bundle) {
    case 'billing':
      $matches_erp = _evergreen_erp_autocomplete($string);
      break;
    case 'organization':
      $matches_erp = _evergreen_erp_budget_autocomplete($string);
      break;
  }

  //Merge in results
  if (!empty($matches)) {
    $matches = array_merge($matches, $matches_erp);
  }
  else {
    $matches = $matches_erp;
  }

  // Return the result to the form in json
  drupal_json_output($matches);
}