<?php 
require_once(dirname(__FILE__)."/config.php");
if(empty($action)) $action = '';

//模型信息
$dsql = new DedeSql(false);
$mysql_version = $dsql->GetVersion();
$mysql_versions = explode(".",trim($mysql_version));
$mysql_version = $mysql_versions[0].".".$mysql_versions[1];
$row = $dsql->GetOne("Select fieldset,addtable,issystem From #@__channeltype where ID='$ID'");
$fieldset = $row['fieldset'];
require_once(dirname(__FILE__)."/../include/pub_dedetag.php");
$dtp = new DedeTagParse();
$dtp->SetNameSpace("field","<",">");
$dtp->LoadSource($fieldset);
foreach($dtp->CTags as $ctag){
	if($ctag->GetName()==$fname) break;
}
$ds = file(dirname(__FILE__)."/inc/fieldtype.txt");
foreach($ds as $d){
   $dds = explode(',',trim($d));
   $fieldtypes[$dds[0]] = $dds[1];
}

//保存更改
/*
function _SAVE()
*/
//-----------
if($action=='save'){
	if($row['issystem']==1){
		 $dsql->Close();
		 ShowMsg("对不起，你在查看的是系统模型字段，不允许更改！","-1");
		 exit();
	}
	$trueTable = $row['addtable'];
  //检测数据库是否存在附加表，不存在则新建一个
  if(!$dsql->IsTable($trueTable)){
     $tabsql = "CREATE TABLE IF NOT EXISTS `$trueTable`(
	           `aid` int(11) NOT NULL default '0',
             `typeid` int(11) NOT NULL default '0',
     ";
	   if($mysql_version < 4.1){
        $tabsql .= "    PRIMARY KEY  (`aid`), KEY `".$trueTable."_index` (`typeid`)\r\n) TYPE=MyISAM; ";
     }else{
        $tabsql .= "    PRIMARY KEY  (`aid`), KEY `".$trueTable."_index` (`typeid`)\r\n) ENGINE=MyISAM DEFAULT CHARSET=".$cfg_db_language."; ";
     }
     $dsql->ExecuteNoneQuery($tabsql);
  }
  //检测附加表里含有的字段
	$fields = Array();
	$rs = mysql_query("show fields from $trueTable",$dsql->linkID);
	while($row = mysql_fetch_array($rs)){
		$fields[$row['Field']] = $row['Type'];
	}
	//修改字段配置信息
	$dfvalue = $vdefault;
	$isnull = ($isnull==1 ? "true" : "false");
	$mxlen = $maxlength;
	$fieldname = $fname;
	//检测被修改的字段类型，并更新数据表
	if($dtype=="int"||$dtype=="datetime"){
    	if($dfvalue=="" || ereg("[^0-9-]",$dfvalue)){ $dfvalue = 0; }
    	$tabsql = " `$fieldname` int(11) NOT NULL default '$dfvalue';";
    	$buideType = "int(11)";
  }else if($dtype=="float"){
      if($dfvalue=="" || ereg("[^0-9\.-]",$dfvalue)){ $dfvalue = 0; }
      $tabsql = " `$fieldname` float NOT NULL default '$dfvalue';";
    	$buideType = "float";
  }else if($dtype=="img"||$dtype=="media"||$dtype=="addon"||$dtype=="imgfile"){
    	if($mxlen=="") $mxlen = 200;
    	if($mxlen > 255) $mxlen = 50;
    	$tabsql = " `$fieldname` varchar($mxlen) NOT NULL default '$dfvalue';";
    	$buideType = "varchar($mxlen)";
  }else if($dtype=="multitext"||$dtype=="htmltext"){
    	$tabsql = " `$fieldname` mediumtext;";
    	$buideType = "mediumtext";
  }else if($dtype=="textdata"){
    	$tabsql = " `$fieldname` varchar(100) NOT NULL default '';";
    	$buideType = "varchar(100)";
  }else{
    	if($mxlen=="") $mxlen = 50;
    	if($mxlen > 255) $mxlen = 250;
    	$tabsql = " `$fieldname` varchar($mxlen) NOT NULL default '$dfvalue';";
    	$buideType = "varchar($mxlen)";
  }
	//检测旧数据类型，并替换为新配置
	foreach($dtp->CTags as $tagid=>$ctag){
		if($ctag->GetName()==$fieldname){
      $dfvalue = $ctag->GetAtt('default');
      $isnull = $ctag->GetAtt('isnull');
      $dtype = $ctag->GetAtt('type');
      $mxlen = $ctag->GetAtt('maxlength');
      if(isset($fields[$fieldname]) && $fields[$fieldname]!=$buideType){
     	  $tabsql = "ALTER TABLE `$trueTable` CHANGE `$fieldname` ".$tabsql;
      }else if(!isset($fields[$fieldname])){
     	  $tabsql = "ALTER TABLE `$trueTable` ADD ".$tabsql;
      }else{
     	  $tabsql = "";
      }
      if($tabsql!=""){
      	echo $tabsql;
      	exit();
     	  $dsql->ExecuteNoneQuery($tabsql);
      }
			$dtp->Assign($tagid,stripslashes($editfield));
		}
	}
	$oksetting = addslashes($dtp->GetResultNP());
	$dsql->ExecuteNoneQuery("Update #@__channeltype set fieldset='$oksetting' where ID='$ID' ");
	$dsql->Close();
	ShowMsg("成功更改一个字段的配置！","mychannel_field.php?ID={$ID}&dopost=edit");
	exit();
}
/*
删除字段
function _DELETE()
*/
else if($action=="delete"){
	if($row['issystem']==1){
		 $dsql->Close();
		 ShowMsg("对不起，你在查看的是系统模型，不允许更改！","-1");
		 exit();
	}
	$trueTable = $row['addtable'];
	//检测旧数据类型，并替换为新配置
	foreach($dtp->CTags as $tagid=>$ctag){
		if($ctag->GetName()==$fname){
			$dtp->Assign($tagid,'#@Delete@#');
		}
	}
	$oksetting = addslashes($dtp->GetResultNP());
	$dsql->ExecuteNoneQuery("Update #@__channeltype set fieldset='$oksetting' where ID='$ID' ");
	$dsql->ExecuteNoneQuery("ALTER TABLE `$trueTable` DROP `$fname` ");
	$dsql->Close();
	ShowMsg("成功删除一个字段！","mychannel_field.php?ID={$ID}&dopost=edit");
	exit();
}

require_once(dirname(__FILE__)."/templets/mychannel_field_edit.htm");

$dsql->Close();
?>