<?php
session_start();

include $_SERVER['DOCUMENT_ROOT'].'/common/lib/comm.php';

$upBaseDir = $_SERVER['DOCUMENT_ROOT'].UPLOAD_PATH."board";

//게시판 글쓰기  
$cd_seq = trim(avoid_crack($_POST["cd_seq"]));
$cd_board = trim(avoid_crack($_POST["cd_board"]));
$cd_user = trim(avoid_crack($_POST["cd_user"]));
$cd_userid = trim(avoid_crack($_POST["cd_userid"]));
$nm_title = trim(avoid_crack($_POST["nm_title"]));
$nm_content =$_POST["nm_content"];
$nm_reply =$_POST["nm_reply"];
$ds_open = trim(avoid_crack($_POST["ds_open"]));
$ds_kind = trim(avoid_crack($_POST["ds_kind"]));
$ds_top = trim(avoid_crack($_POST["ds_top"]));
$ds_main = trim(avoid_crack($_POST["ds_main"]));
$nm_name = trim(avoid_crack($_POST["nm_name"]));
$nm_etc1 = trim(avoid_crack($_POST["nm_etc1"]));
$nm_etc2 = trim(avoid_crack($_POST["nm_etc2"]));
$nm_etc3 = trim(avoid_crack($_POST["nm_etc3"]));
$nm_etc4 = trim(avoid_crack($_POST["nm_etc4"]));
$nm_etc5 = trim(avoid_crack($_POST["nm_etc5"]));
$nm_title = trim(avoid_crack($_POST["nm_title"]));
$url = trim(avoid_crack($_POST["url"]));

$no_pcd_seq = trim(avoid_crack($_POST["no_pcd_seq"]));
$org_upload_file_name = trim(avoid_crack($_POST["org_upload_file_name"]));


