<?php
/**
 * @file
 * VS Common settings 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_common_feature_info() {
  return array(
    'virtual_site_common_feature' => array(
      'name' => t('Common settings'),
      'description' => t('Set common settings for your virtual site, like the primary and secondary menu and language.'),
    ),
  );
}

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

  $form['virtual_site_common_feature'] = array(
    '#tree' => TRUE,
  );

  // Primary and secondary menu.
  $menu_options = menu_get_menus();
  $primary = isset($context['menu_primary_links_source']) ? $context['menu_primary_links_source'] : variable_get('menu_primary_links_source', 'primary-links');
  $primary_options = array_merge($menu_options, array('' => t('No primary links')));
  $form['virtual_site_common_feature']['menu_primary_links_source'] = array(
    '#type' => 'select',
    '#title' => t('Source for the primary links'),
    '#default_value' => $primary,
    '#options' => $primary_options,
    '#description' => t('Select what should be displayed as the primary links.'),
  );
  $secondary = isset($context['menu_secondary_links_source']) ? $context['menu_secondary_links_source'] : variable_get('menu_secondary_links_source', 'secondary-links');
  $secondary_options = array_merge($menu_options, array('' => t('No secondary links')));
  $form['virtual_site_common_feature']['menu_secondary_links_source'] = array(
    '#type' => 'select',
    '#title' => t('Source for the secondary links'),
    '#default_value' => $secondary,
    '#options' => $secondary_options,
    '#description' => t('Select what should be displayed as the secondary links. You can choose the same menu for secondary links as for primary links. If you do this, the children of the active primary menu link will be displayed as secondary links.'),
  );

  // Language.
  $languages = language_list('enabled');
  $languages = $languages[1];

  foreach ($languages as $code => $language) {
    $options[$code] = check_plain($language->name);
  }

  $form['virtual_site_common_feature']['language'] = array(
    '#type' => 'select',
    '#title' => t('Language'),
    '#default_value' => isset($context['language']) ? $context['language'] : language_default('language'),
    '#options' => $options,
    '#description' => t('Select in what (enabled) language the site should be presented.'),
  );

  // Base URL.
  $form['virtual_site_common_feature']['base'] = array(
    '#type' => 'textfield',
    '#title' => t('Base URL'),
    '#default_value' => isset($context['base']) ? $context['base'] : '',
    '#description' => t('Force a certain base URL. For example force <code>http://example.com</code> for all visitors coming in on <code>http://www.example.com</code>. Of course it would be logical to make sure that this virtual site responds to both, otherwise it would just be a cheap redirection.'),
  );

  return $form;
}

/**
 * Form validation handler for virtual_site_common_feature_form().
 */
function virtual_site_common_feature_validate($form, &$form_state) {
  if (!empty($form_state['values']['virtual_site_common_feature']['base'])) {
    $url_parts = parse_url($form_state['values']['virtual_site_common_feature']['base']);

    if (!is_array($url_parts) || !isset($url_parts['host'])) {
      form_set_error('virtual_site_common_feature][base', t('Base URL is malformed.'));
    }
    else {
      $url = isset($url_parts['scheme']) ? $url_parts['scheme'] : 'http';
      $url .= '://'.$url_parts['host'];
      $path = isset($url_parts['path']) ? $url_parts['path'] : '';
      $url .= (drupal_substr($path, -1) == '/') ? drupal_substr($path, 0, drupal_strlen($path) - 1) : $path;
      form_set_value($form['features']['virtual_site_common_feature']['virtual_site_common_feature']['base'], $url, $form_state);
    }
  }
}

/**
 * Custom feature handler.
 * Modifies global variables $conf and $language according to the context.
 *
 * @param array $context
 *   Context of the virtual site.
 */
function virtual_site_common_feature($context) {
  global $conf, $language, $base_url;

  if (is_array($context) && count($context)) {
    // Base URL.
    if ($context['base'] && $base_url != $context['base']) {
      header("HTTP/1.1 301 Moved Permanently");
      header("Location: ". $context['base'] . request_uri());
      header("Connection: close");
      exit;
    }

    // Settings by variable.
    $conf = array_merge($conf, $context);

    // Language.
    $languages = language_list('enabled');
    $languages = $languages[1];

    if (isset($languages[$context['language']])) {
      $language = $languages[$context['language']];
    }
  }
}
