<?php
/**
 * @file
 * VS Theme feature module - main module file.
 *
 * @todo theme_get_setting() not supported on D7. Is there a workaround?
 */

/**
 * 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_theme_feature_info() {
  return array(
    'virtual_site_theme_feature' => array(
      'name' => t('Theme'),
      'description' => t('Select a different theme for this virtual site.'),
    ),
  );
}

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

  $form = array();

  $themes = system_rebuild_theme_data();
  foreach ($themes as $key => $data) {
    if ($data->status) {
      $options[$key] = $data->info['name'];
    }
  }

  $form['virtual_site_theme_feature'] = array(
    '#tree' => TRUE,
  );
  $form['virtual_site_theme_feature']['theme'] = array(
    '#title' => t('Theme'),
    '#description' => t('The theme to use for this site. After choosing or changing the theme, first save the form and then return to set any theme specific settings. You might need to clear the cache for changes to take effect!'),
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => isset($context['theme']) ? $context['theme'] : '',
  );

  if ($context['theme']) {
    if (!is_array($context['settings']) || !count($context['settings'])) {
      foreach (array('toggle_logo', 'toggle_name', 'toggle_slogan', 'toggle_node_user_picture', 'toggle_comment_user_picture', 'toggle_comment_user_verification', 'toggle_favicon', 'toggle_main_menu', 'toggle_secondary_menu') as $setting) {
        $context['settings'][$setting] = theme_get_setting($setting, $context['theme']);
      }
    }

    module_load_include('inc', 'system', 'system.admin');
    $form_state = array('values' => $context);
    $subform = system_theme_settings(NULL, $form_state, $context['theme']);
    foreach (array('toggle_logo', 'toggle_name', 'toggle_slogan', 'toggle_node_user_picture', 'toggle_comment_user_picture', 'toggle_comment_user_verification', 'toggle_favicon', 'toggle_main_menu', 'toggle_secondary_menu') as $setting) {
      $subform['theme_settings'][$setting]['#default_value'] = $context['settings'][$setting];
    }
    $form['virtual_site_theme_feature']['settings'] = $subform['theme_settings'];
  }

  return $form;
}

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

/**
 * Custom feature handler.
 * Modifies global variable $conf according to the context.
 *
 * We need a different approach here than with the other feature modules.
 * This callback, called from hook_init(), comes too late for the theme to be changed,
 * see http://api.drupal.org/api/drupal/includes--common.inc/function/_drupal_bootstrap_full/7.
 * The actual theme change is moved to virtual_site_theme_custom_theme().
 *
 * @param array $context
 *   Context of the virtual site.
 *
 * @see virtual_site_theme_custom_theme()
 */
function virtual_site_theme_feature($context) {
  global $conf;

  if (is_array($context['settings']) && count($context['settings'])) {
    $var = 'theme_'. $context['theme'] .'_settings';
    $conf[$var] = $context['settings'];
  }
}

/**
 * Implements hook_custom_theme().
 */
function virtual_site_theme_custom_theme() {
  if ($site = virtual_site_load_active()) {
    if (isset($site->features['virtual_site_theme_feature']['theme'])) {
      return $site->features['virtual_site_theme_feature']['theme'];
    }
  }
}