switch($mode) {

	case "Add" :

		$nm_content = addslashes($nm_content);
		
		$upload_file_name = "";
		$upload_path = $upBaseDir."/board_".$cd_board;
		
		if (is_uploaded_file($_FILES['nm_file']['tmp_name'])) { 
			if($_FILES['nm_file']['size'] > 0 ){
				$upload_file_name = upload($_FILES['nm_file'],$upload_path, 50, array('gif',  'jpeg', 'jpg','png','bmp','hwp','doc','docx','xls','xlsx','ppt','docx','pptx' ,'pdf', 'txt' ));

				if($upload_file_name==false){
					AlertBack("첨부파일 업로드 실패 하였습니다."); 
					die();
				}
			}
		}

		//계층형 관련 데이터 설정
		$no_famally=@mysql_result(mysql_query("SELECT (IFNULL(MIN(no_famally),0)) -(1) FROM board_master"),0,0);
		$no_order = 0;
		$no_step = 0;
		
		//파일 저장 
		$sql="insert into board_master
					(cd_board, cd_user, cd_userid, nm_name, nm_title, no_hit , ds_open, nm_content, nm_file, nm_updir, no_famally, no_order, no_step, nm_reply, dt_insert,nm_insert, dt_update, nm_update,ds_delind,  ds_top, ds_main, ds_kind,  nm_etc1, nm_etc2, nm_etc3,nm_etc4,nm_etc5  )
				values
					('$cd_board','$cd_user','$cd_userid','$nm_name','$nm_title',0,'$ds_open','$nm_content','$upload_file_name','$upload_path',$no_famally, $no_order, $no_step, '$nm_reply', now(),'$cd_userid',now(),'$cd_userid','N','$ds_top','$ds_main','$ds_kind','$nm_etc1','$nm_etc2','$nm_etc3','$nm_etc4','$nm_etc5')
				";
//echo $sql;
//exit;

		$result  = mysql_query($sql,$connect);

		if ($url == "") $url = "/mypage/qna_list.php?cd_board=".$cd_board."&ds_kind=".$ds_kind."";

		if ($result) {
			Redirect($url); 
		}else{
			AlertBack("등록 실패 하였습니다."); 
		}
	break;

	case "Reply" :

		$nm_content = addslashes($nm_content);
		
		$upload_file_name = "";
		$upload_path = $upBaseDir."/board_".$cd_board;
		if (is_uploaded_file($_FILES['nm_file']['tmp_name'])) { 
			if($_FILES['nm_file']['size'] > 0 ){
				$upload_file_name = upload($_FILES['nm_file'],$upload_path, 50, array('gif',  'jpeg', 'jpg','png','bmp','hwp','doc','docx','xls','xlsx','ppt','docx','pptx' ,'ppt' ,'pdf', 'txt' ));

				if($upload_file_name==false){
					AlertBack("첨부파일 업로드 실패 하였습니다."); 
					die();
				}
			}
		}
		
		//부모글 관련 정보 가져온다. 
		$sqlParent = "SELECT no_famally, no_order, no_step FROM  board_master WHERE cd_seq = $no_pcd_seq";
		$col=mysql_fetch_array(mysql_query($sqlParent));

		if (!empty($col[no_famally])){
			$no_famally = $col[no_famally];
			$no_order = $col[no_order];
			$no_step = $col[no_step];
			//ORDER 재정렬
			$sqlOrder = "UPDATE board_master SET no_order=no_order+1 WHERE no_famally=$no_famally and no_order>$no_order";
			mysql_query($sqlOrder,$connect);
		}else{
			AlertBack("등록 실패 하였습니다."); 
			die();
		}

		//DB저장 
		$sql="insert into board_master
					(cd_board, cd_userid, nm_name, nm_title, no_hit , ds_open, nm_content, nm_file, nm_updir, no_famally, no_order, no_step, dt_insert,nm_insert, dt_update, nm_update,ds_delind, ds_top, ds_main, ds_kind )
				values
					('$cd_board','$cd_userid','$nm_name' ,'$nm_title',0,'$ds_open','$nm_content','$upload_file_name','$upload_path',$no_famally, ($no_order+1), ($no_step+1), now(),'$cd_userid',now(),'$cd_userid','N','$ds_top','$ds_main','A')
				";

		$result  = mysql_query($sql,$connect);
		if ($result) {
			Redirect("/mypage/qna_list.php?cd_board=".$cd_board."&ds_kind=".$ds_kind.""); 
		}else{
			AlertBack("등록 실패 하였습니다."); 
		}
	break;
	
	//reply inquiry
	case "ReplyInquiry":
		
		//파일 저장 
		$sql="update board_master 
				set 
					nm_reply = '$nm_reply'
					,dt_reply = now()
					,dt_update = now()
					,nm_update = '$cd_userid'
				WHERE  cd_seq =$cd_seq";

		$result  = mysql_query($sql,$connect);
		if ($result) {
			Redirect("/mypage/qna_list.php?cd_board=".$cd_board."&ds_kind=".$ds_kind.""); 
		}else{
			AlertBack("수정 실패 하였습니다."); 
		}
	break;

	//글수정
	case "Update":
		
		$upload_file_name = "";
		$upload_path = $upBaseDir."/board_".$cd_board;
		if (is_uploaded_file($_FILES['nm_file']['tmp_name'])) { 
			if($_FILES['nm_file']['size'] > 0 ){
				$upload_file_name = upload($_FILES['nm_file'],$upload_path, 50, array('gif',  'jpeg', 'jpg','png','bmp','hwp','doc','docx','xls','xlsx','ppt','docx','pptx' ,'ppt' ,'pdf', 'txt' ));
				if($upload_file_name==false){
					AlertBack("첨부파일 업로드 실패 하였습니다."); 
					die();
				}
			}else{
				$upload_file_name = $org_upload_file_name;
			}
		}		
		if($upload_file_name=="" || $upload_file_name == null || $upload_file_name == false){
			$upload_file_name = $org_upload_file_name;
		}

		//파일 저장 
		$sql="update board_master 
				set 
					cd_board = '$cd_board'
					,nm_title = '$nm_title'
					,nm_name = '$nm_name'
					,ds_open = '$ds_open'
					,nm_content = '$nm_content'
					,nm_reply = '$nm_reply'
					,nm_file = '$upload_file_name'
					,nm_updir = '$upload_path'
					,dt_update = now()
					,nm_update = '$cd_userid'
					,ds_top = '$ds_top'
					,ds_main = '$ds_main'
					,ds_kind ='$ds_kind'
					,nm_etc1 ='$nm_etc1'
					,nm_etc2 ='$nm_etc2'
					,nm_etc3 ='$nm_etc3'
					,nm_etc4 ='$nm_etc4'
					,nm_etc5 ='$nm_etc5'
				WHERE  cd_seq =$cd_seq";

		$result  = mysql_query($sql,$connect);
		if ($result) {
			Redirect("/mypage/qna_list.php?cd_board=".$cd_board."&ds_kind=".$ds_kind.""); 
		}else{
			AlertBack("수정 실패 하였습니다."); 
		}
	break;

	//글삭제 처리 
	case "Delete" :

		$cd_seq = trim(avoid_crack($_POST["cd_seq"]));
		$cd_userid =  trim(avoid_crack($_POST["cd_userid"]));
		$cd_board =  trim(avoid_crack($_POST["cd_board"]));
		$PageNo = trim(avoid_crack($_POST["PageNo"]));
	
		$sql = "update board_master set ds_delind='Y' ,  nm_update = '$cd_userid' , dt_update = now() where cd_seq='$cd_seq'";

		$result  = mysql_query($sql,$connect);
		if ($result) {
			Redirect("/mypage/qna_list.php?cd_board=".$cd_board."&ds_kind=".$ds_kind.""); 
		 }else{
			AlertRedirect("삭제 실패하였습니다.","/mypage/qna_list.php?cd_board=".$cd_board."&ds_kind=".$ds_kind.""); 
		 }			 
	break;
	default :
		AlertRedirect("지정되지않은 요청입니다",ADMIN_PATH."index.php");
	break;
}
mysql_close($connect);
?>