You are here

function casetracker_nodeapi in Case Tracker 6

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

Implementation of hook_nodeapi().

File

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

Code

function casetracker_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {

  // CASES
  if (casetracker_is_case($node->type)) {
    switch ($op) {
      case 'delete':

        // delete case and its comments.
        $comment_results = db_query("SELECT cid FROM {comments} WHERE nid = %d", $node->nid);
        while ($comment_result = db_fetch_object($comment_results)) {
          db_query("DELETE FROM {casetracker_comment_status} WHERE cid = %d", $comment_result->cid);
        }
        db_query('DELETE FROM {casetracker_case} WHERE nid = %d', $node->nid);
        break;
      case 'presave':

        // $node->casetracker is an Array and we wanna it to be an object.
        $node->casetracker = (object) $node->casetracker;
        break;
      case 'insert':

        // cases: generate a case ID and send it along.
        $record = $node->casetracker;
        $record->assign_to = is_numeric($record->assign_to) ? $record->assign_to : casetracker_get_uid($record->assign_to);
        $record->nid = $node->nid;
        $record->vid = $node->vid;
        drupal_write_record('casetracker_case', $record);
        break;
      case 'load':
        $casetracker = db_fetch_object(db_query('SELECT pid, case_priority_id, case_type_id, assign_to, case_status_id FROM {casetracker_case} WHERE nid = %d AND vid = %d', $node->nid, $node->vid));
        if ($casetracker) {
          return array(
            'casetracker' => $casetracker,
          );
        }
        break;
      case 'update':
        $record = (object) $node->casetracker;
        $record->assign_to = is_numeric($record->assign_to) ? $record->assign_to : casetracker_get_uid($record->assign_to);
        $record->nid = $node->nid;
        $record->vid = $node->vid;
        $primary = $node->revision ? array(
          'nid',
        ) : array(
          'nid',
          'vid',
        );
        drupal_write_record('casetracker_case', $record, $primary);
        break;
      case 'view':

        // On preview the case will be an array, we want an object.
        if (isset($node->build_mode) && $node->build_mode == NODE_BUILD_PREVIEW) {
          $node->casetracker = (object) $node->casetracker;
        }

        // used in the breadcrumb and our theme function, mostly for nid and project number display.
        $project = node_load($node->casetracker->pid);
        if ($page) {
          $trail = array(
            l(t('Home'), NULL),
            l(t('Case Tracker'), 'casetracker/projects'),
            l($project->title, "node/{$node->casetracker->pid}"),
            l(t('All cases'), "casetracker/cases/{$node->casetracker->pid}/all"),
          );
          drupal_set_breadcrumb($trail);
        }
        $node->content['casetracker_case_summary'] = array(
          '#value' => theme('casetracker_case_summary', $node, $project),
          '#weight' => -10,
        );
        break;
    }
  }
  else {
    if (casetracker_is_project($node->type)) {
      switch ($op) {
        case 'delete':

          // projects: delete all the cases under the project and all the comments under each case.
          $case_results = db_query("SELECT nid from {casetracker_case} WHERE pid = %d", $node->nid);
          while ($case_result = db_fetch_object($case_results)) {
            db_query("DELETE FROM {casetracker_case} WHERE nid = %d", $case_result->nid);
            $comment_results = db_query("SELECT cid FROM {comments} WHERE nid = %d", $case_result->nid);
            while ($comment_result = db_fetch_object($comment_results)) {
              db_query("DELETE FROM {casetracker_comment_status} WHERE cid = %d", $comment_result->cid);
            }
            node_delete($case_result->nid);

            // this'll handle comment deletion too.
          }
          break;
        case 'view':
          if ($page) {
            $trail = array(
              l(t('Home'), NULL),
              l(t('Case Tracker'), 'casetracker/projects'),
            );
            drupal_set_breadcrumb($trail);
          }
          $node->content['casetracker_project_summary'] = array(
            '#value' => theme('casetracker_project_summary', $node),
            '#weight' => -10,
          );
          break;
      }
    }
  }
}