<?

//將頁面處理值送回主模組	

function main_page()
{	
	global $action;
	
	//流程控制
	switch ($action)
	{   

		//重傳
   		case "upload_ok":
      		$buffer = upload_ok();      		
      		break;
	
		//重傳
   		case "upload":      	
      		$buffer = upload();      		
      		break;
	
		//刪除
   		case "delete":      	
      		$buffer = delete();      		
      		break;

		//修改
   		case "modify_ok":      	
      		$buffer = modify_ok();   		
      		break;

		//修改
   		case "modify":      	
      		$buffer = modify();   		
      		break;
        
	        //預設畫面
   		default:
      		$buffer = step0();      		
      		break;
	}	
	
	return $buffer;
}


//預設頁	
function step0()
{
	global $page,$uid,$file;	

	$buffer = "<br>";
	
	$array['{TOP1_A}'] = "熱　情";
	$array['{TOP1_B}'] = "智　慧";
	$array['{TOP1_C}'] = "科　技";
	
	$buffer .= read_and_parser("top1.php",$array);	
	unset($array);		
	
	/* 查詢論文類別 */
	$info = sql_b("SELECT A.id,A.paper_type,B.name,A.name,A.author,A.file_type,A.owner FROM paper A, paper_type_list B WHERE A.paper_type = B.type ORDER BY A.upload_time DESC");	
	$replay = '<tr><td bgcolor="#CCFFCC" align=center>{1}</td><td>{2}</div></td><td><a href="papers/{1}.{5}" target="_blank">{3}</a></td><td>{4}</td><td bgcolor="#FF0000"><input type="button" value="編" onclick="location.href=\'administrator.php?page=paper_submit_manage&action=modify&id={1}&owner={6}\'"><input type="button" value="刪" onclick="if(confirm(\'您確定要刪除？\'))location.href=\'administrator.php?page=paper_submit_manage&action=delete&id={1}&owner={6}\'"><input type="button" value="傳" onclick="location.href=\'administrator.php?page=paper_submit_manage&action=upload&id={1}&owner={6}\'"></td></tr>';

	for($i=0;$i<sizeof($info);$i++)
	{
		$tempx = $replay;
		
		unset($temp);
		$change[1] = $info[$i][1] . checklen($info[$i][0],3);
		$change[2] = $info[$i][2];
		$change[3] = $info[$i][3];
		$change[4] = $info[$i][4];
		$change[5] = $info[$i][5];
		$change[6] = $info[$i][6];
				
		for($j=1;$j<7;$j++)
		{
			$tempx = str_replace("{" . $j . "}", $change[$j] , $tempx);
		}
		
		$my_temp .= $tempx;
	}
		
	$tempn = file_get_contents('require/page_manage.htm');
	$tempn = str_replace('{LIST_TAG}',$my_temp, $tempn);
				
	$array['{MIDDLE_PANEL_TITLE}'] = '論　文　管　理';
	$array['{MIDDLE_PANEL_CONTENT}'] = $tempn;
	
	//if($uid=='SATURN') $array['{MIDDLE_PANEL_CONTENT}'] = '您必須先 <a href="administrator.php?page=member_login">登入系統</a>，才能夠執行這個動作唷！';
	
	$buffer = read_and_parser("middle.php",$array);
	
	unset($array);
	
	return $buffer;
}

