You are here

function search_files_attachments_search_execute in Search Files 7.2

Implements hook_search_execute().

Build a file list whose content matches $keys

File

./search_files_attachments.module, line 76
Used to index files in attachments

Code

function search_files_attachments_search_execute($keys = NULL, $conditions = NULL) {
  if (!user_access('view search_files results')) {
    return array();
  }

  // Select attached files id from content with attached fields.
  $field_id_list = _search_files_attachments_create_field_id_list();

  // Only continue if attached files found.
  if (count($field_id_list) == 0) {
    return array();
  }

  // Search $keys expression in the selected files
  $query = db_select('search_index', 'i', array(
    'target' => 'slave',
  ))
    ->extend('SearchQuery')
    ->extend('PagerDefault');
  $query
    ->join('search_dataset', 'd', 'd.sid = i.sid');
  $query
    ->join('file_managed', 'm', 'm.fid = d.sid');
  $query
    ->fields('i', array(
    'score',
  ))
    ->fields('d', array(
    'data',
  ))
    ->fields('m', array(
    'filename',
    'uri',
  ))
    ->condition('i.sid', $field_id_list, 'IN')
    ->condition('i.type', 'search_files_att');
  $query
    ->searchExpression($keys, 'search_files_att');

  // 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) {
    $results[] = array(
      'link' => file_create_url($item->uri),
      'title' => $item->filename,
      'score' => $item->score,
      'snippet' => search_excerpt($keys, $item->data),
    );
  }
  return $results;
}