<?
//제품 등록하기
function insertProduct(){
	//제품정보 테이블
	$tbl = $GLOBALS["_conf_tbl"]["product"];

	$arrInfo = getCategoryInfo(mysql_real_escape_string($_POST[cat_no]));

	//대표이미지 설정
	if(strlen(mysql_real_escape_string($_POST[p_image])) > 1){
		$p_image = mysql_real_escape_string($_POST[p_image]);
	}else{
		$p_image = $arrCurInfo["list"][0][p_image];
	}

	//아이콘등록
	for($i=0; $i < count($_POST[shop_icon]); $i++){
			$str_icons .= $_POST[shop_icon][$i];
			if($i != count($_POST[shop_icon])-1){
					$str_icons .= "|";
			}
	}

	for ($i=0; $i<count($_POST['rel_product']); $i++) {
		$_ridx .= $_POST['rel_product'][$i].",";
	}
	$ridx = rtrim($_ridx,",");

	//제품정보 테이블에 입력
	$sql = "INSERT INTO ".$tbl." set 
		cat_no='".$arrInfo["list"][0][cat_no]."',
		cat_code='".$arrInfo["list"][0][cat_code]."',
		sort_num='".mysql_real_escape_string($_POST[sort_num])."',
		p_name='".mysql_real_escape_string($_POST[p_name])."',
		p_image='".$p_image."',
		memo='".mysql_real_escape_string($_POST[memo])."',
		memo1='".mysql_real_escape_string($_POST[memo1])."',
		memo2='".mysql_real_escape_string($_POST[memo2])."',
		contents='".mysql_real_escape_string($_POST[contents])."',
		contents1='".mysql_real_escape_string($_POST[contents1])."',
		contents2='".mysql_real_escape_string($_POST[contents2])."',
		show_recommand='".mysql_real_escape_string($_POST[show_recommand])."',
		show_best_lg='".mysql_real_escape_string($_POST[show_best_lg])."',
		show_best='".mysql_real_escape_string($_POST[show_best])."',
		show_main='".mysql_real_escape_string($_POST[show_main])."',
		etc_1='".mysql_real_escape_string($_POST[etc_1])."',
		etc_2='".mysql_real_escape_string($_POST[etc_2])."',
		etc_3='".mysql_real_escape_string($_POST[etc_3])."',
		etc_4='".mysql_real_escape_string($_POST[etc_4])."',
		etc_5='".mysql_real_escape_string($_POST[etc_5])."',
		etc_6='".mysql_real_escape_string($_POST[etc_6])."',
		etc_7='".mysql_real_escape_string($_POST[etc_7])."',
		etc_8='".mysql_real_escape_string($_POST[etc_8])."',
		etc_9='".mysql_real_escape_string($_POST[etc_9])."',
		etc_10='".mysql_real_escape_string($_POST[etc_10])."',
		icons='".mysql_real_escape_string($str_icons)."',
		rel_idx='".$ridx."',
		wdate=now()
	";

	$rs = mysql_query($sql, $GLOBALS[dblink]);
	$insert_idx = mysql_insert_id($GLOBALS[dblink]);
	$total = mysql_affected_rows($GLOBALS[dblink]);

	//파일 저장 디렉토리 생성
	rmkdir($GLOBALS["_SITE"]["UPLOADED_DATA"]."/product/".$insert_idx, 0777);

	//이미지 파일처리
	inputProductFiles($insert_idx, $_FILES);
	//카탈로그 파일
	inputCatalogFiles($insert_idx, $_FILES);

	if($total > 0){
		return true;
	}else{
		return false;
	}

}

