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

auth_check($auth[$sub_menu], "r");

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

$g4['title'] = "그누XE 메뉴관리 수정";
include_once ("$g4[admin_path]/admin.head.php");

if(!$code){
// 메뉴 지금 코드 계산 //
	if(!$add){
		$chk_count = mysql_num_rows(mysql_query("select * from xe_menu"));
		if($chk_count == 0){
			$rt_code = "001";
		}else{
			$load_code = sql_fetch( "select * from xe_menu where length(code)=3 and code not like '999%' order by code desc ");
			$rt_code = $load_code[code] +1;
			$rt_code = str_pad($rt_code,"3","0",STR_PAD_LEFT); 
		}
	}else{
		$chk_count = mysql_num_rows(mysql_query("select * from xe_menu where code like '$add%' and length(code) != '".strlen($add)."'"));
		if($chk_count == 0){
			$rt_code = $add."001";
		}else{
			$load_code = sql_fetch( "select * from xe_menu where code like '$add%' and length(code) != '".strlen($add)."' and length(code) = '".(strlen($add)+3)."' order by code desc");
			$a = $load_code[code];
			$b = str_pad("1",strlen($add)+3,"0",STR_PAD_LEFT);
			$c = bcadd($a, $b);
			$rt_code = str_pad($c ,strlen($a),"0",STR_PAD_LEFT);
		}
	}
// 메뉴 지금 코드 계산 끝 //
}else{
// 메뉴 수정 페이지라면 //
	$menu = sql_fetch( "select * from xe_menu where code = '$code' ");
	$rt_code = $menu[code];
// 메뉴 수정 페이지라면 끝 //
}

// 스킨디렉토리 라이브러리
// 테마
function xe_theme_dir($theme, $len=''){
    global $g4;

    $result_array = array();
    $dirname = "$g4[path]/xe/theme/$theme/";
    $handle = opendir($dirname);
    while ($file = readdir($handle)) 
    {
        if($file == "."||$file == "..") continue;

        if (is_dir($dirname.$file)) $result_array[] = $file;
    }
    closedir($handle);
    sort($result_array);

    return $result_array;
}
// 스킨디렉토리 라이브러리


if($add){
	$link_code = substr($add, 0, 3);
}

if($add){
	$max_or = strlen($add)+3;
	$order_data = sql_fetch("select * from xe_menu where code like '$add%' and length(code) = '$max_or' order by `order` desc");
	$order_num = $order_data[order]+1;
	unset($max_or, $order_data);
}
?>
<script type="text/javascript">
<?if($menu[theme]){?>
$("#theme").val("<?=$menu[theme]?>");
<?}?>

function board_cate(bo_table){
	
		if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}else{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				if(xmlhttp.responseText){
					$("#board_category").show();
					$("#board_category").html(xmlhttp.responseText);
				}
			}
		}
		xmlhttp.open("GET","./xe_menu_cate_chk.php?bo_table="+bo_table,true);
		xmlhttp.send();
}


function link_item(item){
		if(item == ""){
			$("#link").val("http://");
			$("#link_id").val("");
			$("#link").attr("readonly", false);
		}else if(item == "content"){
			$("#board").hide();
			$("#board").val("");
		}else if(item == "board"){
			$("#board").show();
		}
	}

function chk_code(code){
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}else{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState==4 && xmlhttp.status==200){
			if(xmlhttp.responseText == "true"){
				$("#chk_rut").css("color", "#0600FF");
				$("#chk_rut").html("사용 가능한 코드입니다.");
			}else{
				$("#chk_rut").css("color", "#FF0000");
				$("#chk_rut").html("사용 불가능한 코드입니다.");
				alert("코드가 중복되어 사용이 불가능 합니다.");
			}
		}
	}
	xmlhttp.open("GET","./xe_menu_code_chk.php?code="+code,true);
	xmlhttp.send();
}
</script>

