<?php
/**
 * 會員模型字段增加
 *
 * @version        $Id: member_model_field_add.php 1 11:20 2010年7月20日Z tianya $
 * @package        DedeCMS.Administrator
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */
require_once(dirname(__FILE__)."/config.php");

//增加權限檢查

require_once(DEDEADMIN.'/inc/inc_admin_channel.php');
if(empty($action)) $action = '';

$mysql_version = $dsql->GetVersion();
$mysql_versions = explode(".",trim($mysql_version));
$mysql_version = $mysql_versions[0].".".$mysql_versions[1];
/*----------------------
function Save()
---------------------*/
if($action=='save')
{
    //模型信息
    $fieldname = strtolower($fieldname);
    $row = $dsql->GetOne("SELECT `table`,`info` FROM #@__member_model WHERE id='$id'");
    $fieldset = $row['info'];
    require_once(DEDEINC."/dedetag.class.php");
    $dtp = new DedeTagParse();
    $dtp->SetNameSpace("field","<",">");
    $dtp->LoadSource($fieldset);
    $trueTable = $row['table'];

    //修改字段配置信息
    $dfvalue = trim($vdefault);
    $isnull = ($isnull==1 ? "true" : "false");
    $mxlen = $maxlength;

    //檢測被修改的字段類型
    $fieldinfos = GetFieldMake($dtype, $fieldname, $dfvalue, $mxlen);
    $ntabsql = $fieldinfos[0];
    $buideType = $fieldinfos[1];

    $rs = $dsql->ExecuteNoneQuery(" ALTER TABLE `$trueTable` ADD  $ntabsql ");

    if(!$rs)
    {
        $gerr = $dsql->GetError();
        ShowMsg("增加字段失敗，錯誤提示為：".$gerr,"javascript:;");
        exit();
    }
    $ok = false;

    //檢測舊配置信息，並替換為新配置
    if(is_array($dtp->CTags))
    {
        //遍歷舊配置
        foreach($dtp->CTags as $tagid=>$ctag)
        {
            if($fieldname == strtolower($ctag->GetName()))
            {
                $dtp->Assign($tagid,stripslashes($fieldstring),false);
                $ok = true;
                break;
            }
        }
        $oksetting = $ok ? $dtp->GetResultNP() : $fieldset."\n".stripslashes($fieldstring);
    }
    else
    {
        //原來的配置為空
        $oksetting = $fieldset."\n".stripslashes($fieldstring);
    }
    $addlist = GetAddFieldList($dtp, $oksetting);
    $oksetting = addslashes($oksetting);
    $rs = $dsql->ExecuteNoneQuery("UPDATE #@__member_model SET `info`='$oksetting' WHERE id='$id' ");
    if(!$rs)
    {
        $grr = $dsql->GetError();
        ShowMsg("保存節點配置出錯！".$grr,"javascript:;");
        exit();
    }
    ShowMsg("成功增加一個字段！","member_model_edit.php?id=$id");
    exit();
}

//檢測模型相關信息，並初始化相關數據
/*----------------------
function ShowPage()
---------------------*/
$row = $dsql->GetOne("SELECT `table` FROM #@__member_model WHERE id='$id'");
$trueTable = $row['table'];
$tabsql = "CREATE TABLE IF NOT EXISTS  `$trueTable`(
`mid` int(10) unsigned NOT NULL auto_increment,
";
if($mysql_version < 4.1)
{
    $tabsql .= " PRIMARY KEY  (`mid`)\r\n) TYPE=MyISAM; ";
}
else
{
    $tabsql .= " PRIMARY KEY  (`mid`)\r\n) ENGINE=MyISAM DEFAULT CHARSET=".$cfg_db_language."; ";
}
$dsql->ExecuteNoneQuery($tabsql);

//檢測附加表裡含有的字段
$fields = array();
$rs = $dsql->SetQuery("SHOW fields FROM `$trueTable`");
$dsql->Execute('a');
while($nrow = $dsql->GetArray('a',MYSQL_ASSOC))
{
    $fields[strtolower($nrow['Field'])] = 1;
}
$f = '';
foreach($fields as $k=>$v)
{
    $f .= ($f=='' ? $k : ' '.$k);
}
require_once(DEDEADMIN."/templets/member_model_field_add.htm");