<?php
/**
 * {{$Page.Header}}: 此文件用来添加记录到 {{$Page.TableName}}。
 *
 * copyright
 * @author          {{$Page.Author}}
 * @package         {{$Page.PackageName}}
 * @version         {{$IdTag}}
 */
/* 初始化系统. */
require_once("{{$Page.IncludePath}}Include/InitAdmin.php");
langLoadTable("{{$Page.TableName}}");

/* 用户提交了数据，检查并入库。*/
if($_POST)
{
    {{if $Page.FieldsToPHP}}
    {{foreach from=$Page.FieldsToPHP key=FieldName item=InputParam}}
    {{if $InputParam.Type=="Checkbox"}}

    /* 如果变量以复选框的形式传递，使用逗号连接起来。*/
    {{$CFG.GEN.EmptyLine}}
    $_POST["{{$FieldName}}"] = join(",", $_POST["{{$FieldName}}"]);
    {{/if}}
    {{/foreach}}
    {{/if}}
    
    /* 检查数据. */
    {{foreach from=$Page.Fields.Required key=FieldName item=Field}}
    {{if $Field.DataRule=="Length"}}
    {{$CFG.GEN.EmptyLine}}
    $ErrorMsg = sysCheck($_POST["{{$FieldName}}"], $_LANG["{{$FieldName}}"], "{{$Field.DataRule}}", {{$Field.Size}});
    $ErrorMsg ? $ErrorMsgList[] = $ErrorMsg : "";
    {{elseif $Field.DataRule != "None"}}
    {{$CFG.GEN.EmptyLine}}
    $ErrorMsg = sysCheck($_POST["{{$FieldName}}"], $_LANG["{{$FieldName}}"], "{{$Field.DataRule}}");
    $ErrorMsg ? $ErrorMsgList[] = $ErrorMsg : "";
    {{/if}}
    {{/foreach}}
    {{foreach from=$Page.Fields.Optional key=FieldName item=Field}}
    {{$CFG.GEN.EmptyLine}}
    if(!empty($_POST["{{$FieldName}}"]))
    {
        {{if $Field.DataRule=="Length"}}
        {{$CFG.GEN.EmptyLine}}
        $ErrorMsg = sysCheck($_POST["{{$FieldName}}"], $_LANG["{{$FieldName}}"], "{{$Field.DataRule}}", {{$Field.Size}});
        $ErrorMsg ? $ErrorMsgList[] = $ErrorMsg : "";
        {{elseif $Field.DataRule != "None"}}
        {{$CFG.GEN.EmptyLine}}
        $ErrorMsg = sysCheck($_POST["{{$FieldName}}"], $_LANG["{{$FieldName}}"], "{{$Field.DataRule}}");
        $ErrorMsg ? $ErrorMsgList[] = $ErrorMsg : "";
        {{/if}}
        {{$CFG.GEN.EmptyLine}}
    }
    {{$CFG.GEN.EmptyLine}}
    {{/foreach}}

    /* 如果有错误，提示用户。 */
    if(!empty($ErrorMsgList))
    {
        $MyJS->alert(join("\\n", $ErrorMsgList));
        $MyJS->goto("Back");
        exit;
    }

    /* 插入数据库。 */
    $SQL = "INSERT INTO {{$Page.TableName}} ({{$Page.FieldsString}})
            VALUES({{$Page.InsertFields}})";
    if(!$MyDB->query($SQL))
    {
        die($MyDB->errorMsg());
    }
    else
    {
        $MyTPL->assign("ItemID", $MyDB->insert_ID());
        $MyTPL->display($_CFG["TPL"]);
        exit;
    }
}
{{if $Page.FieldsToPHP}}

{{foreach from=$Page.FieldsToPHP key=FieldName item=InputParam}}
{{if $InputParam.Table}}
/* 查找{{$FieldName}}列表从{{$InputParam.Table}}中。*/
$SQL        = "SELECT {{$InputParam.Fields.Value}} AS Value, {{$InputParam.Fields.Label}} AS Label FROM {{$InputParam.Table}}";
$ReaultID   = $MyDB->query($SQL) or die($MyDB->errorMsg());
while($Item = $ReaultID->fetchRow())
{
    ${{$FieldName}}List[$Item['Value']] = $Item["Label"];
}
$MyTPL->assign("{{$FieldName}}List", html{{$InputParam.Type}}(${{$FieldName}}List, "{{$FieldName}}"));
{{elseif $InputParam.List}}
/* 创建{{$FieldName}}列表。*/
{{foreach from=$InputParam.List key=ItemValue item=ItemLabel}}
${{$FieldName}}List["{{$ItemValue}}"] = "{{$ItemLabel}}";
{{/foreach}}
$MyTPL->assign("{{$FieldName}}List", htmlSelect(${{$FieldName}}List, "{{$FieldName}}"));
{{elseif $InputParam.FromGET}}
/* 赋值{{$FieldName}}，通过_GET方式获得。*/
$MyTPL->assign("{{$FieldName}}", $_GET["{{$FieldName}}"]);
{{elseif $InputParam.Func}}
/* 赋值{{$FieldName}}，通过{{$InputParam.Func}}创建。*/
$MyTPL->assign("{{$FieldName}}List", htmlSelect({{$InputParam.Func}}(), "{{$FieldName}}"));
{{/if}}

{{/foreach}}
{{/if}}
/* 赋值，显示模板。 */
$MyTPL->assign("PageTitle", $_LANG["TableLabel"] . $_LANG["AddPage"]);
$MyTPL->display($_CFG["TPL"]);
?>