You are here

function language_selection_page_init in Language Selection Page 6

Implements hook_init().

File

./language_selection_page.module, line 40
The main module page of language_selection_page.

Code

function language_selection_page_init() {
  global $language;
  if (variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE) != 2) {
    return;
  }
  module_load_include('inc', 'language_selection_page', 'includes/language_selection_page.helpers');
  $lang_count = _language_selection_page_check_language_count();
  if ($lang_count['db'] < 2) {
    return;
  }
  $black_paths = variable_get('language_selection_page_blacklisted_paths', array());
  if ((bool) drupal_match_path($_GET['q'], implode("\r\n", $black_paths))) {
    return;
  }
  if ($_GET['q'] == 'language_selection') {
    return;
  }
  if (!isset($_SERVER['SERVER_ADDR'])) {
    return;
  }
  drupal_init_language();
  $url_parsed = _language_selection_page_parse_url_custom();
  $lang_from_url_argument = _language_selection_page_detect_lang_from_url_argument();
  $lang_from_url = _language_selection_page_detect_lang_from_url();
  $lang_from_cookie = _language_selection_page_detect_lang_from_cookie();
  $language = _language_selection_page_resolve_lang($lang_from_url_argument, $lang_from_url, $lang_from_cookie);
  if ($lang_from_url || $lang_from_url_argument) {
    $cookie = variable_get('language_selection_page_use_language_cookie', 8) == 16 ? $language->language : NULL;
    setcookie(LANGUAGE_SELECTION_PAGE_LANGUAGE_COOKIE_KEY, $cookie, time() + variable_get('language_selection_page_cookie_lifetime', 60 * 60 * 24 * 30), '/');
    return;
  }
  $query = array();
  $query['destination'] = $_GET['q'];
  $query = array_merge((array) $query, (array) $url_parsed['query']);
  unset($query['q']);
  $destination = NULL;

  // It may be hard to understand at first sight but this way of working
  // reduce the number of level of 'if-else' and improve code readability.
  $behavior = (int) variable_get('language_selection_page_use_language_cookie', 8) + (int) variable_get('language_selection_page_enable', 2);

  /***************************************************************
   * $behavior explanations                                       *
   ****************************************************************
   * 9 = 1 + 8                                                    *
   ****************************************************************
   * Do not redirect unless a valid language cookie is found.     *
   * Cookie functionality is disabled.                            *
   ****************************************************************
   * 10 = 2 + 8                                                   *
   ****************************************************************
   * Disable redirection and cookie.                              *
   ****************************************************************
   * 18 = 2 + 16                                                  *
   ****************************************************************
   * Disable redirection and enable cookie.                       *
   ****************************************************************
   * 17 = 1 + 16                                                  *
   ****************************************************************
   * Do not redirect unless a valid language cookie is found.     *
   * Disable redirection and enable cookie.                       *
   ****************************************************************
   * 12 = 4 + 8                                                   *
   ****************************************************************
   * Enable redirection and disable cookie.                       *
   ****************************************************************
   * 20 = 4 + 16                                                  *
   ****************************************************************
   * Enable redirection and enable cookie.                        *
   ***************************************************************/
  switch ($behavior) {
    case 9:
    case 10:
    case 18:
      $destination = NULL;
      break;
    case 17:
      $destination = $_GET['q'];
      if ($lang_from_cookie) {
        unset($query['destination']);
        break;
      }

    // Intentionally, no break.
    case 12:
    case 20:
      $language = new stdClass();
      $destination = 'language_selection';
      break;
  }
  if ($destination) {
    drupal_goto($destination, drupal_query_string_encode($query));
  }
}