You are here

function sbp_paths_sbp_query_modify in Search by Page 7

Same name and namespace in other branches
  1. 6 sbp_paths.module \sbp_paths_sbp_query_modify()

Implements Search by Page hook_sbp_query_modify().

Adds an access permission check to the search query.

File

./sbp_paths.module, line 54
Module file for Search by Page Paths, a sub-module for Search by Page.

Code

function sbp_paths_sbp_query_modify($environment, $query) {

  // Unfortunately, we don't have a wholesale way to check access permissions.
  // So we have to go through all the paths in our table, and check whether the
  // user has permission to see each one, by querying the menu system.
  // Hopefully, there aren't too many! We store them in a temporary table.
  // Create temporary table
  $table = db_query_temporary('SELECT p.pid, 1 as perm FROM {sbpp_path} p WHERE p.environment=:env', array(
    ':env' => $environment,
  ));

  // Check permissions on each path, store in temporary table
  $res = db_query('SELECT p.pid, p.page_path FROM {sbpp_path} p WHERE p.environment=:env', array(
    ':env' => $environment,
  ))
    ->fetchAll();
  foreach ($res as $item) {
    $parts = search_by_page_path_parts($item->page_path);
    $mitem = menu_get_item($parts[0]);
    if (!$mitem['access']) {
      db_delete($table)
        ->condition('pid', $item->pid)
        ->execute();
    }
  }

  // Join to our temporary table
  $query
    ->leftJoin($table, 'sbpp_p', 'sbpp_p.pid = sp.modid');
  $cond = db_and();
  $cond
    ->condition('sbpp_p.perm', 1);
  return $cond;
}