You are here

function drupalchat_init in DrupalChat 7.2

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

@todo Please document this function.

See also

http://drupal.org/node/1354

File

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

Code

function drupalchat_init() {
  global $user, $base_url;
  $user_data = false;

  //print_r(variable_get('drupalchat_external_api_key'));

  //print_r(variable_get('drupalchat_app_id'));
  if (check_plain(variable_get('drupalchat_polling_method')) == DRUPALCHAT_COMMERCIAL) {
    if (user_access('access drupalchat')) {
      $iflychat_auth_url = url('drupalchat/auth', array(
        'query' => array(
          't' => time(),
        ),
      ));
      if (user_is_logged_in()) {
        $user_data = json_encode(_drupalchat_get_user_details());
      }
      if (check_plain(variable_get('drupalchat_session_caching', '1')) == '1' && isset($_SESSION['user_data']) && $_SESSION['user_data'] == $user_data) {
        if (isset($_SESSION['token']) && !empty($_SESSION['token'])) {
          drupal_add_js('var iflychat_auth_token = "' . $_SESSION['token'] . '";', array(
            'type' => 'inline',
            'scope' => 'footer',
          ));
        }
      }
      if (user_is_logged_in()) {
        drupal_add_js('var iflychat_auth_url = "' . $iflychat_auth_url . '";', array(
          'type' => 'inline',
          'scope' => 'footer',
        ));
      }
      if (drupalchat_verify_access()) {
        drupal_add_js('var drupalchat_popup=document.createElement("DIV"); drupalchat_popup.className="iflychat-popup";document.body.appendChild(drupalchat_popup);', array(
          'type' => 'inline',
          'scope' => 'footer',
        ));
      }
      $iflychat_api_key = check_plain(variable_get('drupalchat_external_api_key', NULL));
      $iflychat_app_id = check_plain(variable_get('drupalchat_app_id', NULL));
      if (!empty($iflychat_api_key) && !empty($iflychat_app_id)) {
        drupal_add_js('var drupalchat_bundle = document.createElement("SCRIPT"); drupalchat_bundle.src = "//' . DRUPALCHAT_EXTERNAL_CDN_HOST . '/js/iflychat-v2.min.js?app_id=' . variable_get('drupalchat_app_id') . '"' . '; drupalchat_bundle.async = "async";document.body.appendChild(drupalchat_bundle)', array(
          'type' => 'inline',
          'scope' => 'footer',
        ));
      }
    }
  }
  else {
    if (drupalchat_verify_access()) {
      if (check_plain(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 {

          //print_r('guest');
          $sid = _drupalchat_get_sid();
          if (!$sid || $sid == -1) {
            return;
          }
          else {
            $user_name = check_plain(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 = check_plain(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();
            }
          }
        }
      }
      $theme = check_plain(variable_get('drupalchat_theme', 'light'));
      $polling_method = check_plain(variable_get('drupalchat_polling_method', DRUPALCHAT_AJAX));
      if (!$user->uid && check_plain(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,
        )),
        'formId' => 'drupalchat_send',
        'formToken' => drupal_get_token('drupalchat_send'),
        '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' => check_plain(variable_get('drupalchat_notification_sound', 1)),
        'exurl' => url('drupalchat/auth', array(
          'query' => array(
            't' => time(),
          ),
        )),
        'soffurl' => url('drupalchat/send-offline-message'),
        'chat_type' => check_plain(variable_get('drupalchat_show_admin_list', 2)),
        'guestPrefix' => check_plain(variable_get('drupalchat_anon_prefix', 'Guest')) . " ",
        'changeurl' => url('drupalchat/change-guest-name', array(
          'query' => array(
            't' => time(),
          ),
        )),
        'allowSmileys' => check_plain(variable_get('drupalchat_enable_smiley', 1)),
      );

      //print_r($my_settings);
      if ($polling_method == DRUPALCHAT_AJAX) {
        $my_settings['refresh_rate'] = check_plain(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'] = check_plain(variable_get('drupalchat_user_picture', 1));
      if (check_plain(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 ((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');
      }
      drupal_add_js(array(
        'drupalchat' => $my_settings,
      ), array(
        'type' => 'setting',
      ));
    }
  }

  // end of drupalchat_verify_access condition
}