You are here

function support_save_message in Support Ticketing System 6

Same name and namespace in other branches
  1. 7 support.module \support_save_message()

Save the message.

2 calls to support_save_message()
support_client_fetch in ./support.module
Fetch mail for a specific client.
support_page_form_submit in ./support.module
Update selected tickets.

File

./support.module, line 1336
support.module

Code

function support_save_message($message, $client, $manual = FALSE) {
  if (isset($message['uid'])) {
    $account = user_load($message['uid']);
  }
  else {
    $account = support_account_load($message['from'], $message['nid'], $message['subject'], $client);
  }
  $ticket = support_ticket_load($message['nid']);
  if (is_object($message['headers']) && isset($message['headers']->message_id)) {
    $message_id = $message['headers']->message_id;
  }
  else {
    $message_id = NULL;
  }
  if (is_object($account) && is_object($ticket) && $ticket->nid) {

    // by retrieving the maximum thread level.
    $max = db_result(db_query('SELECT MAX(thread) FROM {comments} WHERE nid = %d', $ticket->nid));

    // Strip the "/" from the end of the thread.
    $max = rtrim($max, '/');

    // Finally, build the thread field for this new comment.
    $thread = int2vancode(vancode2int($max) + 1) . '/';

    // TODO: format?
    // TODO: ip_address?
    db_query("INSERT INTO {comments} (nid, pid, uid, subject, comment, format, hostname, timestamp, status, thread, name, mail, homepage) VALUES (%d, %d, %d, '%s', '%s', %d, '%s', %d, %d, '%s', '%s', '%s', '%s')", $ticket->nid, 0, $account->uid, $message['subject'], $message['body'], 1, ip_address(), time(), 0, $thread, $account->name, $account->mail, isset($account->homepage) ? $account->homepage : '');

    // Tell the other modules a new comment has been submitted.
    $comment['cid'] = db_last_insert_id('comments', 'cid');
    $comment['subject'] = truncate_utf8(trim($message['subject']), 64, TRUE, TRUE);
    $comment['comment'] = $message['body'];
    $comment['nid'] = $ticket->nid;
    $comment['uid'] = $account->uid;
    $comment['message_id'] = $message_id;
    $comment['state'] = isset($message['state']) ? $message['state'] : $ticket->state;
    $comment['priority'] = isset($message['priority']) ? $message['priority'] : $ticket->priority;
    $comment['client'] = $ticket->client;
    $comment['assigned'] = isset($message['assigned']) ? $message['assigned'] : $ticket->assigned;
    $comment['notification'] = db_result(db_query('SELECT 1 FROM {support_assigned} WHERE nid = %d AND uid = %d', $ticket->nid, $account->uid));
    $comment['support_email'] = TRUE;
    $comment['files'] = _support_save_attachments($message['attachments'], $account, $manual);
    if (isset($message['suppress'])) {
      $comment['suppress'] = $message['suppress'];
    }

    // convert indivudal files to arrays to work with the comment_upload module
    foreach ($comment['files'] as $fid => $file) {
      $comment['files'][$fid] = (array) $file;
    }
    if (isset($message['_support_extra_fields'])) {
      foreach ($message['_support_extra_fields'] as $k => $v) {
        $comment[$k] = $v;
      }
    }
    comment_invoke_comment($comment, 'insert');

    // Add an entry to the watchdog log.
    watchdog('content', 'Comment: added %subject.', array(
      '%subject' => $comment['subject'],
    ), WATCHDOG_NOTICE, l(t('view'), 'node/' . $comment['nid'], array(
      'fragment' => 'comment-' . $comment['cid'],
    )));
    _comment_update_node_statistics($comment['nid']);

    // Clear the cache so an anonymous user can see his comment being added.
    cache_clear_all();
    return TRUE;
  }
  else {
    if (is_object($account)) {
      $node = new stdClass();

      // Create new ticket if none matches and valid from address.
      $node->title = $message['subject'];
      $node->body = $message['body'];
      $node->type = 'support_ticket';
      $node->log = t('Support ticket created from email.');
      $node->comment = COMMENT_NODE_READ_WRITE;
      $node->uid = $account->uid;
      $node->message_id = $message_id;
      $node->state = isset($message['state']) ? $message['state'] : _support_state_default();
      $node->priority = isset($message['priority']) ? $message['priority'] : _support_priority_default();
      $node->client = $client->clid;
      $node->assigned = _support_autoassign($client->clid, $account->uid);
      $node->notification = TRUE;
      $node->support_email = TRUE;
      $node->language = $account->language;

      // Save/record attachments
      if (isset($message['attachments'])) {
        $node->files = _support_save_attachments($message['attachments'], $account, $manual);
      }
      $node->created_by_email = TRUE;
      if (isset($message['_support_extra_fields'])) {
        foreach ($message['_support_extra_fields'] as $k => $v) {
          $node->{$k} = $v;
        }
      }
      node_save($node);
      if ($manual) {
        drupal_set_message(t('Created new ticket: !title.', array(
          '!title' => l($node->title, 'node/' . $node->nid),
        )));
      }
      watchdog('content', 'Node: created %title.', array(
        '%title' => $node->title,
      ), WATCHDOG_NOTICE, l(t('view'), 'node/' . $node->nid));
    }
  }
}