<?php
include_once('./_common.php');

ob_end_clean();

include_once(G5_LIB_PATH.'/connect.lib.php');
include_once(G5_LIB_PATH.'/outlogin.lib.php');

set_time_limit ( 0 );
ini_set('memory_limit', '100M');

$g5['title'] = '영카트4 DB 데이터 이전';
include_once(G5_PATH.'/head.sub.php');

echo '<link rel="stylesheet" href="'.G5_URL.'/g4_import.css">';

//if(empty($_POST))
//    alert('올바른 방법으로 이용해 주십시오.', G5_URL);
//
//if(get_session('yc4_tables_copied') == 'done')
//    alert('DB 데이터 변환을 이미 실행하였습니다. 중복 실행시 오류가 발생할 수 있습니다.', G5_URL);

if($is_admin != 'super')
    alert('최고관리자로 로그인 후 실행해 주십시오.', G5_URL);

/*<!-- ▨▨▨▨▨▨▨ 영카트4 config.php 위치 수정해 주세요  ▨▨▨▨▨▨▨ -->*/
$g4_config_file = trim('../config.php');

if(!$g4_config_file)
    alert('config.php 파일의 경로를 입력해 주십시오.');

$g4_config_file = preg_replace('#/config.php$#i', '', $g4_config_file).'/config.php';

if(!is_file($g4_config_file))
    alert('입력하신 경로에 config.php 파일이 존재하지 않습니다.');

$shop_config_file = str_replace('config.php', 'shop.config.php', $g4_config_file);

if(!is_file($shop_config_file))
    alert('입력하신 경로에 shop.config.php 파일이 존재하지 않습니다.\\nshop.config.php 파일은 config.php 파일과 동일한 위치에 있어야 합니다.');

$is_euckr = false;
?>
<script>
// 새로고침 방지
function noRefresh()
{
    /* CTRL + N키 막음. */
    if ((event.keyCode == 78) && (event.ctrlKey == true))
    {
        event.keyCode = 0;
        return false;
    }
    /* F5 번키 막음. */
    if(event.keyCode == 116)
    {
        event.keyCode = 0;
        return false;
    }
}

document.onkeydown = noRefresh ;
</script>

<style>
#g4_import_run {}
#g4_import_run ol {margin: 0;padding: 0 0 0 25px;border: 1px solid #E9E9E9;border-bottom: 0;background: #f5f8f9;list-style:none;zoom:1}
#g4_import_run li {padding:7px 10px;border-bottom:1px solid #e9e9e9}
#g4_import_run #run_msg {padding:30px 0;text-align:center}
</style>

<hr>

<!-- 콘텐츠 시작 { -->
<div id="wrapper">
    <div id="aside">
        <?php // echo outlogin('basic'); // 외부 로그인  ?>
    </div>
    <div id="container">
        <?php if ((!$bo_table || $w == 's' ) && !defined("_INDEX_")) { ?><div id="container_title"><?php echo $g5['title'] ?></div><?php } ?>

        <div id="g4_import_run">
            <ol>
        <?php
        flush();

        // yc4의 confing.php, shop.config.php
        require($g4_config_file);
        require($shop_config_file);

        if(preg_replace('/[^a-z]/', '', strtolower($g4['charset'])) == 'euckr')
            $is_euckr = true;

        // item table option 복사
        $sql = " select * from {$g4['yc4_item_table']} where it_opt1 != '' order by it_id ";
        $result = sql_query($sql);

        for($i=0; $row=sql_fetch_array($result); $i++) {
            if($is_euckr)
                $row = array_map('iconv_utf8', $row);

				$it_id = $row['it_id'];

				if($row['it_opt1'] != "") $it_opt_subject = $row['it_opt1_subject'];
				if($row['it_opt2'] != "") $it_opt_subject .= ",".$row['it_opt2_subject'];
				if($row['it_opt3'] != "") $it_opt_subject .= ",".$row['it_opt3_subject'];
				if($row['it_opt4'] != "") $it_opt_subject .= ",".$row['it_opt4_subject'];
				if($row['it_opt5'] != "") $it_opt_subject .= ",".$row['it_opt5_subject'];
				if($row['it_opt6'] != "") $it_opt_subject .= ",".$row['it_opt6_subject'];

                $sql_common = " it_supply_subject = '$it_opt_subject' ";
            sql_query(" UPDATE {$g5['g5_shop_item_table']} SET $sql_common where it_id = '$it_id' ");

		 $it_opt1 = $row['it_opt1'];
		 $it_opt2 = $row['it_opt2'];
		 $it_opt3 = $row['it_opt3'];
		 $it_opt4 = $row['it_opt4'];
		 $it_opt5 = $row['it_opt5'];
		 $it_opt6 = $row['it_opt6'];
		 $it_subject1 = $row['it_opt1_subject'];
		 $it_subject2 = $row['it_opt2_subject'];
		 $it_subject3 = $row['it_opt3_subject'];
		 $it_subject4 = $row['it_opt4_subject'];
		 $it_subject5 = $row['it_opt5_subject'];
		 $it_subject6 = $row['it_opt6_subject'];
		 for($c=1; $c<=6; $c++){

			  if(${it_opt.$c}){
				$pattern = explode("\n", ${it_opt.$c});
				for($j=0; $j<count($pattern); $j++){
					$pattern[$j] = trim($pattern[$j]);

					/*<!-- ▨▨▨▨▨▨▨ 가격이 -(마이너스), 옵션명(없음, 선택)은 제외  ▨▨▨▨▨▨▨ -->*/
					if(preg_match("/;-|없음|선택/", $pattern[$j])) continue;
					list($price1, $price2) = explode(";+", $pattern[$j]);

					$io_id = ${it_subject.$c}.chr(30).$price1;
					$io_price = $price2;

				$sql_common2 = " io_id = '$io_id',
                  io_type = 1,
                  it_id = '$it_id',
                  io_price = '$io_price',
                  io_stock_qty = 9999,
                  io_noti_qty = 100,
                  io_use = 1
					";

					sql_query(" INSERT INTO {$g5['g5_shop_item_option_table']} SET $sql_common2 ");
				}
              }

		 }

        }
        echo '<li>item table option 복사</li>'.PHP_EOL;

        echo '</ol>'.PHP_EOL;

        echo '<div id="run_msg">영카트4 DB 옵션 데이터 이전 완료</div>'.PHP_EOL;

        // 실행완료 세션에 기록
        set_session('yc4_tables_copied', 'done');
        ?>
        </div>

    </div>
</div>

<!-- } 콘텐츠 끝 -->

<hr>

<script>
$(function() {
    // 폰트 리사이즈 쿠키있으면 실행
    font_resize("container", get_cookie("ck_font_resize_rmv_class"), get_cookie("ck_font_resize_add_class"));
});
</script>

<?php
include_once(G5_PATH.'/tail.sub.php');
?>