You are here

olark.module in Olark Chat 6

Same filename and directory in other branches
  1. 8 olark.module
  2. 7 olark.module

Integrates Olark Chat in a Drupal site.

File

olark.module
View source
<?php

/**
 * @file
 *   Integrates Olark Chat in a Drupal site.
 */

/**
 * Implements hook_menu().
 */
function olark_menu() {
  $items = array();
  $items['admin/settings/olark'] = array(
    'title' => 'Olark settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'olark_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
  );
  return $items;
}

/**
 * Method which checks whether or not current user contains a role which would restrict display of Olark Chat
 *
 * @return Bool TRUE if user can view Olark Chat. FALSE if user is prevented from seeing Olark Chat
 */
function olark_check_role_access() {

  //check to see if current user has access to view Olark Chat
  global $user;

  //create a bool with value set to TRUE for if statement down below on whether to display Olark Chat
  $display_olark_chat = TRUE;

  //obtain the list from the settings page, and call array_unique to remove duplicate 0's, and obtain only the array_values
  $olark_ignore_user_roles = array_unique(variable_get('olark_ignore_user_roles', array()));
  if (count($olark_ignore_user_roles) > 0) {
    $olark_ignore_user_roles = array_flip($olark_ignore_user_roles);

    //check to see if key 0 is set, which is not a rid, but a result of array_unique
    if (isset($olark_ignore_user_roles[0])) {

      //unset the key to reduce load on foreach
      unset($olark_ignore_user_roles[0]);
    }

    //check to make sure that there is at least one rid that should be ignored to save resources

    //access all of the user roles to see if one of their roles in is in the disallowed list
    foreach ($user->roles as $rid => $role) {
      if (in_array($rid, $olark_ignore_user_roles)) {

        //a rid that should ignore Olark Chat has been found. Setting bool to FALSE and breaking out
        $display_olark_chat = FALSE;
        break;
      }
    }
  }

  //return the result
  return $display_olark_chat;
}

/**
 * Implements hook_footer().
 */
function olark_footer() {

  // Note that the 'olark_enable' variable can be (un)set in
  // the $conf array in settings.php, or by another module to enable/disable
  // however, there is not a user interface for this variable.
  $role_display_olark_chat = olark_check_role_access();

  //if the display olark chat AND the variable for olark enable is true, then display Olark Chat
  if ($role_display_olark_chat && variable_get('olark_enable', 1) && !olark_suppress(FALSE)) {
    if (!variable_get('olark_context', 0)) {
      return variable_get('olark_code', '');
    }
    else {
      if ($plugin = context_get_plugin('reaction', 'olark_add')) {
        if ($plugin
          ->execute()) {
          return variable_get('olark_code', '');
        }
      }
    }
  }
}

/** 	
 * Implements hook_init().
 */
function olark_init() {

  // Note that the 'olark_enable' variable can be (un)set in
  // the $conf array in settings.php, or by another module to enable/disable
  // however, there is not a user interface for this variable.
  $role_display_olark_chat = olark_check_role_access();

  //if the display olark chat AND the variable for olark enable is true, then display Olark Chat
  if ($role_display_olark_chat && variable_get('olark_enable', 1)) {
    if (!variable_get('olark_context', 0)) {
      olark_add_js();
    }
    else {
      if ($plugin = context_get_plugin('reaction', 'olark_add')) {
        if ($plugin
          ->execute()) {
          olark_add_js();
        }
      }
    }
  }
}

/**
 * Implements hook_context_registry().
 */
function olark_context_registry() {
  return array(
    'reactions' => array(
      'olark_add' => array(
        'title' => t('Olark'),
        'plugin' => 'olark_context_reaction_add',
        'description' => t('Add Olark chat code to the page'),
      ),
    ),
  );
}

/**
 * Notify CTools that we'll be using the plugin API with Context module.
 */
function olark_ctools_plugin_api($module, $api) {
  if ($module == 'context' && $api == 'plugins') {
    return array(
      'version' => 3,
    );
  }
}

/**
 * CTools plugin API hook for Context.
 */
function olark_context_plugins() {
  $plugins = array();
  $plugins['olark_context_reaction_add'] = array(
    'handler' => array(
      'path' => drupal_get_path('module', 'olark') . '/plugins',
      'file' => 'olark_context_reaction_add.inc',
      'class' => 'olark_context_reaction_add',
      'parent' => 'context_reaction',
    ),
  );
  return $plugins;
}

