You are here

function _support_node_insert_update in Support Ticketing System 7

Common code for inserting or updating a support ticket

2 calls to _support_node_insert_update()
support_node_insert in ./support.module
Implementation of hook_node_insert().
support_node_update in ./support.module
Implementation of hook_node_update().

File

./support.module, line 979
support.module

Code

function _support_node_insert_update($node) {
  $autocomplete = 'subscribed-users';
  if (isset($node->move) && is_numeric($node->move) && $node->move) {
    $destination = node_load($node->move);
    _support_node_move($node, $destination);
  }
  if (isset($node->assigned) && !is_numeric($node->assigned)) {
    $assigned = db_query("SELECT uid FROM {users} WHERE name = :name", array(
      ':name' => $node->assigned,
    ))
      ->fetchField();
    if ($assigned) {
      $node->assigned = $assigned;
    }
    else {
      $node->assigned = 0;
    }
  }
  db_merge('support_ticket')
    ->key(array(
    'nid' => $node->nid,
  ))
    ->fields(array(
    'message_id' => isset($node->message_id) ? $node->message_id : '',
    'state' => $node->state,
    'priority' => $node->priority,
    'client' => $node->client,
    'assigned' => $node->assigned,
  ))
    ->execute();
  if (isset($node->notifications) && !empty($node->notifications)) {
    $notifications = explode(',', $node->notifications);
    foreach ($notifications as $notify) {
      $enabled = "notify-{$notify}";
      support_subscribe_user($node->nid, $notify, $node->{$enabled});
    }
  }
  else {
    if (isset($node->{$autocomplete})) {
      $notifications = explode(',', $node->{$autocomplete});
      foreach ($notifications as $notify) {
        $accounts = user_load_multiple(array(), array(
          'name' => trim($notify),
        ));
        $account = array_shift($accounts);
        if (!empty($account)) {
          support_subscribe_user($node->nid, $account->uid);
        }
      }
    }
  }
}