You are here

function search_by_page_attach_search_by_page_details in Search by Page 8

Implements Search by Page hook_search_by_page_details().

Returns details for a particular file ID, for particular search keys.

File

search_by_page_attach/search_by_page_attach.module, line 252
Module file for Search by Page Attachments, a sub-module for Search by Page.

Code

function search_by_page_attach_search_by_page_details($id, $environment, $keys = NULL) {

  // Find the information from our table for this item.
  $id = intval($id);
  if (!$id) {

    // no ID given to us
    return NULL;
  }
  $info = \Drupal::database()
    ->query('SELECT * FROM {sbpa_attachments} WHERE sbpaid=:id', array(
    ':id' => $id,
  ))
    ->fetchObject();
  if (!$info) {

    // We have no record of this ID.
    return NULL;
  }

  // Load the file (we need file name, at a minimum)
  $file = file_load($info->fileid);
  if (!$file) {

    // File no longer exists.
    return NULL;
  }

  // Load the object (i.e. $node) this is attached to, making sure
  // to reset the object cache in case someone else did a node_view(),
  // which might have removed our non-displayed files!
  $obj = @entity_load($info->objtype, array(
    $info->objid,
  ), array(), TRUE);
  if (!$obj || !isset($obj[$info->objid]) || !$obj[$info->objid]) {

    // Item we're attached to can't be found.
    return NULL;
  }
  $obj = $obj[$info->objid];

  // Locate the info about this file attached to this object.
  $fields = _search_by_page_attach_object_fields($obj, $info->fieldname);
  foreach ($fields as $stuff) {
    if ($stuff['fid'] == $info->fileid) {
      $fieldstuff = $stuff;
      break;
    }
  }
  if (is_null($fieldstuff)) {

    // field is no longer on this node/object.
    return NULL;
  }
  $only_listed = \Drupal::service('search_by_page.settings')
    ->getSetting('search_by_page_attach_only_listed', $environment, 0);
  if ($only_listed && isset($fieldstuff['display']) && !$fieldstuff['display']) {

    // Asked for only displayed files and this one isn't.
    return NULL;
  }
  $fullpath = drupal_realpath($file->uri);
  if (!$fullpath || !is_file($fullpath)) {

    // file was not in an acceptable location or doesn't exist
    return NULL;
  }

  // Construct information for this entry.
  $ret = array();
  $ret['object'] = $file;
  $use_desc = \Drupal::service('search_by_page.settings')
    ->getSetting('search_by_page_attach_use_description', $environment, 0);
  $prepend_node_title = \Drupal::service('search_by_page.settings')
    ->getSetting('search_by_page_attach_prepend_node_title', $environment, 0);
  $ret['title'] = $file->filename;
  if ($use_desc && isset($fieldstuff['description']) && $fieldstuff['description']) {
    $ret['title'] = $fieldstuff['description'];
  }
  if ($prepend_node_title) {
    $sep = \Drupal::service('search_by_page.settings')
      ->getSetting('search_by_page_attach_title_sep', $environment, '/');
    if (isset($obj->title) && $obj->title) {
      $ret['title'] = $obj->title . $sep . $ret['title'];
    }
  }
  $ret['title'] = Html::escape($ret['title']);

  // Set the content to an empty string, in case we cannot extract it.
  // In this case, we'll just return the title we've constructed.
  $ret['content'] = '';

  // Extract file content, using helper function
  $helpers = search_files_get_helpers();
  $fileinfo = pathinfo($file->filename);
  $extension = strtolower($fileinfo['extension']);
  if (!isset($helpers[$extension])) {

    // no text extraction function for this file extension defined
    return $ret;
  }
  $help = $helpers[$extension];
  $quoted_file_path = '"' . escapeshellcmd($fullpath) . '"';
  $helper_command = preg_replace('/%file%/', $quoted_file_path, $help);
  $content = search_files_convert_to_utf8(shell_exec($helper_command));
  if (!$content) {

    // no text in file
    return $ret;
  }
  if (!$keys) {

    // Return file content, if there are no search keys
    $ret['content'] = $content;
  }
  else {

    // If there are search keys, return a snippet, user name, date, type, etc.
    $ret['user'] = theme('username', array(
      'account' => $obj,
    ));
    if (isset($obj->changed)) {
      $ret['date'] = $obj->changed;
    }
    $ret['snippet'] = \Drupal::service('search_by_page.settings')
      ->excerpt($keys, $content);
    $ret['related_node'] = $obj;
  }
  return $ret;
}