You are here

function drupalchat_settings_form_validate in DrupalChat 7

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

@todo Please document this function.

See also

http://drupal.org/node/1354

File

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

Code

function drupalchat_settings_form_validate($form, &$form_state) {
  global $user;
  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),
        )));
      }
    }
  }
  if ($form_state['values']['drupalchat_polling_method'] == DRUPALCHAT_COMMERCIAL && !$form_state['values']['drupalchat_external_api_key']) {
    form_set_error('drupalchat_external_api_key', t('Please enter API key.'));
  }
  if ($form_state['values']['drupalchat_polling_method'] == DRUPALCHAT_COMMERCIAL && $form_state['values']['drupalchat_external_api_key']) {
    if ($form_state['values']['drupalchat_show_admin_list'] == 1) {
      $form_state['values']['drupalchat_enable_chatroom'] = 2;
    }
    $data = array(
      'api_key' => $form_state['values']['drupalchat_external_api_key'],
      'enable_chatroom' => $form_state['values']['drupalchat_enable_chatroom'],
      'theme' => $form_state['values']['drupalchat_theme'],
      'notify_sound' => $form_state['values']['drupalchat_notification_sound'],
      'smileys' => $form_state['values']['drupalchat_enable_smiley'],
      'log_chat' => $form_state['values']['drupalchat_log_messages'],
      'chat_topbar_color' => $form_state['values']['drupalchat_chat_topbar_color'],
      'chat_topbar_text_color' => $form_state['values']['drupalchat_chat_topbar_text_color'],
      'font_color' => $form_state['values']['drupalchat_font_color'],
      'chat_list_header' => $form_state['values']['drupalchat_chat_list_header'],
      'public_chatroom_header' => $form_state['values']['drupalchat_public_chatroom_header'],
      'rel' => $form_state['values']['drupalchat_rel'],
      'version' => '7.x-1.4',
      'show_admin_list' => $form_state['values']['drupalchat_show_admin_list'],
      'clear' => $form_state['values']['drupalchat_allow_single_message_delete'],
      'delmessage' => $form_state['values']['drupalchat_allow_clear_room_history'],
      'ufc' => $form_state['values']['drupalchat_allow_user_font_color'],
      'guest_prefix' => $form_state['values']['drupalchat_anon_prefix'] . " ",
      'enable_guest_change_name' => $form_state['values']['drupalchat_anon_change_name'],
      'use_stop_word_list' => $form_state['values']['drupalchat_use_stop_word_list'],
      'stop_word_list' => $form_state['values']['drupalchat_stop_word_list'],
      'file_attachment' => $form_state['values']['drupalchat_enable_file_attachment'],
      'mobile_browser_app' => $form_state['values']['drupalchat_enable_mobile_browser_app'],
      'mobile_sdk_integration' => $form_state['values']['drupalchat_enable_mobile_sdk_integration'],
      'enable_groups' => $form_state['values']['drupalchat_rel'] == 3 ? '1' : '2',
    );
    if ($form_state['values']['drupalchat_rel'] > DRUPALCHAT_REL_AUTH) {
      $new_valid_uids = _drupalchat_get_buddylist($user->uid, $form_state['values']['drupalchat_ur_name']);
      if (!isset($_SESSION['drupalchat_valid_uids']) || $_SESSION['drupalchat_valid_uids'] != $new_valid_uids) {
        $data['valid_uids'] = $new_valid_uids;
        $_SESSION['drupalchat_valid_uids'] = $new_valid_uids;
      }
      else {
        $data['valid_uids'] = $new_valid_uids;
      }
    }
    $data = json_encode($data);
    $options = array(
      'method' => 'POST',
      'data' => $data,
      'timeout' => 15,
      'headers' => array(
        'Content-Type' => 'application/json',
      ),
    );
    $result = drupal_http_request(DRUPALCHAT_EXTERNAL_A_HOST . ':' . DRUPALCHAT_EXTERNAL_A_PORT . '/z/', $options);
    if ($result->code == 200) {
      $result = drupal_json_decode($result->data);
    }
    else {
      form_set_error('drupalchat_external_api_key', "Unable to connect to iFlyChat server. Error code - " . $result->code . ". Error message - " . $result->error . ".");
    }
  }
}