<?php
	!defined('DIRCMS_MANAGE') && exit('Access Denied!');
	if($_roleid!=1 && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');
	$action=empty($action)?'manage':trim($action);
	require_once(DIRCMS_ROOT.'include/list.class.php');
	$l=new dircmslist();
	$c=new cache();
	switch($action)
	{
		case 'manage':
			if(isset($do_submit))
			{
				foreach($id as $modelid)
				{
					$modelid=intval($modelid);
					$db->update(DB_PRE.'membermodel',array('modelname'=>$modelname[$modelid]),'id='.$modelid);
				}
				$c->cache_model();
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membermodel&action=manage ');
			}
			$where=1;
			$orderby="id ASC";
			$result=$l->getlist(DB_PRE.'membermodel',$where,$orderby,1,20);
			include admin_tlp('membermodel_manage');
			break;
		case 'add':
			if(isset($do_submit))
			{
				if(!$newmodel['name'] || !$newmodel['modelname'])showmsg('模型名称和数据表名不能为空!');
				if(!preg_match('/[a-z0-9_]{1,30}/i',$newmodel['name']))showmsg('数据表名不可用，请更换!');
				if(in_array(DB_PRE.$newmodel['name'],$db->get_tables()))showmsg('数据表名已存在，请更换!');
				if($db->insert(DB_PRE.'membermodel',$newmodel,false) && $db->create_table(DB_PRE.$newmodel['name'])) 
				{
					$c->cache_model();
					showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membermodel&action=manage');
				}
				else
				{
					showmsg($LANG['MANAGE_ERROR'],ADMIN_FILE.'?file=membermodel&action=manage');
				}
				$c->cache_model();
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membermodel&action=manage ');
			}
			$templateselect='<select name="newmodel[template]">';
			$stemplateselect='<select name="newmodel[stemplate]">';
			$cache=cache_read('template.cache.php',TPL_ROOT.TPL_NAME.'/');
			if($cache)foreach($cache as $r)
			{
				if(substr($r['template'],0,6)=='space_')
				{
					$templateselect.='<option value="'.$r['template'].'">'.$r['template'].'.html</option>';
				}
				if(substr($r['template'],0,7)=='search_')
				{
					$stemplateselect.='<option value="'.$r['template'].'">'.$r['template'].'.html</option>';
				}
			}
			$templateselect.='</select>';
			$stemplateselect.='</select>';
			include admin_tlp('membermodel_add');
			break;
		case 'delete':
			if($db->drop_table($tbname)&&$db->mysql_delete(DB_PRE.'membermodel',intval($id)) && $db->mysql_delete(DB_PRE.'memberdb_fields',intval($id),'modelid'))
			{
				$c->cache_model();
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membermodel&action=manage');
			}
			else
			{
				showmsg($LANG['MANAGE_ERROR']);
			}
			break;
		case 'chkmodel':
			echo in_array(DB_PRE.$data,$db->get_tables())?'yes':'no';
			exit();
			break;
		case 'setbase_fields':
			$isbase=intval($isbase);
			if($db->query("UPDATE ".DB_PRE."memberdb_fields SET ".DB_PRE."memberdb_fields.isbase=$isbase WHERE  ".DB_PRE."memberdb_fields.id=".intval($id),true)){$c->cache_fields($modelid);showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membermodel&action=manage_fields&modelid='.$modelid);}
			else showmsg($LANG['MANAGE_ERROR'],ADMIN_FILE.'?file=membermodel&action=manage_fields&modelid='.$modelid);
			break;
		case 'register_enabled':
			$db->update(DB_PRE.'membermodel',array('register_enabled'=>intval($register_enabled)),'id='.intval($id));
			showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membermodel&action=manage');
			break;
		case 'install':
			$db->update(DB_PRE.'membermodel',array('isinstall'=>intval($isinstall)),'id='.intval($id));
			showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membermodel&action=manage');
			break;
		case 'iscompany':
			$db->update(DB_PRE.'membermodel',array('iscompany'=>intval($iscompany)),'id='.intval($id));
			showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membermodel&action=manage');
			break;
		case 'template':
			$db->update(DB_PRE.'membermodel',array($tlp=>$tlpname),'id='.intval($modelid));
			$c->cache_model();
			showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membermodel&action=manage ');
			break;
		case 'manage_fields':
			if(isset($do_submit)) //排序
			{
				$i=0;
				foreach($id as $val)
				{
					$db->update(DB_PRE."memberdb_fields",array('orderby'=>intval($orderby[$i]),'css'=>$css[$i],'fieldname'=>$fieldsname[$i]),'id='.intval($val));
					$i++;
				}
				$c->cache_model();
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membermodel&action=manage_fields&modelid='.$modelid);
			}
			$result=$db->fetch_all("SELECT * FROM ".DB_PRE."memberdb_fields WHERE ".DB_PRE."memberdb_fields.modelid=".intval($modelid)." ORDER BY ".DB_PRE."memberdb_fields.orderby ASC");
			include admin_tlp('membermodel_manage_fields');
			break;
		case 'disable_fields':
			$disable=intval($disable);
			if($db->query("UPDATE ".DB_PRE."memberdb_fields SET ".DB_PRE."memberdb_fields.disabled=$disable WHERE  ".DB_PRE."memberdb_fields.id=".intval($id),true))
			{
				$c->cache_model();
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membermodel&action=manage_fields&modelid='.$modelid);
			}
			else 
			{
				showmsg($LANG['MANAGE_ERROR'],ADMIN_FILE.'?file=membermodel&action=manage_fields&modelid='.$modelid);
			}
			break;
		case 'delete_fields':
			$r=$db->fetch_one("SELECT name FROM ".DB_PRE."membermodel WHERE ".DB_PRE."membermodel.id=".intval($modelid));
			$db->query("ALTER TABLE `".DB_PRE."{$r['name']}` DROP `{$name}`",true);
			$db->query("DELETE FROM ".DB_PRE."memberdb_fields WHERE ".DB_PRE."memberdb_fields.id=".intval($id),true);
			$c->cache_model();
			showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membermodel&action=manage_fields&modelid='.$modelid);
			break;
		case 'add_fields':
			if(isset($do_submit))
			{
				$modelid=$info['modelid'];
				$info['maxsize']=(isset($info['maxsize']) && intval($info['maxsize']))?min(intval($info['maxsize']),255):255;
				switch($info['preg'])
				{
					case 'limit3_50':
						$minsize=3;
						$maxsize=50;
						break;
					case 'userName':
						$minsize=3;
						$maxsize=30;
						break;
					case 'usePsw':
						$minsize=3;
						$maxsize=30;
						break;
					case 'email':
						$minsize=3;
						$maxsize=80;
						break;
					case 'qq':
						$minsize=3;
						$maxsize=15;
						break;
					case 'mobile':
						$minsize=10;
						$maxsize=15;
						break;
					case 'phone':
						$minsize=3;
						$maxsize=50;
						break;
					case 'ip':
						$minsize=12;
						$maxsize=15;
						break;	
					default:
						$minsize=0;
						$maxsize=255;
						break;						
				}
				$r=$db->fetch_one("SELECT name FROM ".DB_PRE."membermodel WHERE ".DB_PRE."membermodel.id=".intval($modelid));
				if(in_array($info['name'],$db->get_fields(DB_PRE.$r['name'])))showmsg('指定字段已存在!',ADMIN_FILE.'?file=membermodel&action=manage_fields&modelid='.$modelid);
				if(in_array($info['formtype'],array('catid','typeid','areaid','inputtime','title','keyword','groupid','posid','template','pages','id','modelid','managegroupid','username','password','email','spaceurl','message','amount','point','regtime','logintime','logintimes','movetime','loginip','ispassed','islocked','userid')))$info['name']=$info['formtype'];
				if(in_array($info['name'],array('catid','typeid','areaid','inputtime','title','keyword','groupid','posid','template','pages','id','modelid','managegroupid','username','password','email','spaceurl','message','amount','point','regtime','logintime','logintimes','movetime','loginip','ispassed','islocked','userid')))showmsg('指定字段无法使用!',ADMIN_FILE.'?file=membermodel&action=manage_fields&modelid='.$modelid);
				if(in_array($info['formtype'],array('textarea','editor','images')))
				{
					$db->create_fields(DB_PRE.$r['name'],$info['name'],false);
				}else{
					$db->create_fields(DB_PRE.$r['name'],$info['name'],intval($info['maxsize']));
				}
				$db->insert(DB_PRE.'memberdb_fields',$info,false);
				unset($r);
				$c->cache_fields($modelid);
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membermodel&action=manage_fields&modelid='.$modelid);
			}
			include admin_tlp('membermodel_add_fields');
			break;
		case 'edit_fields':
			$r=$db->fetch_one("SELECT * FROM ".DB_PRE."memberdb_fields WHERE ".DB_PRE."memberdb_fields.id=".intval($id));
			if(isset($do_submit))
			{	
				$modelid=$info['modelid'];
				unset($info['modelid'],$info['name'],$info['formtype']);
				$info['maxsize']=(isset($info['maxsize']) && intval($info['maxsize']))?min(intval($info['maxsize']),255):255;
				switch($info['preg'])
				{
					case 'limit3_50':
						$minsize=3;
						$maxsize=50;
						break;
					case 'userName':
						$minsize=3;
						$maxsize=30;
						break;
					case 'usePsw':
						$minsize=3;
						$maxsize=30;
						break;
					case 'email':
						$minsize=3;
						$maxsize=80;
						break;
					case 'qq':
						$minsize=3;
						$maxsize=15;
						break;
					case 'mobile':
						$minsize=10;
						$maxsize=15;
						break;
					case 'phone':
						$minsize=3;
						$maxsize=50;
						break;
					case 'ip':
						$minsize=12;
						$maxsize=15;
						break;	
					default:
						$minsize=0;
						$maxsize=255;
						break;						
				}
				$db->update(DB_PRE.'memberdb_fields',$info,"id=".intval($id));
				$c->cache_fields($modelid);
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=membermodel&action=manage_fields&modelid='.$modelid);
			}
			include admin_tlp('membermodel_edit_fields');
			unset($r);
			break;
		case 'preview':
			@header("Cache-Control: no-cache, must-revalidate");
			$r=cache_read('cache_membermodel'.$modelid.'.cache.php',DIRCMS_ROOT.'data/cache_model/');
			$m_table=$r['name'];
			$c=$db->fetch_one("SELECT a.*,b.* FROM ".DB_PRE."member a LEFT JOIN ".DB_PRE.$m_table." b ON a.id=b.userid WHERE a.id=".intval($_userid));
			require_once(DIRCMS_ROOT.'include/select.class.php');
			$s=new select();
			require_once(DIRCMS_ROOT.'include/form.class.php');	
			$f=new form('_m'.$modelid);
			$base_r=$f->get($c);
			include admin_tlp('membermodel_preview');
			break;
	}
?>