<?php
if(!defined('DEDEINC'))
{
	exit("dedecms");
}

$systemcache = DEDEDATA.'/enums/system.php';
if(!file_exists($systemcache))
{
	WriteEnumsCache();
	WriteAreaCache();
}

//更新枚举缓存
function WriteEnumsCache()
{
	global $dsql;
	$egroups = array();
	$dsql->SetQuery("Select egroup From `#@__sys_enum` group by egroup ");
	$dsql->Execute('enum');
	while($nrow = $dsql->GetArray('enum'))
	{
		$egroups[] = $nrow['egroup'];
	}
	foreach($egroups as $egroup)
	{
		$cachefile = DEDEDATA.'/enums/'.$egroup.'.php';
		$fp = fopen($cachefile,'w');
		fwrite($fp,'<'."?php\r\nglobal \$em_{$egroup}s;\r\n\$em_{$egroup}s = array();\r\n");
		$dsql->SetQuery("Select ename,evalue From `#@__sys_enum` where egroup='$egroup' order by disorder asc, evalue asc ");
		$dsql->Execute('enum');
		while($nrow = $dsql->GetArray('enum'))
		{
			fwrite($fp,"\$em_{$egroup}s[{$nrow['evalue']}] = '{$nrow['ename']}';\r\n");
		}
		fwrite($fp,'?'.'>');
		fclose($fp);
	}
	return '成功更新所有枚举缓存！';
}

//获取枚举的select表单
function GetEnumsForm($egroup,$evalue=0,$formid='',$seltitle='')
{
	$cachefile = DEDEDATA.'/enums/'.$egroup.'.php';
	include($cachefile);
	if($formid=='')
	{
		$formid = $egroup;
	}
	$forms = "<select name='$formid' id='$formid' class='enumselect'>\r\n";

	//if($evalue=='0')
	//{
		$forms .= "\t<option value='0' selected='selected'>--请选择--{$seltitle}</option>\r\n";
	//}
	foreach(${'em_'.$egroup.'s'} as $v=>$n)
	{
		$prefix = ($v > 500 && $v%500 != 0) ? '└─' : '';
		if($v==$evalue)
		{
			$forms .= "\t<option value='$v' selected='selected'>$prefix$n</option>\r\n";
		}
		else
		{
			$forms .= "\t<option value='$v'>$prefix$n</option>\r\n";
		}
	}
	$forms .= "</select>";
	return $forms;
}

//获取一级数据
function getTopData($egroup)
{
	$data = array();
	$cachefile = DEDEDATA.'/enums/'.$egroup.'.php';
	include($cachefile);
	foreach(${'em_'.$egroup.'s'} as $v=>$n)
	{
		if($v >= 500 && $v%500 == 0)
		{
			$data[$v] = $n;
		}
	}
	return $data;
}
//获取枚举的值
function GetEnumsValue($egroup,$evalue=0)
{
	$cachefile = DEDEDATA.'/enums/'.$egroup.'.php';
	include_once($cachefile);
	if(isset(${'em_'.$egroup.'s'}[$evalue]))
	{
		return ${'em_'.$egroup.'s'}[$evalue];
	}
	else
	{
		return "保密";
	}
}

//更新地区缓存
function WriteAreaCache()
{
	global $dsql;

	//更新area缓存
	$cachefile = DEDEDATA.'/enums/area.php';
	$dsql->SetQuery("Select * From `#@__area` group by id asc ");
	$dsql->Execute('area');
	$fp = fopen($cachefile,'w');
	fwrite($fp,'<'."?php\r\nglobal \$em_areas;\r\n\$em_areas = array();\r\n");
	while($nrow = $dsql->GetArray('area'))
	{
		fwrite($fp,"\$em_areas[{$nrow['reid']}][] = '{$nrow['id']},{$nrow['name']}';\r\n");
	}
	fwrite($fp,'?'.'>');
	fclose($fp);

	//更新Js文件
	include($cachefile);
	$jsfile = DEDEDATA.'/enums/area.js';
	$fp = fopen($jsfile,'w');
	fwrite($fp,"<!--\r\n");
	fwrite($fp,"Ap=new Array();\r\n");
	$i = 1;
	foreach($em_areas[0] as $n=>$btype)
	{
		list($_id,$_name) = explode(',',$btype);
		fwrite($fp,"Ap[$i]=\"{$_id}~{$_name}\";\r\n");
		fwrite($fp,"  B{$i}=new Array();\r\n");
		$j = 0;
		if(isset($em_areas[$_id]))
		{
			foreach($em_areas[$_id] as $k2=>$v2)
			{
				list($_id2,$_name2) = explode(',',$v2);
				fwrite($fp,"  B{$i}[$j]=\"{$_id2}~{$_name2}\";\r\n");
				$j++;
			}
			$i++;
		}
	}
	fwrite($fp,'-->');
	fclose($fp);
	return '成功更新地区枚举缓存！';
}

//获取地区的省区和城市相关值
function GetPlace($myplace)
{
	if( !file_exists(DEDEDATA.'/enums/area.php') )
	{
		WriteAreaCache();
	}
	require_once(DEDEDATA.'/enums/area.php');
	if($myplace==0)
	{
		$rePlace['province'] = 0;
		$rePlace['city'] = 0;
	}
	else
	{
		if(strlen($myplace)<3)
		{
			$rePlace['province'] = $myplace;
			$rePlace['city'] = 0;
		}
		else
		{
			$rePlace['province'] = (strlen($myplace)==3 ? $myplace[0] : $myplace[0].$myplace[1]);
			$rePlace['city'] = $myplace;
		}
	}
	return $rePlace;
}

function GetPlaceName($rePlace)
{
	global $em_areas;
	if($rePlace['province']==0)
	{
		$rePlace['province'] = '';
		$rePlace['city'] = '';
		return $rePlace;
	}
	if( !file_exists(DEDEDATA.'/enums/area.php') )
	{
		WriteAreaCache();
	}
	require_once(DEDEDATA.'/enums/area.php');
	foreach($em_areas[0] as $v)
	{
		list($_id,$_name) = explode(',',$v);
		if($_id==$places['province'])
		{
			echo "<option value='{$_id}' selected='selected'>{$_name}</option>\r\n";
		}
		else
		{
			echo "<option value='{$_id}'>{$_name}</option>\r\n";
		}
	}
}

?>