You are here

function support_client_form_validate in Support Ticketing System 7

File

./support.admin.inc, line 234
support.admin.inc

Code

function support_client_form_validate($form, &$form_state) {
  $client = $form_state['support_client'];
  if (preg_match('/[&*(),<>?\\[\\]]/', $form_state['values']['name'])) {
    form_set_error('name', t('You have entered an unsupported character.  The following characters can not be used in the client name: &*(),<>?[]'));
  }
  elseif (!isset($form_state['values']['clid'])) {
    $result = db_query('SELECT 1 FROM {support_client} WHERE name = :value', array(
      ':value' => $form_state['values']['name'],
    ));
    foreach ($result as $exists) {
      form_set_error('name', t('The client name %name is already in use. It must be unique.', array(
        '%name' => $form_state['values']['name'],
      )));
    }
  }
  if (!empty($form_state['values']['autosubscribe'])) {
    $subscribe = array();
    $autosubscribe = explode(',', $form_state['values']['autosubscribe']);
    foreach ($autosubscribe as $name) {
      $accounts = user_load_multiple(array(), array(
        'name' => trim($name),
      ));
      $account = array_shift($accounts);
      if (!is_object($account) || !$account->uid) {
        form_set_error('autosubscribe', t('User %username does not exist.', array(
          '%username' => $name,
        )));
      }
      else {
        if (!user_access('access ' . $form_state['values']['name'] . ' tickets', $account) && !user_access('administer support')) {
          form_set_error('autosubscribe', t('User %username does not have permission to access %client tickets.', array(
            '%username' => $name,
            '%client' => $form_state['values']['name'],
          )));
        }
      }
    }
  }
  if (!empty($form_state['values']['autoassign'])) {
    $autoassign = $form_state['values']['autoassign'];
    switch ($autoassign) {
      case '<nobody>':
      case '<creator>':
        break;
      default:
        $accounts = user_load_multiple(array(), array(
          'name' => trim($form_state['values']['autoassign']),
        ));
        $account = array_shift($accounts);
        $name = $form_state['values']['name'];
        if (empty($account)) {
          form_set_error('autoassign', t('Invalid %field user, %username does not exist.', array(
            '%field' => t('auto-assign'),
            '%username' => trim($form_state['values']['autoassign']),
          )));
        }
        else {
          if (!user_access('administer support', $account) && !user_access("access {$name} tickets", $account)) {
            form_set_error('autoassign', t('Invalid %field  user, %username does not have permission to access %client tickets.', array(
              '%field' => t('auto-assign'),
              '%username' => trim($form_state['values']['autoassign']),
              '%client' => $name,
            )));
          }
        }
        break;
    }
  }
  if (!empty($form_state['values']['domains'])) {
    _support_validate_domains('domains', explode(',', $form_state['values']['domains']));
  }
  if ($form_state['values']['integrate_email'] && !function_exists('imap_open')) {
    form_set_error('integrate_email', t('PHP must be !compiled in order to enable inbound email integration.  The IMAP functions are required to support the IMAP, POP3, and local mailbox access methods.', array(
      '!compiled' => l(t('compiled with the IMAP extension'), 'http://php.net/imap'),
    )));
  }
  field_attach_form_validate('support_client', $client, $form, $form_state);
}