<?php
//显示添加语言表单
function ShowCreateLanguage($hidden = 1) {
	$objResponse = new XajaxResponse();
	if($hidden == 1){
		$objResponse->addAssign('createlanguage','innerHTML',"<a href=\"\" onclick=\"xajax_ShowCreateLanguage(2);return false\" title=\"\">create language</a>");
		$objResponse->addAssign('h','style.display','block');
		}else{
		$objResponse->addAssign('createlanguage','innerHTML',"<a href=\"\" onclick=\"xajax_ShowCreateLanguage(1);return false\" title=\"\">create language</a>");
		$objResponse->addAssign('h','style.display','none');
		}
	return $objResponse;
}

function CreateLanguage($f1) {
	global $db,$tablefix,$LANG;
	$objResponse = new xajaxResponse();
	$name = xml_escape($f1['name']);
	$charset = xml_escape($f1['charset']);
	$ord = xml_escape($f1['ord']);
	$ord = (($ord == '') OR !is_numeric($ord)) ? '1' : $ord;

	if(empty($name)){
		$objResponse->addAlert("Language name is empty");
		}

	if(empty($charset)){
		$objResponse->addAlert("charset is empty");
		}
	
	if(!empty($name) AND !empty($charset)){
		//判断是否有上传文件
		if($_FILES['filename']['tmp_name'] <>""){
			require_once(ROOT_PATH . '/includes/class.upload.php');
			
			$fileFormat = array('gif','jpg','jpge','png');
			$upload =  new Upload( ROOT_PATH . '/attaches/', $fileFormat);
			if(!$upload->run('filename')){
				$objResponse->addAlert("upload image file false");
			}else{
			//如果成功上传则添加进数据库
			$filename = $upload->returnArray[0][saveName];
			$insertsql = "INSERT INTO `{$tablefix}language` (`languageid`,`name`,`filename`,`charset`,`ord`) VALUES (NULL,'$name','$filename','$charset','$ord')";
			if($db->query($insertsql)){
				//如果添加成功
				$languageid = $db->insertid();
				$innerHTML = <<<EOT
 <ul id="page_{$languageid}" class="two">
 <li>$languageid</li>
 <li>$name</li>
 <li><img src="../attaches/{$filename}" width="21" /></li>
 <li>$charset</li>
 <li>$ord</li>
 <li style="width:180px;"><a class="button1" href="" onclick="if(confirm('sure?')) xajax_DeleteLanguage($languageid);return false;" title="">$LANG[delete]</a> <a class="button1" href="" onclick="xajax_ModifyLanguage($languageid); return false;" title="">$LANG[edit]</a></li>
 </ul>
EOT;
				$objResponse->addAppend("language","innerHTML",$innerHTML);
				}
			}
			//如果没有文件上传
			}else{
			$insertsql = "INSERT INTO `{$tablefix}language` (`languageid`,`name`,`filename`,`charset`,`ord`) VALUES (NULL,'$name','','$charset','$ord')";
			if($db->query($insertsql)){
				//如果添加成功
				$languageid = $db->insertid();
				$innerHTML = <<<EOT
 <ul id="page_{$languageid}" class="two">
 <li>$languageid</li>
 <li>$name</li>
 <li>&nbsp;</li>
 <li>$charset</li>
 <li>$ord</li>
 <li style="width:180px;"><a class="button1" href="" onclick="if(confirm('sure?')) xajax_DeleteLanguage($languageid);return false;" title="">$LANG[delete]</a> <a class="button1" href="" onclick="xajax_ModifyLanguage($languageid); return false;" title="">$LANG[edit]</a></li>
 </ul>
EOT;
				$objResponse->addAppend("language","innerHTML",$innerHTML);
				}
			}
			//清空表单
			$objResponse->addClear('name','value');
			$objResponse->addClear('filename','value');
			$objResponse->addClear('charset','value');
			$objResponse->addClear('ord','value');
			$objResponse->addAssign("h","style.display","none");
		}
	
	return $objResponse;
}

//此函数未完全
function DeleteLanguage($languageid) {
	global $db,$tablefix;
	$languageid = xml_escape($languageid);
	$objResponse = new XajaxResponse();
	if(!is_numeric($languageid)){
		$objResponse->addAlert("language id is empty");
		}else{
		//取得filename,并删除图片
		$result = $db->query("SELECT `filename` FROM `{$tablefix}language` WHERE `languageid` = '$languageid'");
		$r = $db->fetch_array($result);
		@unlink('../attaches/'.$r['filename']);
		
		if($db->query("DELETE FROM `{$tablefix}language` WHERE `languageid` = '$languageid' LIMIT 1")){
			//执行删除 应该还要删除与语言相关的东西
			//$objResponse->addAlert("success delete a session");
			$objResponse->addRemove("page_".$languageid);
			}else{
			$objResponse->addAlert("failed to delete a session");
			}
		}

	return $objResponse;
}

