You are here

function support_nodeapi in Support Ticketing System 6

Implementation of hook_nodeapi().

File

./support.module, line 781
support.module

Code

function support_nodeapi(&$node, $op, $teaser, $page) {
  global $user;
  if ($node->type == 'support_ticket') {
    $autocomplete = 'subscribed-users';
    switch ($op) {
      case 'view':

        // viewing a ticket
        drupal_add_css(drupal_get_path('module', 'support') . '/support-tickets.css');
        $breadcrumb = array();
        $breadcrumb[] = l(t('Home'), NULL);
        $breadcrumb[] = l(t('Support tickets'), 'support');
        if (is_numeric($node->client)) {
          $_SESSION['support_client'] = $node->client;
          $client = support_client_load($node->client);
          if (!empty($client->parent)) {
            $parent = support_client_load($client->parent);
            $breadcrumb[] = l(check_plain($parent->name), "support/{$parent->path}");
            $breadcrumb[] = l(check_plain($client->name), "support/{$parent->path}/{$client->path}");
          }
          else {
            $breadcrumb[] = l(check_plain($client->name), "support/{$client->path}");
          }
        }
        drupal_set_breadcrumb($breadcrumb);
        break;
      case 'load':
        return db_fetch_array(db_query('SELECT message_id, state, priority, client, assigned FROM {support_ticket} WHERE nid = %d', $node->nid));
      case 'validate':
        $client = db_result(db_query('SELECT name FROM {support_client} WHERE clid = %d', $node->client));
        if (!isset($node->client) || $node->client == 0) {
          form_set_error('client', t('You must select a client'));
        }
        if (isset($node->assigned) && !is_numeric($node->assigned)) {
          $assigned = db_result(db_query("SELECT uid FROM {users} WHERE name = '%s'", $node->assigned));
          if ($node->assigned && !$assigned) {
            form_set_error('assigned', t('You must specify a valid user.'));
          }
          else {
            if ($assigned) {
              $valid = _support_validate_assigned_user($assigned, $client);
              if (!$valid) {
                form_set_error('assigned', t('You must specify a user that has permission to view this ticket.'));
              }
            }
          }
        }
        if (isset($node->move) && is_numeric($node->move) && $node->move) {
          $destination = node_load(array(
            'nid' => $node->move,
          ));
          if (!is_object($destination) || !$destination->nid) {
            form_set_error('move', t('Destination node does not exist.'));
          }
        }

        // check for users subscribed during ticket creation (checkboxes)
        if (isset($node->notifications) && !empty($node->notifications)) {
          $notifications = explode(',', $node->notifications);
          foreach ($notifications as $notify) {
            $valid = _support_validate_assigned_user($notify, $client);
            if (!$valid) {
              $account = user_load(array(
                'uid' => $notify,
              ));
              form_set_error("notify-{$notify}", t('Unable to subscribe user, %user does not have permission to view this ticket.', array(
                '%user' => $account->name,
              )));
            }
          }
        }
        else {
          if (!empty($node->{$autocomplete})) {
            $notifications = explode(',', $node->{$autocomplete});
            foreach ($notifications as $notify) {
              $account = user_load(array(
                'name' => trim($notify),
              ));
              if (empty($account) || !user_access("access {$client} tickets", $account) && !user_access('administer support', $account)) {
                form_set_error('subscribed-users', t('Unable to subscribe user, %user does not have permission to view this ticket.', array(
                  '%user' => $notify,
                )));
              }
            }
          }
        }
        break;
      case 'insert':
      case 'update':
        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_result(db_query("SELECT uid FROM {users} WHERE name = '%s'", $node->assigned));
          if ($assigned) {
            $node->assigned = $assigned;
          }
          else {
            $node->assigned = 0;
          }
        }
        db_query("UPDATE {support_ticket} SET message_id = '%s', state = %d, priority = %d, client = %d, assigned = %d WHERE nid = %d", isset($node->message_id) ? $node->message_id : '', $node->state, $node->priority, $node->client, $node->assigned, $node->nid);
        if (!db_affected_rows()) {
          @db_query("INSERT INTO {support_ticket} (nid, message_id, state, priority, client, assigned) VALUES(%d, '%s', %d, %d, %d, %d)", $node->nid, isset($node->message_id) ? $node->message_id : '', $node->state, $node->priority, $node->client, $node->assigned);
        }
        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) {
              $account = user_load(array(
                'name' => trim($notify),
              ));
              if (!empty($account)) {
                support_subscribe_user($node->nid, $account->uid);
              }
            }
          }
        }
        if ($op == 'insert') {

          // auto-subscribe ticket creator
          if (variable_get('support_autosubscribe_creator', FALSE) || isset($node->created_by_email)) {
            support_subscribe_user($node->nid, $node->uid);
          }
          else {
            support_subscribe_user($node->nid, $node->uid, $node->notification);
          }

          // auto-subscribe assigned user
          if ($node->assigned || isset($node->created_by_email) || !user_access('can subscribe other users to notifications')) {
            support_subscribe_user($node->nid, $node->assigned);
          }

          // auto-subscribe configured users
          if (variable_get('support_autosubscribe_force', FALSE) || isset($node->created_by_email) || !user_access('can subscribe other users to notifications')) {
            _support_autosubscribe($node->nid, $node->client);
          }

          // generate notification emails
          support_notification(array(), $node->nid, 'ticket_new', isset($node->suppress) ? $node->suppress : FALSE);
        }
        cache_clear_all();
        break;
      case 'delete':
        db_query('DELETE FROM {support_ticket} WHERE nid = %d', $node->nid);
        break;
    }
  }
}