You are here

function casetracker_comment in Case Tracker 6

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

Implementation of hook_comment().

File

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

Code

function casetracker_comment(&$comment, $op) {

  // Load the node here anyway -- it is almost certainly static cached already.
  $node = is_array($comment) ? node_load($comment['nid']) : node_load($comment->nid);

  // Bail if this is not a casetracker node.
  if (!casetracker_is_case($node->type)) {
    return;
  }
  if ($op == 'insert' || $op == 'update') {
    $new = (object) $comment['casetracker'];
    $new->cid = $comment['cid'];
    $new->nid = $comment['nid'];
    $new->vid = $comment['revision_id'];
    $new->state = 1;
    $new->assign_to = casetracker_get_uid($new->assign_to);

    // Populate old state values from node
    $old = $node->casetracker;
    $old->cid = $comment['cid'];
    $old->state = 0;
    drupal_write_record('casetracker_case', $new, array(
      'nid',
      'vid',
    ));
  }
  switch ($op) {
    case 'insert':
      drupal_write_record('casetracker_comment_status', $old);
      drupal_write_record('casetracker_comment_status', $new);
      break;
    case 'update':
      drupal_write_record('casetracker_comment_status', $old, array(
        'cid',
        'state',
      ));
      drupal_write_record('casetracker_comment_status', $new, array(
        'cid',
        'state',
      ));
      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':

      // If this is a preview we won't have a cid yet.
      if (empty($comment->cid)) {
        $case_data['new'] = (object) $comment->casetracker;
        $case_data['new']->assign_to = casetracker_get_uid($case_data['new']->assign_to);
        $case = node_load($comment->nid);
        $case_data['old'] = drupal_clone($case->casetracker);
      }
      else {
        $results = db_query("SELECT * FROM {casetracker_comment_status} WHERE cid = %d", $comment->cid);
        while ($result = db_fetch_object($results)) {
          $state = $result->state ? 'new' : 'old';
          $case_data[$state] = $result;
        }
      }
      $comment->comment = theme('casetracker_comment_changes', $case_data['old'], $case_data['new']) . $comment->comment;
      break;
  }
}