You are here

function spaces_og_db_rewrite_sql in Spaces 7.3

Same name and namespace in other branches
  1. 6.3 spaces_og/spaces_og.module \spaces_og_db_rewrite_sql()
  2. 7 spaces_og/spaces_og.module \spaces_og_db_rewrite_sql()

Implements hook_db_rewrite_sql().

File

spaces_og/spaces_og.module, line 529

Code

function spaces_og_db_rewrite_sql($query, $primary_table, $primary_field, $args) {
  switch ($primary_table) {
    case 'taxonomy_term_data':
    case 'td':
    case 't':

      // This check prevents rewrites of queries run against single nodes
      // (e.g. node_load()) and rewrites in the admin section of the site.
      if (arg(0) !== 'admin' && strpos($query, 'r.vid = %d') === FALSE) {
        $return = array();
        $return['distinct'] = 1;
        $return['join'] = "\n          LEFT JOIN {taxonomy_term_node} spaces_og_tn ON {$primary_table}.{$primary_field} = spaces_og_tn.tid\n          LEFT JOIN {taxonomy_vocabulary} spaces_og_v ON {$primary_table}.vid = spaces_og_v.vid\n          LEFT JOIN {og_ancestry} spaces_og_oga ON spaces_og_tn.nid = spaces_og_oga.nid\n          LEFT JOIN {node} spaces_og_n ON spaces_og_oga.group_nid = spaces_og_n.nid";

        // Show tags that haven't been used or are on non-og enabled nodes.
        $return['where'] = "(spaces_og_oga.group_nid IS NULL)";

        // Allow any terms that do not belong to a freetagging vocab.
        $return['where'] .= " OR (spaces_og_v.tags = 0)";

        // Allow any tags that belong to nodes in a user's groups.
        global $user;
        if (!empty($user->og_groups)) {
          $groups = array_keys($user->og_groups);
          $groups = implode(', ', $groups);
          $return['where'] .= " OR (spaces_og_oga.group_nid IN ({$groups}) AND spaces_og_n.status = 1)";
        }
        return $return;
      }
      break;
    case 'users':
    case 'u':

      // This check prevents rewrites of queries run to retrive single users,
      // (e.g. user_load()) and rewrites in the admin sectin of the site.
      if (arg(0) !== 'admin' && !user_access('view users outside groups') && strpos($query, 'uid = %d') === FALSE) {
        $return = array();
        $return['distinct'] = 1;
        $return['join'] = "\n          LEFT JOIN {og_uid} spaces_og_ogu ON {$primary_table}.{$primary_field} = spaces_og_ogu.uid\n          JOIN {node} spaces_og_n ON spaces_og_ogu.nid = spaces_og_n.nid";

        // Filter by user's groups.
        global $user;
        if (!empty($user->og_groups)) {
          $groups = array_keys($user->og_groups);
          $groups = implode(', ', $groups);
          $return['where'] = "\n            (spaces_og_ogu.nid IN ({$groups}) AND spaces_og_n.status = 1) OR\n            ({$primary_table}.{$primary_field} = {$user->uid})";
        }
        else {
          $return['where'] = "FALSE OR ({$primary_table}.{$primary_field} = {$user->uid})";
        }
        return $return;
      }
      break;
  }
}