//제품 수정하기
function editProduct($idx){
	//제품정보 테이블
	$tbl = $GLOBALS["_conf_tbl"]["product"];

	$arrInfo = getCategoryInfo(mysql_real_escape_string($_POST[cat_no]));

	//현재 정보 가져오기
	$arrCurInfo = getArticleInfo($tbl, $idx);

	//대표이미지 설정
	if(strlen(mysql_real_escape_string($_POST[p_image])) > 1){
		$p_image = mysql_real_escape_string($_POST[p_image]);
	}else{
		$p_image = $arrCurInfo["list"][0][p_image];
	}
	
	//아이콘등록
	for($i=0; $i < count($_POST[shop_icon]); $i++){
			$str_icons .= $_POST[shop_icon][$i];
			if($i != count($_POST[shop_icon])-1){
					$str_icons .= "|";
			}
	}
	
	for ($i=0; $i<count($_POST['rel_product']); $i++) {
		$_ridx .= $_POST['rel_product'][$i].",";
	}
	$ridx = rtrim($_ridx,",");

	//제품정보 테이블에 입력
	$sql = "UPDATE ".$tbl." set 
		cat_no='".$arrInfo["list"][0][cat_no]."',
		cat_code='".$arrInfo["list"][0][cat_code]."',
		sort_num='".mysql_real_escape_string($_POST[sort_num])."',
		p_name='".mysql_real_escape_string($_POST[p_name])."',
		p_image='".$p_image."',
		memo='".mysql_real_escape_string($_POST[memo])."',
		memo1='".mysql_real_escape_string($_POST[memo1])."',
		memo2='".mysql_real_escape_string($_POST[memo2])."',
		contents='".mysql_real_escape_string($_POST[contents])."',
		contents1='".mysql_real_escape_string($_POST[contents1])."',
		contents2='".mysql_real_escape_string($_POST[contents2])."',
		show_recommand='".mysql_real_escape_string($_POST[show_recommand])."',
		show_best_lg='".mysql_real_escape_string($_POST[show_best_lg])."',
		show_best='".mysql_real_escape_string($_POST[show_best])."',
		show_main='".mysql_real_escape_string($_POST[show_main])."',
		etc_1='".mysql_real_escape_string($_POST[etc_1])."',
		icons='".mysql_real_escape_string($str_icons)."',
		etc_2='".mysql_real_escape_string($_POST[etc_2])."',
		etc_3='".mysql_real_escape_string($_POST[etc_3])."',
		etc_4='".mysql_real_escape_string($_POST[etc_4])."',
		etc_5='".mysql_real_escape_string($_POST[etc_5])."',
		etc_6='".mysql_real_escape_string($_POST[etc_6])."',
		etc_7='".mysql_real_escape_string($_POST[etc_7])."',
		etc_8='".mysql_real_escape_string($_POST[etc_8])."',
		etc_9='".mysql_real_escape_string($_POST[etc_9])."',
		etc_10='".mysql_real_escape_string($_POST[etc_10])."',
		rel_idx='".$ridx."' 
		WHERE idx = '".$arrCurInfo["list"][0][idx]."'
	";

	$rs = mysql_query($sql, $GLOBALS[dblink]);

	//이미지 파일처리
	delProductFiles($arrCurInfo["list"][0][idx], $_FILES);
	inputProductFiles($arrCurInfo["list"][0][idx], $_FILES);

	//카탈로그 파일처리
	delCatalogFiles($arrCurInfo["list"][0][idx], $_FILES);
	inputCatalogFiles($arrCurInfo["list"][0][idx], $_FILES);
	//exit;

	if($rs > 0){
		return true;
	}else{
		return false;
	}


}

