You are here

function _support_node_move in Support Ticketing System 7

Same name and namespace in other branches
  1. 6 support.module \_support_node_move()
1 call to _support_node_move()
_support_node_insert_update in ./support.module
Common code for inserting or updating a support ticket

File

./support.module, line 1376
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_query('SELECT MAX(thread) FROM {comment} WHERE nid = :nid', array(
    ':nid' => $destination->nid,
  ))
    ->fetchField();

  // 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($node->uid);
  $comment = new stdClass();
  $comment->cid = NULL;
  $comment->pid = 0;
  $comment->uid = $node->uid;
  $comment->nid = $destination->nid;
  $comment->status = COMMENT_PUBLISHED;
  $comment->thread = $thread;
  $comment->hostname = ip_address();
  $comment->language = $node->language;
  $comment->name = isset($account->name) ? $account->name : '';
  $comment->mail = isset($account->mail) ? $account->mail : '';
  $comment->homepage = isset($account->homepage) ? $account->homepage : '';
  $comment->subject = $node->title;
  $comment->timestamp = $node->changed;
  $comment->comment_body = $node->body;
  $comment->message_id = isset($node->message_id) ? $node->message_id : NULL;
  $comment->state = $destination->state;
  $comment->priority = $destination->priority;
  $comment->client = $destination->client;
  $comment->assigned = $destination->assigned;

  // Preserve to re-use when transfering comments from this node.
  $notification = db_query('SELECT 1 FROM {support_assigned} WHERE nid = :nid AND uid = :uid', array(
    ':nid' => $destination->nid,
    ':uid' => $account->uid,
  ))
    ->fetchField();
  $comment->notification = $notification;

  // Transfer attachments.
  $field = variable_get('support_mail_upload_field', 'support_ticket_upload');
  if (isset($node->{$field})) {
    $comment->{$field} = $node->{$field};
  }
  comment_save($comment);
  $new_cid = $comment->cid;

  // Also move any comments from this node.
  $comments = db_select('comment', 'c')
    ->condition('c.nid', $node->nid)
    ->fields('c', array(
    'cid',
  ))
    ->execute();
  $cids = array();
  foreach ($comments as $comment) {
    $cids[] = $comment->cid;
  }
  if (!empty($cids)) {
    $comments = comment_load_multiple($cids);
    foreach ($comments as $comment) {
      $max = rtrim($thread, '/');
      $thread = int2vancode(vancode2int($max) + 1) . '/';
      $comment->thread = $thread;
      $comment->nid = $destination->nid;
      $comment->cid = NULL;
      $comment->pid = 0;
      $comment->state = $destination->state;
      $comment->priority = $destination->priority;
      $comment->client = $destination->client;
      $comment->assigned = $destination->assigned;
      $comment->notification = $notification;
      comment_save($comment);
    }
  }

  // Now 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/' . $destination->nid, array(
    'fragment' => 'comment-' . $new_cid,
  )));
  cache_clear_all();
  drupal_goto("node/{$destination->nid}", array(
    'fragment' => 'comment-' . $new_cid,
  ));
}