<?php
/*-------------------------------------
	版权所有：南京牛昆网络科技有限公司（TEL-15896098989）
	程序版本：1.0
	更新日期：2014-10-31  
-------------------------------------*/
class model
{
	private $db;
	private $table;
	private $fieldtable;

	function __construct()
	{
		global $db;
		$this->db=$db;
		$this->table=LA_PRE.'model';
		$this->fieldtable=LA_PRE.'field';
	}
	
	function modellist()
	{
		global $page;
		$where='mid=0';
		include SYSTEM_ROOT.'include/datalist.class.php';
		$datalist = new datalist();
		$orderby='typeid ASC ,id ASC';
		$page=max(isset($page)?intval($page):1,1);
		$pagesize=8;
		$result=$datalist->getlist($this->table,$where,$orderby,$page,$pagesize);
		$this->pagestring=$datalist->pagestring;
		return $result;
	}

	function submodellist($id)
	{
		global $page;
		$id=intval($id);
		$where='mid='.$id;
		include SYSTEM_ROOT.'include/datalist.class.php';
		$datalist = new datalist();
		$orderby='typeid ASC ,id ASC';
		$page=max(isset($page)?intval($page):1,1);
		$pagesize=8;
		$result=$datalist->getlist($this->table,$where,$orderby,$page,$pagesize);
		$this->pagestring=$datalist->pagestring;
		return $result;
	}

	function modulemodel($typeid='news')
	{
		return $this->db->fetch_all("SELECT * FROM `$this->table` WHERE `$this->table`.`mid`=0 AND `$this->table`.`typeid`='".preg_replace('/[^a-z0-9_]/i','',$typeid)."'");
	}

	function getmodelfields($modelid=0)
	{
		return $this->db->fetch_all("SELECT * FROM `$this->fieldtable` WHERE `$this->fieldtable`.`modelid`=".intval($modelid));
	}

	function fieldlist($id)
	{
		global $page;
		$id=intval($id);
		$where='modelid='.$id;
		include SYSTEM_ROOT.'include/datalist.class.php';
		$datalist = new datalist();
		$orderby='orderby ASC,id ASC';
		$page=max(isset($page)?intval($page):1,1);
		$pagesize=15;
		$result=$datalist->getlist($this->fieldtable,$where,$orderby,$page,$pagesize);
		$this->pagestring=$datalist->pagestring;
		return $result;
	}

	function field_delete($id)
	{
		$id=intval($id);
		$fieldinfo=$this->fieldinfo($id);
		$modelinfo=$this->info($fieldinfo['modelid']);

		if($fieldinfo['issystem'])
		{
			return false;
		}
		
		$this->db->query("ALTER TABLE `".LA_PRE.$modelinfo['tbname']."` DROP `".$fieldinfo['enname']."`",true);
		$this->db->mysql_delete($this->fieldtable,intval($id));

		if($modelinfo['mid']!=0)
		{
			$r=$this->db->fetch_all("SELECT * FROM `$this->table` WHERE `$this->table`.`mid`=".intval($modelinfo['mid']));
			foreach($r as $_r)
			{
				if($_r['tbname']!=$modelinfo['tbname'])
				{
					$this->db->query("ALTER TABLE `".LA_PRE.$_r['tbname']."` DROP `".$fieldinfo['enname']."`",true);
					$this->db->query("DELETE FROM `$this->fieldtable` WHERE `$this->fieldtable`.`modelid`=".$_r['id']." AND `$this->fieldtable`.`enname`='".$fieldinfo['enname']."'");
				}
			}
		}

		return true;
	}

