<?php
/**
 * @file
 * VS Head feature module - main module file.
 */

/**
 * Custom hook_feature_info() to return details about this feature module.
 *
 * @return array
 *   Array with the following keys:
 *   - name: human readable name of the feature module,
 *   - description: human readable description of the feature module.
 */
function virtual_site_head_feature_info() {
  return array(
    'virtual_site_head_feature' => array(
      'name' => t('Header'),
      'description' => t('Add styles, scripts and other stuff to <code>&lt;head /&gt;</code>'),
    ),
  );
}


/**
 * Implements hook_preprocess_hook().
 */
function virtual_site_head_preprocess_page(&$variables) {
  if ($site = virtual_site_load_active()) {
    // Insert inline CSS here to make sure it's the last CSS.
    if (isset($site->features['virtual_site_head_feature']['css_inline'])) {
      if (!isset($variables['styles'])) {
        $variables['styles'] = '';
      }
      $variables['styles'] .= "\n<style type=\"text/css\">\n".$site->features['virtual_site_head_feature']['css_inline']."\n</style>\n";
    }
  }
}

/**
 * Implements hook_footer().
 */
function virtual_site_head_footer($main = 0) {
  if ($site = virtual_site_load_active()) {
    if (isset($site->features['virtual_site_head_feature']['html_footer'])) {
      return $site->features['virtual_site_head_feature']['html_footer'];
    }
  }
}

/**
 * Form constructor for the feature module settings form.
 *
 * @see virtual_site_menu()
 * @see virtual_site_head_feature_validate()
 *
 * @ingroup forms
 */
function virtual_site_head_feature_form($context) {
  $form = array();

  $form['virtual_site_head_feature'] = array(
    '#tree' => TRUE,
  );
  $form['virtual_site_head_feature']['html_head'] = array(
    '#type' => 'textarea',
    '#title' => t('HTML in header'),
    '#description' => t('Enter HTML code to include in the <code>&lt;head /&gt;</code>.'),
    '#default_value' => isset($context['html_head']) ? $context['html_head'] : '',
  );
  $form['virtual_site_head_feature']['html_footer'] = array(
    '#type' => 'textarea',
    '#title' => t('HTML in closure'),
    '#description' => t('Enter HTML code to include just before <code>&lt;/body&gt;</code>.'),
    '#default_value' => isset($context['html_footer']) ? $context['html_footer'] : '',
  );
  $form['virtual_site_head_feature']['js_include'] = array(
    '#type' => 'textarea',
    '#title' => t('Referenced JavaScript files'),
    '#description' => t('Enter URLs of JavaScript files (one on each line) to refer to.'),
    '#default_value' => isset($context['js_include']) ? $context['js_include'] : '',
  );
  $form['virtual_site_head_feature']['js_inline'] = array(
    '#type' => 'textarea',
    '#title' => t('Inline JavaScript'),
    '#description' => t('Enter JavaScript code to include inline.'),
    '#default_value' => isset($context['js_inline']) ? $context['js_inline'] : '',
  );
  $form['virtual_site_head_feature']['css_include'] = array(
    '#type' => 'textarea',
    '#title' => t('Referenced CSS files'),
    '#description' => t('Enter URLs of CSS files (one on each line) to refer to.'),
    '#default_value' => isset($context['css_include']) ? $context['css_include'] : '',
  );
  $form['virtual_site_head_feature']['css_inline'] = array(
    '#type' => 'textarea',
    '#title' => t('Inline CSS'),
    '#description' => t('Enter CSS code to include inline.'),
    '#default_value' => isset($context['css_inline']) ? $context['css_inline'] : '',
  );

  return $form;
}

/**
 * Form validation handler for virtual_site_common_feature_form().
 */
function virtual_site_head_feature_validate($form, &$form_state) {
}

/**
 * Custom feature handler.
 * Adds various header elements.
 *
 * @param array $context
 *   Context of the virtual site.
 */
function virtual_site_head_feature($context) {
  if (trim($context['html_head'])) {
    drupal_set_html_head($context['html_head']);
  }

  if (trim($context['js_inline'])) {
    drupal_add_js($context['js_inline'], 'inline');
  }

  if (trim($context['js_include'])) {
    $files = preg_split('/[\n\r]+/s', $context['js_include']);
    if (count($files)) {
      foreach ($files as $file) {
        drupal_add_js(check_url($file), 'module');
      }
    }
  }

  if (trim($context['css_include'])) {
    $files = preg_split('/[\n\r]+/s', $context['css_include']);
    if (count($files)) {
      foreach ($files as $file) {
        drupal_add_css(check_url($file), 'theme');
      }
    }
  }
}
