You are here

function casetracker_comment in Case Tracker 5

Same name and namespace in other branches
  1. 6 casetracker.module \casetracker_comment()

Implementation of hook_comment().

File

./casetracker.module, line 1205
Enables the handling of projects and their cases.

Code

function casetracker_comment(&$comment, $op) {
  $case_data = array();

  // stores old and new values for comparison.
  $case_fields = array(
    'case_priority_id',
    'case_type_id',
    'case_status_id',
    'assign_to',
  );
  if ($op == 'insert' || $op == 'update') {
    $node = node_load($comment['nid']);

    // only care about nodes on insert and update.
    if (!in_array($node->type, variable_get('casetracker_case_node_types', array(
      'casetracker_basic_case',
    )), TRUE)) {
      return;

      // if this isn't a casetracker case node type, return without sullying our beautiful code. BEAUTY!
    }

    // this will also short circuit the other insert/updates in our switch below.
    // note: we're using 'prid' here for our project ID because the comment forms
    // already use 'pid' to represent the parent comment of a reply. be friendly!
    $case_data['old']->prid = $node->pid;
    $case_data['new']->prid = $comment['prid'];
    foreach ($case_fields as $case_field) {
      $case_data['old']->{$case_field} = $node->{$case_field};
      $case_data['new']->{$case_field} = $comment[$case_field];
      if ($case_field == 'assign_to') {
        $case_data['new']->assign_to = casetracker_get_uid($comment['assign_to']);
      }
    }
    $case_data['old']->case_title = $node->title;
    $case_data['new']->case_title = $comment['case_title'];
    db_query("UPDATE {node} SET title = '%s' WHERE nid = %d AND vid = %d", $case_data['new']->case_title, $comment['nid'], $comment['revision_id']);
    db_query("UPDATE {node_revisions} SET title = '%s' WHERE nid = %d AND vid = %d", $case_data['new']->case_title, $comment['nid'], $comment['revision_id']);
    db_query("UPDATE {casetracker_case} SET assign_to = %d, case_status_id = %d, case_priority_id = %d, case_type_id = %d, pid = %d WHERE nid = %d AND vid = %d ", $case_data['new']->assign_to, $case_data['new']->case_status_id, $case_data['new']->case_priority_id, $case_data['new']->case_type_id, $case_data['new']->prid, $comment['nid'], $comment['revision_id']);
  }
  switch ($op) {
    case 'insert':
      db_query("INSERT INTO {casetracker_comment_status} (cid, pid, assign_to, case_priority_id, case_type_id, case_status_id, state, title) VALUES (%d, %d, '%d', %d, %d, '%d', %d, '%s')", $comment['cid'], $case_data['old']->prid, $case_data['old']->assign_to, $case_data['old']->case_priority_id, $case_data['old']->case_type_id, $case_data['old']->case_status_id, 0, $case_data['old']->case_title);
      db_query("INSERT INTO {casetracker_comment_status} (cid, pid, assign_to, case_priority_id, case_type_id, case_status_id, state, title) VALUES (%d, %d, '%d', %d, %d, '%d', %d, '%s')", $comment['cid'], $case_data['new']->prid, $case_data['new']->assign_to, $case_data['new']->case_priority_id, $case_data['new']->case_type_id, $case_data['new']->case_status_id, 1, $case_data['new']->case_title);
      break;
    case 'update':
      db_query("UPDATE {casetracker_comment_status} SET pid = %d, assign_to = %d, case_priority_id = %d, case_type_id = %d, case_status_id = %d, title = '%s' WHERE cid = %d AND state = %d", $case_data['old']->prid, $case_data['old']->assign_to, $case_data['old']->case_priority_id, $case_data['old']->case_type_id, $case_data['old']->case_status_id, $case_data['old']->case_title, $comment['cid'], 0);
      db_query("UPDATE {casetracker_comment_status} SET pid = %d, assign_to = %d, case_priority_id = %d, case_type_id = %d, case_status_id = %d, title = '%s' WHERE cid = %d AND state = %d", $case_data['new']->prid, $case_data['new']->assign_to, $case_data['new']->case_priority_id, $case_data['new']->case_type_id, $case_data['new']->case_status_id, $case_data['new']->case_title, $comment['cid'], 1);
      break;
    case 'delete':

      // @todo theoretically, if you delete a comment, we should reset all the values
      // to what they were before the comment was submitted. this doesn't happen yet.
      db_query("DELETE FROM {casetracker_comment_status} WHERE cid = %d", $comment->cid);
      break;
    case 'view':
      $results = db_query("SELECT * FROM {casetracker_comment_status} WHERE cid = %d", $comment->cid);

      // hoo-hah. HOO-HAHAHAH!
      while ($result = db_fetch_object($results)) {
        $state = $result->state ? 'new' : 'old';
        $case_data[$state] = $result;
      }
      $comment->comment = casetracker_comment_changes($case_data) . $comment->comment;
      break;
  }
}