You are here

function support_node_validate in Support Ticketing System 7

Implementation of hook_node_validate().

File

./support.module, line 919
support.module

Code

function support_node_validate($node, $form, &$form_state) {
  if ($node->type == 'support_ticket') {
    $autocomplete = 'subscribed-users';
    $client = db_query('SELECT name FROM {support_client} WHERE clid = :clid', array(
      ':clid' => $node->client,
    ))
      ->fetchField();
    if (!isset($node->client) || $node->client == 0) {
      form_set_error('client', t('You must select a client'));
    }

    // Autocomplete version
    if (isset($node->assigned) && !is_numeric($node->assigned)) {
      $assigned = db_query("SELECT uid FROM {users} WHERE name = :name", array(
        ':name' => $node->assigned,
      ))
        ->fetchField();
      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.'));
          }
        }
      }
    }

    // Dropdown version. (Still need to validate in case the ticket moved between clients.)
    if (isset($node->assigned) && is_numeric($node->assigned) && $node->assigned != 0) {
      if (!_support_validate_assigned_user($node->assigned, $client)) {
        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($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($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) {
          $accounts = user_load_multiple(array(), array(
            'name' => trim($notify),
          ));
          $account = array_shift($accounts);
          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,
            )));
          }
        }
      }
    }
  }
}