<?php

function ptyGoUrl($where, $debug = FALSE)
{
    if ($debug) {
        ptyDebug($where);
        exit;
    }
    echo '<head><base target="_top"></head>';
    echo("<meta http-equiv='Refresh' content='0; URL=$where'>");
    exit;
}

function ptyHeaderGoUrl($url)
{
    header('Location: ' . $url);
}

function ptyAlertGo($message, $where, $frame = "", $only_javascript = FALSE)
{
    if ($only_javascript) {
        return "alert(\"$message\"); location.href = '$where';";
    } else {
        if ($frame)
            echo '<base target="$frame">';

        echo "<BODY><SCRIPT LANGUAGE=\"JavaScript\">
		// console.log(\"$message\");
		alert(\"$message\");
		location.href = '$where';
		</SCRIPT></BODY>
		";
        exit;
    }

}

function ptyBack()
{
    echo "<BODY>
			<SCRIPT LANGUAGE=\"JavaScript\">
				history.go(-1);
			</SCRIPT>
			</BODY>
		";
    exit;
}

function ptyAlertBackGo($message, $backPos = -1)
{
    // echo '<base target="_top">';
    echo "<BODY>
			<SCRIPT LANGUAGE=\"JavaScript\">
				alert(\"$message\");
				history.go($backPos);
			</SCRIPT>
			</BODY>
		";
    exit;
}

function ptyAlert($message, $only_javascript = FALSE)
{
    if ($only_javascript) {
        return "alert(\"$message\");";
    } else {
        echo "<BODY>
			<SCRIPT LANGUAGE=\"JavaScript\">
				alert(\"$message\");
			</SCRIPT>
			</BODY>
		";
    }
    exit;
}

function ptyAlertOnly($message)
{
    echo "<BODY>
			<SCRIPT LANGUAGE=\"JavaScript\">
				alert(\"$message\");
			</SCRIPT>
			</BODY>
		";
}

/**
 * @param $label
 * @param $data
 * data : array("예" => "T", "아니오" => "F")
 * @param $name
 *
 * @return string
 */
function ptyGetSearchFormSelect($label, $data, $name, $value = NULL)
{
    global $platyFramework;
    $inputName = "sec[$name]";

    if (is_null($value))
        $requestValue = $platyFramework->request->request[sec][$name];
    else
        $requestValue = $value;

    $out = "
	<div class='form-group'>
		<label class='control-label' for='$name'>$label</label>
		<select name='$inputName' class='form-control'>
		";

    foreach ($data as $selectLabel => $selectData) {
        if ($requestValue == $selectData)
            $selected = " selected";
        else
            $selected = " ";

        $out .= "<option $selected value='$selectData'>$selectLabel</option>\n";
    }

    $out .= "
		</select>
	</div>
	";

    return $out;
}

function ptyGetSearchFormSelect2($label, $data, $name, $value = NULL)
{
    global $platyFramework;
    $inputName = "sec[$name]";

    if (is_null($value))
        $requestValue = $platyFramework->request->request[sec][$name];
    else
        $requestValue = $value;
    $out = "
	<div class='btn-group'>
		<div class=\"input-group input-group-sm\" style=\"width: 150px;\">
			<select name='$inputName' class='form-control pull-right'>
		";

    foreach ($data as $selectLabel => $selectData) {
        if ($requestValue == $selectData)
            $selected = " selected";
        else
            $selected = " ";

        $out .= "<option $selected value='$selectData'>$selectLabel</option>\n";
    }

    $out .= "
		</select>
		</div>
		</div>
	";

    return $out;
}

function ptyGetSearchFormSelect3($label, $data, $name, $value = NULL)
{
    global $platyFramework;
    $inputName = "sec[$name]";

    if (is_null($value))
        $requestValue = $platyFramework->request->request[sec][$name];
    else
        $requestValue = $value;
    $out = "
<div class='col-sm-2'>
	<div class='btn-group'>
		<div class=\"input-group input-group-sm\" style=\"width: 150px;\">
			<label class='control-label' for='$name'>$label</label>
			<select name='$inputName' class='form-control pull-right'>
		";

    foreach ($data as $selectLabel => $selectData) {
        if ($requestValue == $selectData)
            $selected = " selected";
        else
            $selected = " ";

        $out .= "<option $selected value='$selectData'>$selectLabel</option>\n";
    }

    $out .= "
		</select>
		</div>
		</div>
		</div>
	";

    return $out;
}


function ptyGetSearchFormInput($label, $data, $name)
{
    global $platyFramework;
    $inputName = "sec[$name]";
    $requestValue = $platyFramework->request->get['sec'][$name];

    $out = "
	<div class='form-group'>
		<label class='control-label' for='$name'>$label</label>
		<input name='$inputName' class='form-control' value='$requestValue'>
	</div>
	";

    return $out;
}

function ptyGetSearchFormInput2($label, $data, $name)
{
    global $platyFramework;
    $inputName = "sec[$name]";
    $requestValue = $platyFramework->request->get['sec'][$name];

    $out = "
						<div class=\"btn-group\">
							<div class=\"input-group input-group-sm\" style=\"width: 150px;\">
								<input type=\"text\" name=\"q\" class=\"form-control pull-right\" value='$requestValue' placeholder=\"$label\">
								<!--
								<div class=\"input-group-btn\">
									<button type=\"submit\" class=\"btn btn-default\"><i class=\"fa fa-search\"></i></button>
								</div>
								-->
							</div>
						</div>
	";

    return $out;
}

function ptyGetSearchFormInput3($label, $data, $name)
{
    global $platyFramework;

    // slc = search like column
    $inputName = "slc[$name]";
    $requestValue = $platyFramework->request->get['slc'][$name];

    $out = "
<div class='col-sm-2'>
		<label class='control-label' for='$name'>$label</label>
		<input name='$inputName' class='form-control' value='$requestValue'>
</div>		
	";

    return $out;
}


function ptyGetSearchFormInputLike($label, $data, $name)
{
    global $platyFramework;
    $inputName = "slc[$name]";
    $requestValue = $platyFramework->request->get['slc'][$name];

    $out = "
	<div class='form-group'>
		<label class='control-label' for='$name'>$label</label>
		<input name='$inputName' class='form-control' value='$requestValue'>
		";

    $out .= "
	</div>
	";

    return $out;
}

function ptyGetParams($replace = "", $merge = true)
{
    $url = $_SERVER['QUERY_STRING'];
    parse_str($url, $params);
    parse_str($replace, $requests);

    if ($merge) {
        $s = array_merge($params, $requests);
    } else
        $s = $requests;

    foreach ($s as $k => $v) {
        if ($v == "{NULL}")
            unset($s[$k]);
    }
    $s = http_build_query($s);
    if ($s)
        return "?$s";
    else
        return "";

}

/**
 * 배열 내 모든 항목을 htmlspecialchars_decode 을 변환
 *
 * @param $data
 * @return array|string
 */
function ptyHtmlspecialchars_decodes($data) {
    if (is_array($data)) {
        foreach ($data as $key => $value) {
            $data[$key] = ptyHtmlspecialchars_decodes($value);
        }
        return $data;
    } else {
        return htmlspecialchars_decode($data);
    }
}


/**
 * 모든 GET 인자를 유지하면서 특정 항목만 변경하거나 삭제할 수 있습니다
 *
 * @param string $replace "id={NULL}&test={NULL}
 * @return string
 */
function ptyStayParams($replace = "", $merge = true)
{
    global $platyFramework;
    $params = $platyFramework->request->gets->_item;
    $params = ptyHtmlspecialchars_decodes($params);

    parse_str($replace, $requests);

    if ($merge) {
        $s = array_merge($params, $requests);
    } else
        $s = $requests;

    foreach ($s as $k => $v) {
        // pdf($v);
        if ($v == "{NULL}")
            unset($s[$k]);
    }
    $s = http_build_query($s);

    // pdfe($s);

    if ($s)
        return "?$s";
    else
        return "";
}

/**
 * @param null $array "id={NULL}&test=1" 등등으로 지정
 *
 *
 * @return string
 * @deprecated
 */
function ptyRebuildParams($array = null)
{
    $url = $_SERVER['QUERY_STRING'];
    parse_str($url, $params);
    parse_str($array, $requests);

    $s = array_merge($params, $requests);
    foreach ($s as $k => $v) {
        if ($v == "{NULL}")
            unset($s[$k]);
    }
    $s = http_build_query($s);
    if ($s)
        return "?$s";
    else
        return "";
}


function ptyReplaceUrlParams($url, $array)
{
    if ($url == NULL) $url = $_SERVER['QUERY_STRING'];

    $urlInfo = parse_url($url);
    parse_str($urlInfo['query'], $requests);

    $Request = array_merge($requests, $array);

    $s = "";
    foreach ($Request as $k => $v) {
        if (is_array($v)) {
            foreach ($v as $k2 => $v2) {
                if ($s) $s .= "&";
                $s .= sprintf("%s[%s]=%s", $k, $k2, urlencode($v2));
            }
        } else {
            if (!is_null($v)) {
                if ($s) $s .= "&";
                $s .= "$k=" . urlencode($v);
            }
        }
    }

    if ($s) $s = "?" . $s;

    if ($urlInfo['scheme'] != "" && $urlInfo['host'] != "")
        $newUrl = $urlInfo['scheme'] . "://" . $urlInfo['host'] . $urlInfo['path'] . $s;
    else
        $newUrl = $urlInfo['path'] . $s;

    return $newUrl;
}

function ptyRebuildFunctionWithUrl($functionName, $array = array())
{
    $n = explode('/', $_SERVER['REQUEST_URI']);
    // $n = $_REQUEST;
    ptyDebug($n);


}

function ptyRebuildUrl($array = array(), $request = NULL)
{
    global $platyFramework;
    global $SCRIPT_NAME;

    $Link = $SCRIPT_NAME;
    $r = "";
    $s = "";
    $Request = $r ? $r : $platyFramework->request->gets->_item;

    // returnType 은 기본적으로 제외처리
    $array['returnType'] = NULL;

    $Request = array_merge($Request, $array);

    foreach ($Request as $k => $v) {
        if (is_array($v)) {
            foreach ($v as $k2 => $v2) {
                if ($s) $s .= "&";
                $s .= sprintf("%s[%s]=%s", $k, $k2, $v2);
            }
        } else {

            if (is_null($v))
                continue;

            if ($s) $s .= "&";
            $s .= "$k=$v";
        }
    }

    return sprintf("%s?%s", $SCRIPT_NAME, $s);
    /*
    if ($s)
        return sprintf("%s?%s", $SCRIPT_NAME, $s);
    else
        return $SCRIPT_NAME;
    */

}

function ptyGetInputHiddenFromUrlQuery()
{
    return ptyGetRebuildFormUrl();
}

function ptyGetRebuildFormUrl()
{
    $s = "";
    $out = "\n";

    $Request = $_REQUEST;

    foreach ($Request as $k => $v) {
        if (is_array($v)) {
            foreach ($v as $k2 => $v2) {
                // if ($s) $s .= "&";
                // $s .= sprintf("%s[%s]=%s", $k, $k2, $v2);
            }
        } else {
            if ($s) $s .= "&";
            $s .= "$k=$v";

            $out .= "<input ptyGetRebuildFormUrl type=hidden name='$k' value='$v'>\n";
        }
    }

    $out .= "<input ptyGetRebuildFormUrl type=hidden name='page' value='1'>\n";

    return $out;

    // return sprintf("%s?%s", $SCRIPT_NAME, $s);
}


?>
