You are here

function sbp_attach_sbp_query_modify in Search by Page 7

Same name and namespace in other branches
  1. 6 sbp_attach.module \sbp_attach_sbp_query_modify()

Implements Search by Page hook_sbp_query_modify().

Adds to the Search by Page query.

File

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

Code

function sbp_attach_sbp_query_modify($environment, $query) {

  // Note: At the moment, this function is assuming that we're only
  // searching for files attached to nodes. So we use the node access
  // permissions, and assume tacitly that the object type is node.
  // If this module is ever expanded to do other attachments, this will
  // need to be addressed!
  $cond = db_and();
  if (!user_access('access content')) {

    // This user cannot access content, so don't bother with the query
    // mods, they should not see anything from this module.
    $cond
      ->where('0=1');
    return $cond;
  }

  // Attach to our attachments table and the node table, using a subquery.
  $subquery = db_select('sbpa_attachments', 'sbpa_sq');
  $subquery
    ->condition('sbpa_sq.objtype', 'node');
  $subquery
    ->condition('sbpa_sq.environment', $environment);
  $subquery
    ->leftJoin('node', 'sbpa_n', 'sbpa_sq.objid = sbpa_n.nid');
  $subquery
    ->addTag('node_access');
  $subquery
    ->condition('sbpa_n.status', 1);
  $subquery
    ->addField('sbpa_sq', 'sbpaid', 'sbpaid');

  // If we're only looking for listed attachments, add condition.
  $listed_only = search_by_page_setting_get('sbp_attach_only_listed', $environment, 0);
  if ($listed_only) {
    $subquery
      ->condition('sbpa_sq.display', 1);
  }
  $query
    ->leftJoin($subquery, 'sbpa_a', 'sbpa_a.sbpaid = sp.modid');
  return $cond;
}