You are here

environment_indicator.install in Environment Indicator 7.2

Install implementation file.

File

environment_indicator.install
View source
<?php

/**
 * @file
 * Install implementation file.
 */

/**
 * Implements hook_schema().
 */
function environment_indicator_schema() {
  $schema['environment_indicator_environment'] = array(
    'description' => 'Table storing preset definitions.',
    'export' => array(
      'key' => 'machine',
      'key name' => 'Machine name',
      'primary key' => 'envid',
      // Exports will be defined as $environment.
      'identifier' => 'environment',
      // Function hook name.
      'default hook' => 'default_environment_indicator_environment',
      'api' => array(
        'owner' => 'environment_indicator',
        // Base name for api include files.
        'api' => 'default_environment_indicator_environments',
        'minimum_version' => 1,
        'current_version' => 1,
      ),
    ),
    'fields' => array(
      'machine' => array(
        'type' => 'varchar',
        'length' => '32',
        'description' => 'Unique ID for environments.',
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => '255',
        'description' => 'Name for the environments.',
      ),
      'envid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Primary ID field for the table. Not used for anything except internal lookups.',
        // Do not export database-only keys.
        'no export' => TRUE,
      ),
      'regexurl' => array(
        'type' => 'varchar',
        'length' => '255',
        'description' => 'A regular expression to match against the url.',
      ),
      'settings' => array(
        'type' => 'text',
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'Serialized array with the configuration for the environment.',
      ),
    ),
    'primary key' => array(
      'envid',
    ),
    'unique keys' => array(
      'name' => array(
        'machine',
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function environment_indicator_uninstall() {
  variable_del('environment_indicator_suppress_pages');
  variable_del('environment_indicator_integration');
  variable_del('environment_indicator_git_support');

  // Delete environment_indicator_remote_release.*
  $query = "SELECT name FROM {variable} WHERE name LIKE 'environment_indicator_remote_release.%'";
  $results = db_query($query);
  foreach ($results as $row) {

    // Use variable_del to allow appropriate hooks to run.
    variable_del($row->name);
  }
}

/**
 * Implements hook_requirements().
 */
function environment_indicator_requirements($phase) {
  $requirements = array();

  // Ensure translations don't break at install time.
  $t = get_t();

  // Report Drupal version.
  if ($phase == 'runtime') {
    if (_environment_indicator_has_old_vars()) {
      $requirements['environment_indicator'] = array(
        'title' => $t('Environment indicator'),
        'value' => $t('You are using old environment indicator variables. Please check the !help on how to upgrade your variables for the new release, and then remove the old ones.', array(
          '!help' => l(t('help pages'), 'admin/help/environment_indicator'),
        )),
        'severity' => REQUIREMENT_ERROR,
      );
    }
    if (!empty($_ENV['AH_SITE_ENVIRONMENT']) && variable_get('environment_indicator_git_support', TRUE)) {
      $severity = REQUIREMENT_WARNING;
      $env = $_ENV['AH_SITE_ENVIRONMENT'];
      if (environment_indicator_ah_valid_post_deploy_hook($env)) {

        // If there is no value in the release variable it can mean that the
        // hook never run. Tell the user to check execute permisions on the
        // script.
        $release = variable_get('environment_indicator_remote_release.' . $env, NULL);
        if ($release) {
          $message = $t('The Acquia Cloud hook is installed and reports release: ' . $release);
          $severity = REQUIREMENT_INFO;
        }
        else {
          $message = $t('The Acquia Cloud hook is installed but there is no information about the release. This is normal if you never deployed your code. Otherwise check if the hook script has the execution permisions.');
        }
      }
      else {
        $message = $t('Missing Acquia Cloud hook for environment indicator integration. Please look at the <code>samples/environment-indicator.sh</code> folder and copy it to your <code>hooks/[your-environment]/post-code-deploy</code> directory. See the README.txt for more information.');
      }
      $requirements['environment_indicator_ah'] = array(
        'title' => $t('Environment indicator'),
        'value' => $message,
        'severity' => $severity,
      );
    }
  }
  return $requirements;
}

/**
 * Add table when updating from 1.x.
 */
function environment_indicator_update_7200(&$sandbox) {
  if (!db_table_exists('environment_indicator_environment')) {
    $schema = environment_indicator_schema();
    db_create_table('environment_indicator_environment', $schema['environment_indicator_environment']);
  }
}

/**
 * Add the default text color for existing environments.
 */
function environment_indicator_update_7201(&$sandbox) {
  module_load_include('inc', 'ctools', 'includes/export');
  $environments = ctools_export_crud_load_all('environment_indicator_environment');
  foreach ($environments as $machine => $environment) {
    if (!isset($environment->settings['text_color'])) {
      $environment->settings['text_color'] = '#ffffff';
      ctools_export_crud_save('environment_indicator_environment', $environment);
    }
  }
}

/**
 * Migrate the old variables to the default environment.
 */
function environment_indicator_update_7202(&$sandbox) {
  module_load_include('inc', 'ctools', 'includes/export');
  $default_environment = ctools_export_crud_load('environment_indicator_environment', 'default_environment');

  // We want to load the environment variables directly from the database, not
  // from settings.php. Using variable_get() is thus not an option, since it
  // uses the values from settings.php to override those in the database.
  $result = db_query("SELECT * FROM {variable} WHERE name LIKE 'environment_indicator_%'");
  $updated = FALSE;
  if ($result) {
    while ($row = $result
      ->fetchAssoc()) {
      switch ($row['name']) {
        case 'environment_indicator_color':
          $default_environment->settings['color'] = unserialize($row['value']);
          variable_del($row['name']);
          $updated = TRUE;
          break;
        case 'environment_indicator_text':
          $default_environment->name = unserialize($row['value']);
          variable_del($row['name']);
          $updated = TRUE;
          break;
        case 'environment_indicator_enabled':
        case 'environment_indicator_margin':
        case 'environment_indicator_position':

          // This variable is no longer used. Remove it.
          variable_del($row['name']);
          break;
        case 'environment_indicator_suppress_pages':

          // This variable is still used, keep it.
          break;
      }
    }
  }

  // Only save if the default environment was not edited yet. The old variables
  // have been removed nonetheless.
  if (!($defaut_environment->export_type & EXPORT_IN_DATABASE) && $default_environment->export_module == 'environment_indicator' && $updated) {
    ctools_export_crud_save('environment_indicator_environment', $default_environment);
    return t('Your default environment was updated with your previous configuration.');
  }
}

Functions

Namesort descending Description
environment_indicator_requirements Implements hook_requirements().
environment_indicator_schema Implements hook_schema().
environment_indicator_uninstall Implements hook_uninstall().
environment_indicator_update_7200 Add table when updating from 1.x.
environment_indicator_update_7201 Add the default text color for existing environments.
environment_indicator_update_7202 Migrate the old variables to the default environment.