//제품 파일처리
function inputProductFiles($idx, $HTTP_POST_FILES){
	//이미지파일 처리

	for($i=0;$i<count($HTTP_POST_FILES[photo_file][error]);$i++){
		if ($HTTP_POST_FILES[photo_file][error][$i] == 0){
		    //확장자 검사후 파일이름 생성
		    $filename = $HTTP_POST_FILES[photo_file][name][$i];
		    $attach_ext = explode(".",$filename);
		    $extension = $attach_ext[sizeof($attach_ext)-1];
		    $extension = strtolower($extension);		    
		    $filerename = md5(time()) . $i . "." . $extension;
	  		$filesize = $HTTP_POST_FILES[photo_file][size][$i];
	  		$filetype = $HTTP_POST_FILES[photo_file][type][$i];
				
		    // 파일 확장자 검사
		    if(!strcmp($extension,"htm") ||!strcmp($extension,"html") ||!strcmp($extension,"phtml") ||!strcmp($extension,"php") ||!strcmp($extension,"php3") ||!strcmp($extension,"php4") ||!strcmp($extension,"inc") ||!strcmp($extension,"pl") ||!strcmp($extension,"cgi")){
				jsMsg("not allowed file extension");
		        jsHistory("-1");
		    }
			
			if (is_uploaded_file($HTTP_POST_FILES[photo_file][tmp_name][$i])) {	
				move_uploaded_file ($HTTP_POST_FILES[photo_file][tmp_name][$i],$GLOBALS["_SITE"]["UPLOADED_DATA"]."/product/".$idx."/".$filerename);
				//썸네일 만들기
				if($filetype=="image/pjpeg" || $filetype=="image/x-png" || $filetype=="image/jpeg" || $filetype=="image/png" || $filetype=="image/gif"){
					$tmpImageSize = @getimagesize($GLOBALS["_SITE"]["UPLOADED_DATA"]."/product/".$idx."/".$filerename);
					MakeThum($GLOBALS["_SITE"]["UPLOADED_DATA"]."/product/".$idx."/".$filerename, $GLOBALS["_SITE"]["UPLOADED_DATA"]."/product/".$idx."/s_".$filerename, $GLOBALS["_SITE"]["SHOP"]["IMAGE_S_WIDTH"]);
					MakeThum($GLOBALS["_SITE"]["UPLOADED_DATA"]."/product/".$idx."/".$filerename, $GLOBALS["_SITE"]["UPLOADED_DATA"]."/product/".$idx."/m_".$filerename, $GLOBALS["_SITE"]["SHOP"]["IMAGE_M_WIDTH"]);
					MakeThum($GLOBALS["_SITE"]["UPLOADED_DATA"]."/product/".$idx."/".$filerename, $GLOBALS["_SITE"]["UPLOADED_DATA"]."/product/".$idx."/l_".$filerename, $GLOBALS["_SITE"]["SHOP"]["IMAGE_L_WIDTH"]);

				}
			}

			$sql = "insert into ".$GLOBALS["_conf_tbl"]["product_files"]." set 
				b_idx='".$idx."',/* 글 번호 id*/
				ori_name='".$filename."',/*파일원본이름*/
				re_name='".$filerename."',/*md5로 변환된 파일이름*/
				type='".$filetype."',/*파일타입*/
				ext ='".$extension."',/*파일확장자*/
				size='".$filesize."',/*첨부파일 용량*/
				width='".$tmpImageSize[0]."',/*첨부파일 가로길이*/
				height='".$tmpImageSize[1]."',/*첨부파일 세로길이*/
				wdate=now()
			";
			$rsf = mysql_query($sql,$GLOBALS[dblink]);

			//대표이미지 업데이트
			if($_POST[p_image]==$i){
				$sql = "update ".$GLOBALS["_conf_tbl"]["product"]." set p_image='$filerename' WHERE idx='$idx' ";
				mysql_query($sql,$GLOBALS[dblink]);
			}
		}
	}
}

//파일정보 가져오기
function getProductFileInfo($b_idx, $idx){
	$tbl = $GLOBALS["_conf_tbl"]["product_files"];

    $sql  = "SELECT * ";
    $sql .= "FROM " .$tbl." ";
    $sql .= "WHERE b_idx = '$b_idx' ";
    $sql .= "AND idx = '$idx' ";

    $rs = mysql_query($sql, $GLOBALS[dblink]);
    $total_rs = mysql_num_rows($rs);
    
    if($total_rs > 0){
        $list['total'] = $total_rs;
        for($i=0; $i < $total_rs; $i++){
            $list['list'][$i] = mysql_fetch_assoc($rs);
        }
    }else{
        $list['total'] = 0;
    }
    return $list;
}

//제품 파일 삭제 처리
function delProductFiles($idx, $HTTP_POST_FILES){
	//제품정보 테이블
	$tbl = $GLOBALS["_conf_tbl"]["product"];
	$tbl_files = $GLOBALS["_conf_tbl"]["product_files"];

	//현재 정보 가져오기
	$arrCurInfo = getArticleInfo($tbl, $idx);

	//이미지 파일삭제 코딩 시작 - 삭제체크 한것만 처리
	for($i=0;$i<count($_POST[delPhoto]);$i++){
		if($_POST[delPhoto][$i]>0){
			$fileinfo = getProductFileInfo($arrCurInfo["list"][0][idx], $_POST[delPhoto][$i]);
			//디비에서 파일정보 삭제
			mysql_query("DELETE FROM ".$tbl_files." WHERE idx='".$fileinfo["list"][0][idx]."' ", $GLOBALS[dblink]);
			//디스크에서 파일 삭제
			unlink($GLOBALS["_SITE"]["UPLOADED_DATA"]."/product/" . $arrCurInfo["list"][0][idx]."/".$fileinfo["list"][0][re_name]);
			unlink($GLOBALS["_SITE"]["UPLOADED_DATA"]."/product/" . $arrCurInfo["list"][0][idx]."/l_".$fileinfo["list"][0][re_name]);
			unlink($GLOBALS["_SITE"]["UPLOADED_DATA"]."/product/" . $arrCurInfo["list"][0][idx]."/m_".$fileinfo["list"][0][re_name]);
			unlink($GLOBALS["_SITE"]["UPLOADED_DATA"]."/product/" . $arrCurInfo["list"][0][idx]."/s_".$fileinfo["list"][0][re_name]);
		}
	}
	//이미지 파일삭제 코딩 종료
}



