You are here

function taxonomy_access_db_rewrite_sql in Taxonomy Access Control 5

Same name and namespace in other branches
  1. 5.2 taxonomy_access.module \taxonomy_access_db_rewrite_sql()
  2. 6 taxonomy_access.module \taxonomy_access_db_rewrite_sql()

Implementation of hook_db_rewrite_sql()

File

./taxonomy_access.module, line 663
Allows administrators to specify how each category (in the taxonomy) can be used by various roles.

Code

function taxonomy_access_db_rewrite_sql($query, $table, $field) {
  if (!user_access('administer taxonomy') && ($field == 'vid' || $field == 'tid')) {
    global $user;
    $op = arg(0) == 'node' && (arg(1) == 'add' || arg(2) == 'edit') ? 'create' : 'list';

    // let's cache
    static $taxonomy_access_sql_clause;
    $clause = array();
    if (!isset($taxonomy_access_sql_clause)) {
      $taxonomy_access_sql_clause = array();
    }
    if (!isset($taxonomy_access_sql_clause[$op][$field])) {
      if (isset($user) && is_array($user->roles)) {
        $rids = array_keys($user->roles);
      }
      else {
        $rids[] = 1;
      }
      $sql = db_query("SELECT t.tid, d.vid, BIT_OR(t.grant_{$op}) AS grant_{$op} FROM {term_access} t INNER JOIN {term_data} d ON t.tid = d.tid WHERE t.rid IN ('" . implode("','", $rids) . "') AND grant_{$op} = 1 GROUP BY t.tid, d.vid");
      while ($result = db_fetch_object($sql)) {
        $tids[] = $result->tid;
        $vids[$result->vid] = $result->vid;
      }
      $clause[$op]['tid'] = isset($tids) ? implode("','", $tids) : '';
      $clause[$op]['vid'] = isset($vids) ? implode("','", $vids) : '';
      $taxonomy_access_sql_clause = $clause;
    }
    else {
      $clause[$op][$field] = $taxonomy_access_sql_clause[$op][$field];
    }
    $return['where'] = $clause[$op][$field] ? "{$table}.{$field} IN ('" . $clause[$op][$field] . "')" : "{$table}.{$field} IS NULL";
    return $return;
  }
  else {
    return array();
  }
}