You are here

function _support_node_move in Support Ticketing System 6

Same name and namespace in other branches
  1. 7 support.module \_support_node_move()
1 call to _support_node_move()
support_nodeapi in ./support.module
Implementation of hook_nodeapi().

File

./support.module, line 1094
support.module

Code

function _support_node_move($node, $destination) {
  if (!user_access('move ticket') && user_access('administer support')) {
    drupal_set_message('Permission denied, unable to move ticket.');
  }

  // Move the ticket, making it an update on another ticket.
  $max = db_result(db_query('SELECT MAX(thread) FROM {comments} WHERE nid = %d', $destination->nid));

  // 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) . '/';
  $account = user_load(array(
    'uid' => $node->uid,
  ));
  db_query("INSERT INTO {comments} (nid, pid, uid, subject, comment, format, hostname, timestamp, status, thread, name, mail, homepage) VALUES (%d, %d, %d, '%s', '%s', %d, '%s', %d, %d, '%s', '%s', '%s', '%s')", $destination->nid, 0, $node->uid, $node->title, $node->body, 1, ip_address(), $node->changed, 0, $thread, $account->name, $account->mail, $account->homepage);

  // Tell the other modules a new comment has been submitted.
  $comment = array();
  $comment['cid'] = db_last_insert_id('comments', 'cid');
  $comment['subject'] = $node->title;
  $comment['comment'] = $node->body;
  $comment['nid'] = $destination->nid;
  $comment['uid'] = $account->uid;
  $comment['message_id'] = db_result(db_query('SELECT message_id FROM {support_ticket} WHERE nid = %d', $node->nid));
  $comment['support_email'] = TRUE;
  $comment['notification'] = db_result(db_query('SELECT 1 FROM {support_assigned} WHERE nid = %d AND uid = %d', $destination->nid, $account->uid));

  // Preserve the existing values for the support ticket status.
  $comment['state'] = $destination->state;
  $comment['priority'] = $destination->priority;
  $comment['client'] = $destination->client;
  $comment['assigned'] = $destination->assigned;
  comment_invoke_comment($comment, 'insert');

  // transfer attachments
  if (db_table_exists('upload')) {
    if (module_exists('comment_upload')) {

      // transfer attachments to new comment
      $result = db_query('SELECT fid, description, list, weight FROM {upload} WHERE nid = %d', $node->nid);
      while ($upload = db_fetch_object($result)) {
        db_query("INSERT INTO {comment_upload} (fid, nid, cid, description, list, weight) VALUES(%d, %d, %d, '%s', %d, %d)", $upload->fid, $destination->nid, $comment['cid'], $upload->description, $upload->list, $upload->weight);
      }
      db_query('DELETE FROM {upload} WHERE nid = %d', $node->nid);
    }
    else {

      // transfer attachments to new node
      db_query('UPDATE {upload} SET nid = %d WHERE nid = %d', $destination->nid, $node->nid);
    }
  }

  // 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'],
  )));
  $result = db_query('SELECT cid, uid, subject, comment, format, hostname, timestamp, timestamp, status, thread, name, mail, homepage FROM {comments} WHERE nid = %d', $node->nid);
  while ($update = db_fetch_array($result)) {
    $oldcid = $update['cid'];
    $max = rtrim($thread, '/');
    $thread = int2vancode(vancode2int($max) + 1) . '/';
    db_query("INSERT INTO {comments} (nid, pid, uid, subject, comment, format, hostname, timestamp, status, thread, name, mail, homepage) VALUES (%d, %d, %d, '%s', '%s', %d, '%s', %d, %d, '%s', '%s', '%s', '%s')", $destination->nid, 0, $update['uid'], $update['subject'], $update['comment'], $update['format'], $update['hostname'], $update['timestamp'], $update['status'], $thread, $update['name'], $update['mail'], $update['homepage']);

    // Tell the other modules a new comment has been submitted.
    $update['cid'] = db_last_insert_id('comments', 'cid');
    $update['nid'] = $destination->nid;
    $comment['message_id'] = db_query('SELECT message_id FROM {support_ticket_comment} WHERE cid = %d', $oldcid);
    $update['support_email'] = TRUE;
    $update['notification'] = $comment['notification'];

    // Preserve the existing values for the support ticket status.
    $update['state'] = $destination->state;
    $update['priority'] = $destination->priority;
    $update['client'] = $destination->client;
    $update['assigned'] = $destination->assigned;
    comment_invoke_comment($update, 'insert');

    // transfer attachments to new comment
    if (module_exists('comment_upload')) {
      db_query('UPDATE {comment_upload} SET nid = %d, cid = %d WHERE nid = %d AND cid = %d', $destination->nid, $update['cid'], $node->nid, $oldcid);
    }
  }
  _comment_update_node_statistics($comment['nid']);

  // remove the original node
  node_delete($node->nid);
  drupal_set_message(t('Successfully moved support ticket.'));
  watchdog('content', 'support_ticket: moved ticket %title from node/%old to node/%new.', array(
    '%title' => $node->title,
    '%old' => $node->nid,
    '%new' => $destination->nid,
  ), WATCHDOG_NOTICE, l(t('view'), 'node/' . $node->nid, array(
    'fragment' => 'comment-' . $comment['cid'],
  )));
  cache_clear_all();
  drupal_goto("node/{$destination->nid}", NULL, 'comment-' . $comment['cid']);
}