You are here

function drupalchat_settings_form_validate in DrupalChat 7.2

Same name and namespace in other branches
  1. 6.2 drupalchat.admin.inc \drupalchat_settings_form_validate()
  2. 7 drupalchat.admin.inc \drupalchat_settings_form_validate()

@todo Please document this function.

See also

http://drupal.org/node/1354

File

./drupalchat.admin.inc, line 272
Administrative functions to configure DrupalChat.

Code

function drupalchat_settings_form_validate($form, &$form_state) {
  global $user;
  if ($form_state['values']['drupalchat_polling_method'] == DRUPALCHAT_COMMERCIAL) {
    $form_state['values']['drupalchat_app_id'] = trim($form_state['values']['drupalchat_app_id']);
    $form_state['values']['drupalchat_external_api_key'] = trim($form_state['values']['drupalchat_external_api_key']);
    $formValues = array(
      'api_key' => $form_state['values']['drupalchat_external_api_key'],
      'app_id' => $form_state['values']['drupalchat_app_id'],
    );
    $result = _drupalchat_get_auth($formValues);
    if (gettype($result) == 'object') {
      form_set_error('drupalchat_external_api_key', "Unable to connect to iFlyChat server. Error code - " . $result->code . ". Error message - " . $result->error . ".");
    }
    $app_id = $form_state['values']['drupalchat_app_id'];
    if (!(strlen($app_id) == 36 && $app_id[14] == '4')) {
      form_set_error('drupalchat_app_id', "Invalid APP ID.");
    }
    if (!$form_state['values']['drupalchat_app_id']) {
      form_set_error('drupalchat_app_id', t('Please enter APP ID.'));
    }
    if (!$form_state['values']['drupalchat_external_api_key']) {
      form_set_error('drupalchat_external_api_key', t('Please enter API key.'));
    }
  }
  if ($form_state['values']['drupalchat_rel'] == DRUPALCHAT_REL_UR) {

    /*if (!$form['drupalchat_ur_name']['#value']) {
      form_set_error('drupalchat_ur_name', t('You must provide the user relationship name in the Drupal UR Settings section.));
       }*/
    if ($form_state['values']['drupalchat_ur_name']) {
      $array = drupal_explode_tags($form_state['values']['drupalchat_ur_name']);
      $error = array();
      foreach ($array as $key) {
        if (!db_query("SELECT COUNT(*) FROM {user_relationship_types} WHERE name = :name", array(
          ':name' => $key,
        ))
          ->fetchField()) {
          $error[] = $key;
        }
      }
      if (!empty($error)) {
        form_set_error('drupalchat_ur_name', t('User Relationship type %type was not found.', array(
          '%type' => drupal_implode_tags($error),
        )));
      }
    }
  }
}