	function field_edit($id,$info)
	{
		$id=intval($id);
		$fieldinfo=$this->fieldinfo($id);
		$modelinfo=$this->info($fieldinfo['modelid']);
		$info['oldenname']=preg_replace('/[^a-z_0-9]/i','',$info['oldenname']);
		$info['enname']=preg_replace('/[^a-z_0-9]/i','',$info['enname']);

		$info['length']=empty($info['length'])?'8':$info['length'];
		
		if(!$fieldinfo['issystem'])
		{
			
			if(!$info['name'] || !$info['enname'])
			{
				return false;
			}

			if(in_array($info['enname'],$this->db->get_fields(LA_PRE.$modelinfo['tbname'])) && $info['oldenname']!=$info['enname'])
			{
				return false;
			}

			if(in_array($info['form'],array('videourl','more','downurl','attachment','images','fckeditor')))
			{
				$this->db->query("ALTER TABLE  `".LA_PRE.$modelinfo['tbname']."` CHANGE  `".$info['oldenname']."`  `".$info['enname']."` TEXT NOT NULL",true);
			}
			else if(in_array($info['form'],array('number')))
			{
				if(strpos($info['length'],','))
				{
					$_length=explode(',',$info['length']);
					$this->db->query("ALTER TABLE  `".LA_PRE.$modelinfo['tbname']."` CHANGE  `".$info['oldenname']."`  `".$info['enname']."` DECIMAL( ".max(1,intval($_length[0])).", ".max(1,intval($_length[1]))." ) NOT NULL",true);
				}
				else
				{
					$this->db->query("ALTER TABLE  `".LA_PRE.$modelinfo['tbname']."` CHANGE  `".$info['oldenname']."`  `".$info['enname']."` INT( ".max(1,intval($info['length']))." ) NOT NULL",true);
				}
			}
			else
			{
				if(intval($info['length'])>255)
				{
					$this->db->query("ALTER TABLE  `".LA_PRE.$modelinfo['tbname']."` CHANGE  `".$info['oldenname']."`  `".$info['enname']."` TEXT NOT NULL",true);
				}
				else
				{
					$this->db->query("ALTER TABLE  `".LA_PRE.$modelinfo['tbname']."` CHANGE  `".$info['oldenname']."`  `".$info['enname']."` VARCHAR( ".max(1,intval($info['length']))." ) NOT NULL",true);
				}
			}
		}
		$info['membergrade']=implode(',',$info['membergrade']);

		if($fieldinfo['issystem'])
		{
			unset($info['enname'],$info['form'],$info['options'],$info['length']);
		}
		$this->db->update($this->fieldtable,$info,'id='.$id);
	
		if($modelinfo['mid']!=0)
		{
			$r=$this->db->fetch_all("SELECT * FROM `$this->table` WHERE `$this->table`.`mid`=".intval($modelinfo['mid']));
			foreach($r as $_r)
			{
				$info['modelid']=$_r['id'];
				if(!$fieldinfo['issystem'])
				{
						if($_r['tbname']!=$modelinfo['tbname'] && (!in_array($info['enname'],$this->db->get_fields(LA_PRE.$_r['tbname'])) || $info['oldenname']==$info['enname']))
						{
							if(in_array($info['form'],array('videourl','more','downurl','attachment','images','fckeditor')))
							{
								$this->db->query("ALTER TABLE  `".LA_PRE.$_r['tbname']."` CHANGE  `".$info['oldenname']."`  `".$info['enname']."` TEXT NOT NULL",true);
							}
							else if(in_array($info['form'],array('number')))
							{
								if(strpos($info['length'],','))
								{
									$_length=explode(',',$info['length']);
									$this->db->query("ALTER TABLE  `".LA_PRE.$_r['tbname']."` CHANGE  `".$info['oldenname']."`  `".$info['enname']."` DECIMAL( ".max(1,intval($_length[0])).", ".max(1,intval($_length[1]))." ) NOT NULL",true);
								}
								else
								{
									$this->db->query("ALTER TABLE  `".LA_PRE.$_r['tbname']."` CHANGE  `".$info['oldenname']."`  `".$info['enname']."` INT( ".max(1,intval($info['length']))." ) NOT NULL",true);
								}
							}
							else
							{
								if(intval($info['length'])>255)
								{
									$this->db->query("ALTER TABLE  `".LA_PRE.$_r['tbname']."` CHANGE  `".$info['oldenname']."`  `".$info['enname']."` TEXT NOT NULL",true);
								}
								else
								{
									$this->db->query("ALTER TABLE  `".LA_PRE.$_r['tbname']."` CHANGE  `".$info['oldenname']."`  `".$info['enname']."` VARCHAR( ".max(1,intval($info['length']))." ) NOT NULL",true);
								}
							}
						}
				}
				if($fieldinfo['issystem'])
				{
					unset($info['enname'],$info['form'],$info['options'],$info['length']);
				}
				$this->db->update($this->fieldtable,$info,'modelid='.$_r['id'].' AND enname=\''.$info['oldenname'].'\'');
			}
		}

		return true;
	}

