You are here

brb.module in Browser Bouncer (brb) 7

Display a message to let the user knows his/her browser is not supported. This message also offer to the user different browsers that can be used instead.

File

brb.module
View source
<?php

/**
 * @file
 * Display a message to let the user knows his/her browser is not supported.
 * This message also offer to the user different browsers that can be used instead.
 */
define('BRB_TITLE', t('Your browser is not supported on this site'));
define('BRB_BODY_DEFAULT', '<h2>' . t('Sorry but it seems like your browser is a little bit old.') . '</h2>' . '<h4>' . t('We strongly recommend you to upgrade it or download one of the following:') . '</h4>');
define('BRB_IE_CONDITIONAL_DEFAULT', 'lte IE 7');

/**
 * Display help and module information
 * @param path which path of the site we're displaying help
 * @param arg array that holds the current path as would be returned from arg() function
 * @return help text for the path
 */
function brb_help($path, $arg) {
  $output = '';

  //declare your output variable
  switch ($path) {
    case "admin/help#brb":
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Browser Bouncer (BrB) module displays a message to users informing them that their browser is not supported. For more information, see the online handbook entry for <a href="@brb">Browser Bouncer (BrB)</a>.', array(
        '@brb' => 'http://drupal.org/handbook/modules/brb',
      )) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Prompting users to upgrade their browser') . '</dt>';
      $output .= '<dd>' . t('When the module is enabled, anyone using a browser that is included in the "not supported" list will see a warning message on the site.') . '</dd>';
      $output .= '<dt>' . t('Administering the module') . '</dt>';
      $output .= '<dd>' . t('The <a href="@settings">settings page</a> allows you to change: the blocked browsers, displayed text, overlay, and the list of alternative browsers.', array(
        '@settings' => url('admin/config/brb'),
      )) . '</dd>';
      $output .= '<dt>' . t('User permissions') . '</dt>';
      $output .= '<dd>' . t('The Browser Bouncer (BrB) module makes a number of permissions available for administration and browsing without the warning message, which can be set by role on the <a href="@permissions">permissions page</a>.', array(
        '@permissions' => url('admin/settings/permissions'),
      )) . '</dd>';
      $output .= '</dl>';
  }
  return $output;
}

/**
 * Implementation of hook_permission().
 */
function brb_permission() {
  return array(
    'browse without warning' => array(
      'title' => t('Browse without warning'),
      'description' => t('If selected the warning message will not be displayed.'),
    ),
    'administer brb' => array(
      'title' => t('Administer BrB'),
      'description' => t('Perform administration tasks for Browser Bouncer (BrB)'),
    ),
  );
}

/**
 * Implementation of hook_menu().
 */
function brb_menu() {
  $items = array();
  $items['admin/config/brb'] = array(
    'title' => 'Browser Bouncer',
    'description' => 'Modify this settings for a better fit with your design. Remember you can also style the module by CSS.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'brb_admin_settings',
    ),
    'access arguments' => array(
      'administer brb',
    ),
    'file' => 'brb.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function brb_theme() {
  $items = array();
  $items['brb_widget'] = array(
    'variables' => array(
      'body' => NULL,
      'browsers' => NULL,
    ),
    'template' => 'brb-widget',
  );
  $items['brb_admin_settings'] = array(
    'render element' => 'form',
  );
  return $items;
}

/**
 * Implementation of hook_preprocess_page().
 *
 * The script and style will be added for any not supported browser before the page is rendered.
 */
