<?php
class AddField
{
	var $db;
	var $tpl;
	var $page;
	var $module;
	
	function AddField($module)
	{
		global $db;
		global $config;
		$this -> db     = & $db;
		$this->module=$module;
	}
	
	//$id:要编辑的ID（为0时表示增加）,$catid:产品类别ID列表, $table_name扩展表名
	function addfieldinput($id,$table_name,$frant=NULL)
	{
		global $config;$list = array();
		if(empty($table_name))
			return;
			
		if($id*1>"0")
		{
			$this->db->query("SHOW TABLES LIKE '".$table_name."'");
			if($this->db->num_rows()==1)
			{
				$sql="select * from $table_name where infoid='$id' and infotype='".$this->module."'";
				$this->db->query($sql);
				$de=$this->db->fetchRow();
			}
			else
				return;
		}
		$tn=explode('_',$table_name);
		$catid=$tn[2];
		$sql = "select * from ".EXTENDFILE." where catid ='$catid' and statu=1";
		
		$this->db->query($sql);
		$res=$this->db->getRows();
		foreach($res as $v)
		{
			$t=array();
			$t["fieldName"]=$v["fieldName"];//字段名称
			$t["fieldDisplay"]=$v["fieldDisplay"];//字段描述
			if($id*1>"0"&&!empty($de[$v['fieldName']]))
				$t["defaultValue"]=$de[$v['fieldName']];//编辑状态
			else
				$t["defaultValue"]=$v['defaultValue'];//默认值
			$t["displayType"]=$v["displayType"];//显示类型
			$t["catItem"]=$v["catItem"];//列表值
			if($frant)
			{
				if($t['displayType']==5)
					$list['d'][]=$this->front_li($t);
				else
					$list['s'][]=$this->front_li($t);
			}
			else
				$list[]=$this->addtabletr($t);
		}
		return $list;
	}
	
	function front_li($ob)
	{
		$type = $ob["displayType"];
		$htmlstr="";
		$returnstr ="<dl id='$ob[fieldName]'><dt>".$ob["fieldDisplay"]."</dt>";
		$value=$ob["defaultValue"];
		
		switch($type)
		{
		case "1":
			$htmlstr="$value";		
			break;
		case "2":
			$htmlstr="$value";
			break;
		case "3"://下拉框
			$numarray = explode(",",$ob["catItem"]);
			for($ii=0;$ii<count($numarray);$ii++){
				$itemvalue = explode("|",$numarray[$ii]);//.split('|');
				if($itemvalue[0]==$value){
					$selectnum=$itemvalue[1];
				}
			}
			$htmlstr=$selectnum;				
			break;
		case "4"://单选框
			$selectnum = "";
			$numarray = explode(",",$ob["catItem"]);//value.split(',');
			for($ii=0;$ii<count($numarray);$ii++){
				$itemvalue = explode("|",$numarray[$ii]);//.split('|');
				if($itemvalue[0]==$value){
					$selectnum=$itemvalue[1];
				}
			}
			$htmlstr=$selectnum;
			break;
		
		case "5"://多选框 <input name="asd2" type="checkbox" class="checkbox" id="asd2" checked>
			$selectnum = "";
			$numarray = explode(",",$ob["catItem"]);//value.split(',');
			for($ii=0;$ii<count($numarray);$ii++)
			{
				$itemvalue = explode("|",$numarray[$ii]);//.split('|');				
				$nv=explode(",",$value);			
				if(in_array($itemvalue[0],$nv))
				{
					if($ob['fieldName']=='color')
						$selectnum.='<font style="background-color:'.$itemvalue[1].';color:'.$itemvalue[1].'" />'.$itemvalue[1].'</font>';
					else
						$selectnum.='<font>'.$itemvalue[1].'</font>';
				}
			}
			$htmlstr=$selectnum;
			break;
		}
		return $returnstr.'<dd>'.$htmlstr."</dd></dl>";
	}
	
