You are here

function sbp_attach_sbp_paths in Search by Page 6

Same name and namespace in other branches
  1. 7 sbp_attach.module \sbp_attach_sbp_paths()

Implementation of Search by Page hook_sbp_paths().

Returns a list of all the files that should be indexed, and also saves information in a DB table for future reference.

File

./sbp_attach.module, line 17
Module file for Search by Page Attachments, a sub-module for Search by Page.

Code

function sbp_attach_sbp_paths($environment) {
  $ret = array();
  $listed_only = search_by_page_setting_get('sbp_attach_only_listed', $environment, 0);

  // What node types' attachments do they want to index?
  // Read from setting, and convert to an array of just the 1 values
  $typelist = search_by_page_setting_get('sbp_attach_node_types', $environment, array());
  if (!is_array($typelist) || !count($typelist)) {
    return $ret;
  }
  $nodetypes = array();
  foreach ($typelist as $key => $item) {
    if ($item) {
      $nodetypes[] = $key;
    }
  }
  if (!count($nodetypes)) {
    return $ret;
  }
  $role = search_by_page_setting_get('sbp_attach_role', $environment, DRUPAL_ANONYMOUS_RID);
  $langs = language_list();
  $langs = array_keys($langs);
  $min_time = search_by_page_setting_get('sbp_attach_min_time', $environment, 0);
  $max_time = search_by_page_setting_get('sbp_attach_max_time', $environment, 0);

  // Find all file attachments on those types, build array of paths,
  // and save info in DB.
  // Note that we don't want to check for access permissions here! We
  // want to index everything, independent of access rights. Access
  // permission checking is done during the search step -- see
  // hook_sbp_query_modify() implementation below.
  // First find files managed via the Upload module, if it is installed
  if (module_exists('upload')) {
    $extrawhere = '';
    if ($listed_only) {
      $extrawhere = ' AND u.list = 1 ';
    }
    $res = db_query('SELECT f.fid, f.filepath, n.nid, n.vid, n.language FROM {node} n LEFT JOIN ({upload} u LEFT JOIN {files} f ON u.fid = f.fid) ON n.nid = u.nid AND n.vid = u.vid WHERE f.status = 1 AND n.status=1 AND n.type IN (' . db_placeholders($nodetypes, 'varchar') . ')' . $extrawhere, $nodetypes);
    while ($item = db_fetch_object($res)) {

      // Add to array for return to Search by Page
      $stuff = array(
        'id' => $item->fid,
        'role' => $role,
        'min_time' => $min_time,
        'max_time' => $max_time,
      );

      // Check the node's language.
      if ($item->language) {
        $stuff['languages'] = array(
          $item->language,
        );
      }
      else {

        // language-neutral - index in all languages
        $stuff['languages'] = $langs;
      }
      $ret[$item->filepath] = $stuff;

      // Add to our DB table to record where this came from
      db_query('DELETE FROM {sbpa_files} WHERE fid = %d', $item->fid);
      db_query('INSERT INTO {sbpa_files} (fid, source, nid, vid) VALUES (%d, \'upload\', %d, %d)', $item->fid, $item->nid, $item->vid);
    }
  }

  // end of if (db_table_exists('upload')
  // Now see if they selected any CCK fields.
  if (!function_exists('content_fields') || !function_exists('content_database_info')) {
    return $ret;
  }
  $fieldlist = search_by_page_setting_get('sbp_attach_field_types', $environment, array());
  if (!is_array($fieldlist) || !count($fieldlist)) {
    return $ret;
  }

  // Go through CCK fields they selected, and find file attachments
  // Note that in $fieldlist, only items with value = 1 are selected
  foreach ($fieldlist as $fieldname => $value) {
    if (!$value) {
      continue;
    }

    // Check each content type we're indexing, and see if it has this field
    foreach ($nodetypes as $type) {

      // See if this content type has this field on it.
      // Note: This may seem inefficient, but CCK caches the information,
      // so this is actually a pretty fast function call.
      $field = content_fields($fieldname, $type);
      if (!$field) {
        continue;
      }

      // Verify it is a FileField type field -- i.e. has 'fid' component
      $dbinfo = content_database_info($field);
      if (!$dbinfo['columns']['fid']) {
        continue;
      }
      $fidcol = $dbinfo['columns']['fid']['column'];

      // If we're restricting to Listed, and we have a 'list' component,
      // create WHERE clause for that
      $extrawhere = '';
      $listcol = '';
      if ($listed_only && $dbinfo['columns']['list']) {
        $listcol = $dbinfo['columns']['list']['column'];
        $extrawhere = ' AND u.' . $listcol . ' = 1 ';
      }

      // Query DB to find files
      $res = db_query('SELECT f.fid, f.filepath, n.nid, n.vid, n.language FROM {node} n LEFT JOIN ({' . $dbinfo['table'] . '} u LEFT JOIN {files} f ON u.' . $fidcol . ' = f.fid) ON n.nid = u.nid AND n.vid = u.vid WHERE f.status = 1 AND n.status=1 AND n.type=\'' . $type . "' " . $extrawhere);
      while ($item = db_fetch_object($res)) {

        // Add to return list for Search by Page
        $stuff = array(
          'id' => $item->fid,
          'role' => $role,
          'min_time' => $min_time,
          'max_time' => $max_time,
        );
        if ($item->language && $item->language != LANGUAGE_NONE) {
          $stuff['languages'] = array(
            $item->language,
          );
        }
        else {

          // language-neutral - index in all languages
          $stuff['languages'] = $langs;
        }
        $ret[$item->filepath] = $stuff;

        // Add to our DB table to record where this came from
        db_query('DELETE FROM {sbpa_files} WHERE fid = %d', $item->fid);
        db_query('INSERT INTO {sbpa_files} (fid, source, nid, vid, typename, fieldname) VALUES (%d, \'cck\', %d, %d, \'%s\', \'%s\')', $item->fid, $item->nid, $item->vid, $type, $fieldname);
      }

      // end of where loop over files
    }

    // end of foreach loop over node types
  }

  // end of foreach loop over CCK field types
  return $ret;
}