function ModifyLanguage($languageid) {
	global $db,$tablefix,$LANG;
	$languageid = xml_escape($languageid);
	$objResponse = new XajaxResponse();
	if(!is_numeric($languageid)){
		$objResponse->addAlert("Language id error");
		}else{
		$result = $db->query("SELECT * FROM `{$tablefix}language` WHERE `languageid` = '$languageid' LIMIT 1");
		$language = $db->fetch_array($result);

		if(count($language)>0){
			if($language['filename'] != ''){
				$image = "<img src=\"../attaches/{$language[filename]}\" width=\"21\">";
				}else{
				$image = '';
				}
			$innerHTML = <<<EOT
<div id="midifyinput" style="margin:0 auto;padding:0;width:760px;">
<form id="f2" name="f2" onsubmit="return updateForm();" method="post" enctype="multipart/form-data">
<input type="hidden" name="languageid" value="{$language[languageid]}" />
<table id="addlanguage" class="th">
 <th>Language Name</th>
 <th>Icon(21px * 14px)</th>
 <th>Charset</th>
 <th>Order</th>
 <th width="150">Control</th>
   <tr>
 <td><input type="text" name="name" id="name" value="{$language[name]}" /></td>
 <td>$image<input type="file" name="filename" /></td>
 <td><input type="text" name="charset" id="charset" value="{$language[charset]}" /></td>
 <td><input type="text" name="ord" id="ord" value="{$language[ord]}" /></td>
 <td><input type="submit" class="button1" name="" value="$LANG[add]" /></td>
 </tr>
 </table>
 </form>
 </div>
EOT;
			$objResponse->addAssign("h","style.display",'');
			$objResponse->addAssign("h","innerHTML",$innerHTML);
			}else{
			$objResponse->addAlert("Language is not installed");
			}
		}
	return $objResponse;
}

function UpdateLanguage($f2) {
	global $db,$tablefix,$LANG;
	$objResponse = new xajaxResponse();
	$name = xml_escape($f2['name']);
	$charset = xml_escape($f2['charset']);
	$languageid = xml_escape($f2['languageid']);
	$ord = xml_escape($f2['ord']);
	$ord = (($ord == '') OR !is_numeric($ord)) ? '1' : $ord;

	if(empty($name)){
		$objResponse->addAlert("Language name is empty");
		}

	if(empty($charset)){
		//$objResponse->addAlert("charset is empty");
		}
	
	if(!empty($name) AND !empty($charset)){
		//判断是否有上传文件

		if($_FILES['filename']['tmp_name'] != ""){
			require_once(ROOT_PATH . '/includes/class.upload.php');
			
			$fileFormat = array('gif','jpg','jpge','png');
			$upload =  new Upload( ROOT_PATH . '/attaches/', $fileFormat);
			if(!$upload->run('filename')){
				$objResponse->addAlert("upload image file false");
			}else{
			//如果成功上传则添加进数据库
			$filename = $upload->returnArray[0][saveName];
			//获取原来的图片路径并删除
			$result = $db->query("SELECT `filename` FROM `{$tablefix}language` WHERE `languageid` = '$languageid' LIMIT 1");
			$image = $db->fetch_array($result);
			if(count($image)>0){
				@unlink('../attaches/'.$image['filename']);
				}
			
			//更新数据库
			$updatesql = "UPDATE `{$tablefix}language` SET `name` = '$name',`charset` = '$charset',`ord` = '$ord',`filename` = '$filename' WHERE `languageid` = '$languageid' LIMIT 1";
			if($db->query($updatesql)){
				//如果添加成功
				$RooPath = ROOT_PATH;
				$innerHTML = <<<EOT
 <li>$languageid</li>
 <li>$name</li>
 <li><img src="../attaches/{$filename}" width="21" /></li>
 <li>$charset</li>
 <li>$ord</li>
 <li style="width:180px;"><a class="button1" href="" onclick="if(confirm('sure?')) xajax_DeleteLanguage($languageid);return false;" title="">$LANG[delete]</a> <a class="button1" href="" onclick="xajax_ModifyLanguage($languageid); return false;" title="">$LANG[edit]</a></li>
EOT;
				$objResponse->addAssign("page_{$languageid}","innerHTML",$innerHTML);
				
				}
			}
			//如果没有文件上传
			}else{
			//更新数据库
			$updatesql = "UPDATE `{$tablefix}language` SET `name` = '$name',`charset` = '$charset',`ord` = '$ord' WHERE `languageid` = '$languageid' LIMIT 1";
			if($db->query($updatesql)){
				//如果添加成功
				$result = $db->query("SELECT `filename` FROM `{$tablefix}language` WHERE `languageid` = '$languageid' LIMIT 1");
				$image = $db->fetch_array($result);
				if(count($image)>0){
					$filename = $image['filename'];
					}
				$innerHTML = <<<EOT
 <li>$languageid</li>
 <li>$name</li>
 <li><img src="../attaches/{$filename}" width="21" /></li>
 <li>$charset</li>
 <li>$ord</li>
 <li style="width:180px;"><a class="button1" href="" onclick="if(confirm('sure?')) xajax_DeleteLanguage($languageid);return false;" title="">$LANG[delete]</a> <a class="button1" href="" onclick="xajax_ModifyLanguage($languageid); return false;" title="">$LANG[edit]</a></li>
EOT;
				$objResponse->addAssign("page_{$languageid}","innerHTML",$innerHTML);
				}
			}
			//隐藏表单
			$objResponse->addAssign("h","style.display","none");
		}

	return $objResponse;
}
?>