//카탈로그 파일처리
function inputCatalogFiles($idx, $HTTP_POST_FILES){

	for($i=0;$i<count($HTTP_POST_FILES[catalog_file][error]);$i++){
		if ($HTTP_POST_FILES[catalog_file][error][$i] == 0){
		    //확장자 검사후 파일이름 생성
		    $filename = $HTTP_POST_FILES[catalog_file][name][$i];
		    $attach_ext = explode(".",$filename);
		    $extension = $attach_ext[sizeof($attach_ext)-1];
		    $extension = strtolower($extension);		    
		    $filerename = "catalog_" . md5(time()) . $i . "." . $extension;
	  		$filesize = $HTTP_POST_FILES[catalog_file][size][$i];
	  		$filetype = $HTTP_POST_FILES[catalog_file][type][$i];
				
		    // 파일 확장자 검사
		    if(!strcmp($extension,"htm") ||!strcmp($extension,"html") ||!strcmp($extension,"phtml") ||!strcmp($extension,"php") ||!strcmp($extension,"php3") ||!strcmp($extension,"php4") ||!strcmp($extension,"inc") ||!strcmp($extension,"pl") ||!strcmp($extension,"cgi")){
				jsMsg("not allowed file extension");
		        jsHistory("-1");
		    }
			
			if (is_uploaded_file($HTTP_POST_FILES[catalog_file][tmp_name][$i])) {	
				move_uploaded_file ($HTTP_POST_FILES[catalog_file][tmp_name][$i],$GLOBALS["_SITE"]["UPLOADED_DATA"]."/product/".$idx."/".$filerename);
			}

			$sql = "insert into ".$GLOBALS["_conf_tbl"]["catalog_files"]." set 
				b_idx='".$idx."',/* 글 번호 id*/
				ori_name='".$filename."',/*파일원본이름*/
				re_name='".$filerename."',/*md5로 변환된 파일이름*/
				type='".$filetype."',/*파일타입*/
				ext ='".$extension."',/*파일확장자*/
				size='".$filesize."',/*첨부파일 용량*/
				wdate=now()
			";
			$rsf = mysql_query($sql,$GLOBALS[dblink]);
		}
	}

	for($i=0;$i<count($HTTP_POST_FILES[catalog_file1][error]);$i++){
		if ($HTTP_POST_FILES[catalog_file1][error][$i] == 0){
		    //확장자 검사후 파일이름 생성
		    $filename = $HTTP_POST_FILES[catalog_file1][name][$i];
		    $attach_ext = explode(".",$filename);
		    $extension = $attach_ext[sizeof($attach_ext)-1];
		    $extension = strtolower($extension);		    
		    $filerename = "catalog1_" . $filename . "." . $extension;
	  		$filesize = $HTTP_POST_FILES[catalog_file1][size][$i];
	  		$filetype = $HTTP_POST_FILES[catalog_file1][type][$i];
				
		    // 파일 확장자 검사
		    if(!strcmp($extension,"htm") ||!strcmp($extension,"html") ||!strcmp($extension,"phtml") ||!strcmp($extension,"php") ||!strcmp($extension,"php3") ||!strcmp($extension,"php4") ||!strcmp($extension,"inc") ||!strcmp($extension,"pl") ||!strcmp($extension,"cgi")){
				jsMsg("not allowed file extension");
		        jsHistory("-1");
		    }
			
			if (is_uploaded_file($HTTP_POST_FILES[catalog_file1][tmp_name][$i])) {	
				move_uploaded_file ($HTTP_POST_FILES[catalog_file1][tmp_name][$i],$GLOBALS["_SITE"]["UPLOADED_DATA"]."/product/".$idx."/".$filerename);
			}

			$sql = "insert into ".$GLOBALS["_conf_tbl"]["catalog_files"]." set 
				b_idx='".$idx."',/* 글 번호 id*/
				ori_name='".$filename."',/*파일원본이름*/
				re_name='".$filerename."',/*md5로 변환된 파일이름*/
				type='".$filetype."',/*파일타입*/
				ext ='".$extension."',/*파일확장자*/
				size='".$filesize."',/*첨부파일 용량*/
				type2='catalog1',/*첨부파일 용량*/
				wdate=now()
			";
			$rsf = mysql_query($sql,$GLOBALS[dblink]);
		}
	}
	for($i=0;$i<count($HTTP_POST_FILES[catalog_file2][error]);$i++){
		if ($HTTP_POST_FILES[catalog_file2][error][$i] == 0){
		    //확장자 검사후 파일이름 생성
		    $filename = $HTTP_POST_FILES[catalog_file2][name][$i];
		    $attach_ext = explode(".",$filename);
		    $extension = $attach_ext[sizeof($attach_ext)-1];
		    $extension = strtolower($extension);		    
		    $filerename = "catalog2_" . $filename . "." . $extension;
	  		$filesize = $HTTP_POST_FILES[catalog_file2][size][$i];
	  		$filetype = $HTTP_POST_FILES[catalog_file2][type][$i];
				
		    // 파일 확장자 검사
		    if(!strcmp($extension,"htm") ||!strcmp($extension,"html") ||!strcmp($extension,"phtml") ||!strcmp($extension,"php") ||!strcmp($extension,"php3") ||!strcmp($extension,"php4") ||!strcmp($extension,"inc") ||!strcmp($extension,"pl") ||!strcmp($extension,"cgi")){
				jsMsg("not allowed file extension");
		        jsHistory("-1");
		    }
			
			if (is_uploaded_file($HTTP_POST_FILES[catalog_file2][tmp_name][$i])) {	
				move_uploaded_file ($HTTP_POST_FILES[catalog_file2][tmp_name][$i],$GLOBALS["_SITE"]["UPLOADED_DATA"]."/product/".$idx."/".$filerename);
			}

			$sql = "insert into ".$GLOBALS["_conf_tbl"]["catalog_files"]." set 
				b_idx='".$idx."',/* 글 번호 id*/
				ori_name='".$filename."',/*파일원본이름*/
				re_name='".$filerename."',/*md5로 변환된 파일이름*/
				type='".$filetype."',/*파일타입*/
				ext ='".$extension."',/*파일확장자*/
				size='".$filesize."',/*첨부파일 용량*/
				type2='catalog2',/*첨부파일 용량*/
				wdate=now()
			";
			$rsf = mysql_query($sql,$GLOBALS[dblink]);
		}
	}
	for($i=0;$i<count($HTTP_POST_FILES[catalog_file3][error]);$i++){
		if ($HTTP_POST_FILES[catalog_file3][error][$i] == 0){
		    //확장자 검사후 파일이름 생성
		    $filename = $HTTP_POST_FILES[catalog_file3][name][$i];
		    $attach_ext = explode(".",$filename);
		    $extension = $attach_ext[sizeof($attach_ext)-1];
		    $extension = strtolower($extension);		    
		    $filerename = "catalog3_" . $filename . "." . $extension;
	  		$filesize = $HTTP_POST_FILES[catalog_file3][size][$i];
	  		$filetype = $HTTP_POST_FILES[catalog_file3][type][$i];
				
		    // 파일 확장자 검사
		    if(!strcmp($extension,"htm") ||!strcmp($extension,"html") ||!strcmp($extension,"phtml") ||!strcmp($extension,"php") ||!strcmp($extension,"php3") ||!strcmp($extension,"php4") ||!strcmp($extension,"inc") ||!strcmp($extension,"pl") ||!strcmp($extension,"cgi")){
				jsMsg("not allowed file extension");
		        jsHistory("-1");
		    }
			
			if (is_uploaded_file($HTTP_POST_FILES[catalog_file3][tmp_name][$i])) {	
				move_uploaded_file ($HTTP_POST_FILES[catalog_file3][tmp_name][$i],$GLOBALS["_SITE"]["UPLOADED_DATA"]."/product/".$idx."/".$filerename);
			}

			$sql = "insert into ".$GLOBALS["_conf_tbl"]["catalog_files"]." set 
				b_idx='".$idx."',/* 글 번호 id*/
				ori_name='".$filename."',/*파일원본이름*/
				re_name='".$filerename."',/*md5로 변환된 파일이름*/
				type='".$filetype."',/*파일타입*/
				ext ='".$extension."',/*파일확장자*/
				size='".$filesize."',/*첨부파일 용량*/
				type2='catalog3',/*첨부파일 용량*/
				wdate=now()
			";
			$rsf = mysql_query($sql,$GLOBALS[dblink]);
		}
	}
}

