Hi, I do development on local machine and than move data do public server. Is is very useful if it can autodetect your environment. Months ago I wrote small script to do that and now tried to integrate into this module. I am not a php programmer so sorry for bugs. But is seems to work fine.
How does it work: detect if you are at IP 127.x.x.x. If so => you are local. If not => you are public.
I added setting so you can set text and color for both.
Maybe someone will find is useful.

CommentFileSizeAuthor
environment_indicator.zip2.57 KBagir

Comments

agir’s picture

Here is the code:

 /**
 * Module settings form.
 */
function environment_indicator_settings() {
  $form['environment_indicator_enabled'] = array(
    '#type' => 'radios',
    '#title' => t('Status'),
    '#default_value' => variable_get('environment_indicator_enabled', 1),
    '#options' => array(t('Disabled'), t('Enabled')),
    '#description' => t('Should the Environment Indicator display?'),
  );
  $form['environment_indicator_position'] = array(
    '#type' => 'select',
    '#title' => t('Position'),
    '#description' => t('Choose a position for the environment indicator.'),
    '#options' => array(
      'left' => t('Left'),
      'right' => t('Right'),
    ),
    '#default_value' => variable_get('environment_indicator_position', 'left'),
  );
  $form['environment_indicator_margin'] = array(
    '#type' => 'checkbox',
    '#title' => t('Adjust margin'),
    '#default_value' => variable_get('environment_indicator_margin', 1),
    '#description' => t('If enabled, the site output is shifted to the left or right approximately 30 pixels to display the environment indicator. If disabled, some absolute- or fixed-positioned page elements may be covered by the environment indicator strip.'),
  );
  $form['environment_indicator_textlocal'] = array(
    '#type' => 'textfield',
    '#title' => t('Text to display in LOCAL'),
    '#default_value' => variable_get('environment_indicator_textlocal', 'LOCAL SERVER'),
    '#description' => t('Text to display in the environment indicator. Override this for each environment in settings.php.'),
  );
  $form['environment_indicator_textpublic'] = array(
    '#type' => 'textfield',
    '#title' => t('Text to display in PUBLIC'),
    '#default_value' => variable_get('environment_indicator_textpublic', 'PUBLIC SERVER'),
    '#description' => t('Text to display in the environment indicator. Override this for each environment in settings.php.'),
  );
  $form['environment_indicator_colorlocal'] = array(
    '#type' => 'textfield',
    '#title' => t('Colour in LOCAL'),
    '#default_value' => variable_get('environment_indicator_colorlocal', '#d00c0c'),
    '#description' => t('The colour of the environment indicator. Override this for each environment in settings.php.'),
    '#size' => 7,
  );
  $form['environment_indicator_colorpublic'] = array(
    '#type' => 'textfield',
    '#title' => t('Colour in PUBLIC'),
    '#default_value' => variable_get('environment_indicator_colorpublic', '#009700'),
    '#description' => t('The colour of the environment indicator. Override this for each environment in settings.php.'),
    '#size' => 7,
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_init().
 */
function environment_indicator_init() {
  if (!user_access('access environment indicator') || environment_indicator_suppress(FALSE)) {
    return;
  }

  global $user, $language;

  $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.js', 'module', 'header', TRUE);

  if ($setting = variable_get('environment_indicator_margin', 1)) {
    $settings['margin'] = $setting;
  }
  if ($setting = variable_get('environment_indicator_position', 'left')) {
    $settings['position'] = $setting;
  }
  if ($setting = variable_get('environment_indicator_text', 'ENVIRONMENT INDICATOR')) {
    $settings['text'] = $setting;
  }
  if ($setting = variable_get('environment_indicator_color', '#d00c0c')) {
    $settings['color'] = $setting;
  } 

  // AUTODETECT LOCAL machine 
  if (isset($_SERVER['SERVER_ADDR']) || isset($_SERVER['LOCAL_ADDR'])) { 
    $addr = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR'];
    $addr_e = explode('.', $addr);
    $addr_e[0] == '127' ? $env = "LOCAL" : $env = "PUBLIC";
  }

  if ($env=='LOCAL') {
    $settings['text'] = variable_get('environment_indicator_textlocal', 'LOCAL SERVER');
    $settings['color'] = variable_get('environment_indicator_colorlocal', '#d00c0c');
  }
  elseif ($env=='PUBLIC'){
    $settings['text'] = variable_get('environment_indicator_textpublic', 'PUBLIC SERVER');
    $settings['color'] = variable_get('environment_indicator_colorpublic', '#009700');
  }
 
  drupal_add_js(array('environment_indicator' => $settings), 'setting');
}
agir’s picture

Please, can you correct misspell in title? Autodetect enviriment > Autodetect environment
Thx

mrfelton’s picture

Title: Autodetect enviriment » Autodetect environment
Status: Active » Closed (won't fix)

To be honest I don't like the idea of having that hardcoded into the module. The module is supposed to be simple, flexible and should support any number of environments, not just local and remote. You can already do this by overriding the settings in settings.php - since this file is unique for each of your installations/servers/environments.

I don't see the need for automatic detection - you simply configure the module once for each of your installations, using settings.php, and then forget about it.

agir’s picture

OK I get your point. Never mind. Maybe someone will find this useful.
For me it is good because I use only local and public environment. And I want to have only one settings.php.

Thanks

mrfelton’s picture

How can you only have one settings.php if you have two servers?

ps. thanks for submitting your code anyway. I do appreciate it, but just don't feel that it should be a part of the module for the reasons I outlined above. Please don't hesitate to offer any more suggestions though!

agir’s picture

How can you only have one settings.php if you have two servers?

Because I have same db name and password on both. I is then easier to move between local and public. I just copy everything and don't have to remember not to overwrite settings.php

To the submitting: ok ;-)