<?php
// $Id: warnme.module,v 1.1.2.2 2009/03/04 22:36:05 lyricnz Exp $ 

/**
 * @file
 * Allow the configuration of a list of modules which are undesirable on a
 * production site, and therefor produce warnings for site adminstrators.
 */


/**
 * Implementation of hook_menu().
 */
function warnme_menu() {
  $items = array();
  $items['admin/settings/warnme'] = array(
    'title' => 'Warn Me',
    'description' => 'Warn site administrators about development modules being enabled in production',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('warnme_admin_settings'),
    'access arguments' => array('administer site configuration'),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Configuration settings form for Warn Me module.
 */
function warnme_admin_settings(&$form_state) {
  $form = array();

  // TODO: offer to send email to administrator?

  $form['warnme_mode'] = array(
    '#type' => 'radios',
    '#title' => t('Operation Mode'),
    '#description' => t('Select the way in which Warn Me module will behave when modules listed below are found to be enabled.'),
    '#options' => array(
      'warn' => t('Warn me if selected modules are enabled'),
      'ignore' => t('Ignore selected modules (dev)'),
      'disable' => t('Disable selected modules if found'),
      'enable' => t('Enable selected modules if found'),
    ),
    '#default_value' => variable_get('warnme_mode', 'warn'),
  );

  $form['warnme_modules'] = array(
    '#type' => 'textarea',
    '#title' => t('Modules'),
    '#default_value' => _warnme_modules_list(),
    '#description' => t('List the modules you want to warn about, one per line. This is the machine-readable name, like "admin_menu".  Having modules listed here that are not enabled on your site is NOT an error.'),
    '#rows' => 10,
  );

  // TODO: provide checkboxes for all modules currently enabled on site, with
  // the current ones selected.  Maybe jquery update the textarea?

  return system_settings_form($form);
}

/**
 * Implementation of hook_init().
 * Show site administrators a warning if any of the modules configured in the
 * settings page are enabled on a production site.
 */
function warnme_init() {
  global $conf;
  if (isset($conf['warnme_mode'])) {
  	  $mode = $conf['warnme_mode'];
  } else {
  	  $mode = variable_get('warnme_mode', 'warn');
  }
  
  if (!user_access('administer site configuration') || $mode == 'ignore') {
    return;
  }

  $loaded_modules = module_list();
  $check_modules = explode("\n", _warnme_modules_list());

  // TODO: avoid duplicate messages on redirect.
  
  // Create a list of any modules that should not be enabled.
  $module_list = array();
  foreach ($check_modules as $module) {
    $module = trim($module);
    if ($mode != 'enable') {
	    if (array_key_exists($module, $loaded_modules)) {
      		$module_list[] = $module;
      		drupal_set_message(
          		t('Warning: Module %module is enabled in production!', array('%module' => $module, )),
          		'warning');
		}
		
	} elseif ($mode == 'enable') {
		if (!array_key_exists($module, $loaded_modules)) {
    		$module_list[] = $module;
		}
	}
  }

  // Take action on this list.
  if (count($module_list)) {
  	if ($mode == 'enable') {
  		drupal_set_message(t('Enabling modules: !modules', array('!modules' => join(',', $module_list))), 'warning');
  		module_enable($module_list);
	} elseif ($mode == 'disable') {
      drupal_set_message(t('Disabling modules: !modules', array('!modules' => join(',', $module_list))), 'warning');
      module_disable($module_list);
    }
    drupal_set_message(
      t(
        '!modlink | !warnmelink',
        array(
          '!modlink' => l('Configure Modules', 'admin/build/modules'),
          '!warnmelink' => l('Configure Warn Me', 'admin/settings/warnme'),
        )
      ),
      'warning'
    );
  }
}

/**
 * Get a list of modules to warn about.
 * @return string a newline-seperated list of modules currently selected for
 *                warning
 */
function _warnme_modules_list() {
  $modules = array('api', 'coder', 'devel', 'simpletest', 'devel_themer');
  return variable_get('warnme_modules', implode("\n", $modules));
}