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

/**
 * Custom hook_feature_info() to return details about this feature module.
 *
 * @return
 *   Array with the following keys:
 *   - name: human readable name of the feature module,
 *   - description: human readable description of the feature module.
 */
function virtual_site_information_feature_info() {
  return array(
    'virtual_site_information_feature' => array(
      'name' => t('Site information'),
      'description' => t('Set site information settings for your virtual site, like the name and frontpage.'),
    ),
  );
}

/**
 * Form constructor for the feature module settings form.
 *
 * @see virtual_site_menu()
 * @see virtual_site_information_feature_validate()
 *
 * @ingroup forms
 */
function virtual_site_information_feature_form($context) {
  global $conf;

  $form = array();

  // Temporarily replace the current settings by the ones of the virtual site.
  $orig = $conf;
  $conf = array_merge($conf, (array) $context);

  // Retrieve theme settings form
  module_load_include('inc', 'system', 'system.admin');
  $form['virtual_site_information_feature'] = system_site_information_settings();
  $form['virtual_site_information_feature']['#tree'] = TRUE;

  // Restore current settings
  $conf = $orig;

  // Unset unwanted form parts
  unset($form['virtual_site_information_feature']['actions']);
  unset($form['virtual_site_information_feature']['#submit']);
  unset($form['virtual_site_information_feature']['#validate']);
  unset($form['virtual_site_information_feature']['#theme']);

  return $form;
}

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

/**
 * Custom feature handler.
 * Modifies global variable $conf according to the context.
 *
 * @param $context
 *   Context of the virtual site.
 */
function virtual_site_information_feature($context) {
  global $conf;

  if (is_array($context) && count($context)) {
    $old_front_path = drupal_get_normal_path(variable_get('site_frontpage', 'node'));

    // Overwrite front page
    if (isset($context['front_page']) && isset($context['front_page']['site_frontpage']) && $old_front_path == $_GET['q'] && $old_front_path != $context['front_page']['site_frontpage']) {
      $_GET['q'] = drupal_get_normal_path($context['front_page']['site_frontpage']);
    }

    // Others except site object are all just variables
    unset($context['site']);
    foreach ($context as $group) {
      foreach ($group as $key => $value) {
        $conf[$key] = $value;
      }
    }
  }
}