//修改
function modify()
{
	global $page,$id,$uid,$owner;	
	
	$paper_type = substr($id,0,1);
	$paper_id = substr($id,1,3) * 1;	
	
	/*讀取資料庫*/	
	$info = sql_b("SELECT paper_type, name, author, file_type FROM paper WHERE owner = '$owner' AND id = '$paper_id'");
	
	$buffer = "<br>";
	
	$array['{TOP1_A}'] = "熱　情";
	$array['{TOP1_B}'] = "智　慧";
	$array['{TOP1_C}'] = "科　技";
	
	$buffer .= read_and_parser("top1.php",$array);	
	unset($array);
	
	$array['{MIDDLE_PANEL_TITLE}'] = '論　文　管　理(修改)';	
	
	/* 查詢論文類別 */
	$info2 = sql_b("SELECT type,name FROM paper_type_list order by type");
	$xtemp = buffer_select_default('temp_array[0]',$info2,$info[0][0]);
	
	$old_type = $info[0][0];
	
	$change[1] = $id;	
	$change[2] = $xtemp;
	$change[3] = $info[0][1];
	$change[4] = $info[0][2];
	$file_type = $info[0][3];
		
	$tempn = file_get_contents('require/paper_modify.htm');
			
	for($j=1;$j<6;$j++)
	{
		$tempn = str_replace("{" . $j . "}", $change[$j] , $tempn);
	}	
	
	$array['{MIDDLE_PANEL_CONTENT}'] = '<form method="POST" action="administrator.php">';
	$array['{MIDDLE_PANEL_CONTENT}'] .= "<input type=hidden name=page value=$page>";
	$array['{MIDDLE_PANEL_CONTENT}'] .= "<input type=hidden name=owner value=$owner>";
	$array['{MIDDLE_PANEL_CONTENT}'] .= "<input type=hidden name=action value=modify_ok>";
	$array['{MIDDLE_PANEL_CONTENT}'] .= "<input type=hidden name=paper_id value=$paper_id>";
	$array['{MIDDLE_PANEL_CONTENT}'] .= "<input type=hidden name=file_type value=$file_type>";
	$array['{MIDDLE_PANEL_CONTENT}'] .= "<input type=hidden name=old_type value=$old_type>";
	$array['{MIDDLE_PANEL_CONTENT}'] .= $tempn;
	$array['{MIDDLE_PANEL_CONTENT}'] .= "</form>";	
	
	$buffer .= read_and_parser("middle.php",$array);
	unset($array);

	return $buffer;
}


//修改完成
function modify_ok()
{
	global $page,$temp_array,$uid,$paper_id,$file_type,$old_type,$owner;
	
	if(!$temp_array[1]) error_message("您的【論文名稱】欄沒有輸入唷!");
	if(!$temp_array[2]) error_message("您的【論文作者】欄沒有輸入唷!");	
	
	/*回寫資料庫*/	
	
	$sql = "UPDATE paper SET paper_type = '$temp_array[0]', name = '$temp_array[1]', author ='$temp_array[2]' WHERE owner = '$owner' AND id = '$paper_id'";		
	sql_a($sql);	
	
	/*更改檔名*/	
	@rename('./papers/'. $old_type . checklen($paper_id,3) . '.' . $file_type , './papers/'. $temp_array[0] . checklen($paper_id,3) . '.' . $file_type); 
			
	$buffer = "<br>";
	
	$array['{TOP1_A}'] = "熱　情";
	$array['{TOP1_B}'] = "智　慧";
	$array['{TOP1_C}'] = "科　技";
	
	$buffer .= read_and_parser("top1.php",$array);	
	unset($array);
	
	$array['{MIDDLE_PANEL_TITLE}'] = '論　文　管　理(修改)';	
	$array['{MIDDLE_PANEL_CONTENT}'] .= file_get_contents('require/modify_ok.htm');	
	$buffer .= read_and_parser("middle.php",$array);
	unset($array);
	
	/* 顯示上傳資料 */
	
	$temp = sql_b("SELECT name FROM paper_type_list WHERE type = '$temp_array[0]'");
	$kkk[] = $temp[0][0];	
	
	$temp_ccc[] = $temp_array[0] . checklen($paper_id,3);
	$temp_ccc[] = $temp_array[0] . '-' . $kkk[0];
	$temp_ccc[] = $temp_array[1];
	$temp_ccc[] = $temp_array[2];
	$temp_ccc[] = $temp_array[0] . checklen($paper_id,3). '.' . $file_type;
	
	for($i=0;$i<sizeof($temp_ccc);$i++)
	{
		$buffer = str_replace('{' . ($i+1) . '}',$temp_ccc[$i],$buffer);
	}
	
	return $buffer;
}