//카탈로그 파일정보 가져오기
function getCatalogFileInfo($b_idx, $idx){
		$tbl = $GLOBALS["_conf_tbl"]["catalog_files"];

    $sql  = "SELECT * ";
    $sql .= "FROM " .$tbl." ";
    $sql .= "WHERE b_idx = '$b_idx' ";
    $sql .= "AND idx = '$idx' ";
	//echo $sql;
    $rs = mysql_query($sql, $GLOBALS[dblink]);
    $total_rs = mysql_num_rows($rs);
    
    if($total_rs > 0){
        $list['total'] = $total_rs;
        for($i=0; $i < $total_rs; $i++){
            $list['list'][$i] = mysql_fetch_assoc($rs);
        }
    }else{
        $list['total'] = 0;
    }
    return $list;
}

//카탈로그 파일 삭제 처리
function delCatalogFiles($idx, $HTTP_POST_FILES){
	//제품정보 테이블
	$tbl = $GLOBALS["_conf_tbl"]["product"];
	$tbl_files = $GLOBALS["_conf_tbl"]["catalog_files"];

	//현재 정보 가져오기
	$arrCurInfo = getArticleInfo($tbl, $idx);

	//파일삭제 코딩 시작 - 삭제체크 한것만 처리
	for($i=0;$i<count($_POST[delCatalog]);$i++){
		if($_POST[delCatalog][$i]>0){
			$fileinfo = getCatalogFileInfo($arrCurInfo["list"][0][idx], $_POST[delCatalog][$i]);
			//디비에서 파일정보 삭제
			mysql_query("DELETE FROM ".$tbl_files." WHERE idx='".$fileinfo["list"][0][idx]."' ", $GLOBALS[dblink]);
			//디스크에서 파일 삭제
			unlink($GLOBALS["_SITE"]["UPLOADED_DATA"]."/product/" . $arrCurInfo["list"][0][idx]."/".$fileinfo["list"][0][re_name]);
		}
	}
	//파일삭제 코딩 종료
}

