You are here

function support_save_message in Support Ticketing System 7

Same name and namespace in other branches
  1. 6 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 1632
support.module

Code

function support_save_message($message, $client) {
  $ticket = FALSE;
  if (!isset($message['nid'])) {
    $message['nid'] = 0;
  }
  if (isset($message['uid'])) {

    // TODO Convert "user_load" to "user_load_multiple" if "$message['uid']" is other than a uid.
    // To return a single user object, wrap "user_load_multiple" with "array_shift" or equivalent.
    // Example: array_shift(user_load_multiple(array(), $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 (array_key_exists('headers', $message) && 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_query('SELECT MAX(thread) FROM {comment} WHERE nid = :nid', array(
      ':nid' => $ticket->nid,
    ))
      ->fetchField();

    // 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) . '/';
    $comment = new stdClass();
    $comment->cid = NULL;
    $comment->pid = 0;
    $comment->uid = $account->uid;
    $comment->nid = $ticket->nid;
    $comment->status = COMMENT_PUBLISHED;
    $comment->thread = $thread;
    $comment->hostname = ip_address();
    $comment->language = LANGUAGE_NONE;
    $comment->name = $account->name;
    $comment->mail = $account->mail;
    $comment->subject = truncate_utf8(trim($message['subject']), 64, TRUE, TRUE);
    $comment->timestamp = REQUEST_TIME;
    $comment->comment_body[LANGUAGE_NONE][] = array(
      //'summary' => '',
      'value' => $message['body'],
      'format' => filter_default_format($account),
    );
    if ($upload_field = _support_upload_field_name('comment')) {
      if (!empty($message['attachments'])) {
        $comment->{$upload_field} = _support_save_attachments($message['attachments'], $account, 'comment');
      }
    }
    $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_query('SELECT 1 FROM {support_assigned} WHERE nid = :nid AND uid = :uid', array(
      ':nid' => $ticket->nid,
      ':uid' => $account->uid,
    ))
      ->fetchField();
    $comment->support_email = 1;
    if (isset($message['suppress'])) {
      $comment->suppress = $message['suppress'];
    }
    if (isset($message['_support_extra_fields'])) {
      foreach ($message['_support_extra_fields'] as $k => $v) {
        $comment->{$k} = $v;
      }
    }
    comment_save($comment);

    // 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)) {

      // Create new ticket if none matches and valid from address.
      // Create stub node.
      $node = (object) array(
        'uid' => $account->uid,
        'name' => isset($account->name) ? $account->name : '',
        'type' => 'support_ticket',
        'language' => LANGUAGE_NONE,
      );
      $node->title = $message['subject'];
      node_object_prepare($node);
      $node->body[LANGUAGE_NONE][] = array(
        //'summary' => '',
        'value' => $message['body'],
        'format' => filter_default_format($account),
      );
      $node->log = 'Support ticket created from email.';
      $node->uid = $account->uid;

      // Set uid again because node_object_prepare likes to mess with it.
      $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;
      if ($upload_field = _support_upload_field_name('node')) {
        if (!empty($message['attachments'])) {
          $node->{$upload_field} = _support_save_attachments($message['attachments'], $account, 'node');
        }
      }
      $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);
    }
  }
}