function brb_preprocess_page(&$variables) {
  global $user;
  if (user_access('browse without warning', $user) == FALSE) {
    drupal_add_library('system', 'ui.dialog');
    $module_path = drupal_get_path('module', 'brb');

    // Add script with conditional

    /*
    / Note: This is the right way to do it but unfortunately drupal_add_js() doesn't support the browser option.
    /       See http://drupal.org/node/865536 for more information about this issue.
    /
    / Way around: See the way around solution on function brb_process_html(&$variables);

    drupal_add_js(
      array('brb' => array('widget' => $clean_widget)),
      array(
        'scope' => 'header',
        'type' => 'setting',
        'weight' => JS_THEME,
        'browsers' => array('IE' => variable_get('brb_ie_conditional', BRB_IE_CONDITIONAL_DEFAULT), '!IE' => FALSE),
      )
    );
    drupal_add_js(
      $module_path . '/brb.js',
      array(
        'scope' => 'header',
        'type' => 'file',
        'weight' => JS_THEME,
        'browsers' => array('IE' => variable_get('brb_ie_conditional', BRB_IE_CONDITIONAL_DEFAULT), '!IE' => FALSE),
      )
    );
    */

    // Add style with conditional
    drupal_add_css($module_path . '/brb.css', array(
      'type' => 'file',
      'weight' => CSS_DEFAULT,
      'browsers' => array(
        'IE' => variable_get('brb_ie_conditional', BRB_IE_CONDITIONAL_DEFAULT),
        '!IE' => FALSE,
      ),
    ));
  }
}

/**
 * Implementation of hook_init().
 *
 * Add an empty variable in Drupal.settings to force the load of jquery.js and drupal.js,
 * the variable will be use later to store this module settings.
 *
 * Note: This wouldn't be needed if drupal_add_js() was supporting the browser option.
 *       See http://drupal.org/node/865536 for more information about this issue.
 */
function brb_init() {
  drupal_add_js(array(
    'brb' => NULL,
  ), 'setting');
}

/**
 * Implementation of hook_preprocess_html().
 *
 * The script and style will be added for any not supported browser before the page is rendered.
 */
function brb_process_html(&$variables) {
  global $user;
  if (user_access('browse without warning', $user) == FALSE) {
    $module_path = drupal_get_path('module', 'brb');
    $clean_widget = str_replace(array(
      "\r\n",
      "\n",
      "\r",
      '"',
    ), array(
      "",
      "",
      "",
      '\\"',
    ), brb_widget());

    //$clean_widget = brb_widget();

    /* This is the way around */
    global $base_path;
    $variables['scripts'] .= '
      <!--[if ' . variable_get('brb_ie_conditional', BRB_IE_CONDITIONAL_DEFAULT) . ']>
        <script type="text/javascript">
          var brb = {
            overlay: ' . strtolower(variable_get('brb_overlay', TRUE)) . ',
            title: "' . variable_get('brb_title', BRB_TITLE) . '",
            widget: "' . $clean_widget . '"
          };
        </script>
        <script type="text/javascript" src="' . $base_path . $module_path . '/brb.js"></script>
      <![endif]-->
    ';
  }
}

/**
* Create the brb widget to display after the page is loaded.
*/
function brb_widget() {
  $body = variable_get('brb_body', 'Sorry but your browser is not supported.');

  // Drag and drop table
  $browsers = variable_get('brb_browsers', array());
  uasort($browsers, 'brb_cmp');

  // Remove excluded browsers
  $ok_browsers = array();
  foreach ($browsers as $browser) {
    if (!$browser->exclude) {
      $ok_browsers[] = array(
        "id" => 'brb-' . drupal_strtolower(str_replace(' ', '-', $browser->name)),
        "name" => $browser->name,
        'url' => $browser->url,
        "attributes" => array(
          'class' => 'brb-browser',
          'title' => $browser->name,
        ),
      );
    }
  }
  return theme('brb_widget', array(
    'body' => $body,
    'browsers' => $ok_browsers,
  ));
}

/**
* Function to order the array of browsers by weight.
*/
function brb_cmp($a, $b) {
  if ($a->weight == $b->weight) {
    return 0;
  }
  return $a->weight < $b->weight ? -1 : 1;
}

Functions

Namesort descending Description
brb_cmp Function to order the array of browsers by weight.
brb_help Display help and module information
brb_init Implementation of hook_init().
brb_menu Implementation of hook_menu().
brb_permission Implementation of hook_permission().
brb_preprocess_page Implementation of hook_preprocess_page().
brb_process_html Implementation of hook_preprocess_html().
brb_theme Implements hook_theme().
brb_widget Create the brb widget to display after the page is loaded.

Constants

Namesort descending Description
BRB_BODY_DEFAULT
BRB_IE_CONDITIONAL_DEFAULT
BRB_TITLE @file Display a message to let the user knows his/her browser is not supported. This message also offer to the user different browsers that can be used instead.