<?php
$sub_menu = "400400";
include_once('./_common.php');

auth_check($auth[$sub_menu], 'w');

if ($is_admin != 'super' && $w == '') alert('최고관리자만 접근 가능합니다.');


$arr = array();

//===========================================================================
// 로고파일 업로드 시
//===========================================================================
if($_FILES['bp_file']['size'] > 0){
	$fu = new FileUpload();
	$param = array(
		"mkdir" => true,                    		//디렉토리가 없으면 만들것인가
		"updir" => G5_DATA_PATH."/file/popup",        //업로드 디렉토리
		"field" => "bp_file",                 		//폼 필드 이름
		"naming" => FileUpload::NAME_AUTO,  //실제파일명변경 규칙[NAME_ORGINAL_NUMERIC | NAME_ORIGINAL_OVERLAP | NAME_FORCE]
		"limit_size" => 1024*1024,       			//제한 용량
		"limit_width" => 1024,                		//가로 제한 픽셀
		"limit_height" => 1024,               		//세로 제한 픽셀
		"allow_ext" => "jpg|png|gif"           		//허용하는 확장자
	);

	$info = $fu->add($param);               		//세팅된 파일 정보들 배열로 리턴(DB에 입력하는 등의 용도)

	try{
		$fu->upload();                          	//실제 업로드
	}catch(Exception $e){
		alert("파일업로드 에러 ".$e->getMessage());
	}
	
	$arr['bp_width'] = $info['width'];
	$arr['bp_height'] = $info['height'];
	$arr['bp_file']	= $info['rname'];
}


switch($w){
	//===========================================================================
	// 수정
	//===========================================================================
	case 'u':
	
		if(Str::isBlank($_POST['bp_idx'])){
			alert('잘못된 접근입니다');
		}
		
		Val::checkBlank($_POST['bp_subject'], '제목');
		Val::checkBlank($_POST['sdate'], '시작날짜');
		Val::checkBlank($_POST['edate'], '종료날짜');
		
		$bp_idx = $bdb->esc($_POST['bp_idx']);
		
		$sql = "SELECT bp_file FROM ".$bt['popup_table']." WHERE bp_idx=".$bp_idx;
		$file = $bdb->result($sql);
		
	
		$arr['bp_subject'] = $_POST['bp_subject'];
		$arr['bp_type'] = $_POST['bp_type'];
		$arr['bp_show_subject'] = $_POST['bp_show_subject'];
		$arr['bp_show_closeopt'] = $_POST['bp_show_closeopt'];
		
		if(!isset($arr['bp_width'])) $arr['bp_width'] = $_POST['bp_width'];
		if(!isset($arr['bp_height'])) $arr['bp_height'] = $_POST['bp_height'];
		
		$arr['bp_url'] = $_POST['bp_url'];
		$arr['bp_target'] = $_POST['bp_target'];
		$arr['bp_sdate'] = $_POST['sdate'].' '.$_POST['shour'].':'.$_POST['smin'].':00';
		$arr['bp_edate'] = $_POST['edate'].' '.$_POST['ehour'].':'.$_POST['emin'].':00';
		$arr['bp_close_hour'] = $_POST['bp_close_hour'];
		
		if(Str::isValue($_POST['bp_content']))
			$arr['bp_content'] = conv_content(stripslashes($_POST['bp_content']), 1);
			
		$arr['bp_hide'] = $_POST['bp_hide'];
			
		$bdb->update($bt['popup_table'], $arr, "bp_idx='".$bdb->esc($_POST['bp_idx'])."'");
		
		if(isset($arr['bp_file'])){
			@unlink(G5_DATA_PATH."/file/popup/".$file);
		}
		
		alert('수정되었습니다', './popup_list.php?'.$qstr);
	
		break;
	
	//===========================================================================
	// 삭제
	//===========================================================================
	case 'd':
	
		if(Str::isBlank($_GET['bp_idx'])){
			alert('잘못된 접근입니다');
		}
		
		$bp_idx = $bdb->esc($_GET['bp_idx']);
		
		$sql = "SELECT bp_file FROM ".$bt['popup_table']." WHERE bp_idx=".$bp_idx;
		$file = $bdb->result($sql);
	
		$bdb->delete($bt['popup_table'], "bp_idx='".$bdb->esc($_GET['bp_idx'])."'");
		
		//파일삭제
		@unlink(G5_DATA_PATH."/file/popup/".$file);
		
		goto_url('./popup_list.php?'.$qstr);
	
		break;
	
	//===========================================================================
	// 멀티 삭제
	//===========================================================================
	case 'md':
	
		$aidx = array();
		for ($i=0; $i<count($_POST['chk']); $i++) {
			
			// 실제 번호를 넘김
        	$k = $_POST['chk'][$i];
        	
        	$aidx[] = $_POST['bp_idx'][$k];
		}
		
		if(count($aidx) > 0){
			$sidx = @implode("','", $aidx);
			
			//파일목록 구함
			$sql = "SELECT bp_file FROM ".$bt['popup_table']." WHERE bp_idx IN('".$sidx."')";
			$flist = $bdb->fetchAll($sql);
			
			//레코드 삭제
			$bdb->delete($bt['popup_table'], "bp_idx IN('".$sidx."')");
			
			//파일삭제
			foreach($flist as $item){
				@unlink(G5_DATA_PATH."/file/popup/".$item['bp_file']);
			}
		}
		
		goto_url('./popup_list.php?'.$qstr);
	
		break;
	
	//===========================================================================
	// 등록
	//===========================================================================
	case '':
	
		Val::checkBlank($_POST['bp_subject'], '제목');
		Val::checkBlank($_POST['sdate'], '시작날짜');
		Val::checkBlank($_POST['edate'], '종료날짜');
		
		$arr['bp_subject'] = $_POST['bp_subject'];
		$arr['bp_type'] = $_POST['bp_type'];
		$arr['bp_show_subject'] = $_POST['bp_show_subject'];
		$arr['bp_show_closeopt'] = $_POST['bp_show_closeopt'];
		
		if(!isset($arr['bp_width'])) $arr['bp_width'] = $_POST['bp_width'];
		if(!isset($arr['bp_height'])) $arr['bp_height'] = $_POST['bp_height'];
		
		$arr['bp_url'] = $_POST['bp_url'];
		$arr['bp_target'] = $_POST['bp_target'];
		$arr['bp_sdate'] = $_POST['sdate'].' '.$_POST['shour'].':'.$_POST['smin'].':00';
		$arr['bp_edate'] = $_POST['edate'].' '.$_POST['ehour'].':'.$_POST['emin'].':00';
		$arr['bp_close_hour'] = $_POST['bp_close_hour'];
		
		if(Str::isValue($_POST['bp_content']))
			$arr['bp_content'] = conv_content(stripslashes($_POST['bp_content']), 1);
			
		$arr['bp_hide'] = $_POST['bp_hide'];
		
		$arr['bp_regdate'] = date('Y-m-d H:i:s');
		
		$bdb->insert($bt['popup_table'], $arr);
		
		goto_url('./popup_list.php?'.$qstr);
		
		break;
	
	default:
}