//제품 가져오기 - 파일 포함
function getProductListBaseNFile($cat_no, $orderby, $sw="", $sk="", $scale, $offset=0){
	//테이블 지정
	$tbl = $GLOBALS["_conf_tbl"]["product"];//제품정보
	$tbl_files = $GLOBALS["_conf_tbl"]["product_files"];//제품파일
	$tbl_category = $GLOBALS["_conf_tbl"]["category"];//카테고리

	//카테고리가 있을경우
	if($cat_no !=""){
		$arrCategoryInfo = getCategoryInfo(mysql_real_escape_string($cat_no));
		$que_where .= " and A.cat_code like '" . $arrCategoryInfo["list"][0][cat_code] . "%' ";
	}

	//검색키워드가 있을경우
	if($sk !=""){
		switch($sw){
		case("name") :
			$que_where .= " and (A.p_name like '%$sk%') ";
		break;
		case("category") :
			$que_where .= " and (C.cat_name like '%$sk%') ";
		break;
		case("contents") :
			$que_where .= " and (A.contents like '%$sk%') ";
		break;
		case("recommand") :
			$que_where .= " and (A.show_recommand = 'Y' ) ";
		break;
		case("best_lg") :
			$que_where .= " and (A.show_best_lg = 'Y' ) ";
		break;
		case("best") :
			$que_where .= " and (A.show_best= 'Y' ) ";
		break;
		case("main") :
			$que_where .= " and (A.show_main= 'Y' ) ";
		break;
		default :
			$que_where .= " and (A.p_name like '%$sk%' or C.cat_name like '%$sk%' or A.contents like '%$sk%') ";
		}
	}

	//order by 가 있을경우
	if($orderby !=""){
		$orderby = $orderby;
	}else{
		$orderby = "A.sort_num DESC, A.idx DESC";
	}
	
	//카운트
	$sql = "select count(A.idx) from $tbl A LEFT JOIN ".$tbl_category." C ON A.cat_no=C.cat_no WHERE 1=1 $que_where ";
//	echo $sql;
    $rs = mysql_query($sql, $GLOBALS[dblink]);
    $row = mysql_fetch_row($rs);
    $total_rs = $row[0];

	//목록
    $sql  = "SELECT A.*, B.idx AS f_idx, B.ori_name, B.re_name, B.type, B.size, C.cat_name ";
    $sql .= "FROM ".$tbl." A ";
		$sql .= "LEFT JOIN ".$tbl_files." B ON A.idx=B.b_idx ";
    $sql .= "LEFT JOIN ".$tbl_category." C ON A.cat_no=C.cat_no ";
    $sql .= "WHERE 1=1 $que_where group by A.idx order by $orderby ";


    if($total_rs > 0){
        $list['total'] = $total_rs;
        // 페이지 네비게이션 오프셋 지정.
		    if(!$offset){
		        $offset=0;
		    }else{
		        $offset=$offset;
		    }

		    // offset 이 전체 게시물수보다 작을때 offset 을 전체게시물 - 페이지당 보여줄 글 수로 offset 설정
		    if($total_rs<=$offset){
		        $offset = $total_rs - $scale;
		    }

				if($scale != "0"){
					$sql .= " limit $offset,$scale ";
				}
		    $rs = mysql_query($sql,$GLOBALS[dblink]);

		    // offset 을 이용한 limit 가 적용된 갯수
		    $total = mysql_num_rows($rs);
		    $list['list']['total'] = $total;
		    // 페이지 네비게이션 오프셋 지정.
		    
        for($i=0; $i < $total; $i++){
            $list['list'][$i] = mysql_fetch_assoc($rs);
        }
    }else{
        $list['total'] = 0;
    }
    return $list;
}


