You are here

function support_db_rewrite_sql in Support Ticketing System 6

Imeplementation of hook_db_rewrite_sql(). Optionally remove support tickets from content search.

File

./support.module, line 1904
support.module

Code

function support_db_rewrite_sql($query, $primary_table, $primary_field, $args) {
  global $user;

  // If enabled, remove support tickets from core search results.
  if (variable_get('support_remove_tickets', TRUE) && $query == '' && $primary_table == 'n' && ($primary_field = 'nid' && empty($args))) {
    return array(
      'where' => " n.type != 'support_ticket'",
    );
  }

  // Filter tickets user doesn't have access to from core tracker.
  if ($primary_table == 'n' and $primary_field == 'nid' && !strpos($query, '{support_ticket}')) {
    $clients = support_search_available_clients();
    if (!empty($clients)) {
      if (user_access('view other users tickets') || user_access('administer support') || user_access('edit any ticket') || user_access('delete any ticket')) {
        return array(
          'join' => ' LEFT JOIN {support_ticket} st ON n.nid = st.nid',
          'where' => ' (st.client IN (' . implode(',', $clients) . ')  OR st.client IS NULL)',
        );
      }
      else {
        return array(
          'join' => ' LEFT JOIN {support_ticket} st ON n.nid = st.nid',
          'where' => ' ((st.client IN (' . implode(',', $clients) . ') AND n.uid = ' . $user->uid . ') OR st.client IS NULL)',
        );
      }
    }
    else {
      return array(
        'where' => " n.type != 'support_ticket'",
      );
    }
  }
  else {
    if ($primary_table == 'nc' and $primary_field == 'nid' && !strpos($query, '{support_ticket_comment}')) {
      $clients = support_search_available_clients();
      if (!empty($clients)) {
        if (user_access('view other users tickets') || user_access('administer support') || user_access('edit any ticket') || user_access('delete any ticket')) {
          return array(
            'join' => ' LEFT JOIN {support_ticket} st ON nc.nid = st.nid',
            'where' => ' (st.client IN (' . implode(',', $clients) . ')  OR st.client IS NULL)',
          );
        }
        else {
          return array(
            'join' => ' LEFT JOIN {node} n ON nc.nid = n.nid LEFT JOIN {support_ticket} st ON nc.nid = st.nid',
            'where' => ' (st.client IN (' . implode(',', $clients) . ') AND (n.uid = ' . $user->uid . ' OR st.assigned = ' . $user->uid . ")) OR n.type != 'support_ticket'",
          );
        }
      }
      else {
        return array(
          'join' => ' LEFT JOIN {node} n ON nc.nid = n.nid',
          'where' => " n.type != 'support_ticket'",
        );
      }
    }
    else {
      if ($primary_table == 'n' && $primary_field == 'nid' && strpos($query, 'support_ticket_timer') && strpos($query, 't.client IN')) {
        $tid = isset($_GET['tid']) ? $_GET['tid'] : '';
        if ($tid) {
          $alter = array(
            'join' => ' LEFT JOIN {term_node} tn ON n.nid = tn.nid',
          );
          if ($tid == 'null') {
            $alter['where'] = "ISNULL(tn.tid)";
          }
          else {
            if ($tid && $tid == preg_replace('/[^0-9,]/', '', $tid)) {
              $alter['where'] = "tn.tid IN({$tid})";
            }
          }
          return $alter;
        }
      }
      else {
        if ($primary_table == 'c' && $primary_field == 'cid' && strpos($query, 'support_ticket_comment_timer') && strpos($query, 't.client IN')) {
          $tid = isset($_GET['tid']) ? $_GET['tid'] : '';
          if ($tid) {
            $alter = array(
              'join' => ' LEFT JOIN {term_node} tn ON c.nid = tn.nid',
            );
            if ($tid == 'null') {
              $alter['where'] = "ISNULL(tn.tid)";
            }
            else {
              if ($tid && $tid == preg_replace('/[^0-9,]/', '', $tid)) {
                $alter['where'] = "tn.tid IN({$tid})";
              }
            }
            return $alter;
          }
        }
      }
    }
  }
}