You are here

environment_indicator.module in Environment Indicator 6

Adds a coloured strip to the side of the site informing the user which environment they are in (Development, Staging Production etc).

@author Tom Kirkpatrick (mrfelton), www.systemseed.com

File

environment_indicator.module
View source
<?php

/**
 * @file
 * Adds a coloured strip to the side of the site informing the user which
 * environment they are in (Development, Staging Production etc).
 *
 * @author Tom Kirkpatrick (mrfelton), www.systemseed.com
 */

/**
 * Implementation of hook_help().
 */
function environment_indicator_help($path, $arg) {
  switch ($path) {
    case 'admin/settings/environment_indicator':
      return t('The Environment Indicator adds a coloured strip to the side of the site informing you which environment you\'re currently in (Development, Staging Production etc). Use the settings below to customize the appearance of the indicator. You may override these settings in your settings.php file in each of your environments.');
    case 'admin/help#environment_indicator':
      $output = '<p>' . t('The Environment Indicator adds a coloured strip to the side of the site informing you which environment you\'re currently in (Development, Staging Production etc') . '</p>';
      $output .= '<p>' . t('The Environment Indicator <a href="@settings">settings page</a> allows you to modify some elements of the indicator\'s behavior and appearance. Since the appearance of the indicator is dependent on your site theme, substantial customisations require modifications to your site\'s theme and CSS files.', array(
        '@settings' => url('admin/settings/environment_indicator'),
      )) . '</p>';
      $output .= '<p>' . t('The Environment Indicator\'s visibility depends upon the permissions of the viewer. The <a href="@permissions">access environment indicator</a> permission must be enabled for a user role in order for users of that role to see the indicator.', array(
        '@permissions' => url('admin/user/permissions', array(
          'fragment' => 'module-environment_indicator',
        )),
      )) . '</p>';
      $output .= '<p>' . t('The settings for the Environment Indicator, such as the text to display and the color can be overridden for each of your specific environments in the site\'s settings.php file. This allows you to customise the indicator for each environment without needing to make any changes in the database. This means that the Environment Indicator will always display correctly when moving your site from development to staging to production. For example:') . '</p>';
      $output .= '<dl>';
      $output .= '<dt><em>environment_indicator_text</em></dt><dd>' . t('The text that will be displayed vertically down the indicator. e.g:<br/>$conf[\'environment_indicator_text\'] = \'STAGING SERVER\';') . '</dd></dt>';
      $output .= '<dt><em>environment_indicator_color</em></dt><dd>' . t('A valid css color. e.g:<br/>$conf[\'environment_indicator_color\'] = \'red\';') . '</dd></dt>';
      $output .= '<dt><em>environment_indicator_enabled</em></dt><dd>' . t('A boolean value indicating whether the Environment Indicator should be enabled or not. On your production environment, you should probably set this to FALSE. e.g:<br/>$conf[\'environment_indicator_enabled\'] = FALSE;') . '</dd></dt>';
      $output .= '</dl>';
      return $output;
  }
}

/**
 * Implementation of hook_perm().
 */
function environment_indicator_perm() {
  return array(
    'access environment indicator',
  );
}

/**
 * Implementation of hook_menu().
 */
function environment_indicator_menu() {
  $items['admin/settings/environment_indicator'] = array(
    'title' => 'Environment indicator',
    'description' => 'Adjust settings for the Environment Indicator.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'environment_indicator_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'environment_indicator.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_init().
 */
function environment_indicator_init() {
  if (!user_access('access environment indicator') || !variable_get('environment_indicator_enabled', 1)) {
    return;
  }

  // Do not show the indicator on select pages.
  $off_pages = variable_get('environment_indicator_suppress_pages', "imagecrop/*\n");
  $path = drupal_get_path_alias($_GET['q']);

  // Compare with the internal and path alias (if any).
  $page_match = drupal_match_path($path, $off_pages);
  if ($path != $_GET['q']) {
    $page_match = $page_match || drupal_match_path($_GET['q'], $off_pages);
  }
  if ($page_match) {
    return;
  }
  $path = drupal_get_path('module', 'environment_indicator');
  drupal_add_css($path . '/environment_indicator.css', 'module', 'all', FALSE);

  // Performance: Defer execution.
  drupal_add_js($path . '/environment_indicator.min.js', 'module', 'header', TRUE);

  //drupal_add_js($path .'/environment_indicator.js', 'module', 'header', TRUE);
  if ($setting = variable_get('environment_indicator_margin', 1)) {
    $settings['margin'] = check_plain($setting);
  }
  if ($setting = variable_get('environment_indicator_position', 'left')) {
    $settings['position'] = check_plain($setting);
  }
  if ($setting = variable_get('environment_indicator_text', 'ENVIRONMENT INDICATOR')) {
    $settings['text'] = check_plain($setting);
  }
  if ($setting = variable_get('environment_indicator_color', '#d00c0c')) {
    $settings['color'] = check_plain($setting);
  }
  drupal_add_js(array(
    'environment_indicator' => $settings,
  ), 'setting');
}

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

  // drupal_add_js() must only be invoked once.
  if (!empty($set) && $suppress === FALSE) {
    $suppress = TRUE;
    drupal_add_js(array(
      'environment_indicator' => array(
        'suppress' => 1,
      ),
    ), 'setting');
  }
  return $suppress;
}

Functions

Namesort descending Description
environment_indicator_help Implementation of hook_help().
environment_indicator_init Implementation of hook_init().
environment_indicator_menu Implementation of hook_menu().
environment_indicator_perm Implementation of hook_perm().
environment_indicator_suppress Suppress display of administration menu.