You are here

habitat_ui.module in Habitat 7

Same filename and directory in other branches
  1. 8 habitat_ui/habitat_ui.module

User Interface for Habitat.

File

habitat_ui/habitat_ui.module
View source
<?php

/**
 * @file
 * User Interface for Habitat.
 */

/**
 * Implements hook_menu().
 */
function habitat_ui_menu() {
  $items['admin/config/development/habitat'] = array(
    'title' => 'Habitat',
    'description' => 'Administer habitat settings.',
    'access arguments' => array(
      'administer habitat',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'habitat_ui_settings',
    ),
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function habitat_ui_permission() {
  return array(
    'administer habitat' => array(
      'title' => t('Administer habitat settings'),
    ),
  );
}

/**
 * Page callback.
 * Settings page.
 */
function habitat_ui_settings($form, $form_state) {
  $form['habitat_variable'] = array(
    '#type' => 'textfield',
    '#title' => t('Habitat variable name'),
    '#required' => TRUE,
    '#default_value' => variable_get('habitat_variable', 'fetcher_environment'),
    '#description' => t('The variable used in your settings.php files to indicate the habitat. This should be placed into settings.php like $conf[\'foo\'] = \'dev\'. Defaults to \'fetcher_environment\' which is added to settings.php when sites are built with the !fetcher system.', array(
      '!fetcher' => '<a href="http://drupal.org/project/fetcher">fetcher</a>',
    )),
  );
  $form['habitat_habitats'] = array(
    '#type' => 'textarea',
    '#title' => t('Habitats'),
    '#description' => t('The habitats to manage. Use machine_name conventions and enter one per line.'),
    '#required' => TRUE,
    '#default_value' => implode("\n", variable_get('habitat_habitats', array(
      'local',
      'dev',
      'test',
      'prod',
    ))),
  );
  $habitats = variable_get('habitat_habitats', array(
    'local',
    'dev',
    'test',
    'prod',
  ));
  foreach ($habitats as $habitat) {
    $form['habitat_enable_' . $habitat] = array(
      '#type' => 'textarea',
      '#title' => t('%habitat enabled modules', array(
        '%habitat' => $habitat,
      )),
      '#description' => t('The modules to force enable in this habitat. Use machine_name conventions and enter one per line.'),
      '#default_value' => implode("\n", variable_get('habitat_enable_' . $habitat, array())),
    );
    $form['habitat_disable_' . $habitat] = array(
      '#type' => 'textarea',
      '#title' => t('%habitat disabled modules', array(
        '%habitat' => $habitat,
      )),
      '#description' => t('The modules to force disable in this habitat. Use machine_name conventions and enter one per line.'),
      '#default_value' => implode("\n", variable_get('habitat_disable_' . $habitat, array())),
    );
  }
  $form['#submit'] = array(
    'habitat_ui_settings_submit',
  );
  return system_settings_form($form);
}
function habitat_ui_settings_submit($form, &$form_state) {
  $form_state['values']['habitat_habitats'] = explode("\n", $form_state['values']['habitat_habitats']);
  foreach ($form_state['values']['habitat_habitats'] as &$habitat) {
    $habitat = trim($habitat);
    $form_state['values'] = habitat_ui_settings_module_values($form_state['values'], 'habitat_enable_' . $habitat);
    $form_state['values'] = habitat_ui_settings_module_values($form_state['values'], 'habitat_disable_' . $habitat);
  }

  // Delete any stale settings.
  $variables = db_select('variable', 'v')
    ->fields('v', array(
    'name',
  ))
    ->condition('name', db_like("habitat_enable_") . '%', 'LIKE')
    ->execute()
    ->fetchCol();
  foreach ($variables as $variable) {
    if (!in_array(substr($variable, 15), $form_state['values']['habitat_habitats'])) {
      variable_del($variable);
    }
  }
  $variables = db_select('variable', 'v')
    ->fields('v', array(
    'name',
  ))
    ->condition('name', db_like("habitat_disable_") . '%', 'LIKE')
    ->execute()
    ->fetchCol();
  foreach ($variables as $variable) {
    if (!in_array(substr($variable, 16), $form_state['values']['habitat_habitats'])) {
      variable_del($variable);
    }
  }
}

/**
 * Prepare submit module values for saving.
 */
function habitat_ui_settings_module_values($values, $type) {
  if (!empty($values[$type])) {
    $values[$type] = explode("\n", $values[$type]);
    foreach ($values[$type] as &$module) {
      $module = trim($module);
    }
  }
  else {
    $values[$type] = array();
  }
  return $values;
}

Functions

Namesort descending Description
habitat_ui_menu Implements hook_menu().
habitat_ui_permission Implements hook_permission().
habitat_ui_settings Page callback. Settings page.
habitat_ui_settings_module_values Prepare submit module values for saving.
habitat_ui_settings_submit