You are here

function casetracker_mailhandler in Case Tracker 5

Implementation of hook_mailhandler().

project_number: 500 type: casetracker_case case_title: This is a case title! Yes! assign_to: Morbus Iff case_status: open case_priority: 1-high case_type: bug

File

./casetracker_mail.module, line 193
Enables mail sending and Mailhandler integration for Case Tracker.

Code

function casetracker_mailhandler($node, $result, $msg_number, $header, $mailbox) {
  $source_msg_id = $header->references;

  // used for comment replies. actually
  // dependent on a few assumptions: the angle brackets are stored in the
  // casetracker_mail table and that a reply can never be replied too. both
  // are accurate, but it's not the most flexible of designs.
  // determine the right values based on those in the email.
  $node->title = $node->case_title ? $node->case_title : $node->title;
  $node->assign_to = $node->assign_to ? $node->assign_to : $node->uid;
  foreach (array(
    'priority',
    'status',
    'type',
  ) as $state) {
    $options = casetracker_case_state_load($state);
    $node->{'case_' . $state . '_id'} = $node->{'case_' . $state} ? db_result(db_query("SELECT csid FROM {casetracker_case_states} WHERE case_state_name = LOWER('%s') AND case_state_realm = '%s'", drupal_strtolower($node->{'case_' . $state}), $state)) : variable_get('casetracker_default_case_' . $state, array_shift(array_keys($options)));
  }

  // @todo potential hack for CCK fields: explode on [ and make
  // a new array level for every one you see. maybe, maybe.
  $node->pid = db_result(db_query("SELECT nid FROM {casetracker_project} WHERE project_number = %d", $node->project_number));
  if ($node->pid && !$source_msg_id) {

    // if we've got a project number, and this isn't a reply, make a new case.
    $node->status = 1;
    return $node;

    // we'll publish the node by default, but @todo this should be configurable.
  }
  elseif ($source_msg_id) {
    $comment = array();

    // a source message ID exists, so this is a comment via email.
    $result = db_fetch_object(db_query("SELECT msg_id, nid, cid FROM {casetracker_mail} WHERE msg_id = '%s'", $source_msg_id));
    $case = node_load($result->nid);
    $comment['nid'] = $result->nid;
    $comment['pid'] = $result->cid;
    $comment['comment'] = $node->body;
    $comment['uid'] = $node->uid;
    $comment['subject'] = $node->title;
    $comment['case_priority_id'] = $case->case_priority_id;
    $comment['case_type_id'] = $case->case_type_id;
    $comment['case_status_id'] = $case->case_status_id;
    $comment['case_title'] = $case->title;
    $comment['pid'] = $case->pid;
    $comment['assign_to'] = casetracker_get_name($case->assign_to);

    // @todo allow emailed comments to change these states.
    comment_save($comment);
  }
  else {
    return $node;
  }
}