<?php
(!defined('M_COM') || !defined('M_ADMIN')) && exit('No Permission');
$datatype = 'text';
$isfunc = $fnew ? (isset($fieldnew['isfunc']) ? $fieldnew['isfunc'] : 0) : (isset($field['isfunc']) ? $field['isfunc'] : 0);
if(!$fsave){
	load_cache('permissions');
	trbasic(lang('field_type'),'',$datatypearr[$datatype],'');
	if($fnew){
		echo "<input type=\"hidden\" name=\"fieldnew[datatype]\" value=\"$datatype\">\n";
		echo "<input type=\"hidden\" name=\"fieldnew[isfunc]\" value=\"$isfunc\">\n";
	}
	trbasic(lang('field_cname'),'fieldnew[cname]',empty($field['cname']) ? '' : $field['cname']);
	$submitstr .= makesubmitstr('fieldnew[cname]',1,0,0,30);
	trbasic(lang('field_ename'),$fnew ? 'fieldnew[ename]' : '',empty($field['ename']) ? '' : $field['ename'],$fnew ? 'text' : '');
	if($fnew) $submitstr .= makesubmitstr('fieldnew[ename]',1,'tagtype',0,30);
	if(!$isfunc){
		!in_array($fmode,array('cn')) && empty($field['issystem']) && trbasic(lang('field_pmid'),'fieldnew[pmid]',makeoption(pmidsarr('field',lang('frommsg')),empty($field['pmid']) ? 0 : $field['pmid']),'select');
		empty($field['issystem']) && trbasic(lang('input_notnull'),'fieldnew[notnull]',empty($field['notnull']) ? 0 : $field['notnull'],'radio');
		trbasic(lang('form_guide'),'fieldnew[guide]',empty($field['guide']) ? '' : $field['guide'],'btext',lang('agguide'));
		$submitstr .= makesubmitstr('fieldnew[guide]',0,0,0,80);
		trbasic(lang('default_value'),'fieldnew[vdefault]',empty($field['vdefault']) ? '' : $field['vdefault'],'btext');
		trbasic(lang('controller_mode'),'fieldnew[mode]',makeradio('fieldnew[mode]',array(0 => lang('normal_size1'),1 => lang('enlarge_size1')),empty($field['mode']) ? '' : $field['mode']),'');
	}
	if(empty($field['issystem']) && empty($field['mcommon'])){//通用字段是不能更改一些属性的
		trbasic(lang('table_fieldlength'),'fieldnew[length]',empty($field['length']) ? '' : $field['length'],'text',lang('agtlength'));
		$submitstr .= makesubmitstr('fieldnew[length]',0,0,1,255,'int');
	}
	if(!$isfunc){
		trrange(lang('value_length'),array('fieldnew[min]',(!isset($field['min']) || $field['min'] == '') ? '' : $field['min'],'','&nbsp; -&nbsp; ',5),array('fieldnew[max]',(!isset($field['max']) || $field['max'] == '') ? '' : $field['max'],'','',5));
		$submitstr .= makesubmitstr('fieldnew[min]',0,'int');
		$submitstr .= makesubmitstr('fieldnew[max]',0,'int');
		trbasic(lang('nohtml'),'fieldnew[nohtml]',empty($field['nohtml']) ? 0 : $field['nohtml'],'radio');
		trbasic(lang('format_limited'),'fieldnew[mlimit]',makeoption($limitarr,empty($field['mlimit']) ? '0' : $field['mlimit']),'select');
		trbasic(lang('format_regular_check_str'),'fieldnew[regular]',empty($field['regular']) ? '' : $field['regular']);
		trbasic(lang('remote_download'),'fieldnew[rpid]',makeoption($rpidsarr,empty($field['rpid']) ? '0' : $field['rpid']),'select');
	}
	if($isfunc){
		trbasic(lang('php_func'),'fieldnew[func]',empty($field['func']) ? '' : $field['func'],'textarea');
	}
	if(in_array($fmode,array('a','i','m','im','ma')) && ($fnew || !empty($field['iscustom']))){
		$issearcharr = array('0' => lang('nosearch'),'1' => lang('onesearch'),'2' => lang('multisearch'));
		trbasic(lang('issearch'),'',makeradio('fieldnew[issearch]',$issearcharr,empty($field['issearch']) ? '0' : $field['issearch']),'');
	}
	trbasic(lang('custom_1'),'fieldnew[custom_1]',empty($field['custom_1']) ? '' : $field['custom_1'],'text',lang('agcustom_1'));
	trbasic(lang('custom_2'),'fieldnew[custom_2]',empty($field['custom_2']) ? '' : $field['custom_2'],'text',lang('agcustom_1'));
	
}else{
	$sqlstr = empty($fconfigarr['sqlstr']) ? "" : $fconfigarr['sqlstr'];
	$fieldnew['cname'] = trim(strip_tags($fieldnew['cname']));
	if($fnew){
		$filterstr = empty($fconfigarr['filterstr']) ? "/[^a-zA-Z_0-9]+|^[0-9_]+/" : $fconfigarr['filterstr'];
		(empty($fieldnew['ename']) || empty($fieldnew['cname'])) && amessage('field_data_miss',$fconfigarr['errorurl']);
		preg_match($filterstr,$fieldnew['ename']) && amessage('field_ename_illegal',$fconfigarr['errorurl']);
		$fieldnew['ename'] = strtolower($fieldnew['ename']);
		in_array($fieldnew['ename'], $fconfigarr['enamearr']) && amessage('field_ename_repeat',$fconfigarr['errorurl']);
		in_array($fieldnew['ename'], $fieldwords) && amessage('field_ename_notuse',$fconfigarr['errorurl']);
		$fieldnew['length'] = empty($fieldnew['length']) ? 10 : min(255,max(1,intval($fieldnew['length'])));
		$db->query("ALTER TABLE $fconfigarr[altertable] ADD $fieldnew[ename] varchar($fieldnew[length]) NOT NULL default ''");
	}else{
		$fieldnew['cname'] = empty($fieldnew['cname']) ? $field['cname'] : $fieldnew['cname'];
		if(isset($fieldnew['length'])){
			$fieldnew['length'] = empty($fieldnew['length']) ? 10 : min(255,max(1,intval($fieldnew['length'])));
			if($field['length'] != $fieldnew['length']){
				$db->query("ALTER TABLE $fconfigarr[altertable] CHANGE $field[ename] $field[ename] varchar($fieldnew[length]) NOT NULL default ''");
			}
		}
	}
	if(!$isfunc){
		$fieldnew['guide'] = empty($fieldnew['guide']) ? '' : trim($fieldnew['guide']);
		$fieldnew['vdefault'] = empty($fieldnew['vdefault']) ? '' : trim($fieldnew['vdefault']);
		foreach(array('min','max') as $key){
			$fieldnew[$key] = max(0,intval($fieldnew[$key]));
			$fieldnew[$key] = empty($fieldnew[$key]) ? '' : $fieldnew[$key];
		}
		$fieldnew['regular'] = empty($fieldnew['regular']) ? '' : trim($fieldnew['regular']);
	}else{
		$fieldnew['func'] = empty($fieldnew['func']) ? '' : trim($fieldnew['func']);
	}
	foreach(array('datatype','ename','length','cname','notnull','nohtml','mode','guide','mlimit','rpid','issearch','innertext','min','max','regular','isfunc','func','vdefault','pmid','custom_1','custom_2',) as $var){
		isset($fieldnew[$var]) && $sqlstr .= (!$sqlstr ? '' : ',')."$var='".$fieldnew[$var]."'";
	}
	if($fnew){
		$db->query("INSERT INTO $fconfigarr[fieldtable] SET $sqlstr");
	}else{
		$wherestr = empty($fconfigarr['wherestr']) ? "WHERE ename='$field[ename]'" : $fconfigarr['wherestr'];
		$db->query("UPDATE $fconfigarr[fieldtable] SET $sqlstr $wherestr");
	}
}
?>