//제품 가져오기 - 파일 포함
function getProductListMain($limit){
	//테이블 지정
	$tbl = $GLOBALS["_conf_tbl"]["product"];//제품정보
	$tbl_files = $GLOBALS["_conf_tbl"]["product_files"];//제품파일

	//목록
    $sql  = "SELECT A.idx, A.p_name, A.wdate , B.re_name, B.width, B.height ";
    $sql .= "FROM ".$tbl." A ";
	$sql .= "LEFT JOIN ".$tbl_files." B ON A.idx=B.b_idx ";
    $sql .= "WHERE show_main='Y' group by A.idx order by A.sort_num DESC, A.idx DESC limit $limit ";


	$rs = mysql_query($sql,$GLOBALS[dblink]);
	$total = mysql_num_rows($rs);

	$list['list']['total'] = $total;
		
	for($i=0; $i < $total; $i++){
		$list['list'][$i] = mysql_fetch_assoc($rs);
	}

    return $list;
}

//제품정보 가져오기 - id
function getProductInfo($idx){
	//테이블 지정
	$tbl = $GLOBALS["_conf_tbl"]["product"];//제품정보
	$tbl_files = $GLOBALS["_conf_tbl"]["product_files"];//제품파일
	$tbl_catalog_files = $GLOBALS["_conf_tbl"]["catalog_files"];//카탈로그 파일

	//기본정보 가져오기
	$sql  = "SELECT A.* ";
	$sql .= "FROM ".$tbl." A ";
	$sql .= " WHERE A.idx = '$idx' ";
	$rs = mysql_query($sql, $GLOBALS[dblink]);
//	echo $sql;
	$total_rs = mysql_num_rows($rs);
	
	if($total_rs > 0){
			$list['total'] = $total_rs;
			for($i=0; $i < $total_rs; $i++){
					$list['list'][$i] = mysql_fetch_assoc($rs);
			}
	}else{
			$list['total'] = 0;
	}


	//파일정보 가져오기(제품)
	$sql  = "SELECT * ";
	$sql .= "FROM ".$tbl_files." ";
	$sql .= "WHERE b_idx = '$idx' order by idx ";
	$rs = mysql_query($sql, $GLOBALS[dblink]);
	$total_rs = mysql_num_rows($rs);
	
	if($total_rs > 0){
			$list['total_files'] = $total_rs;
			for($i=0; $i < $total_rs; $i++){
					$list['files'][$i] = mysql_fetch_assoc($rs);
			}
	}else{
			$list['total_files'] = 0;
	}
	
	//파일정보 가져오기(카탈로그)
	$sql  = "SELECT * ";
	$sql .= "FROM ".$tbl_catalog_files." ";
	$sql .= "WHERE b_idx = '$idx' and type2 = '' order by idx";
	$rs = mysql_query($sql, $GLOBALS[dblink]);
	$total_rs = mysql_num_rows($rs);
	
	if($total_rs > 0){
			$list['total_catalog_files'] = $total_rs;
			for($i=0; $i < $total_rs; $i++){
					$list['catalog_files'][$i] = mysql_fetch_assoc($rs);
			}
	}else{
			$list['catalog_total_files'] = 0;
	}

	//파일정보 가져오기(카탈로그)
	$sql  = "SELECT * ";
	$sql .= "FROM ".$tbl_catalog_files." ";
	$sql .= "WHERE b_idx = '$idx' and type2 = 'catalog1' order by idx";
	$rs = mysql_query($sql, $GLOBALS[dblink]);
	$total_rs = mysql_num_rows($rs);
	
	if($total_rs > 0){
			$list['total_catalog_files1'] = $total_rs;
			for($i=0; $i < $total_rs; $i++){
					$list['catalog_files1'][$i] = mysql_fetch_assoc($rs);
			}
	}else{
			$list['catalog_total_files1'] = 0;
	}
	//파일정보 가져오기(카탈로그)
	$sql  = "SELECT * ";
	$sql .= "FROM ".$tbl_catalog_files." ";
	$sql .= "WHERE b_idx = '$idx' and type2 = 'catalog2' order by idx";
	$rs = mysql_query($sql, $GLOBALS[dblink]);
	$total_rs = mysql_num_rows($rs);
	
	if($total_rs > 0){
			$list['total_catalog_files2'] = $total_rs;
			for($i=0; $i < $total_rs; $i++){
					$list['catalog_files2'][$i] = mysql_fetch_assoc($rs);
			}
	}else{
			$list['catalog_total_files2'] = 0;
	}
	//파일정보 가져오기(카탈로그)
	$sql  = "SELECT * ";
	$sql .= "FROM ".$tbl_catalog_files." ";
	$sql .= "WHERE b_idx = '$idx' and type2 = 'catalog3' order by idx";
	$rs = mysql_query($sql, $GLOBALS[dblink]);
	$total_rs = mysql_num_rows($rs);
	
	if($total_rs > 0){
			$list['total_catalog_files3'] = $total_rs;
			for($i=0; $i < $total_rs; $i++){
					$list['catalog_files3'][$i] = mysql_fetch_assoc($rs);
			}
	}else{
			$list['catalog_total_files3'] = 0;
	}


	return $list;
}

