You are here

function drupalchat_page_attachments in DrupalChat 8

File

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

Code

function drupalchat_page_attachments(array &$attachments) {
  global $base_url;

  // Load the current user.
  $user = \Drupal\user\Entity\User::load(\Drupal::currentUser()
    ->id());
  if (\Drupal::config('drupalchat.settings')
    ->get('drupalchat_polling_method') == DRUPALCHAT_COMMERCIAL) {

    //polling method is iFlyChat Server
    if (\Drupal::currentUser()
      ->hasPermission('access drupalchat')) {
      if ($user
        ->id()) {
        $user_data = json_encode(drupalchatController::_drupalchat_get_user_details());
      }

      //if(\Drupal::config('drupalchat.settings')->get('drupalchat_rel') == "0" && 0){
      if (\Drupal::config('drupalchat.settings')
        ->get('drupalchat_session_caching') == '1' && isset($_SESSION['user_data']) && $_SESSION['user_data'] == $user_data) {
        if (isset($_SESSION['token']) && !empty($_SESSION['token'])) {
          $attachments['#attached']['drupalSettings']['drupalchat_auth_token'] = $_SESSION['token'];
          $attachments['#attached']['library'][] = 'drupalchat/drupalchat-auth-token';
        }
      }
      if ($user
        ->id()) {
        $drupalchat_auth_url = \Drupal\Core\Url::fromUserInput('/drupalchat/auth', array(
          'absolute' => TRUE,
          'query' => array(
            't' => time(),
          ),
        ))
          ->toString();
        $attachments['#attached']['drupalSettings']['drupalchat_auth_url'] = $drupalchat_auth_url;
        $attachments['#attached']['library'][] = 'drupalchat/drupalchat-auth-url';
      }
      $drupalchat_path_visibility = \Drupal::config('drupalchat.settings')
        ->get('drupalchat_path_visibility');
      if ($drupalchat_path_visibility == 0) {
        $attachments['#attached']['library'][] = 'drupalchat/drupalchat-popup';
      }
      else {
        if ($drupalchat_path_visibility == 1 && !\Drupal::service('router.admin_context')
          ->isAdminRoute()) {
          $attachments['#attached']['library'][] = 'drupalchat/drupalchat-popup';
        }
        else {
          if (($drupalchat_path_visibility == 2 || $drupalchat_path_visibility == 3) && drupalchatController::drupalchat_verify_access()) {
            $attachments['#attached']['library'][] = 'drupalchat/drupalchat-popup';
          }
        }
      }

      // if(drupalchat_verify_access()){
      // 	$attachments['#attached']['library'][] = 'drupalchat/drupalchat-popup';
      // }
      $attachments['#attached']['drupalSettings']['drupalchat_app_id'] = \Drupal::config('drupalchat.settings')
        ->get('drupalchat_app_id');
      $attachments['#attached']['drupalSettings']['drupalchat_external_cdn_host'] = DRUPALCHAT_EXTERNAL_CDN_HOST;
      $attachments['#attached']['library'][] = 'drupalchat/drupalchat-bundle';
    }
  }
  else {
    if (drupalchatController::drupalchat_verify_access()) {
      if (\Drupal::config('drupalchat.settings')
        ->get('drupalchat_polling_method') == DRUPALCHAT_AJAX) {
        if ($user
          ->id() > 0) {
          $account = user_load($user
            ->id());
          $user_name = Html::escape(user_format_name($account));
          $database = \Drupal::database();
          $status = $database
            ->query('SELECT status FROM {drupalchat_users} WHERE uid = :uid', array(
            ':uid' => $user
              ->id(),
          ))
            ->fetchField();
          $session_manager = Drupal::service('session_manager');
          $session_id = $session_manager
            ->getId();
          if (!$status) {
            $status = DRUPALCHAT_USER_ONLINE;
            $current_user = array(
              'uid' => $user
                ->id(),
              'session' => $session_id,
              'name' => $user_name,
              'status' => $status,
              'timestamp' => time(),
            );
            \Drupal::database()
              ->insert('drupalchat_users')
              ->fields($current_user)
              ->execute();
          }
          else {
            db_update('drupalchat_users')
              ->fields(array(
              'timestamp' => time(),
              'status' => $status == DRUPALCHAT_USER_OFFLINE ? DRUPALCHAT_USER_ONLINE : $status,
            ))
              ->condition('uid', $user
              ->id())
              ->execute();
          }
        }
        else {

          //print_r('guest');
          $sid = drupalchatController::_drupalchat_get_sid();
          if (!$sid || $sid == -1) {
            return;
          }
          else {
            $drupalchat_anon_prefix = \Drupal::config('drupalchat.settings')
              ->get('drupalchat_anon_prefix') ?: 'Guest';
            $user_name = Html::escape($drupalchat_anon_prefix . time());
            $status = db_query('SELECT status FROM {drupalchat_users} WHERE uid = :uid AND session = :sid', array(
              ':uid' => $user
                ->id(),
              ':sid' => $sid,
            ))
              ->fetchField();
            if (!$status) {
              $status = DRUPALCHAT_USER_ONLINE;
              $drupalchat_anon_use_name = \Drupal::config('drupalchat.settings')
                ->get('drupalchat_anon_use_name') ?: 1;
              if ($drupalchat_anon_use_name == 1) {
                $user_name = $drupalchat_anon_prefix . ' ' . drupalchatController::drupalchat_get_random_name();
              }
              $session_manager = Drupal::service('session_manager');
              $session_id = $session_manager
                ->getId();
              $current_user = (object) array(
                'uid' => $user
                  ->id(),
                'session' => $sid,
                'name' => $user_name,
                'status' => $status,
                'timestamp' => time(),
              );
              \Drupal::database()
                ->insert('drupalchat_users')
                ->fields($current_user)
                ->execute();

              //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
                ->id())
                ->condition('session', $sid)
                ->execute();
            }
          }
        }
      }

      // end of drupalchat_polling_method
      $theme = \Drupal::config('drupalchat.settings')
        ->get('drupalchat_theme') ?: 'light';
      $polling_method = \Drupal::config('drupalchat.settings')
        ->get('drupalchat_polling_method') ?: DRUPALCHAT_AJAX;
      if (!$user
        ->id() && $polling_method != DRUPALCHAT_COMMERCIAL) {
        $a_name = db_query('SELECT name FROM {drupalchat_users} WHERE uid = :uid AND session = :sid', array(
          ':uid' => $user
            ->id(),
          ':sid' => $sid,
        ))
          ->fetchField();
      }
      $token_generator = \Drupal::csrfToken();
      $item_list_render = array(
        '#theme' => 'item_list',
        '#items' => array(
          0 => array(
            '#markup' => t('No users online')
              ->__toString(),
            '#wrapper_attributes' => array(
              'class' => 'drupalchatnousers',
            ),
          ),
        ),
      );
      $my_settings = array(
        'current_timestamp' => time(),
        'polling_method' => $polling_method,
        'pollUrl' => Url::fromRoute('drupalchat.poll', array(), array(
          'absolute' => TRUE,
        ))
          ->toString(),
        'sendUrl' => Url::fromRoute('drupalchat.send', array(), array(
          'absolute' => TRUE,
        ))
          ->toString(),
        'formId' => 'drupalchat_send',
        'formToken' => $token_generator
          ->get('drupalchat_send'),
        'statusUrl' => Url::fromRoute('drupalchat.status', array(), array(
          'absolute' => TRUE,
        ))
          ->toString(),
        'status' => $status,
        'goOnline' => t('Go Online'),
        'goIdle' => t('Go Idle'),
        'newMessage' => t('New chat message!'),
        'images' => $base_url . '/' . drupal_get_path('module', 'drupalchat') . '/css/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' => \Drupal::service('renderer')
          ->renderPlain($item_list_render)
          ->__toString(),
        'smileyURL' => base_path() . drupal_get_path('module', 'drupalchat') . '/smileys/very_emotional_emoticons-png/png-32x32/',
        'addUrl' => Url::fromRoute('drupalchat.channel.add', array(), array(
          'absolute' => TRUE,
        ))
          ->toString(),
        'notificationSound' => \Drupal::config('drupalchat.settings')
          ->get('drupalchat_notification_sound') ?: 1,
        'exurl' => Url::fromRoute('drupalchat.auth', array(
          't' => time(),
        ), array(
          'absolute' => TRUE,
        ))
          ->toString(),
        'soffurl' => Url::fromRoute('drupalchat/send-offline-message'),
        'chat_type' => \Drupal::config('drupalchat.settings')
          ->get('drupalchat_show_admin_list') ?: 2,
        'guestPrefix' => \Drupal::config('drupalchat.settings')
          ->get('drupalchat_anon_prefix') ?: 'Guest' . " ",
        'changeurl' => Url::fromRoute('drupalchat.change-guest-name', array(
          't' => time(),
        )),
        'allowSmileys' => \Drupal::config('drupalchat.settings')
          ->get('drupalchat_enable_smiley') ?: 1,
      );
      if ($polling_method == DRUPALCHAT_AJAX) {
        $my_settings['refresh_rate'] = \Drupal::config('drupalchat.settings')
          ->get('drupalchat_refresh_rate') ?: 2;
      }
      if ($polling_method != DRUPALCHAT_COMMERCIAL) {
        $my_settings['username'] = $user
          ->id() ? $user_name : $a_name;
        $my_settings['uid'] = $user
          ->id() ? $user
          ->id() : '0-' . drupalchatController::_drupalchat_get_sid();
        $my_settings['threadHistoryUrl'] = Url::fromRoute('drupalchat.thread-history', array(), array(
          'absolute' => TRUE,
        ))
          ->toString();
      }
      $my_settings['iup'] = \Drupal::config('drupalchat.settings')
        ->get('drupalchat_user_picture') ?: 1;
      $drupalchat_user_picture = \Drupal::config('drupalchat.settings')
        ->get('drupalchat_user_picture') ?: 1;
      if ($drupalchat_user_picture == 1) {
        if ($polling_method != DRUPALCHAT_COMMERCIAL) {
          $my_settings['up'] = drupalchatController::drupalchat_return_pic_url();
        }
        $my_settings['default_up'] = $base_url . '/' . drupal_get_path('module', 'drupalchat') . '/css/themes/' . $theme . '/images/default_avatar.png';
        $my_settings['default_cr'] = $base_url . '/' . drupal_get_path('module', 'drupalchat') . '/css/themes/' . $theme . '/images/default_room.png';
        $my_settings['default_team'] = $base_url . '/' . drupal_get_path('module', 'drupalchat') . '/css/themes/' . $theme . '/images/default_team.png';
      }
      if ($polling_method != DRUPALCHAT_COMMERCIAL) {
        $attachments['#attached']['library'][] = 'drupalchat/drupalchat-jquery-titlealert';
        $attachments['#attached']['library'][] = 'drupalchat/drupalchat-ba-emotify';
        $attachments['#attached']['library'][] = 'drupalchat/drupalchat-swfobject';
        if ($theme == 'light') {
          $attachments['#attached']['library'][] = 'drupalchat/drupalchat-theme-light';
        }
        else {
          $attachments['#attached']['library'][] = 'drupalchat/drupalchat-theme-dark';
        }
        $attachments['#attached']['library'][] = 'drupalchat/drupalchat-storage';
        $attachments['#attached']['library'][] = 'drupalchat/drupalchat-main';
      }
      $attachments['#attached']['drupalSettings']['drupalchat'] = $my_settings;
    }
  }

  // end of drupalchat_verify_access()
}