You are here

function _support_identify_ticket in Support Ticketing System 7

Same name and namespace in other branches
  1. 6 support.module \_support_identify_ticket()

Search for an existing ticket to associate an incoming message with.

Parameters

&$client: Client object.

&$message: Message being processed.

1 call to _support_identify_ticket()
support_client_fetch in ./support.module
Fetch mail for a specific client.

File

./support.module, line 3695
support.module

Code

function _support_identify_ticket(&$client, &$message) {
  global $base_url;

  // A) Check for ticket number defined in the Subject.
  $key = variable_get('support_key', 'tkt');
  $tickets = array();
  preg_match("/(\\[{$key}:)([0-9]*)(\\])/", $message['subject'], $tickets);
  if (isset($tickets[2])) {
    $message['nid'] = $tickets[2];
    return TRUE;
  }
  if (variable_get('support_thread_by_mail_headers', TRUE)) {
    $id_right = preg_replace('|.+://([a-zA-Z0-9\\._-]+).*|', '\\1', $base_url);

    // Search In-Reply-To and References...
    $check = '';
    if (isset($message['headers']->in_reply_to)) {
      $check .= $message['headers']->in_reply_to;
    }
    if (isset($message['headers']->references)) {

      // Turn references header around.
      $check .= implode(' ', array_reverse(explode(' ', $message['headers']->references)));
    }
    $message_ids = array();
    preg_match_all("/<[^<^>]*/", $check, $message_ids);
    foreach ($message_ids[0] as $message_id) {
      $message_id .= '>';

      // B) Check for a reply to one of the messages generated by us.
      $matches = array();
      if (preg_match('/^<(\\d+)\\.(\\d+)@' . $id_right . '>$/', $message_id, $matches)) {
        $cid = $matches[1];
        $nid = $matches[2];

        // Reply was directly to node.
        if (!$cid) {

          // Check message id against our records.
          if (db_query('SELECT 1 FROM {support_ticket} t WHERE t.nid = :nid AND t.client = :client', array(
            ':nid' => $nid,
            ':client' => $client->clid,
          ))
            ->fetchField()) {
            $message['nid'] = $nid;
            return TRUE;
          }
        }
        else {

          // Check message id against our records.
          if (db_query('SELECT 1 FROM {comment} c INNER JOIN {support_ticket_comment} t ON c.cid = t.cid WHERE c.cid = :cid AND c.nid = :nid AND t.client = :client', array(
            ':cid' => $cid,
            ':nid' => $nid,
            ':client' => $client->clid,
          ))
            ->fetchField()) {
            $message['nid'] = $nid;
            return TRUE;
          }
        }
      }

      // C) Check for reply to the incoming message that created the ticket.
      $nid = db_query("SELECT nid FROM {support_ticket} WHERE message_id = :message_id", array(
        ':message_id' => $message_id,
      ))
        ->fetchField();
      if (isset($nid) && is_numeric($nid)) {
        $message['nid'] = $nid;
        return TRUE;
      }

      // D) Check for reply to an incoming message that created a followup.
      $nid = db_query("SELECT c.nid FROM {support_ticket_comment} j INNER JOIN {comment} c ON j.cid = c.cid WHERE j.message_id = :message_id", array(
        ':message_id' => $message_id,
      ))
        ->fetchField();
      if (isset($nid) && is_numeric($nid)) {
        $message['nid'] = $nid;
        return TRUE;
      }
    }
  }

  // E) Look for tickets with an identical subject.
  if (!$client->thread_subject) {
    $client->thread_subject = variable_get('support_thread_by_subject', 3);
  }
  switch ($client->thread_subject) {
    case 1:

      // No subject matching.
      break;
    case 2:

      // Match against new tickets.
      $message['nid'] = db_query_range("SELECT t.nid FROM {support_ticket} t LEFT JOIN {node} n ON t.nid = n.nid LEFT JOIN {support_states} s ON t.state = s.sid WHERE t.client = :client AND n.title = :title AND s.isdefault = :isdefault ORDER BY t.nid DESC", 0, 1, array(
        ':client' => $client->clid,
        ':title' => $message['subject'],
        ':isdefault' => 1,
      ))
        ->fetchField();
      break;
    case 3:

      // Match against open tickets.
      $message['nid'] = db_query_range("SELECT t.nid FROM {support_ticket} t LEFT JOIN {node} n ON t.nid = n.nid LEFT JOIN {support_states} s ON t.state = s.sid WHERE t.client = :client AND n.title = :title AND s.isclosed = :isclosed ORDER BY t.nid DESC", 0, 1, array(
        ':client' => $client->clid,
        ':title' => $message['subject'],
        ':isclosed' => 0,
      ))
        ->fetchField();
      break;
    case 4:

      // Match against any tickets.
      $message['nid'] = db_query_range("SELECT t.nid FROM {support_ticket} t LEFT JOIN {node} n ON t.nid = n.nid WHERE t.client = :client AND n.title = :title ORDER BY t.nid DESC", 0, 1, array(
        ':client' => $client->clid,
        ':title' => $message['subject'],
      ))
        ->fetchField();
      break;
  }
  return isset($message['nid']);
}