function deleteProduct($idx){
	//제품정보 테이블
	$tbl = $GLOBALS["_conf_tbl"]["product"];//제품정보
	$tbl_files = $GLOBALS["_conf_tbl"]["product_files"];//제품파일
	$tbl_catalog_files = $GLOBALS["_conf_tbl"]["catalog_files"];//카탈로그 파일
	$tbl_board_product = $GLOBALS["_conf_tbl"]["board_product"];//제품+게시물 관련 정보

	$arrInfo = getArticleInfo($tbl, $idx);

	if($arrInfo["total"] > 0){
		//제품 정보 삭제
		$sql = "DELETE FROM ".$tbl." WHERE idx='".$arrInfo["list"][0][idx]."'	";
		//echo $sql . "<br>";
		$rs1 = mysql_query($sql, $GLOBALS[dblink]);

		//제품 파일정보 삭제
		$sql = "DELETE FROM ".$tbl_files." WHERE b_idx='".$arrInfo["list"][0][idx]."'	";
		//echo $sql . "<br>";
		$rs2 = mysql_query($sql, $GLOBALS[dblink]);

		//카탈로그 파일정보 삭제
		$sql = "DELETE FROM ".$tbl_catalog_files." WHERE b_idx='".$arrInfo["list"][0][idx]."'	";
		//echo $sql . "<br>";
		$rs3 = mysql_query($sql, $GLOBALS[dblink]);

		//게시물+제품 관련 삭제
		mysql_query("DELETE FROM ".$tbl_board_product." WHERE p_idx='".$idx."' ", $GLOBALS[dblink]);

		if($rs1 && $rs2 && $rs3 && $arrInfo["list"][0][idx]){
			//제품관련 파일삭제
			rrmdir ($GLOBALS["_SITE"]["UPLOADED_DATA"]."/product/" . $arrInfo["list"][0][idx]);
			//위 함수가 하위에 파일이 없으면 디렉토리를 삭제하지 못하는 버그로 아래줄 추가함
			@rmdir ($GLOBALS["_SITE"]["UPLOADED_DATA"]."/product/" . $arrInfo["list"][0][idx]);
			return true;
		}else{
			return false;
		}
	}else{
		return false;
	}
}

//추가 카테고리 가져오기
function getProductExtCat($p_idx){
	$tbl = $GLOBALS["_conf_tbl"]["product_cat"];

    $sql  = "SELECT * ";
    $sql .= "FROM " .$tbl." ";
    $sql .= "WHERE p_idx = '$p_idx' ";

    $rs = mysql_query($sql, $GLOBALS[dblink]);
    $total_rs = mysql_num_rows($rs);

    if($total_rs > 0){
        $list['total'] = $total_rs;
        for($i=0; $i < $total_rs; $i++){
            $list['list'][$i] = mysql_fetch_assoc($rs);
        }
    }else{
        $list['total'] = 0;
    }
    return $list;
}
?>