zopim.module in Zopim Live Chat 7
Same filename and directory in other branches
File
zopim.moduleView source
<?php
/*
* @file
* Drupal Module: zopim
* Adds the required Javascript for Zopim chat to the bottom of all your Drupal pages
*/
/**
* Implementation of hook_help().
*
* @param string $path
* router menu path.
* @param array $arg
* array corresponding to return value of arg().
* @return string
* localized string containing help text.
*/
function zopim_help($path, $arg) {
switch ($path) {
case 'admin/help#zopim':
return;
}
}
/**
* Implementation of hook_permission().
*
* @return array
* Array of available permissions
*/
function zopim_permission() {
return array(
'administer zopim' => array(
'title' => t('Administer Zopim module'),
'description' => t('Permission to change Zopim settings'),
),
'use PHP for Zopim visibility' => array(
'title' => t('Use PHP for Zopim visibility'),
'description' => t('Permission to set PHP conditions to customize Zopim visibility on various pages'),
),
);
}
/**
* Implementation of hook_menu().
*
* @return array
* structured associative array of menu items.
*/
function zopim_menu() {
$items['admin/config/system/zopim'] = array(
'title' => 'Zopim',
'description' => 'Configure the settings used to generate your Zopim code.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'zopim_admin_settings_form',
),
'access arguments' => array(
'administer zopim',
),
'file' => 'zopim.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implementation of hook_page_alter().
*
* @global object $user
* full user object for current user
* @param $page
* nested array of renderable elements that make up the page.
*/
function zopim_page_alter(&$page) {
global $user;
$id = variable_get('zopim_account', '');
// 1. Check if the Zopim account number has a value.
// 2. Add JS based on visibility value.
// 3. Check if we should add the JS for the currently active user's role.
if (!empty($id) && _zopim_visibility_pages() && _zopim_visibility_user_test($user)) {
$script = <<<EOS
document.write(unescape("%3Cscript src='" + document.location.protocol + "//zopim.com/?{<span class="php-variable">$id</span>}' charset='utf-8' type='text/javascript'%3E%3C/script%3E"));
EOS;
drupal_add_js($script, array(
'type' => 'inline',
'scope' => 'footer',
'group' => JS_THEME,
));
}
}
/**
* Implementation of hook_requirements().
*
* @param string $phase
* either 'install' or 'runtime' depending on install or status report page.
* @return array
* associative array of requirements
*/
function zopim_requirements($phase) {
$requirements = array();
if ($phase == 'runtime') {
// Raise warning if Zopim user account has not been set yet.
if (variable_get('zopim_account', false) == false) {
$requirements['zopim'] = array(
'title' => t('Zopim module'),
'description' => t('Zopim module has not been configured yet. Please configure its settings from the <a href="@url">Zopim settings page</a>.', array(
'@url' => url('admin/settings/zopim'),
)),
'severity' => REQUIREMENT_ERROR,
'value' => t('Not configured'),
);
}
}
return $requirements;
}
/**
* Utility function to determing visibility.
* Based on visibility setting this function returns TRUE if Zopim code should
* be added to the current page and otherwise FALSE.
*
* @staticvar boolean $page_match
* whether page is visible (true) or not (false).
* @return boolean
* a decision on visibility, true/false
*/
function _zopim_visibility_pages() {
static $page_match;
// Cache visibility setting in hook_init for hook_footer.
if (!isset($page_match)) {
$visibility = variable_get('zopim_visibility', 0);
$pages = variable_get('zopim_pages', '');
// Match path if necessary.
if (!empty($pages)) {
if ($visibility < 2) {
$path = drupal_get_path_alias($_GET['q']);
// Compare with the internal and path alias (if any).
$page_match = drupal_match_path($path, $pages);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $pages);
}
// When $visibility has a value of 0, the block is displayed on
// all pages except those listed in $pages. When set to 1, it
// is displayed only on those pages listed in $pages.
$page_match = !($visibility xor $page_match);
}
elseif (module_exists('php')) {
$page_match = php_eval($pages);
}
}
else {
$page_match = TRUE;
}
}
return $page_match;
}
/**
* Utility function to perform tracking visibility check for an user object.
*
* @param object $account
* a user object containing an array of roles to check.
* @return boolean
* a decision on if the current user is being tracked by Zopim.
*/
function _zopim_visibility_user_test($account) {
$enabled = FALSE;
// Is current user a member of a role that should be tracked?
if (_zopim_visibility_roles($account)) {
$enabled = TRUE;
}
return $enabled;
}
/**
* Based on visibility setting this function returns TRUE if Zopim code should
* be added for the current role and otherwise FALSE.
*
* @param object $account
* a full user object.
* @return boolean
* a decision on visibility, true/false
*/
function _zopim_visibility_roles($account) {
$enabled = TRUE;
$roles = variable_get('zopim_roles', array());
if (array_sum($roles) > 0) {
// One or more roles are selected for tracking.
foreach (array_keys($account->roles) as $rid) {
// Is the current user a member of one role selected in admin settings?
if (isset($roles[$rid]) && $rid == $roles[$rid]) {
// Current user is a member of a role that is selected in admin settings.
$enabled = FALSE;
break;
}
}
}
return $enabled;
}
Functions
Name | Description |
---|---|
zopim_help | Implementation of hook_help(). |
zopim_menu | Implementation of hook_menu(). |
zopim_page_alter | Implementation of hook_page_alter(). |
zopim_permission | Implementation of hook_permission(). |
zopim_requirements | Implementation of hook_requirements(). |
_zopim_visibility_pages | Utility function to determing visibility. Based on visibility setting this function returns TRUE if Zopim code should be added to the current page and otherwise FALSE. |
_zopim_visibility_roles | Based on visibility setting this function returns TRUE if Zopim code should be added for the current role and otherwise FALSE. |
_zopim_visibility_user_test | Utility function to perform tracking visibility check for an user object. |