	function field_add($id,$info)
	{
		$id=intval($id);
		$modelinfo=$this->info($id);
		$info['enname']=preg_replace('/[^a-z_0-9]/i','',$info['enname']);
		$info['modelid']=$id;
		if(!$info['name'] || !$info['enname'])
		{
			return false;
		}

		if(in_array($info['enname'],$this->db->get_fields(LA_PRE.$modelinfo['tbname'])))
		{
			return false;
		}
		
		$info['length']=empty($info['length'])?'8':$info['length'];
		if(in_array($info['form'],array('videourl','more','downurl','attachment','images','fckeditor')))
		{
			$this->db->create_fields(LA_PRE.$modelinfo['tbname'],$info['enname']);
		}
		else if(in_array($info['form'],array('number')))
		{
			if(strpos($info['length'],','))
			{
				$this->db->create_fields(LA_PRE.$modelinfo['tbname'],$info['enname'],$info['length'],'DECIMAL');
			}
			else
				{
				$this->db->create_fields(LA_PRE.$modelinfo['tbname'],$info['enname'],min(8,intval($info['length'])),'INT');
			}
		}
		else
		{
			if(intval($info['length'])>255)
			{
				$this->db->create_fields(LA_PRE.$modelinfo['tbname'],$info['enname']);
			}
			else
			{
				$this->db->create_fields(LA_PRE.$modelinfo['tbname'],$info['enname'],intval($info['length']));
			}
		}
		
		$info['membergrade']=implode(',',$info['membergrade']);
		$this->db->insert($this->fieldtable,$info);
		if($modelinfo['mid']!=0)
		{
			$r=$this->db->fetch_all("SELECT * FROM `$this->table` WHERE `$this->table`.`mid`=".intval($modelinfo['mid']));
			foreach($r as $_r)
			{
				if($_r['tbname']!=$modelinfo['tbname'] && !in_array($info['enname'],$this->db->get_fields(LA_PRE.$_r['tbname'])))
				{
					$info['modelid']=$_r['id'];
					if(in_array($info['form'],array('videourl','more','downurl','attachment','images','fckeditor')))
					{
						$this->db->create_fields(LA_PRE.$_r['tbname'],$info['enname']);
					}
					else if(in_array($info['form'],array('number')))
					{
						if(strpos($info['length'],','))
						{
							$this->db->create_fields(LA_PRE.$_r['tbname'],$info['enname'],$info['length'],'DECIMAL');
						}
						else
							{
							$this->db->create_fields(LA_PRE.$_r['tbname'],$info['enname'],min(8,intval($info['length'])),'INT');
						}
					}
					else
					{
						if(intval($info['length'])>255)
						{
							$this->db->create_fields(LA_PRE.$_r['tbname'],$info['enname']);
						}
						else
						{
							$this->db->create_fields(LA_PRE.$_r['tbname'],$info['enname'],intval($info['length']));
						}
					}
					
					$this->db->insert($this->fieldtable,$info);
				}
			}
		}
		return true;
	}

	function info($id)
	{
		return $this->db->fetch_one("SELECT * FROM `$this->table` WHERE `$this->table`.`id`=".intval($id));
	}

	function fieldinfo($id)
	{
		return $this->db->fetch_one("SELECT * FROM `$this->fieldtable` WHERE `$this->fieldtable`.`id`=".intval($id));
	}

	function modelname($id)
	{
		$r=$this->info($id);
		return $r['name'];
	}

	function add($info)
	{
		$info['name']=trim($info['name']);
		$info['tbname']=preg_replace('/[^a-z_0-9]/i','',$info['tbname']);
		$info['typeid']=preg_replace('/[^a-z_0-9]/i','',$info['typeid']);
		$info['siteid']=$_SESSION['siteid'];

		if(!$info['name'] || !$info['tbname'])
		{
			return false;
		}
		
		$info['typeid']=$info['typeid']?$info['typeid']:$info['tbname'];

		$alltables=$this->db->get_tables();
		if(in_array($info['tbname'],$alltables))
		{
			return false;
		}
		
		/*
			主表
		*/
		$this->db->createtable($info['tbname']);
		$mid=$this->db->insert($this->table,$info);

		/*
			副表
		*/
		$this->db->createsubtable($info['tbname'].'_data');
		$_info=array();
		$_info['name']=$info['name'].' 副表';
		$_info['tbname']=$info['tbname'].'_data';
		$_info['typeid']=$info['typeid'];
		$_info['mid']=$mid;
		$_info['siteid']=$_SESSION['siteid'];
		$this->db->insert($this->table,$_info);
		
		return true;
	}

	function split($id)
	{
		$id=intval($id);
		$r=$this->info($id);
		$r=$this->info($r['mid']);

		$sql=$this->db->createsql(LA_PRE.$r['tbname'].'_data');
		$sql=str_replace('`'.LA_PRE.$r['tbname'].'_data`','`'.LA_PRE.$r['tbname'].'_data'.$this->subformcount($id).'`',$sql);

		$_info=array();
		$_info['name']=$r['name'].' 副表';
		$_info['tbname']=$r['tbname'].'_data'.$this->subformcount($id);
		$_info['typeid']=$r['typeid'];
		$_info['mid']=$r['id'];
		$_info['siteid']=$_SESSION['siteid'];
		$modelid=$this->db->insert($this->table,$_info);
		$this->db->query($sql,true);
		$r=$this->db->fetch_one("SELECT * FROM `$this->table` WHERE `$this->table`.`tbname`='".$r['tbname']."_data'");
		$r=$this->db->fetch_all("SELECT * FROM `$this->fieldtable` WHERE `$this->fieldtable`.`modelid`=".$r['id']);
		if($r)foreach($r as $_r)
		{
			$_info=$_r;
			unset($_info['id']);
			$_info['modelid']=$modelid;
			$this->db->insert($this->fieldtable,$_info);
		}
		return true;
	}

	function subformcount($id)
	{
		$id=intval($id);
		$r=$this->info($id);
		return get_cache_counts("SELECT COUNT(*) AS count FROM `$this->table` WHERE mid=".$r['mid']);
	}

	function subformcount2($id)
	{
		return get_cache_counts("SELECT COUNT(*) AS count FROM `$this->table` WHERE mid=".intval($id));
	}
}
?>