<form name="form" method=post action="./xe_menu_update.php" enctype="multipart/form-data">
	<div class="adm_xe_table_list">
		<table>
			<caption>그누XE 메뉴추가 & 설정</caption>
			<colgroup>
				<col width="150px;">
				<col width="*">
			</colgroup>
			<thead>
				<tr>
					<th>이름</th>
					<th>내용</th>
				</tr>
			</thead>
			<tfoot>
				<td colspan="2">
					<span><input type="submit"  value="확인"></span>
					<span><input type="button"  value="목록" onclick="location.href='./xe_menu_list.php';"></span>
					<span><input type="button"  value="동일위치추가" onclick="location.href='./xe_menu_edit.php?add=<?=str_pad(substr($code, "0",strlen($code)-3),strlen($code)-3, "0",STR_PAD_LEFT);?>';"></span>
					<span><input type="button"  value="하위추가" onclick="location.href='./xe_menu_edit.php?add=<?=$code?>';"></span>
					<span><input type="button"  value="새로추가" onclick="location.href='./xe_menu_edit.php';"></span>
					<?if($code){?>
					<span><input type="button" value="삭제" onclick="if(confirm('해당 메뉴를 삭제하시면 해당메뉴에 귀속된 하위 메뉴까지 같이 삭제됩니다.\n삭제하시겠습니까?')){ location.href='./xe_menu_del.php?code=<?=$code?>'; }"></span>
					<?}?>
				</td>
			</tfoot>
			<tbody>
				<tr>
					<th>메뉴코드</th>
					<td>
						<input type="text" name="code" id="code" value="<?=$rt_code?>" required itemname='메뉴 코드'> 
						<input type="button" value="중복확인" onclick="chk_code($('#code').val());"> 
						<span id="chk_rut"></span>
					</td>
				</tr>
				<tr>
					<th>메뉴이름</th>
					<td>
						<input type="text" name="name" required itemname='메뉴 이름' value="<?=$menu[name]?>">
					</td>
				</tr>
				<tr>
					<th>접근레벨</th>
					<td>
						<select name="level">
							<option value="1"<?=($menu[level]=="1")?" selected":"";?>>1</option>
							<option value="2"<?=($menu[level]=="2")?" selected":"";?>>2</option>
							<option value="3"<?=($menu[level]=="3")?" selected":"";?>>3</option>
							<option value="4"<?=($menu[level]=="4")?" selected":"";?>>4</option>
							<option value="5"<?=($menu[level]=="5")?" selected":"";?>>5</option>
							<option value="6"<?=($menu[level]=="6")?" selected":"";?>>6</option>
							<option value="7"<?=($menu[level]=="7")?" selected":"";?>>7</option>
							<option value="8"<?=($menu[level]=="8")?" selected":"";?>>8</option>
							<option value="9"<?=($menu[level]=="9")?" selected":"";?>>9</option>
							<option value="10"<?=($menu[level]=="10")?" selected":"";?>>10</option>
						</select>
					</td>
				</tr>
				<tr>
					<th>메뉴출력</th>
					<td>
						<input type="checkbox" name="visible" value="1"<?if($menu[visible] == "1" || $menu[visible] <> "0"){ echo "checked='checked'"; }?>>
					</td>
				</tr>
				<tr>
					<th>메뉴링크</th>
					<td>
						<select id="link_id_type" onchange="link_item(this.value);">
							<option value="">외부연결</option>
							<option value="board">게시판</option>
						</select>

						<select id="board" style="display:none;" onchange="$('#link').val(this.value); $('#link_id').val(this.value.split('=')[1]); board_cate(this.value.split('=')[1]);">
							<option value="">게시판을 선택하세요</option>
							<?
							$query = "select bo_table, bo_subject from $g4[board_table] where bo_use_search = '1' order by bo_table asc";
							$result = mysql_query($query);
							for ($i = 0; $i < $item = mysql_fetch_array($result); $i++) {
							?>
							<option value="/bbs/board.php?bo_table=<?=$item[bo_table]?>"><?=$item[bo_subject]?> (<?=$item[bo_table]?>)</option>
							<?} unset($query, $result, $item);?>
						</select>

						<select id="board_category" style="display:none;" onclick="if(this.value){ $('#link').val(this.value); $('#link_id').val(this.value.split('=')[1]+'='+this.value.split('=')[2]); }">
						</select>

						<br>
						<input type="text" name="link" id="link" value="<?=$menu[link]?>" size="50">
						<input type="text" name="link_id" id="link_id" value="<?=$menu[link_id]?>">
					</td>
				</tr>
				<tr>
					<th>메뉴타켓</th>
					<td>
						<select name="target">
							<option value="_self"<?=($menu[target]=="_self")?" selected":"";?>>현재창</option>
							<option value="_blank"<?=($menu[target]=="_blank")?" selected":"";?>>새창</option>
						</select>
					</td>
				</tr>
				<tr>
					<th>테마 스킨</th>
					<td>
						<select name="theme" id="theme">
							<?
							$arr = xe_theme_dir();
							for ($i=0; $i<count($arr); $i++) {
								echo "<option value='$arr[$i]'>$arr[$i]</option>\n";
							}
							unset($arr);
							?>
						</select>
						<?if($menu[theme]){?>
						<script language="JavaScript"> document.getElementById('theme').value="<?=$menu[theme]?>";</script>
						<?}?>
					</td>
				</tr>
				<tr>
					<th>출력순서</th>
					<td>
						<input type="text" name="order" value="<?=($menu[order])?$menu[order]:$order_num?>">
					</td>
				</tr>
			</tbody>
		</table>
	</div>
</form>
<?
include_once("$g4[admin_path]/admin.tail.php");
?>