//刪除
function delete()
{
	global $page,$id,$uid,$owner;		
	
	$paper_type = substr($id,0,1);
	$paper_id = substr($id,1,3) * 1;
	
	/*讀取資料庫*/	
	$info = sql_b("SELECT file_type FROM paper WHERE owner = '$owner' AND id = '$paper_id'");		
	$file_type = $info[0][0];
	
	/*先殺檔案*/
	@unlink("./papers/" . $id . '.' . $file_type);	
	
	$sql = "DELETE FROM paper WHERE id  = '$paper_id'";
	sql_a($sql);
	
	$buffer = "<br>";
	
	$array['{TOP1_A}'] = "熱　情";
	$array['{TOP1_B}'] = "智　慧";
	$array['{TOP1_C}'] = "科　技";
	
	$buffer .= read_and_parser("top1.php",$array);	
	unset($array);
	
	$array['{MIDDLE_PANEL_TITLE}'] = '論　文　管　理(刪除)';	
	$array['{MIDDLE_PANEL_CONTENT}'] .= file_get_contents('require/delete_ok.htm');	
	$buffer .= read_and_parser("middle.php",$array);
	unset($array);

	return $buffer;
}


//預設頁	
function upload()
{
	global $page,$uid,$id,$owner;	

	$buffer = "<br>";
	
	$array['{TOP1_A}'] = "熱　情";
	$array['{TOP1_B}'] = "智　慧";
	$array['{TOP1_C}'] = "科　技";
	
	$buffer .= read_and_parser("top1.php",$array);	
	unset($array);	
	
	$array['{MIDDLE_PANEL_TITLE}'] = '重　傳　論　文';
	$array['{MIDDLE_PANEL_CONTENT}'] = '<form method="POST" action="administrator.php" enctype="multipart/form-data">';
	$array['{MIDDLE_PANEL_CONTENT}'] .= "<input type=hidden name=page value=$page>";
	$array['{MIDDLE_PANEL_CONTENT}'] .= "<input type=hidden name=owner value=$owner>";
	$array['{MIDDLE_PANEL_CONTENT}'] .= "<input type=hidden name=action value=upload_ok>";
	$array['{MIDDLE_PANEL_CONTENT}'] .= "<input type=hidden name=id value=$id>";
	$array['{MIDDLE_PANEL_CONTENT}'] .= file_get_contents('require/reupload0.htm');
	$array['{MIDDLE_PANEL_CONTENT}'] .= "</form>";
	
	if($uid=='SATURN') $array['{MIDDLE_PANEL_CONTENT}'] = '您必須先 <a href="administrator.php?page=member_login">登入系統</a>，才能夠執行這個動作唷！';
	
	$tempp = $array['{MIDDLE_PANEL_CONTENT}'];	
	$tempp = str_replace("{1}",$id,$tempp);
	$array['{MIDDLE_PANEL_CONTENT}'] = $tempp;
	
	$buffer .= read_and_parser("middle.php",$array);
	unset($array);
	
	return $buffer;
}

//重傳ok
function upload_ok()
{
	global $page,$temp_array,$uid,$id,$owner;
	global $uploads, $uploads_name, $uploads_size;
	
	if(!$uploads) error_message("您的【要上傳的檔案】欄沒有輸入唷!");
	
	
	/*上傳檔案 & 更新資料庫*/
	
	
	/*讀取資料庫*/	
	$paper_id = substr($id,1,3) * 1;
	$info = sql_b("SELECT file_type FROM paper WHERE owner = '$owner' AND id = '$paper_id'");
	$file_type = $info[0][0];
	
	/*砍舊檔*/
	@unlink("papers/". $id. '.' . $file_type);
	
	/*放新檔*/
	$file_type = strtolower(substr($uploads_name,-3,3));
	@copy($uploads,"papers/". $id. '.' . $file_type);	
	@unlink($uploads);
	
	/*回寫檔案類型*/
	
	$sql = "UPDATE paper SET file_type = '$file_type', upload_time = NOW()";
	sql_a($sql);	
	
	$buffer = "<br>";
	
	$array['{TOP1_A}'] = "熱　情";
	$array['{TOP1_B}'] = "智　慧";
	$array['{TOP1_C}'] = "科　技";
	
	$buffer .= read_and_parser("top1.php",$array);	
	unset($array);
	
	$array['{MIDDLE_PANEL_TITLE}'] = '重　傳　論　文';	
	$array['{MIDDLE_PANEL_CONTENT}'] .= file_get_contents('require/reupload1.htm');	
	$buffer .= read_and_parser("middle.php",$array);
	unset($array);
	
	return $buffer;
}
?>