You are here

function drupalchat_init in DrupalChat 7

Same name and namespace in other branches
  1. 6.2 drupalchat.module \drupalchat_init()
  2. 6 drupalchat.module \drupalchat_init()
  3. 7.2 drupalchat.module \drupalchat_init()

@todo Please document this function.

See also

http://drupal.org/node/1354

File

./drupalchat.module, line 440
Module code for DrupalChat.

Code

function drupalchat_init() {
  if (drupalchat_verify_access()) {
    global $user, $base_url;
    if (variable_get('drupalchat_polling_method', DRUPALCHAT_AJAX) != DRUPALCHAT_COMMERCIAL) {
      if ($user->uid > 0) {
        $account = user_load($user->uid);
        $user_name = check_plain(format_username($account));
        $status = db_query('SELECT status FROM {drupalchat_users} WHERE uid = :uid', array(
          ':uid' => $user->uid,
        ))
          ->fetchField();
        if (!$status) {
          $status = DRUPALCHAT_USER_ONLINE;
          $current_user = (object) array(
            'uid' => $user->uid,
            'session' => $user->sid,
            'name' => $user_name,
            'status' => $status,
            'timestamp' => time(),
          );
          drupal_write_record('drupalchat_users', $current_user);
        }
        else {
          db_update('drupalchat_users')
            ->fields(array(
            'timestamp' => time(),
            'status' => $status == DRUPALCHAT_USER_OFFLINE ? DRUPALCHAT_USER_ONLINE : $status,
          ))
            ->condition('uid', $user->uid)
            ->execute();
        }
      }
      else {
        $sid = _drupalchat_get_sid();
        if (!$sid || $sid == -1) {
          return;
        }
        else {
          $user_name = check_plain(variable_get('drupalchat_anon_prefix', 'Guest') . time());
          $status = db_query('SELECT status FROM {drupalchat_users} WHERE uid = :uid AND session = :sid', array(
            ':uid' => $user->uid,
            ':sid' => $sid,
          ))
            ->fetchField();
          if (!$status) {
            $status = DRUPALCHAT_USER_ONLINE;
            if (variable_get('drupalchat_anon_use_name', 1) == 1) {
              $user_name = variable_get('drupalchat_anon_prefix', 'Guest') . ' ' . drupalchat_get_random_name();
            }
            $current_user = (object) array(
              'uid' => $user->uid,
              'session' => $sid,
              'name' => $user_name,
              'status' => $status,
              'timestamp' => time(),
            );
            drupal_write_record('drupalchat_users', $current_user);
          }
          else {
            db_update('drupalchat_users')
              ->fields(array(
              'timestamp' => time(),
              'status' => $status == DRUPALCHAT_USER_OFFLINE ? DRUPALCHAT_USER_ONLINE : $status,
            ))
              ->condition('uid', $user->uid)
              ->condition('session', $sid)
              ->execute();
          }
        }
      }
    }
    else {
      if ($user->uid) {
        $account = user_load($user->uid);
        $user_name = check_plain(format_username($account));
      }
      else {
        $a_name = drupalchat_get_current_guest_name();
      }
      $status = DRUPALCHAT_USER_ONLINE;
    }
    $theme = variable_get('drupalchat_theme', 'light');
    $polling_method = variable_get('drupalchat_polling_method', DRUPALCHAT_AJAX);
    if (!$user->uid && variable_get('drupalchat_polling_method', DRUPALCHAT_AJAX) != DRUPALCHAT_COMMERCIAL) {
      $a_name = db_query('SELECT name FROM {drupalchat_users} WHERE uid = :uid AND session = :sid', array(
        ':uid' => $user->uid,
        ':sid' => $sid,
      ))
        ->fetchField();
    }
    $my_settings = array(
      'current_timestamp' => time(),
      'polling_method' => $polling_method,
      'pollUrl' => url('drupalchat/poll', array(
        'absolute' => TRUE,
      )),
      'sendUrl' => url('drupalchat/send', array(
        'absolute' => TRUE,
      )),
      'statusUrl' => url('drupalchat/status', array(
        'absolute' => TRUE,
      )),
      'status' => $status,
      'goOnline' => t('Go Online'),
      'goIdle' => t('Go Idle'),
      'newMessage' => t('New chat message!'),
      'images' => $base_url . '/' . drupal_get_path('module', 'drupalchat') . '/themes/' . $theme . '/images/',
      'sound' => base_path() . drupal_get_path('module', 'drupalchat') . '/swf/sound.swf',
      'soundFile' => base_path() . drupal_get_path('module', 'drupalchat') . '/wav/notification.mp3',
      'noUsers' => theme('item_list', array(
        'items' => array(
          0 => array(
            'data' => t('No users online'),
            'class' => array(
              'drupalchatnousers',
            ),
          ),
        ),
      )),
      'smileyURL' => base_path() . drupal_get_path('module', 'drupalchat') . '/smileys/very_emotional_emoticons-png/png-32x32/',
      'addUrl' => url('drupalchat/channel/add', array(
        'absolute' => TRUE,
      )),
      'notificationSound' => variable_get('drupalchat_notification_sound', 1),
      'exurl' => url('drupalchat/auth', array(
        'query' => array(
          't' => time(),
        ),
      )),
      'soffurl' => url('drupalchat/send-offline-message'),
      'chat_type' => variable_get('drupalchat_show_admin_list', 2),
      'guestPrefix' => variable_get('drupalchat_anon_prefix', 'Guest') . " ",
      'changeurl' => url('drupalchat/change-guest-name', array(
        'query' => array(
          't' => time(),
        ),
      )),
      'allowSmileys' => variable_get('drupalchat_enable_smiley', 1),
    );
    if ($polling_method == DRUPALCHAT_AJAX) {
      $my_settings['refresh_rate'] = variable_get('drupalchat_refresh_rate', 2);
    }
    if ($polling_method != DRUPALCHAT_COMMERCIAL) {
      $my_settings['username'] = $user->uid ? $user_name : $a_name;
      $my_settings['uid'] = $user->uid ? $user->uid : '0-' . _drupalchat_get_sid();
      $my_settings['threadHistoryUrl'] = url('drupalchat/thread-history', array(
        'absolute' => TRUE,
      ));
    }
    $my_settings['iup'] = variable_get('drupalchat_user_picture', 1);
    if (variable_get('drupalchat_user_picture', 1) == 1) {
      if ($polling_method != DRUPALCHAT_COMMERCIAL) {
        $my_settings['up'] = drupalchat_return_pic_url();
      }
      $my_settings['default_up'] = $base_url . '/' . drupal_get_path('module', 'drupalchat') . '/themes/' . $theme . '/images/default_avatar.png';
      $my_settings['default_cr'] = $base_url . '/' . drupal_get_path('module', 'drupalchat') . '/themes/' . $theme . '/images/default_room.png';
      $my_settings['default_team'] = $base_url . '/' . drupal_get_path('module', 'drupalchat') . '/themes/' . $theme . '/images/default_team.png';
    }
    if ($polling_method == DRUPALCHAT_COMMERCIAL) {
      global $is_https;
      if ($is_https) {
        $my_settings['external_host'] = DRUPALCHAT_EXTERNAL_A_HOST;
        $my_settings['external_port'] = DRUPALCHAT_EXTERNAL_A_PORT;
        $my_settings['external_a_host'] = DRUPALCHAT_EXTERNAL_A_HOST;
        $my_settings['external_a_port'] = DRUPALCHAT_EXTERNAL_A_PORT;
      }
      else {
        $my_settings['external_host'] = DRUPALCHAT_EXTERNAL_HOST;
        $my_settings['external_port'] = DRUPALCHAT_EXTERNAL_PORT;
        $my_settings['external_a_host'] = DRUPALCHAT_EXTERNAL_HOST;
        $my_settings['external_a_port'] = DRUPALCHAT_EXTERNAL_PORT;
      }
      $my_settings['cdnDomainName'] = DRUPALCHAT_EXTERNAL_CDN_HOST;

      /*$my_settings['session_key'] = _drupalchat_get_auth((($user->uid)?$user_name:$a_name));
        if(empty($my_settings['session_key']))
          return;*/
      $my_settings['text_currently_offline'] = t('!user is currently offline.', array(
        '!user' => 'drupalchat_user',
      ));
      $my_settings['text_is_typing'] = t('!user is typing...', array(
        '!user' => 'drupalchat_user',
      ));
      $my_settings['text_close'] = t('Close');
      $my_settings['text_minimize'] = t('Minimize');
      $my_settings['text_mute'] = t('Click to Mute');
      $my_settings['text_unmute'] = t('Click to Unmute');
      $my_settings['text_available'] = t('Available');
      $my_settings['text_idle'] = t('Idle');
      $my_settings['text_busy'] = t('Busy');
      $my_settings['text_offline'] = t('Offline');
      $my_settings['text_lmm'] = t('Load More Messages');
      $my_settings['text_nmm'] = t('No More Messages');
      $my_settings['text_clear_room'] = t('Clear all messages');
      $my_settings['msg_p'] = t('Type and Press Enter');
      if (user_access('administer drupalchat')) {
        $my_settings['admin'] = "1";
        $my_settings['arole'] = user_roles();
        $my_settings['text_ban'] = t('Ban');
        $my_settings['text_ban_ip'] = t('Ban IP');
        $my_settings['text_kick'] = t('Kick');
        $my_settings['text_ban_window_title'] = t('Banned Users');
        $my_settings['text_ban_window_default'] = t('No users have been banned currently.');
        $my_settings['text_ban_window_loading'] = t('Loading banned user list...');
        $my_settings['text_manage_rooms'] = t('Manage Rooms');
        $my_settings['text_unban'] = t('Unban');
        $my_settings['text_unban_ip'] = t('Unban IP');
      }
      else {
        $my_settings['admin'] = "0";
      }
      if (variable_get('drupalchat_show_admin_list', 2) == 1) {
        $my_settings['text_support_chat_init_label'] = variable_get('drupalchat_support_chat_init_label', 'Chat with us');
        $my_settings['text_support_chat_box_header'] = variable_get('drupalchat_support_chat_box_header', 'Support');
        $my_settings['text_support_chat_box_company_name'] = variable_get('drupalchat_support_chat_box_company_name', 'Support Team');
        $my_settings['text_support_chat_box_company_tagline'] = variable_get('drupalchat_support_chat_box_company_tagline', 'Ask us anything...');
        $my_settings['text_support_chat_auto_greet_enable'] = variable_get('drupalchat_support_chat_auto_greet_enable', 1);
        $my_settings['text_support_chat_auto_greet_message'] = variable_get('drupalchat_support_chat_auto_greet_message', 'Hi there! Welcome to our website. Let us know if you have any query!');
        $my_settings['text_support_chat_auto_greet_time'] = variable_get('drupalchat_support_chat_auto_greet_time', 1);
        $my_settings['text_support_chat_offline_message_label'] = variable_get('drupalchat_support_chat_offline_message_label', 'Message');
        $my_settings['text_support_chat_offline_message_contact'] = variable_get('drupalchat_support_chat_offline_message_contact', 'Contact Details');
        $my_settings['text_support_chat_offline_message_send_button'] = variable_get('drupalchat_support_chat_offline_message_send_button', 'Send Message');
        $my_settings['text_support_chat_offline_message_desc'] = variable_get('drupalchat_support_chat_offline_message_desc', 'Hello there. We are currently offline. Please leave us a message. Thanks.');
        $my_settings['text_support_chat_init_label_off'] = variable_get('drupalchat_support_chat_init_label_off', 'Leave Message');
      }
      $my_settings['open_chatlist_default'] = variable_get('drupalchat_minimize_chat_user_list', 2) == 2 ? '1' : '2';
      $my_settings['useStopWordList'] = variable_get('drupalchat_use_stop_word_list', '1');
      $my_settings['blockHL'] = variable_get('drupalchat_stop_links', '1');
      $my_settings['allowAnonHL'] = variable_get('drupalchat_allow_anon_links', '1');
      $my_settings['renderImageInline'] = variable_get('drupalchat_allow_render_images', '1') == '1' ? '1' : '2';
      $my_settings['searchBar'] = variable_get('drupalchat_enable_search_bar', '1') == '1' ? '1' : '2';
      $my_settings['text_search_bar'] = t('Type here to search');
      $my_settings['text_user_list_reconnect'] = t('Connecting...');
      $my_settings['text_user_list_loading'] = t('Loading...');
      $my_settings['mobileWebUrl'] = url('drupalchat/mobile-chat', array(
        'absolute' => TRUE,
      ));
      $my_settings['theme'] = variable_get('drupalchat_theme', 'light');
    }

    /*if ((variable_get('drupalchat_yui_use_cdn', 1) == 2)) {
        drupal_add_js(drupalchat_yui_path() . '/yui-min.js');
      }
      else {
        drupal_add_js('http://yui.yahooapis.com/3.5.0/build/yui/yui-min.js','external');
      }*/

    //drupal_add_js(drupal_get_path('module', 'drupalchat') . '/js/jquery.json-2.3.min.js');

    //drupal_add_js(drupal_get_path('module', 'drupalchat') . '/js/drupalchat-jstorage.js');

    //drupal_add_js(drupal_get_path('module', 'drupalchat') . '/js/storage-lite.js');

    //drupal_add_js(drupal_get_path('module', 'drupalchat') . '/js/jquery.titlealert.min.js');
    if ($polling_method != DRUPALCHAT_COMMERCIAL) {
      drupal_add_js(drupal_get_path('module', 'drupalchat') . '/js/jquery.titlealert.min.js');
      drupal_add_js(drupal_get_path('module', 'drupalchat') . '/js/ba-emotify.js');
      drupal_add_js(drupal_get_path('module', 'drupalchat') . '/js/swfobject.js');
      drupal_add_css(drupal_get_path('module', 'drupalchat') . '/themes/' . $theme . '/' . $theme . '.css');
      drupal_add_js(drupal_get_path('module', 'drupalchat') . '/js/drupalchat-jstorage.js');
      drupal_add_js(drupal_get_path('module', 'drupalchat') . '/js/drupalchat.js');
    }
    if ($polling_method == DRUPALCHAT_COMMERCIAL) {
      if (variable_get('drupalchat_load_chat_async', 1) == '1') {
        if (variable_get('drupalchat_show_admin_list', 2) == 1) {
          drupal_add_js(drupal_get_path('module', 'drupalchat') . '/js/jquery.titlealert.min.js');
          drupal_add_js(drupal_get_path('module', 'drupalchat') . '/js/ba-emotify.js');
        }
        drupal_add_js(array(
          'drupalchat' => $my_settings,
        ), array(
          'type' => 'setting',
        ));
        drupal_add_js('https://cdn.iflychat.com/integration/drupal-7/js/iflychat-init.min.js', array(
          'scope' => 'footer',
        ));
      }
      else {
        global $user;
        $json = NULL;
        $json = _drupalchat_get_auth($user->uid ? $user_name : $a_name);
        if ($json) {
          $my_settings['session_key'] = $json['key'];
          $my_settings['username'] = $user->uid ? $user_name : $a_name;
          $my_settings['uid'] = $user->uid ? $user->uid : '0-' . _drupalchat_get_sid();
          if (variable_get('drupalchat_user_picture', 1) == 1) {
            $my_settings['up'] = drupalchat_return_pic_url();
          }
          $my_settings['upl'] = drupalchat_return_profile_url();
          global $is_https;
          drupal_add_js(array(
            'drupalchat' => $my_settings,
          ), array(
            'type' => 'setting',
          ));
          drupal_add_css('//cdn.iflychat.com/css/iflychat-' . $theme . '.css', array(
            'type' => 'external',
            'scope' => 'footer',
          ));
          drupal_add_css(($is_https ? DRUPALCHAT_EXTERNAL_A_HOST . ':' . DRUPALCHAT_EXTERNAL_A_PORT : DRUPALCHAT_EXTERNAL_HOST . ':' . DRUPALCHAT_EXTERNAL_PORT) . '/i/' . $json['css'] . '/settings/no.cache.css', array(
            'type' => 'external',
            'scope' => 'footer',
          ));
          drupal_add_js(($is_https ? DRUPALCHAT_EXTERNAL_A_HOST . ':' . DRUPALCHAT_EXTERNAL_A_PORT : DRUPALCHAT_EXTERNAL_HOST . ':' . DRUPALCHAT_EXTERNAL_PORT) . '/h/' . $json['css'] . '/settings/no.cache.js', array(
            'type' => 'external',
          ));
          drupal_add_js('//cdn.iflychat.com/js/iflychat.min.js', array(
            'type' => 'external',
            'scope' => 'footer',
          ));
        }
      }
    }
    else {
      drupal_add_js(array(
        'drupalchat' => $my_settings,
      ), array(
        'type' => 'setting',
      ));
    }
  }
}