You are here

language_selection_page.helpers.inc in Language Selection Page 6

This file contains all the custom functions needed for the module.

File

includes/language_selection_page.helpers.inc
View source
<?php

/**
 * @file
 * This file contains all the custom functions needed for the module.
 */

/**
 * Custom function to parse a URL.
 *
 * @return array
 */
function _language_selection_page_parse_url_custom() {
  $url = url($_REQUEST['q'], array(
    'absolute' => TRUE,
    'language' => new stdClass(),
  ));
  $array_url = parse_url($url . '?' . $_SERVER['QUERY_STRING']);
  if (isset($array_url['query'])) {
    parse_str($array_url['query'], $array_url['query']);
  }
  return $array_url;
}

/**
 * Custom function who return the first argument which is not null.
 * If all arguments are null, it returns the default lang.
 *
 * @return object
 */
function _language_selection_page_resolve_lang() {
  foreach (func_get_args() as $arg) {
    if (!is_null($arg)) {
      return $arg;
    }
  }
  return language_default();
}

/**
 * Custom function who detect the language from URL.
 * @return string
 */
function _language_selection_page_detect_lang_from_url() {
  $url_parsed = _language_selection_page_parse_url_custom();
  $path = explode('/', $url_parsed['query']['q']);
  return _language_selection_page_validate_language($path[0]);
}

/**
 * Custom function who detect the language from a URL argument.
 * @return string
 */
function _language_selection_page_detect_lang_from_url_argument() {
  $url_parsed = _language_selection_page_parse_url_custom();
  $argument = variable_get('language_selection_page_language_url_argument', '');
  if (empty($argument)) {
    return NULL;
  }
  if (empty($url_parsed['query'][$argument])) {
    return NULL;
  }
  return _language_selection_page_validate_language($url_parsed['query'][$argument]);
}

/**
 * Custom function who detect the language from a cookie.
 * @return string
 */
function _language_selection_page_detect_lang_from_cookie() {
  return _language_selection_page_validate_language($_COOKIE[LANGUAGE_SELECTION_PAGE_LANGUAGE_COOKIE_KEY]);
}

/**
 * Validate a string against an existing and enabled language prefix.
 *
 * @param $string
 *  The string to compare to each language prefix.
 *
 * @return object|NULL
 *  Returns a valid language object if $string match an existing prefix.
 *  Returns NULL is the $string doesn't match an existing language prefix.
 */
function _language_selection_page_validate_language($string) {
  $lang_list = language_list('enabled');
  $lang_list = $lang_list[1];
  foreach ($lang_list as $lang) {
    if (!empty($lang->prefix) && $lang->prefix == $string) {
      return $lang;
    }
  }
  return NULL;
}

/**
 * Return an array with the real language count from DB and the one from vars.
 *
 * @return array
 */
function _language_selection_page_check_language_count() {

  // We cannot rely on the variables from Features or system.
  $language_count = language_list('enabled');
  return array(
    'db' => count($language_count[1]),
    'vars' => variable_get('language_count', 1),
  );
}

/**
 * All the error messages are saved in this function so they are the same
 * in the requirements page and/or the settings page.
 *
 * @return array
 */
function _language_selection_page_get_error_messages() {
  $args = func_get_args();
  $t = get_t();
  $messages['language_count'] = $t('There is a problem with the <b>language_count</b> variable.<br/>There are @language_count_db languages enabled in the database and @language_count_vars in the variable table (or from Features/Strongarm).<br/>This can lead to problems in your Drupal website.<br/>Please fix this problem before going further.', array(
    '@language_count_db' => $args[1],
    '@language_count_vars' => $args[2],
  ));
  $messages['language_negotiation'] = $t('The Language Selection Page will be available when %setting_name setting is set to required "%setting_value" value. You should either disable the module or <a href="@setting_url">change your configuration</a>.', array(
    '%setting_name' => $args[1],
    '%setting_value' => $args[2],
    '@setting_url' => $args[3],
  ));
  $messages['language_enabled'] = $t('You should have more than <a href="@setting_url">one language</a> enabled to get the <b>Language Selection Page</b> working.', array(
    '@setting_url' => $args[1],
  ));
  $messages['language_prefix'] = $t('You should add a path prefix to <a href="@language_url">language @language_name</a> if you want to have it enabled in the <b>Language Selection Page</b>.', array(
    '@language_url' => $args[1],
    '@language_name' => $args[2],
  ));
  $messages['language_only_one'] = $t('There is only <a href="@link">one language enabled</a>.', array(
    '@link' => $args[1],
  ));
  $messages['language_all_good'] = $t('All your enabled languages have language prefix, all good.');
  return $args[0] ? $messages[$args[0]] : $messages;
}

Functions

Namesort descending Description
_language_selection_page_check_language_count Return an array with the real language count from DB and the one from vars.
_language_selection_page_detect_lang_from_cookie Custom function who detect the language from a cookie.
_language_selection_page_detect_lang_from_url Custom function who detect the language from URL.
_language_selection_page_detect_lang_from_url_argument Custom function who detect the language from a URL argument.
_language_selection_page_get_error_messages All the error messages are saved in this function so they are the same in the requirements page and/or the settings page.
_language_selection_page_parse_url_custom Custom function to parse a URL.
_language_selection_page_resolve_lang Custom function who return the first argument which is not null. If all arguments are null, it returns the default lang.
_language_selection_page_validate_language Validate a string against an existing and enabled language prefix.