You are here

function crm_core_contact_search_execute in CRM Core 7

Implements hook_search_execute().

File

modules/crm_core_contact/crm_core_contact.module, line 410
Provides default CRM Core Contact entities and the ability to create more.

Code

function crm_core_contact_search_execute($keys = NULL, $conditions = NULL) {

  // Build matching conditions.
  $query = db_select('search_index', 'i', array(
    'target' => 'slave',
  ))
    ->extend('SearchQuery')
    ->extend('PagerDefault');
  $query
    ->join('crm_core_contact', 'c', 'c.contact_id = i.sid');
  $query
    ->searchExpression($keys, 'crm_core_contact');

  // Insert special keywords.
  $query
    ->setOption('type', 'c.type');
  $query
    ->setOption('language', 'c.language');

  // Only continue if the first pass query matches.
  if (!$query
    ->executeFirstPass()) {
    return array();
  }

  // Load results.
  $find = $query
    ->limit(10)
    ->execute();
  $results = array();
  foreach ($find as $item) {

    // Render the contact.
    $contact = crm_core_contact_load($item->sid);
    $build = crm_core_contact_view($contact);
    unset($build['#theme']);
    $contact->rendered = drupal_render($build);
    $title = field_get_items('crm_core_contact', $contact, 'contact_name');
    $title = name_format($title[0], '((((t+ig)+im)+if)+is)+jc');
    $uri = entity_uri('crm_core_contact', $contact);
    $results[] = array(
      'link' => url($uri['path'], array_merge($uri['options'], array(
        'absolute' => TRUE,
      ))),
      'type' => check_plain(crm_core_contact_type_get_name($contact->type)),
      'title' => $title,
      'user' => theme('username', array(
        'account' => user_load($contact->uid),
      )),
      'date' => $contact->changed,
      'contact' => $contact,
      'score' => $item->calculated_score,
      'snippet' => search_excerpt($keys, $contact->rendered),
      'language' => isset($contact->language) ? $contact->language : LANGUAGE_NONE,
    );
  }
  return $results;
}