<?php

/**
 * @file
 * Advanced aggregation css compression module.
 *
 */

/**
 * Default value to see if CSSTidy will preserve CSS.
 */
define('ADVAGG_CSS_COMPRESS_PRESERVE_CSS', TRUE);

/**
 * Default value to see if this will compress aggregated files.
 */
define('ADVAGG_CSS_COMPRESS_AGG_FILES', TRUE);

/**
 * Default value to see if this will compress inline css.
 */
define('ADVAGG_CSS_COMPRESS_INLINE', TRUE);

/**
 * Default value to see if this will cache the compressed inline css.
 */
define('ADVAGG_CSS_COMPRESS_INLINE_CACHE', TRUE);

/**
 * Default value for which css compression library to use.
 */
define('ADVAGG_CSS_COMPRESSOR', 2);

/**
 * Default value for which css compression library to use.
 */
define('ADVAGG_CSS_COMPRESS_COMPRESSOR_LEVEL', 'sane');


/**
 * Implementation of hook_menu
 */
function advagg_css_compress_menu() {
  $items = array();
  $file_path = drupal_get_path('module', 'advagg_css_compress');

  $items['admin/settings/advagg/css-compress'] = array(
    'title' => 'CSS Compression',
    'description' => 'Adjust CSS Compression settings.',
    'page callback' => 'advagg_css_compress_admin_page',
    'type' => MENU_LOCAL_TASK,
    'access arguments' => array('administer site configuration'),
    'file path' => $file_path,
    'file' => 'advagg_css_compress.admin.inc',
    'weight' => 10,
  );

  return $items;
}

/**
 * Implement hook_advagg_css_alter.
 */
function advagg_css_compress_advagg_css_alter(&$contents, $files, $bundle_md5) {
  if (!variable_get('advagg_css_compress_agg_files', ADVAGG_CSS_COMPRESS_AGG_FILES)) {
    return;
  }

  $compressor = variable_get('advagg_css_compressor', ADVAGG_CSS_COMPRESSOR);
  if ($compressor == 0) {
    advagg_css_compress_css_tidy($contents);
  }
  elseif ($compressor == 1) {
    advagg_css_compress_css_compressor($contents);
  }
  elseif ($compressor == 2) {
    advagg_css_compress_yui_cssmin($contents);
  }
}

/**
 * Implement hook_advagg_css_inline_alter.
 */
function advagg_css_compress_advagg_css_inline_alter(&$contents) {
  if (!variable_get('advagg_css_compress_inline', ADVAGG_CSS_COMPRESS_INLINE)) {
    return;
  }
  $compressor = variable_get('advagg_css_compressor', ADVAGG_CSS_COMPRESSOR);

  // If using a cache, try to get the contents of it.
  if (variable_get('advagg_css_compress_inline_cache', ADVAGG_CSS_COMPRESS_INLINE_CACHE)) {
    $key = md5($contents) . $compressor;
    $table = 'cache_advagg_css_compress_inline';
    $data = cache_get($key, $table);
    if (!empty($data->data)) {
      $contents = $data->data;
      return;
    }
  }

  if ($compressor == 0) {
    advagg_css_compress_css_tidy($contents);
  }
  if ($compressor == 1) {
    advagg_css_compress_css_compressor($contents);
  }

  // If using a cache set it.
  if (isset($key)) {
    cache_set($key, $contents, $table, CACHE_TEMPORARY);
  }
}

/**
 * Use the CSS Tidy library to compress the CSS.
 *
 * TODO have set_cfg be fully configurable from GUI.
 */
function advagg_css_compress_css_tidy(&$contents) {
  // Initialize CSSTidy.
  $filename = drupal_get_path('module', 'advagg_css_compress') . '/csstidy/class.csstidy.inc';
  include_once($filename);
  $css = new csstidy();

  // Try to allocate enough time to run CSSTidy.
  if (function_exists('set_time_limit')) {
    @set_time_limit(240);
  }

  // Set configuration.
  $css->set_cfg('preserve_css', variable_get('advagg_css_compress_preserve_css', ADVAGG_CSS_COMPRESS_PRESERVE_CSS));
  $css->set_cfg('remove_last_;', TRUE);
  $css->set_cfg('merge_selectors', TRUE);
  $css->set_cfg('optimise_shorthands', TRUE);
  $css->set_cfg('silent', TRUE);
  $css->set_cfg('compress_colors', TRUE);
  $css->set_cfg('sort_selectors', FALSE);
  $css->set_cfg('sort_properties', FALSE);
  $css->set_cfg('discard_invalid_properties', FALSE);
  $css->set_cfg('timestamp', FALSE);
  $css->load_template("highest_compression");

  // Compress CSS.
  $css->parse($contents);
  $contents = $css->print->plain();
}

/**
 * Use the CSS Compressor library to compress the CSS.
 *
 * TODO have compression level be selectable from GUI.
 */
function advagg_css_compress_css_compressor(&$contents) {
  // Initialize CSSTidy.
  $filename = drupal_get_path('module', 'advagg_css_compress') . '/css-compressor-3.x/src/CSSCompression.inc';
  include_once($filename);

  $CSSC = new CSSCompression(variable_get('advagg_css_compress_compressor_level', ADVAGG_CSS_COMPRESS_COMPRESSOR_LEVEL));
  $contents = $CSSC->compress($contents);
}

/**
 * Use the CSSmin library from YUI to compress the CSS.
 */
function advagg_css_compress_yui_cssmin(&$contents) {
  // Include CSS_Compressor from Stephen Clay.
//   $filename = drupal_get_path('module', 'advagg_css_compress') . '/yui/Compressor.inc';
//   include_once($filename);
//   $contents = preg_replace('/@charset[^;]+;\\s*/', '', $contents);
//   $contents = Minify_CSS_Compressor::process($contents);

  // Include CSSmin from YUI.
  $filename = drupal_get_path('module', 'advagg_css_compress') . '/yui/CSSMin.inc';
  include_once($filename);
  $cssmin = new CSSmin();
  // Create a new line after 4k of text.
  $contents = trim($cssmin->run($contents, 4096));
}


/**
 * Implementation of hook_flush_caches().
 */
function advagg_css_compress_flush_caches() {
  return array('cache_advagg_css_compress_inline');
}
