You are here

function support_pm_db_rewrite_sql in Support Ticketing System 6

File

support_pm/support_pm.module, line 249
Support Project Management. @author Jeremy Andrews <jeremy@tag1consulting.com> @package Support

Code

function support_pm_db_rewrite_sql($query, $primary_table, $primary_field, $args) {

  // We need to ensure that we don't mistake a state for a project.
  $path = FALSE;
  if (arg(3)) {
    $path = arg(3);
    $states = array(
      'all' => 'all',
      'all open' => 'all open',
      'my open' => 'my open',
    ) + _support_states();
    $states = array_values($states);
    if (in_array($path, $states)) {

      // Hmm, arg(3) was a state, we must be on a subclient.
      $path = arg(4);
    }
  }
  if ($primary_table == 'n' && $primary_field == 'nid' && strpos($query, 'ELECT DISTINCT') && strpos($query, 'support_ticket') && strpos($query, 'client =') && $path) {
    if ($path == preg_replace('/[^0-9a-zA-Z_-]/', '', $path)) {
      return array(
        'join' => ' LEFT JOIN {support_project_ticket} spt ON n.nid = spt.nid LEFT JOIN {support_project} sp ON spt.projid = sp.projid',
        'where' => "sp.path = '{$path}'",
      );
    }
  }
  else {
    if ($primary_table == 'n' && $primary_field == 'nid' && strpos($query, 'support_ticket_timer') && strpos($query, 't.client IN')) {
      $project = isset($_GET['project']) ? $_GET['project'] : '';
      if ($project && $project == preg_replace('/[^0-9a-zA-Z_-]:/', '', $project)) {
        list($project, $client) = explode(':', $project);
        if (!$client) {
          $client = _support_current_client();
        }
        $alter = array(
          'join' => ' LEFT JOIN {support_project_ticket} spt ON n.nid = spt.nid LEFT JOIN {support_project} sp ON spt.projid = sp.projid LEFT JOIN {support_project_client} spc ON sp.projid = spc.projid',
        );
        if (strtolower($project) == 'null') {
          $alter['where'] = "ISNULL(sp.path) AND spc.clid = {$client}";
        }
        else {
          $alter['where'] = "sp.path = '{$project}' AND spc.clid = {$client}";
        }
        return $alter;
      }
    }
    else {
      if ($primary_table == 'c' && $primary_field == 'cid' && strpos($query, 'support_ticket_comment_timer') && strpos($query, 't.client IN')) {
        $project = isset($_GET['project']) ? $_GET['project'] : '';
        if ($project && $project == preg_replace('/[^0-9a-zA-Z_-]:/', '', $project)) {
          list($project, $client) = explode(':', $project);
          if (!$client) {
            $client = _support_current_client();
          }
          $alter = array(
            'join' => ' LEFT JOIN {support_project_ticket} spt ON c.nid = spt.nid LEFT JOIN {support_project} sp ON spt.projid = sp.projid LEFT JOIN {support_project_client} spc ON sp.projid = spc.projid',
          );
          if (strtolower($project) == 'null') {
            $alter['where'] = "ISNULL(sp.path) AND spc.clid = {$client}";
          }
          else {
            $alter['where'] = "sp.path = '{$project}' AND spc.clid = {$client}";
          }
          return $alter;
        }
      }
    }
  }
}