<?php

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.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, or
	(at your opinion) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

if (!defined("_LOAD_PAGE_")) {
	die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Error:</b> This file cannot be opened directly!</td></tr></table>");
}

$op = (isset($_GET['op'])) ? inCode($_GET['op']) : "" ;
$id = (isset($_GET['id'])) ? intval($_GET['id']) : 0 ;
$ok = (isset($_GET['ok'])) ? inCode($_GET['ok']) : false ;
$language = (isset($_POST['language'])) ? inCode($_POST['language']) : "" ;


function langList() {
	global $dblink;
	
	$n = 0;
	echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
	echo "<thead>\n";
		echo "<tr><td><b>"._LANGUAGE_."</b></td><td width='10%' align='center'><b>"._DEFAULT_."</b></td><td width='1%'>&nbsp;</td></tr>";
	echo "</thead>\n";
	echo "<tbody>\n";
		
		$default = $dblink->get_num("SELECT id FROM memht_content_languages WHERE def=1");
		$onoff = ($default==0) ? "on" : "off" ;
		echo "<tr><td class='hlight'><font color='#990000'><b>"._GLOBAL_."</b></font></td><td class='hlight' align='center'><a href='admin.php?page=languages&op=switch&id=0' title='"._SET_DEFAULT_."'><img src='images/{$onoff}.gif' alt='"._ON_OFF_."' border='0'></a></td><td width='5%' class='hlight' align='right' nowrap>&nbsp;</td></tr>\n";
		if ($result = $dblink->get_list("SELECT * FROM memht_content_languages ORDER BY language")) {
			foreach ($result as $row) {
				$id = intval($row['id']);
				$language = outCode($row['language']);
				$default = intval($row['def']);
				
				$class = (($n++%2)!=0) ? "hlight" : "clean" ;
				$onoff = ($default==1) ? "on" : "off" ;
				
				echo "<tr><td class='$class'><b>$language</b></td><td class='$class' align='center'><a href='admin.php?page=languages&op=switch&id=$id' title='"._SET_DEFAULT_."'><img src='images/{$onoff}.gif' alt='"._ON_OFF_."' border='0'></a></td><td width='5%' class='$class' align='right' nowrap><a href='admin.php?page=languages&op=edit&id=$id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=languages&op=delete&id=$id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></td></tr>\n";
			}
		} else {
			echo "<tr><td align='center' id='errorText' class='clean' colspan='6'><b>"._EMPTY_."</b></td></tr>";
		}
		
	echo "</tbody>\n";
	echo "</table>";
}

function add($language,$ok=false) {
	global $dblink,$admin,$tzNOW;

	if (!$ok) {
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form' method='post' action='admin.php?page=languages&op=add&ok=true'>";
			echo "<tr><td width='25%'><b>"._LANGUAGE_."</b></td><td><input type='text' name='language' size='40' maxlength='255'></td></tr>\n";
			echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._ADD_."'></td></tr>\n";
		echo "</form>\n";
		echo "</table>\n";
	} else {	
		$save = true;
		if ($language=="") { $save = false; $msg = _LANGUAGE_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
		
		if ($save) {
			$dblink->query("INSERT INTO memht_content_languages (id,language)VALUES (null,'$language')");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=languages'>";
		} else {
			echo "<div align='center' id='errorText'><b>$msg</b></div>";
		}
	}
}

function edit($id,$language,$ok=false) {
	global $dblink,$admin,$tzNOW;

	if (!$ok) {
		$row = $dblink->get_row("SELECT language FROM memht_content_languages WHERE id=$id");
		$language = outCode($row['language']);
		
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form' method='post' action='admin.php?page=languages&op=edit&id=$id&ok=true'>";
			echo "<tr><td width='25%'><b>"._LANGUAGE_."</b></td><td><input type='text' name='language' value=\"$language\" size='40' maxlength='255'></td></tr>\n";
			echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._MODIFY_."'></td></tr>\n";
		echo "</form>\n";
		echo "</table>\n";
	} else {	
		$save = true;
		if ($language=="") { $save = false; $msg = _LANGUAGE_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
		
		if ($save) {
			$dblink->query("UPDATE memht_content_languages SET language='$language' WHERE id=$id");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=languages'>";
		} else {
			echo "<div align='center' id='errorText'><b>$msg</b></div>";
		}
	}
}


function delete($id,$ok=false) {
	global $dblink;
	
	$id = intval($id);
	if ($ok) {
		$dblink->query("DELETE FROM memht_content_languages WHERE id=$id");
		echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=languages'>";
	} else {
		echo "<div align='center'><b>"._SUREDELETE_."</b><br><a href='admin.php?page=languages&op=delete&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=languages' title='"._NO_."'>"._NO_."</a></div>";
	}
}

function changeDef($id) {
	global $dblink;
	
	$dblink->query("UPDATE memht_content_languages SET def='0'");
	if ($id>0) { $dblink->query("UPDATE memht_content_languages SET def='1' WHERE id=$id"); }
	echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=languages'>";
}

require_once("admin/inc/inc_header.php");
	admin_page_title($page);
									
	openTable();
		echo "<div align='center' class='box'>";
			echo "<a href='admin.php?page=languages' title='"._LIST_."'><img src='admin/icons/list.png' border='0' title='"._LIST_."' align='top'> "._LIST_."</a>";
			echo " - <a href='admin.php?page=languages&op=add' title='"._ADD_."'><img src='admin/icons/add.png' border='0' title='"._ADD_."' align='top'> "._ADD_."</a>";
		echo "</div>";
	closeTable();
	
	openTable();
	
		switch($op) {
			case "add":
				add($language,$ok);
			break;
			
			case "edit":
				edit($id,$language,$ok);
			break;
			
			case "delete":
				delete($id,$ok);
			break;
			
			case "switch":
				changeDef($id);
			break;
			
			default:
				langList();
			break;
		}
	
	closeTable();
require_once("admin/inc/inc_footer.php");

?>