/**
 * Settings form.
 */
function olark_settings() {
  $form = array();
  $olark_roles_options = user_roles();
  $form['olark_code'] = array(
    '#type' => 'textarea',
    '#title' => t('Olark code'),
    '#description' => t('Paste the Javascript code block from <a href="http://olark.com/install">Olark.com</a>'),
    '#default_value' => variable_get('olark_code', ''),
  );
  $form['olark_ios'] = array(
    '#type' => 'checkbox',
    '#title' => t('Disable in iOS devices'),
    '#description' => t('Hides it on iPhone, iPad and iPod since it has issues in this plataforms.'),
    '#default_value' => variable_get('olark_ios', ''),
  );
  if (module_exists('context')) {
    $form['olark_context'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use Context to choose when Olark is added to the page.'),
      '#description' => t('The Olark code will be included on all pages of your site by default. You can use <a href="!context_url">Context module</a> to fine tune when and where it will be displayed.', array(
        '!context_url' => url('admin/build/context'),
      )),
      '#default_value' => variable_get('olark_context', 0),
    );
  }
  $form['olark_ignore_user_roles'] = array(
    '#type' => 'checkboxes',
    '#title' => t('User roles that will not display Olark Chat'),
    '#description' => t('Certain roles may exist, such as the administrator role, that you do not wish Olark Chat to display. Use these checkboxes to select possible roles that will prevent Olark Chat from loading'),
    '#default_value' => variable_get('olark_ignore_user_roles', array()),
    '#options' => $olark_roles_options,
  );
  return system_settings_form($form);
}

/**
 * Builds and inserts the correct Olark Javascript into the page.
 */
function olark_add_js() {
  static $added;
  if (!isset($added)) {
    global $user;
    $path = drupal_get_path('module', 'olark');
    drupal_add_js($path . '/olark.js', 'module', 'header', TRUE);
    $setting = array(
      'olark' => array(),
    );

    // If the user is logged in, let's get some pertinent information and
    // pass it along as well.
    if ($user->uid) {
      $setting['olark']['uid'] = $user->uid;
      $setting['olark']['name'] = $user->name;

      //$setting['olark']['mail'] = t('email !mail', array('!mail' => $user->mail));
      $setting['olark']['mail'] = $user->mail;
      $setting['olark']['roles'] = t('roles !roles', array(
        '!roles' => implode(', ', $user->roles),
      ));
      $setting['olark']['userpage'] = url('user/' . $user->uid, array(
        'absolute' => TRUE,
      ));
      $setting['olark']['loggedinas'] = t('logged in as !link', array(
        '!link' => l($user->name, 'user/' . $user->uid, array(
          'absolute' => TRUE,
        )),
      ));
    }
    $setting['olark']['disable_ios'] = variable_get('olark_ios', '');
    $setting['olark']['enabled'] = variable_get('olark_enable', 1);
    drupal_add_js($setting, 'setting');
    $added = TRUE;
  }
}

/**
 * Implementation of hook_suppress()
 *
 * Allows other modules to suppress display of Olark
 *
 * This function should be called from within another module's page callback
 * (preferably using module_invoke()) when the menu should not be displayed.
 * This is useful for modules that implement popup pages or other special
 * pages where the chat would be distracting or break the layout.
 *
 * @param $set
 *   Defaults to TRUE. If called before hook_footer(), the chat will not be
 *   displayed. If FALSE is passed, the suppression state is returned.
 **/
function olark_suppress($set = TRUE) {
  static $suppress = FALSE;
  if (!empty($set) && $suppress === FALSE) {
    $suppress = TRUE;
  }
  return $suppress;
}

Functions

Namesort descending Description
olark_add_js Builds and inserts the correct Olark Javascript into the page.
olark_check_role_access Method which checks whether or not current user contains a role which would restrict display of Olark Chat
olark_context_plugins CTools plugin API hook for Context.
olark_context_registry Implements hook_context_registry().
olark_ctools_plugin_api Notify CTools that we'll be using the plugin API with Context module.
olark_footer Implements hook_footer().
olark_init Implements hook_init().
olark_menu Implements hook_menu().
olark_settings Settings form.
olark_suppress Implementation of hook_suppress()