<?php

/* $Id: date_lang.php,v 1.4 2003/12/22 10:48:55 leyeti Exp $ */

define("CURRENT_DIR",CAL1_INC_DIR);
//========================================================================
$info = isset($_GET["info"]) ? $_GET["info"] : 0;

if (!defined("INCLUDE_DATE_LANG")) {
		   define("INCLUDE_DATE_LANG",1);
	}
else return;


//if file is directly called in browser :  
if (basename($_SERVER["PHP_SELF"])=="date_lang.php") {
	
	if (check_config()) {
	    echo	"Variables dump : <BR>
				\$_SESSION[\"date_lang\"][\"language\"]=\"".$_SESSION["date_lang"]["language"]."\"; //specify the language<BR>
				\$_SESSION[\"date_lang\"][\"lang_dir\"]=\"".$_SESSION["date_lang"]["lang_dir"]."\"; //specify the directory containing language files<BR>
				<BR>The language file must be named \"date.".$_SESSION["date_lang"]["language"].".lang\" <BR>
				<BR>According to the variable \$_SESSION[\"date_lang\"][\"lang_dir\"], your language files must be stored in ".$_SESSION["date_lang"]["lang_dir"]."<BR>
				Language files must be named like \"date.XX.lang\" where XX is the language (e.g.: fr)<BR><BR>";
		print("<BR><BR><A HREF=\"".$_SERVER["PHP_SELF"]."?info=1\">What is date_lang ?</A>");
	}
	else die(); 	
}


//Display infos about date_lang : 
if ($info) {

display_infos();
}
	
	
function check_config() {
		$error_rep = error_reporting(0);
		//verify if $_SESSION["date_lang"] is correctly set :
		if (!isset($_SESSION["date_lang"]) 
			|| (!isset($_SESSION["date_lang"]["language"]) || $_SESSION["date_lang"]["language"]=="") 
			|| !isset($_SESSION["date_lang"]["lang_dir"])
			) {
			error_reporting($error_rep);
		    print("date_lang error : you must specify a langage and a full directory in the \$_SESSION[\"date_lang\"] variable ! <BR>
				e.g.: <BR>
				\$_SESSION[\"date_lang\"][\"language\"]=\"fr\"; //specify the language<BR>
				\$_SESSION[\"date_lang\"][\"lang_dir\"]=\"/var/www/html/langfiles/\"; //specify the directory containing language files<BR>
				<BR>Language files must be named like \"date.XX.lang\" where XX is the language (e.g.: fr)<BR>");
			display_infos();
			return false;
		}
		//verify if the language file exists :
		else if (!fopen($_SESSION["date_lang"]["lang_dir"]."date.".$_SESSION["date_lang"]["language"].".lang","r") && $_SESSION["date_lang"]["language"]!='en') { 
			error_reporting($error_rep);
		    print("date_lang error : unable to read the language file : \"".$_SESSION["date_lang"]["lang_dir"]."date.".$_SESSION["date_lang"]["language"].".lang\" ! <BR>
				Variables dump : <BR>
				\$_SESSION[\"date_lang\"][\"language\"]=\"".$_SESSION["date_lang"]["language"]."\"; //specify the language<BR>
				\$_SESSION[\"date_lang\"][\"lang_dir\"]=\"".$_SESSION["date_lang"]["lang_dir"]."\"; //specify the directory containing language files<BR>
				<BR>The language file must be named \"date.".$_SESSION["date_lang"]["language"].".lang\" <BR>");
			display_infos();
			return false;
		}
	error_reporting($error_rep);
	return true;
}




include(CURRENT_DIR."date_lang.inc.php");

	function read_Dict(){
		if ($_SESSION["date_lang"]["language"] != "en") {
		 	// do translation and give back a hashmap with all keys and values 
			if(check_config()) { 
				// if english -> DO NOTHING !
			    $GLOBALS["date_hashmap"] = readDateLanguageFile($_SESSION["date_lang"]["language"], $_SESSION["date_lang"]["lang_dir"], "date");
			}
			else die();   
		}
	}


    /*
    Ceci retourne le nom du jour en format long
    @param integer $day : le numero du jour dans la semaine comme en PHP
    */
    function jour_ux_l($day=0)
    {	
		//global $date_hashmap;//, $date_language;
        if(($day < 0) or ($day > 6))    {return false;}
        $pp_nom_jour_ux[0] = getDateLanguageString("weekday_1");//"Dimanche";
        $pp_nom_jour_ux[1] = getDateLanguageString("weekday_2");//"Lundi";
        $pp_nom_jour_ux[2] = getDateLanguageString("weekday_3");//"Mardi";
        $pp_nom_jour_ux[3] = getDateLanguageString("weekday_4");//"Mercredi";
        $pp_nom_jour_ux[4] = getDateLanguageString("weekday_5");//"Jeudi";
        $pp_nom_jour_ux[5] = getDateLanguageString("weekday_6");//"Vendredi";
        $pp_nom_jour_ux[6] = getDateLanguageString("weekday_7");//"Samedi";
        return $pp_nom_jour_ux[$day];
    }        
    /*
    Ceci retourne le nom du jour en format abregé
    @param integer $day : le numero du jour dans la semaine comme en PHP
    */
    function jour_ux_c($day=0)
    {
        if(($day < 0) or ($day > 6))    {return false;}
        $pp_nom_jour_ux[0] = getDateLanguageString("short_weekday_1");//"Dim";
        $pp_nom_jour_ux[1] = getDateLanguageString("short_weekday_2");//"Lun";
        $pp_nom_jour_ux[2] = getDateLanguageString("short_weekday_3");//"Mar";
        $pp_nom_jour_ux[3] = getDateLanguageString("short_weekday_4");//"Mer";
        $pp_nom_jour_ux[4] = getDateLanguageString("short_weekday_5");//"Jeu";
        $pp_nom_jour_ux[5] = getDateLanguageString("short_weekday_6");//"Ven";
        $pp_nom_jour_ux[6] = getDateLanguageString("short_weekday_7");//"Sam";
        return $pp_nom_jour_ux[$day];
    }        
    /*
    Ceci retourne le nom du jour en format long
    @param integer $day : le numero du jour dans la semaine comme 
    celui retourné par DAYOFWEEK() en MySQL
    */
    function jour_dw_l($day=0)
    {
        if(($day < 0) or ($day > 6))    {return false;}
        // Mysql dayofweek only
        $pp_nom_jour_dw[0] = getDateLanguageString("weekday_2");//"Lundi";
        $pp_nom_jour_dw[1] = getDateLanguageString("weekday_3");//"Mardi";
        $pp_nom_jour_dw[2] = getDateLanguageString("weekday_4");//"Mercredi";
        $pp_nom_jour_dw[3] = getDateLanguageString("weekday_5");//"Jeudi";
        $pp_nom_jour_dw[4] = getDateLanguageString("weekday_6");//"Vendredi";
        $pp_nom_jour_dw[5] = getDateLanguageString("weekday_7");//"Samedi";
        $pp_nom_jour_dw[6] = getDateLanguageString("weekday_1");//"Dimanche";
        
        return $pp_nom_jour_dw[$day];
    }        
    /*
    Ceci retourne le nom du jour en format abregé
    @param integer $day : le numero du jour dans la semaine comme 
    celui retourné par DAYOFWEEK() en MySQL
    */
    function jour_dw_c($day=0)
    {
        if(($day < 0) or ($day > 6))    {return false;}
        // Mysql dayofweek only
        $pp_nom_jour_dw[0] = getDateLanguageString("short_weekday_2");//"Lun";
        $pp_nom_jour_dw[1] = getDateLanguageString("short_weekday_3");//"Mar";
        $pp_nom_jour_dw[2] = getDateLanguageString("short_weekday_4");//"Mer";
        $pp_nom_jour_dw[3] = getDateLanguageString("short_weekday_5");//"Jeu";
        $pp_nom_jour_dw[4] = getDateLanguageString("short_weekday_6");//"Ven";
        $pp_nom_jour_dw[5] = getDateLanguageString("short_weekday_7");//"Sam";
        $pp_nom_jour_dw[6] = getDateLanguageString("short_weekday_1");//"Dim";
        
        return $pp_nom_jour_dw[$day];
    }        
    /*
    Ceci retourne le nom du jour en format long
    @param integer $day : le numero du jour dans la semaine comme 
    celui retourné par WEEKDAY() en MySQL
    */
    function jour_wd_l($day=0)
    {
        if(($day < 1) or ($day > 7))    {return false;}
        // Mysql weekday and ODBC
        $pp_nom_jour_wd[1] = getDateLanguageString("short_weekday_1");//"Dimanche";
        $pp_nom_jour_wd[2] = getDateLanguageString("short_weekday_2");//"Lundi";
        $pp_nom_jour_wd[3] = getDateLanguageString("short_weekday_3");//"Mardi";
        $pp_nom_jour_wd[4] = getDateLanguageString("short_weekday_4");//"Mercredi";
        $pp_nom_jour_wd[5] = getDateLanguageString("short_weekday_5");//"Jeudi";
        $pp_nom_jour_wd[6] = getDateLanguageString("short_weekday_6");//"Vendredi";
        $pp_nom_jour_wd[7] = getDateLanguageString("short_weekday_7");//"Samedi";
        
        return $pp_nom_jour_wd[$day];
    }
    /*
    Ceci retourne le nom du jour en format abregé
    @param integer $day : le numero du jour dans la semaine comme 
    celui retourné par WEEKDAY() en MySQL
    */
    function jour_wd_c($day=0)
    {
        if(($day < 1) or ($day > 7))    {return false;}
        // Mysql weekday and ODBC
        $pp_nom_jour_wd[1] = getDateLanguageString("short_weekday_1");//"Dim";
        $pp_nom_jour_wd[2] = getDateLanguageString("short_weekday_2");//"Lun";
        $pp_nom_jour_wd[3] = getDateLanguageString("short_weekday_3");//"Mar";
        $pp_nom_jour_wd[4] = getDateLanguageString("short_weekday_4");//"Mer";
        $pp_nom_jour_wd[5] = getDateLanguageString("short_weekday_5");//"Jeu";
        $pp_nom_jour_wd[6] = getDateLanguageString("short_weekday_6");//"Ven";
        $pp_nom_jour_wd[7] = getDateLanguageString("short_weekday_7");//"Sam";
        
        return $pp_nom_jour_wd[$day];
    }
    /*
    Ceci retourne le mois en abgregé
    @param integer $mois : le numero du jour dans le mois
    */
    function mois_court($mois=0)
    {
        if(($mois < 1) or ($mois>12)) {return false;}
        // Mois
        $pp_short_months[1] = getDateLanguageString("short_month_1");//"Jan";
        $pp_short_months[2] = getDateLanguageString("short_month_2");//"Fev";
        $pp_short_months[3] = getDateLanguageString("short_month_3");//"Mar";
        $pp_short_months[4] = getDateLanguageString("short_month_4");//"Avr";
        $pp_short_months[5] = getDateLanguageString("short_month_5");//"Mai";
        $pp_short_months[6] = getDateLanguageString("short_month_6");//"Jun";
        $pp_short_months[7] = getDateLanguageString("short_month_7");//"Jul";
        $pp_short_months[8] = getDateLanguageString("short_month_8");//"Aou";
        $pp_short_months[9] = getDateLanguageString("short_month_9");//"Sep";
        $pp_short_months[10] = getDateLanguageString("short_month_10");//"Oct";
        $pp_short_months[11] = getDateLanguageString("short_month_11");//"Nov";
        $pp_short_months[12] = getDateLanguageString("short_month_12");//"Dec";
        return $pp_short_months[$mois];
    }
    /*
    Ceci retourne le mois en format long
    @param integer $mois : le numero du jour dans le mois
    */
    function mois_long($mois=0)
    {
        if(($mois < 1) or ($mois>12)) {return false;}
        // Mois
        // Mois
        $pp_long_months[1] = getDateLanguageString("month_1");//"Janvier";
        $pp_long_months[2] = getDateLanguageString("month_2");//"Fevrier";
        $pp_long_months[3] = getDateLanguageString("month_3");//"Mars";
        $pp_long_months[4] = getDateLanguageString("month_4");//"Avril";
        $pp_long_months[5] = getDateLanguageString("month_5");//"Mai";
        $pp_long_months[6] = getDateLanguageString("month_6");//"Juin";
        $pp_long_months[7] = getDateLanguageString("month_7");//"Juillet";
        $pp_long_months[8] = getDateLanguageString("month_8");//"Août";
        $pp_long_months[9] = getDateLanguageString("month_9");//"Septembre";
        $pp_long_months[10] = getDateLanguageString("month_10");//"Octobre";
        $pp_long_months[11] = getDateLanguageString("month_11");//"Novembre";
        $pp_long_months[12] = getDateLanguageString("month_12");//"Décembre";
        
        return $pp_long_months[$mois];
    }
    /*
    Ceci retourne le suffixe du jour en francais
    @param integer $day : le numero du jour dans le mois
    */
    function jour_suffix($day=0)
    {
        if(($day < 1) or ($day>31)) {return false;}        
		switch($day){
			case 1:	return getDateLanguageString("st");
			case 2:	return getDateLanguageString("nd");
			case 3: return getDateLanguageString("rd");
			default : return getDateLanguageString("th");
			}
    }
	
    /*
    Ceci est la replication de la fonction native date()
    dans PHP. Les arguments sont similaires.
    Exemple : date('a') -- devient -> date_lang('a')
    @param string $format : le format souhaité
    @param integer $temps : le temps [[cf. time()]]
	@global string $date_lang : la langue sur 2 caractères (ex : 'fr')
    */
    function date_lang($format=null,$temps=null)
    {
		if(!isset($GLOBALS["date_hashmap"])) read_Dict();
        
		if ($_SESSION["date_lang"]["language"] == "en") { // if ENGLISH -> DO NOTHING !
		    return date($format,$temps);
		}
		
		if($format==null) {return "";}
        if($temps==null) {$temps = time();}
        $first_search = array ("'a'",
                "'A'",
                "'B'",
                "'d'",
                "'D'",
                "'F'",
                "'g'",
                "'G'",
                "'h'",
                "'H'",
                "'i'",
                "'I'",
                "'j'",
                "'l'",
                "'L'",
                "'m'",
                "'M'",
                "'n'",
                "'O'",
                "'r'",
                "'s'",
                "'S'",
                "'t'",
                "'T'",
                "'U'",
                "'w'",
                "'W'",
                "'Y'",
                "'y'",
                "'z'",
                "'Z'");
        
		$first_replace = array ("%a",
                "%A",
                "%B",
                "%d",
                "%D",
                "%F",
                "%g",
                "%G",
                "%h",
                "%H",
                "%i",
                "%I",
                "%j",
                "%l",
                "%L",
                "%m",
                "%M",
                "%n",
                "%O",
                "%r",
                "%s",
                "%S",
                "%t",
                "%T",
                "%U",
                "%w",
                "%W",
                "%Y",
                "%y",
                "%z",
                "%Z");
        
		$second_search = array ("'%a'",
                "'%A'",
                "'%B'",
                "'%d'",
                "'%D'",
                "'%F'",
                "'%g'",
                "'%G'",
                "'%h'",
                "'%H'",
                "'%i'",
                "'%I'",
                "'%j'",
                "'%l'",
                "'%L'",
                "'%m'",
                "'%M'",
                "'%n'",
                "'%O'",
                "'%r'",
                "'%s'",
                "'%S'",
                "'%t'",
                "'%T'",
                "'%U'",
                "'%w'",
                "'%W'",
                "'%Y'",
                "'%y'",
                "'%z'",
                "'%Z'");
				
        $second_replace = array (
                date('a',$temps),
                date('A',$temps),
                date('B',$temps),
                date('d',$temps),
                jour_ux_c(date('w',$temps)),
                mois_long(date('n',$temps)),
                date('g',$temps),
                date('G',$temps),
                date('h',$temps),
                date('H',$temps),
                date('i',$temps),
                date('I',$temps),
                date('j',$temps),
                jour_ux_l(date('w',$temps)),
                date('L',$temps),
                date('m',$temps),
                mois_court(date('n',$temps)),
                date('n',$temps),
                date('O',$temps),
                date('r',$temps),
                date('s',$temps),
                jour_suffix(date('j',$temps)),
                date('t',$temps),
                date('T',$temps),
                date('U',$temps),
                date('w',$temps),
                date('W',$temps),
                date('Y',$temps),
                date('y',$temps),
                date('z',$temps),
                date('Z',$temps));
        
        $second_format = preg_replace($first_search,$first_replace,$format);
		return (preg_replace($second_search, $second_replace, $second_format));
    }

	
	
	function display_infos(){
		
		include(CURRENT_DIR."date_lang.infos.html");
	
	}
//phpinfo();
//$date_lang="fr";
//echo date_lang("d F Y - l")."<BR>-------<BR>";
/*echo date("d F Y")."<BR>-------<BR>";
echo date_fr("a A b d D F g G h H i I j l L m M n O r s S t T U w W y Y z Z")."<BR>-------<BR>";
echo date("a A b d D F g G h H i I j l L m M n O r s S t T U w W y Y z Z")."<BR>-------<BR>";
*/

?>