You are here

livechat.module in LiveChat 7

Same filename and directory in other branches
  1. 8.3 livechat.module
  2. 8 livechat.module
  3. 8.2 livechat.module

LiveChat module.

File

livechat.module
View source
<?php

/**
 * @file
 * LiveChat module.
 */

/**
 * Shows LiveChat on every page except the listed pages.
 */
define('LIVECHAT_VISIBILITY_NOTLISTED', 0);

/**
 * Shows LiveChat on only the listed pages.
 */
define('LIVECHAT_VISIBILITY_LISTED', 1);

/**
 * Pages that LiveChat by default will not be shown on.
 */
define('LIVECHAT_VISIBILITY_DEFAULT_PAGES', "admin\nadmin/*\nnode/add*\nnode/*/*\nuser/*/*\n");

/**
 * System paths that LiveChat can be set to always exclude.
 */
define('LIVECHAT_VISIBILITY_SYSTEM_PATHS', "batch\nfile/ajax/*\nsystem/ajax\nadmin_menu/*\njs/admin_menu/*");

/**
 * Implements hook_menu().
 */
function livechat_menu() {
  $items['admin/config/services/livechat'] = array(
    'title' => 'LiveChat',
    'description' => 'Integrate LiveChat with your website.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'livechat_admin_settings_form',
    ),
    'access arguments' => array(
      'administer livechat',
    ),
    'file' => 'livechat.admin.inc',
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/config/services/livechat/settings'] = array(
    'title' => 'Settings',
    'access arguments' => array(
      'administer livechat',
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -1,
  );
  $items['admin/config/services/livechat/install'] = array(
    'title' => 'Install',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'livechat_admin_license_form',
    ),
    'access arguments' => array(
      'administer livechat',
    ),
    'file' => 'livechat.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function livechat_permission() {
  return array(
    'administer livechat' => array(
      'title' => t('Administer LiveChat'),
      'description' => t('Administer the LiveChat settings.'),
    ),
    'use livechat' => array(
      'title' => t('Use LiveChat'),
      'description' => t('Access the LiveChat widget.'),
    ),
  );
}

/**
 * Implements hook_preprocess_HOOK().
 */
function livechat_preprocess_html(&$vars) {

  // Don't do anything if LiveChat is not enabled.
  if (!variable_get('livechat_enabled', TRUE)) {
    return;
  }

  // Add the LiveChat script if the user has permission to use LiveChat, and
  // LiveChat is configured to be visible on the current page.
  if (user_access('use livechat', $GLOBALS['user']) && livechat_check_visibility()) {
    livechat_add_js();
  }
}

/**
 * Adds LiveChat tracking code.
 */
function livechat_add_js() {
  $license = variable_get('livechat_license');
  if (empty($license)) {
    return FALSE;
  }

  // Allow modules to add custom parameters and visitor information.
  $params = array();
  $visitor = array();
  drupal_alter('livechat', $params, $visitor);
  $settings = array(
    'LiveChat' => array(
      'license' => $license,
      'params' => $params,
      'visitor' => $visitor,
    ),
  );

  // Add in the group id if it has been set.
  $group_id = variable_get('livechat_group', '');
  if ($group_id) {
    $settings['LiveChat']['group'] = $group_id;
  }

  // Pass license, parameters and visitor information to the LiveChat script.
  drupal_add_js($settings, 'setting');

  // Add the LiveChat script to the footer.
  drupal_add_js(drupal_get_path('module', 'livechat') . '/livechat.js', array(
    'scope' => 'footer',
  ));
}

/**
 * Checks whether LiveChat is correctly set up.
 */
function livechat_is_installed() {
  $license = variable_get('livechat_license');
  if (empty($license)) {
    return FALSE;
  }
  return livechat_validate_license($license);
}

/**
 * Validates a LiveChat license.
 */
function livechat_validate_license($license) {
  if (empty($license)) {
    return FALSE;
  }
  return preg_match('/^[0-9]{1,20}$/', $license);
}

/**
 * Checks whether LiveChat should be visible on a path, according to settings.
 */
function livechat_check_visibility($path = NULL) {

  // Default to the current path.
  if (!isset($path)) {
    $path = current_path();
  }

  // Visibility settings.
  $visibility = variable_get('livechat_visibility', LIVECHAT_VISIBILITY_NOTLISTED);
  $pages = drupal_strtolower(variable_get('livechat_pages', LIVECHAT_VISIBILITY_DEFAULT_PAGES));

  // If $visibility is set to LIVECHAT_VISIBILITY_NOTLISTED and the setting for
  // excluding system paths is enabled, add system paths to the list of pages.
  $exclude_system_paths = variable_get('livechat_exclude_system_paths', 1);
  if ($visibility == LIVECHAT_VISIBILITY_NOTLISTED && $exclude_system_paths) {
    $pages .= "\n" . LIVECHAT_VISIBILITY_SYSTEM_PATHS;
  }

  // Get the path alias as lowercase.
  $expanded_path = drupal_strtolower(drupal_get_path_alias($path));

  // Compare the lowercase internal and lowercase path alias (if any).
  $page_match = drupal_match_path($expanded_path, $pages);
  if ($expanded_path != $path) {
    $page_match = $page_match || drupal_match_path($path, $pages);
  }

  // When $visibility has a value of 0 (LIVECHAT_VISIBILITY_NOTLISTED), the
  // widget should be displayed on all pages except those listed. When set to 1
  // (LIVECHAT_VISIBILITY_LISTED), it should only be displayed on listed pages.
  return !($visibility xor $page_match);
}

Functions

Namesort descending Description
livechat_add_js Adds LiveChat tracking code.
livechat_check_visibility Checks whether LiveChat should be visible on a path, according to settings.
livechat_is_installed Checks whether LiveChat is correctly set up.
livechat_menu Implements hook_menu().
livechat_permission Implements hook_permission().
livechat_preprocess_html Implements hook_preprocess_HOOK().
livechat_validate_license Validates a LiveChat license.

Constants

Namesort descending Description
LIVECHAT_VISIBILITY_DEFAULT_PAGES Pages that LiveChat by default will not be shown on.
LIVECHAT_VISIBILITY_LISTED Shows LiveChat on only the listed pages.
LIVECHAT_VISIBILITY_NOTLISTED Shows LiveChat on every page except the listed pages.
LIVECHAT_VISIBILITY_SYSTEM_PATHS System paths that LiveChat can be set to always exclude.