<?php

/**
 * @file
 * Handles AdvAgg CSS compress installation and upgrade tasks.
 */

/**
 * Implements hook_requirements().
 */
function advagg_css_compress_requirements($phase) {
  $requirements = array();
  // Ensure translations don't break at install time.
  $t = get_t();

  // If not at runtime, return here.
  if ($phase !== 'runtime') {
    return $requirements;
  }

  // Make sure a compressor is being used.
  // @ignore sniffer_whitespace_openbracketspacing_openingwhitespace
  if ( variable_get('advagg_css_compressor', ADVAGG_CSS_COMPRESSOR) == 0
    && variable_get('advagg_css_compress_inline', ADVAGG_CSS_COMPRESS_INLINE) == 0
  ) {
    // Check all files.
    $file_settings = variable_get('advagg_css_compressor_file_settings', array());
    $compression_used = FALSE;
    foreach ($file_settings as $setting) {
      if (!empty($setting)) {
        $compression_used = TRUE;
        break;
      }
    }

    if (!$compression_used) {
      $config_path = advagg_admin_config_root_path();
      $requirements['advagg_css_compress_not_on'] = array(
        'title' => $t('AdvAgg CSS Compressor'),
        'severity' => REQUIREMENT_WARNING,
        'value' => $t('AdvAgg CSS Compression is disabled.'),
        'description' => $t('Go to the <a href="@settings">advagg css compress settings page</a> and select a compressor, or go to the <a href="@modules">modules page</a> and disable the "AdvAgg Compress CSS" module.', array(
          '@settings' => url($config_path . '/advagg/css-compress'),
          '@modules' => url('admin/modules', array(
            'fragment' => 'edit-modules-advanced-cssjs-aggregation',
          )),
        )),
      );
    }
  }
  return $requirements;
}

/**
 * Upgrade AdvAgg CSS Compress previous versions (6.x-1.x & 7.x-1.x) to 7.x-2.x.
 *
 * Implements hook_update_N().
 */
function advagg_css_compress_update_7200(&$sandbox) {
  // Bail if old DB Table does not exist.
  if (!db_table_exists('cache_advagg_css_compress_inline')) {
    return t('Nothing needed to happen in AdvAgg CSS Compress.');
  }

  // Remove all old advagg css compress variables.
  db_delete('variable')
    ->condition('name', 'advagg_css%compress%', 'LIKE')
    ->execute();

  // Remove old schema.
  db_drop_table('cache_advagg_css_compress_inline');

  return t('Upgraded AdvAgg CSS Compress to 7.x-2.x.');
}

/**
 * Change variable names so they are prefixed with the modules name.
 *
 * Implements hook_update_N().
 */
function advagg_css_compress_update_7201(&$sandbox) {
  // Rename advagg_css_inline_compressor to advagg_css_compress_inline.
  $old = variable_get('advagg_css_inline_compressor', NULL);
  if (!is_null($old)) {
    variable_del('advagg_css_inline_compressor');
  }
  if ($old !== ADVAGG_CSS_COMPRESS_INLINE) {
    variable_set('advagg_css_compress_inline', $old);
  }

  // Rename advagg_css_inline_compress_if_not_cacheable to
  // advagg_css_compress_inline_if_not_cacheable.
  $old = variable_get('advagg_css_inline_compress_if_not_cacheable', NULL);
  if (!is_null($old)) {
    variable_del('advagg_css_inline_compress_if_not_cacheable');
  }
  if ($old !== ADVAGG_CSS_COMPRESS_INLINE_IF_NOT_CACHEABLE) {
    variable_set('advagg_css_compress_inline_if_not_cacheable', $old);
  }
}
