Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
119 lines (101 sloc) 3.61 KB
<?php
/*
* @file
* Drupal Module: SnapEngage
*
* Adds the required Javascript to the bottom of all your Drupal pages
* to add the SnapEngage widget.
*
* Code heavily inspired by the Google Analytics module, http://drupal.org/project/google_analytics.
*/
function snapengage_menu() {
$items['admin/settings/snapengage'] = array(
'title' => 'SnapEngage',
'description' => 'Configure the settings used to add a SnapEngage widget to your site.',
'page callback' => 'drupal_get_form',
'page arguments' => array('snapengage_admin_settings_form'),
'access arguments' => array('administer snapengage'),
'file' => 'snapengage.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implementation of hook_perm
*/
function snapengage_perm() {
return array('administer snapengage');
}
/**
* Implementation of hook_footer() to insert JavaScript at the end of the page.
*/
function snapengage_footer($main = 0) {
global $user;
$id = variable_get('snapengage_widget_id', '');
// 1. Check if the SnapEngage widget id has a value.
// 2. Track page views based on visibility value.
// 3. Check if we should track the currently active user's role.
if (!empty($id) && _snapengage_visibility_pages() && _snapengage_visibility_roles($user)) {
$script = sprintf('document.write(unescape("%%3Cscript src=\'" + ((document.location.protocol=="https:")?"https:":"http:") + "//snapabug.appspot.com/snapabug.js\' type=\'text/javascript\'%%3E%%3C/script%%3E"));</script><script type="text/javascript">
SnapABug.addButton(\'%s\',"%s","%s");', $id, 0, '55%');
drupal_add_js($script, 'inline', 'footer');
}
}
/**
* Based on visibility setting this function returns TRUE if SnapEngage code should
* be added for the current role and otherwise FALSE.
*/
function _snapengage_visibility_roles($account) {
$enabled = FALSE;
$roles = variable_get('_snapengage_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 enabled for tracking?
if (isset($roles[$rid]) && $rid == $roles[$rid]) {
// Current user is a member of a role that should be shown the SnapEngage widget.
$enabled = TRUE;
break;
}
}
}
else {
// No role is selected for tracking, therefor the widget should be shown for all roles.
$enabled = TRUE;
}
return $enabled;
}
/**
* Based on visibility setting this function returns TRUE if SnapEngage code should
* be added to the current page and otherwise FALSE.
*/
function _snapengage_visibility_pages() {
static $page_match;
// Cache visibility setting in hook_init for hook_footer.
if (!isset($page_match)) {
$visibility = variable_get('snapengage_visibility', 0);
$pages = variable_get('snapengage_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);
}
else {
$page_match = drupal_eval($pages);
}
}
else {
$page_match = TRUE;
}
}
return $page_match;
}