<?php

/*************************************************************************/
/* DOCEBO - Content Management System                                    */
/* ============================================                          */
/*                                                                       */
/* Copyright (c) 2004 by Claudio Erba, Fabio Pirovano                    */
/* & Giovanni Derks - http://www.spaghettilearning.com                   */
/*                                                                       */
/* This program is free software. You can redistribute it and/or modify  */
/* it under the terms of the GNU General Public License as published by  */
/* the Free Software Foundation; either version 2 of the License.        */
/*************************************************************************/

if(isset($_SESSION['sesCmsAdmUser']) && isset($_SESSION['sesCmsAdmLevel'])) {


function manlang() {
	funAdminAccess('OP');
	global $prefixCms, $visuItem;

	$ipp=5; // Items per page.
	$ini=(int)$_GET["ini"];

	include('core/class/class.typeone.php');

	reorder($prefixCms."_language");

	//area title
	addTitleArea("manlang");
	echo("<div class=\"stdBlock\">\n");


	$qtxt="";
	$qtxt.="SELECT * FROM ".$prefixCms."_language;";
	$q=mysql_query($qtxt);
	if ($q) $tot=mysql_num_rows($q);

	$qtxt="";
	$qtxt.="SELECT * FROM ".$prefixCms."_language ";
	$qtxt.="ORDER BY ord, name LIMIT $ini,".$ipp;

	$table = new typeOne($ipp);
	$table->OpenTable("");

	$head = array(_LANG,
		'<img src="'.getPathImage().'standard/down.gif" alt="'._MOVEDW.'" title="'._MOVEDW.'" />',
		'<img src="'.getPathImage().'standard/up.gif" alt="'._MOVEUP.'" title="'._MOVEUP.'" />',
		'<img src="'.getPathImage().'standard/mod.gif" alt="'._MOD.'" title="'._MOD.'" />',
		'<img src="'.getPathImage().'standard/rem.gif" alt="'._REM.'" title="'._REM.'" />');
	$head_type = array('', 'img', 'img', 'img', 'img');


	$table->WriteHeader($head, $head_type);

	$q=mysql_query($qtxt);

	if (($q) && (mysql_num_rows($q) > 0)) {
		while($row=mysql_fetch_array($q)) {

			$rowcnt=array($row["name"]);

			if ($row["ord"] < $tot)
				$rowcnt[]=
					"<a href=\"admin.php?modulename=manlang&amp;act_op=movedown$back&amp;ord=".$row["ord"]."\">".
					"<img src=\"".getPathImage()."standard/down.gif\" alt=\""._DOWN."\" title=\""._DOWN."\" /></a>";
			else
				$rowcnt[]="";
			if ($row["ord"] > 1)
				$rowcnt[]=
					"<a href=\"admin.php?modulename=manlang&amp;act_op=moveup$back&amp;ord=".$row["ord"]."\">".
					"<img src=\"".getPathImage()."standard/up.gif\" alt=\""._UP."\" title=\""._UP."\" /></a>";
			else
				$rowcnt[]="";

			$rowcnt[]="<a href=\"admin.php?modulename=manlang&amp;op=editlang&amp;id=".$row["link_name"]."\">".
			'<img src="'.getPathImage().'standard/mod.gif" alt="'._MOD.'" title="'._MOD.'" /></a>';

			if (!lang_is_used($row["link_name"]))
				$rowcnt[]="<a href=\"admin.php?modulename=manlang&amp;op=dellang&amp;id=".$row["link_name"]."\">".
				'<img src="'.getPathImage().'standard/rem.gif" alt="'._REM.'" title="'._REM.'" /></a>';
			else
				$rowcnt[]="&nbsp;";

			$table->WriteRow($rowcnt);
		}
	}

	$table->WriteAddRow('<a href="admin.php?modulename=manlang&amp;op=addlang" title="'._ADD.'">
									<img src="'.getPathImage().'standard/add.gif" alt="'._ADD.'" /> '._ADD.'</a>');

	$table->CloseTable();

	$images = array(
		'start' => '<img src="'.getPathImage().'standard/start.gif" alt="'._START.'" title="'._STARTELEM.'" />',
		'prev' => '<img src="'.getPathImage().'standard/prev.gif" alt="'._PREV.'" title="'._PREVELEM.'" />',
		'next' => '<img src="'.getPathImage().'standard/next.gif" alt="'._NEXT.'" title="'._NEXTELEM.'" />',
		'end' => '<img src="'.getPathImage().'standard/end.gif" alt="'._END.'" title="'._ENDELEM.'" />'
	);

	$table->WriteNavBar($images,
							'admin.php?modname=pagella&amp;ini2='.$ini2.'&amp;ini=',
							$ini,
							$tot);

	echo("</div>\n");
}



function edit_lang() {
	funAdminAccess('MOD');
	global $prefixCms;


	//area title
	addTitleArea("manlang");
	echo("<div class=\"stdBlock\">\n");

	$qtxt="SELECT * FROM ".$prefixCms."_language WHERE link_name='".$_GET["id"]."';";
	$q=mysql_query($qtxt);
	$row=mysql_fetch_array($q);

	echo("<form action=\"admin.php?modulename=manlang&amp;op=manlang\" method=\"POST\">\n");

	echo("<input type=\"hidden\" id=\"act_op\" name=\"act_op\" value=\"savelang\" />\n");
	echo("<input type=\"hidden\" id=\"id\" name=\"id\" value=\"".$_GET["id"]."\" />\n");

	echo("<table>\n");
	echo("<tr><td>\n");
	echo("<div class=\"title\"><label for=\"name\">"._NAME."</label></div>\n");
	echo("</td><td>\n");
	if (lang_is_used($row["link_name"])) $ro=" readonly=\"readonly\""; else $ro="";
	echo("<input type=\"text\" class=\"textfield\" id=\"name\" name=\"name\" value=\"".$row["name"]."\"$ro />\n");
	echo("</td></tr>\n");
	echo("<tr><td>\n");
	echo("<div class=\"title\"><label for=\"name\">"._LINKNAME."</label></div>\n");
	echo("</td><td>\n");
	echo("<input type=\"text\" class=\"textfield\" id=\"link_name\" name=\"link_name\" value=\"".$row["link_name"]."\"$ro />\n");
	echo("</td></tr>\n");
	echo("<tr><td>\n");
	echo("<div class=\"title\"><label for=\"image\">"._IMG."</label></div>\n");
	echo("</td><td>\n");
	echo("<select id=\"image\" name=\"image\">\n");

	$path=getPathImage()."language/";
	$handle=opendir($path);

	while ($file=readdir($handle)) {
		$ext=substr($file, strlen($file)-3);
		if($file != ".." && $file != "." && ($ext == "jpg" || $ext == "gif" || $ext == "png")) {
			if ($file == $row["image"]) $sel=" selected=\"selected\""; else $sel="";
			echo("<option value=\"".$file."\"$sel>$file</option>\n");
		}
	}
	echo("</select>\n");
	echo("</td></tr>\n");
	echo("<tr><td>\n");
	echo("<div class=\"title\"><label for=\"datesep\">"._LANG_DATESEP."</label></div>\n");
	echo("</td><td>\n");
	echo("<select id=\"datesep\" name=\"datesep\">\n");
	if ($row["datesep"] == "-") {
		$sel_1=" selected=\"selected\""; $sel_2="";
	}
	else {
		$sel_2=" selected=\"selected\""; $sel_1="";
	}
	echo("<option value=\"-\"$sel_1>-</option>\n");
	echo("<option value=\"/\"$sel_2>/</option>\n");
	echo("</select>\n");
	echo("</td></tr>\n");
	echo("<tr><td>\n");
	echo("<div class=\"title\"><label for=\"dateformat\">"._LANG_DATEFORMAT."</label></div>\n");
	echo("</td><td>\n");
	$dfv=array("d m Y", "m d Y", "Y d m");
	$dfl=array(_LANG_DMY, _LANG_MDY, _LANG_YMD);
	echo("<select id=\"dateformat\" name=\"dateformat\">\n");

	foreach ($dfv as $key=>$val) {
		if ($val == $row["dateformat"]) $sel=" selected=\"selected\""; else $sel="";
		echo("<option value=\"$val\"$sel>".$dfl[$key]."</option>\n");
	}
	echo("</select>\n");
	echo("</td></tr>\n");
	echo("<tr><td>\n");
	echo("<div class=\"title\"><label for=\"hformat\">"._LANG_HFORMAT."</label></div>\n");
	echo("</td><td>\n");
	$hfv=array("12", "24");
	$hfl=array(_LANG_H12, _LANG_H24);
	echo("<select id=\"hformat\" name=\"hformat\">\n");

	foreach ($hfv as $key=>$val) {
		if ($val == $row["hformat"]) $sel=" selected=\"selected\""; else $sel="";
		echo("<option value=\"$val\"$sel>".$hfl[$key]."</option>\n");
	}
	echo("</select>\n");
	echo("</td></tr>\n");
	echo("<tr><td>\n");
	echo("<div class=\"title\"><label for=\"ltime\">"._LOCAL_TIME."</label></div>\n");
	echo("</td><td>\n");
	echo("<select id=\"ltime\" name=\"ltime\">\n");
	$now=time();
	$local_h=date("H", $now+$row["timeoffset"]*3600);
	for ($i=0; $i<24; $i++) {
		if ((int)$local_h == $i) $sel=" selected=\"selected\""; else $sel="";
		echo("<option value=\"$i\"$sel>".$i."</option>\n");
	}
	echo("</select>\n");
	echo("</td></tr>\n");
	echo("<tr><td>\n");
	echo("<div class=\"title\"><label for=\"default_cms_title\">"._DEFAULT_CMS_TITLE."</label></div>\n");
	echo("</td><td>\n");
	echo("<input type=\"text\" class=\"textfield\" "
		."id=\"default_cms_title\" name=\"default_cms_title\" value=\""
		.$row["default_cms_title"]."\" size=\"40\" />\n");
	echo("</td></tr>\n");
	echo("<tr><td valign=\"top\">\n");
	echo("<div class=\"title\"><label for=\"default_cms_kwords\">"._DEFAULT_CMS_KWORDS."</label></div>\n");
	echo("</td><td>\n");
	echo("<textarea rows=\"4\" cols=\"40\" "
		."id=\"default_cms_kwords\" name=\"default_cms_kwords\">"
		.$row["default_cms_kwords"]."</textarea>\n");
	echo("</td></tr>\n");
	echo("<tr><td valign=\"top\">\n");
	echo("<div class=\"title\"><label for=\"default_cms_desc\">"._DEFAULT_CMS_DESC."</label></div>\n");
	echo("</td><td>\n");
	echo("<textarea rows=\"4\" cols=\"40\" "
		."id=\"default_cms_desc\" name=\"default_cms_desc\">"
		.$row["default_cms_desc"]."</textarea>\n");
	echo("</td></tr>\n");
	echo("</table>\n");

	echo("<input type=\"hidden\" id=\"old_browser_title\" name=\"old_browser_title\" value=\"".$row["default_cms_title"]."\" />\n");
	echo("<input type=\"hidden\" id=\"old_keyword\" name=\"old_keyword\" value=\"".$row["default_cms_kwords"]."\" />\n");
	echo("<input type=\"hidden\" id=\"old_sitedesc\" name=\"old_sitedesc\" value=\"".$row["default_cms_desc"]."\" />\n");

	echo("<br /><br />\n");
	echo("<input type=\"submit\" class=\"button\" value=\""._APPLY."\" />\n");
	echo("</form>\n");

	echo("</div>\n");
}



function save_lang() {
	funAdminAccess('MOD');
	global $prefixCms;

	include_once("core/manDateTime.php");

	$name=$_POST["name"];
	$link_name=strtolower($_POST["link_name"]);
	$image=$_POST["image"];
	$datesep=$_POST["datesep"];
	$dateformat=$_POST["dateformat"];
	$hformat=$_POST["hformat"];
	$timeoffset=(int)$_POST["ltime"]-(int)date("H");
	$default_cms_title=substr($_POST["default_cms_title"], 0, 255);
	$default_cms_kwords=$_POST["default_cms_kwords"];
	$default_cms_desc=$_POST["default_cms_desc"];

	// Reset area browser title keywords and description where them are set to default
	$qtxt="UPDATE ".$prefixCms."_area SET browser_title='' WHERE browser_title='".$_POST["old_browser_title"]."'";
	$q=mysql_query($qtxt);
	$qtxt="UPDATE ".$prefixCms."_area SET keyword='' WHERE keyword='".$_POST["old_keyword"]."'";
	$q=mysql_query($qtxt);
	$qtxt="UPDATE ".$prefixCms."_area SET sitedesc='' WHERE sitedesc='".$_POST["old_sitedesc"]."'";
	$q=mysql_query($qtxt);


	// And then save the settings
	$qtxt="";
	$qtxt.="UPDATE ".$prefixCms."_language SET link_name='$link_name', name='$name', ";
	$qtxt.="image='$image', datesep='$datesep', dateformat='$dateformat', ";
	$qtxt.="hformat='$hformat', timeoffset='$timeoffset', ";
	$qtxt.="default_cms_title='$default_cms_title', default_cms_kwords='$default_cms_kwords', default_cms_desc='$default_cms_desc' ";
	$qtxt.="WHERE link_name='".$_POST["id"]."'";

	$q=mysql_query($qtxt);
}


function add_lang() {
	funAdminAccess('NEW');
	global $prefixCms;

	//area title
	addTitleArea("manlang");
	echo("<div class=\"stdBlock\">\n");

	echo("<form action=\"admin.php?modulename=manlang&amp;op=inslang\" method=\"POST\">\n");
	echo("<div class=\"title\"><label for=\"name\">"._NAME."</label></div>\n");
	echo("<input type=\"text\" class=\"textfield\" id=\"name\" name=\"name\" />\n");
	echo("<br /><br />\n");
	echo("<input type=\"submit\" class=\"button\" value=\""._INS."\" />\n");
	echo("</form>\n");
	echo("</div>\n");
}

function ins_lang() {
	funAdminAccess('NEW');
	global $prefixCms;

	$name=$_POST["name"];
	$link_name=strtolower($name);

	$qtxt="SELECT * FROM ".$prefixCms."_language;";
	$q=mysql_query($qtxt);
	if ($q) $ord=mysql_num_rows($q)+1;

	$qtxt="";
	$qtxt.="INSERT INTO ".$prefixCms."_language (link_name, name, ord, datesep, dateformat, hformat) VALUES ";
	$qtxt.="('$link_name', '$name', '$ord', '/', 'd m Y', '24');";

	$q=mysql_query($qtxt);
	header("location:admin.php?modulename=manlang&op=editlang&id=$link_name");
}


function confdel() {
	funAdminAccess('REM');
	global $prefixCms;

	//area title
	addTitleArea("manlang");
	echo '<div class="stdBlock" align="center">';

	echo (_AREYOUSURE."<br /><br />");

	echo("<table>\n");
	echo("<tr><td>\n");

	$back="&amp;op=manlang";

	echo("<form action=\"admin.php?modulename=manlang$back\" method=\"POST\">\n");

	echo("<input type=\"hidden\" id=\"act_op\" name=\"act_op\" value=\"remlang\" />\n");

	echo("<input type=\"hidden\" id=\"id\" name=\"id\" value=\"".$_GET["id"]."\" />\n");
	echo("<input class=\"button\" type=\"submit\" value=\""._YES."\" />\n");
	echo("</form>\n");

	echo("</td><td>\n");

	echo("<form action=\"admin.php?modulename=manlang$back\" method=\"POST\">\n");
	echo("<input class=\"button\" type=\"submit\" value=\""._NO."\" />\n");
	echo("</form>\n");

	echo("</td></tr>\n");
	echo("</table>\n");

	echo '</div>';
}


function movedown($table, $ord) {

	$qtxt="";
	$qtxt.="UPDATE $table as t1, $table as t2 SET t1.ord='".($ord+1)."', ";
	$qtxt.="t2.ord='$ord' WHERE t1.ord='$ord' AND t2.ord='".($ord+1)."';";
	$q=mysql_query($qtxt);

}


function moveup($table, $ord) {

	$qtxt="";
	$qtxt.="UPDATE $table as t1, $table as t2 SET t1.ord='".($ord-1)."', ";
	$qtxt.="t2.ord='$ord' WHERE t1.ord='$ord' AND t2.ord='".($ord-1)."';";
	$q=mysql_query($qtxt);

}


function reorder($table) {

	$qtxt="SELECT * FROM $table ORDER BY ord;";
	$q=mysql_query($qtxt);

	if (($q) && (mysql_num_rows($q) > 0)) {
		$i=0;
		while ($row=mysql_fetch_array($q)) {
			$link_name=$row["link_name"];

			$i++;

			$qtxt="UPDATE $table SET ord='$i' WHERE link_name='$link_name' LIMIT 1;";
			$uoq=mysql_query($qtxt);
		}
	}
}


if ($_GET["act_op"] != "") $act_op=$_GET["act_op"];
if ($_POST["act_op"] != "") $act_op=$_POST["act_op"];
switch($act_op) {

	case "movedown": {
		movedown($prefixCms."_language", $_GET["ord"]);
	};break;

	case "moveup": {
		moveup($prefixCms."_language", $_GET["ord"]);
	};break;

	case "savelang": {
		save_lang();
	};break;

	case "remlang": {
		$id=$_POST["id"];
		if (!lang_is_used($id))
			mysql_query("DELETE FROM ".$prefixCms."_language WHERE link_name='$id' LIMIT 1;");
	};break;

}



switch($op) {

	default: case "manlang" : {
		manlang();
	};break;

	case "editlang" : {
		edit_lang();
	};break;

	case "addlang" : {
		add_lang();
	};break;

	case "inslang" : {
		ins_lang();
	};break;

	case "dellang" : {
		confdel();
	};break;

}



}
?>
