<?php
session_start();
include_once("config.php");
$error_msg="";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Arabic Translation</title>
<style>
	body {
		font-family: Tahoma, Geneva, sans-serif;
		font-size: 14px;
		font-weight: bold;
	}
	th {
		background-color:#999;
		font-family: Tahoma, Geneva, sans-serif;
		font-size: 14px;
		font-weight: bold;
	}
	td {
		font-family: Tahoma, Geneva, sans-serif;
		font-size: 12px;
	}
	.err_msg {
		font-family: Tahoma, Geneva, sans-serif;
		font-size: 12px;
		color:#F00;
	}
	
</style>
</head>
<?php
if(isset($_REQUEST['submit_button'])) {
	foreach($_POST as $frmky=>$frm_itms) {
		if(substr($frmky,0,2) == "ID") {
			$frm_ary = explode("_",$frmky);
			$frm_en_text = "lan_".$frm_ary[1]."_en";
			$frm_ar_text = "lan_".$frm_ary[1]."_ar";

			$upsql = 'update seas_brd_trans set english = "' . trim($_POST[$frm_en_text]) . '", arabic = "' . trim($_POST[$frm_ar_text]) . '" where id = '.$frm_ary[1];
			
			mysql_query($upsql);
			
			if(strlen(mysql_error()) >0) {
				$error_msg .= "<br>Updation failed for Key: " . $frm_itms . " Error: " . mysql_error();
			}
		}
	}
	
	if(isset($_REQUEST['new_lan_key'])) {
		if(isset($_REQUEST['new_lan_en'])) {	
			if(isset($_REQUEST['new_lan_ar'])) {	
				
				if(strlen(trim($_REQUEST['new_lan_key'])) >0) {
					if(strlen(trim($_REQUEST['new_lan_en'])) >0) {
						
						$key = str_replace(" ","_",strtolower(trim($_REQUEST['new_lan_key'])));
						$ins_query = 'insert into seas_brd_trans (lan_key,english,arabic) values ("' . $key . '","'. trim($_REQUEST['new_lan_en']) . '","' . trim($_REQUEST['new_lan_ar']) . '")';
						mysql_query($ins_query);
						
						if(strlen(mysql_error()) >0) {
							$error_msg .= "<br>New entry failed: Reason: ". mysql_error();	
						}
					}
					else {
						$error_msg .= "<br>English text is empty";	
					}
				}
				else {
					$error_msg .= "<br>Key is empty";
				}
			}
		}
	}
}
?>
<body>
<form id="form1" name="form1" method="post" action="">
<p>
  <?php
$sql  = "select id,lan_key,english,arabic from seas_brd_trans order by id";
$rslt = mysql_query($sql);
?>
<table width="100%" cellpadding="0" cellspacing="0" border="1" style="border-collapse:collapse; border: thin #000">
<tr>
	<th width="20%" height="30px">Key</th>
    <th width="40%">English Text</th>
    <th width="40%">Arabic Text</th>
</tr>
<?php
while($row=mysql_fetch_assoc($rslt)) {
	echo "<tr>";
		echo '<td  align="left" valign="top" bgcolor="#FFFFFF">';
		echo '<input name="ID_'. $row['id'] .'" type="text" style="width:98%;border:0px" maxlength="100" value="'. $row['lan_key'].'" readonly="readonly" />'; 
		echo "</td>";
		echo '<td  align="left" valign="top" bgcolor="#FFFFFF">';
		echo '<textarea name="lan_'. $row['id'] .'_en" cols="45" rows="2" style="width:98%;border:0px" >'.$row['english'].'</textarea>';
		echo "</td>";
		echo '<td  align="left" valign="top" bgcolor="#FFFFFF">';
		echo '<textarea name="lan_'. $row['id'] .'_ar" cols="45" rows="2" style="width:98%;border:0px" dir="rtl" >'.$row['arabic'].'</textarea>';
		echo "</td>";
	echo "</tr>";
}
?>
</table>
  <p>New Entry</p>
  <table width="100%" border="1" cellspacing="0" cellpadding="5" style="border-collapse:collapse; border: thin #000">
    <tr>
      <th width="20%">Key</th>
      <th width="40%">English</th>
      <th width="40%">Arabic</th>
    </tr>
    <tr>
      <td align="left" valign="top"><input name="new_lan_key" type="text" id="new_lan_key" style="width:98%" maxlength="100" />
      <br />
      (Key Text Only in English)</td>
      <td align="left" valign="top"><textarea name="new_lan_en" id="new_lan_en" cols="45" rows="5" style="width:98%" ></textarea></td>
      <td align="left" valign="top"><textarea name="new_lan_ar" id="new_lan_ar" cols="45" rows="5" style="width:98%" dir="rtl" ></textarea></td>
    </tr>
    <tr>
      <td height="30" colspan="3" bgcolor="#FFFF33"><input type="submit" name="submit_button" id="button" value="Save" /><span class="err_msg">
      <?php
      
	  echo $error_msg;
	  
	  ?></span></td>
    </tr>
  </table>
</form>
</body>
</html>