	function addtabletr($ob)
	{
		$type = $ob["displayType"];
		$htmlstr="";
		$returnstr ="<tr><td width=\"16%\" align=\"left\">".$ob["fieldDisplay"]."</td><td width=\"84%\" bgColor=\"#ffffff\">";
		$value=$ob["defaultValue"];
		
		
		switch($type)
		{
		case "1"://单行文本(text)
			$htmlstr="<input class='text' type=\"text\" name=\"".$ob["fieldName"]."\" id=\"".$ob["fieldName"]."\"  value=\"".$ob["defaultValue"]."\" >";		
		break;
		
		case "2":
			$htmlstr="<textarea class='textarea' name=\"".$ob["fieldName"]."\" id=\"".$ob["fieldName"]."\" cols=\"70\" rows=\"15\">".$ob["defaultValue"]."</textarea>";
		break;
		
		
		case "3"://下拉框
			$selectnum = "<select class='select' name=\"".$ob["fieldName"]."\">";
			$numarray = explode(",",$ob["catItem"]);
			for($ii=0;$ii<count($numarray);$ii++){
				$itemvalue = explode("|",$numarray[$ii]);//.split('|');
				if($itemvalue[0]==$value){
					$selectnum .="<option value=\"".$itemvalue[0]."\" selected>".$itemvalue[1]."</option>";
				}
				else{
					$selectnum.="<option value=\"".$itemvalue[0]."\">".$itemvalue[1]."</option>";
				}
			}
			$selectnum .= "</select>";
			$htmlstr=$selectnum;				
		break;
		
		
		case "4"://单选框
			$selectnum = "";
			$numarray = explode(",",$ob["catItem"]);//value.split(',');
			for($ii=0;$ii<count($numarray);$ii++){
				$itemvalue = explode("|",$numarray[$ii]);//.split('|');
				if($itemvalue[0]==$value){
					$selectnum.="<input class='radio' type=\"radio\" name=\"".$ob["fieldName"]."\" id=\"".$ob["fieldName"]."\" value=\"".$itemvalue[0]."\" checked>".$itemvalue[1];
				}
				else{
					$selectnum.="<input class='radio' type=\"radio\" name=\"".$ob["fieldName"]."\" id=\"".$ob["fieldName"]."\" value=\"".$itemvalue[0]."\" >".$itemvalue[1];
				}
			}
			$htmlstr=$selectnum;
		break;
		
		case "5"://多选框 <input name="asd2" type="checkbox" class="checkbox" id="asd2" checked>
			$selectnum = "";
			$numarray = explode(",",$ob["catItem"]);//value.split(',');
			for($ii=0;$ii<count($numarray);$ii++)
			{
				$itemvalue = explode("|",$numarray[$ii]);//.split('|');	
				$nv=explode(",",$value);			
				if(in_array($itemvalue[0],$nv))
					$str="checked";
				else
					$str='';
				$selectnum.="<input class='checkbox' $str type=\"checkbox\" name=\"".$ob["fieldName"]."[]\" id=\"".$ob["fieldName"]."[]\" value=\"".$itemvalue[0]."\" >".$itemvalue[1];
			}
			$htmlstr=$selectnum;
		break;
		}
		
		return $returnstr.$htmlstr."</td></tr>";
	}
	
	function delete_con($infoid,$table_name)
	{
		global $config;
		$this->db->query("SHOW TABLES LIKE '".$table_name."'");
		if($this->db->num_rows()==1)
		{
			$sql="delete from $table_name where infoid='$infoid' and infotype='".$this->module."'";
			$this->db->query($sql);
		}
	}
	
	//将数据写入表中
	function update_con($infoid,$table_name)
	{	
		global $config;	
		$tn=explode('_',$table_name);
		$catid=$tn[2];
		
		$sql= "select id,fieldName,displayType from ".EXTENDFILE." where catid='$catid' and statu=1";
		$this->db->query($sql);
		$re=$this->db->getRows();
		if(empty($re))
			return NULL;
			
		$filed[]='infoid';
		$filed[]='infotype';
		
		$values[]=$infoid;
		$values[]="'".$this->module."'";
		foreach($re as $v)
		{
			$filed[]=$v['fieldName'];
			
			if($v['displayType']=="5")
				$values[] = "'".implode(",",$_POST[$v['fieldName']])."'";
			else
				$values[] =	"'".$_POST[$v['fieldName']]."'";
		}
		$sql="select * from $table_name where infoid='$infoid' and infotype='".$this->module."'";
		$this->db->query($sql);
		if($this->db->num_rows())
		{
			$sql="update $table_name set id=id ";
			foreach($filed as $key=>$v)
			{
				$sql.=",$v=$values[$key]";
			}
			$sql.=" where infoid='$infoid' and infotype='".$this->module."'";
			$this->db->query($sql);
		}
		else
		{
			$sql = "insert into ".$table_name;
			$sql.="(".implode(",",$filed).")";
			$sql.=" values ";
			$sql.="(".implode(',',$values).")";
			$this->db->query($sql);
		}
	}
	
}
?>
