<?php
require_once(dirname(__FILE__)."/config.php");
CheckPurview('c_Stepselect');
require_once(DEDEINC."/datalistcp.class.php");
require_once(DEDEINC.'/enums.func.php');
/*-----------------
前台视图
function __show() { }
------------------*/
$ENV_GOBACK_URL = (isset($ENV_GOBACK_URL) ? $ENV_GOBACK_URL : 'stepselect_main.php');
if(empty($action))
{
  setcookie("ENV_GOBACK_URL",$dedeNowurl,time()+3600,"/");
  if(!isset($egroup)) $egroup = '';
  if(!isset($topvalue)) $topvalue = 0;
  $etypes = array();
  $egroups = array();
  $dsql->Execute('me','Select * from `#@__stepselect` order by id desc');
  while($arr = $dsql->GetArray())
  {
     $etypes[] = $arr;
     $egroups[$arr['egroup']] = $arr['itemname'];
  }

  if($egroup!='')
  {
	  $orderby = 'order by disorder asc, evalue asc';
	  if(!empty($topvalue))
	  {
	  	$egroupsql = " where egroup like '$egroup' And evalue>=$topvalue And evalue < ".($topvalue + 500);
	  }
	  else
	  {
	  	$egroupsql = " where egroup like '$egroup' ";
	  }
	  $sql = "Select * From `#@__sys_enum` $egroupsql $orderby";
  }
  else
  {
	  $egroupsql = '';
	  $sql = "Select * from `#@__stepselect` order by id desc";
  }
  $dlist = new DataListCP();
  $dlist->SetParameter('egroup',$egroup);
  $dlist->SetParameter('topvalue',$topvalue);
  $dlist->SetTemplet(DEDEADMIN."/templets/stepselect_main.htm");
  $dlist->SetSource($sql);
  $dlist->display();
  exit();
}
else if($action=='edit' || $action=='addnew' || $action=='addenum' || $action=='view')
{
   AjaxHead();
   include('./templets/stepselect_showajax.htm');
   exit();
}
/*-----------------
删除类型或枚举值
function __del() { }
------------------*/
else if($action=='del')
{
   $arr = $dsql->GetOne("Select * from `#@__stepselect` where id='$id' ");
   if(!is_array($arr))
   {
      ShowMsg("无法获取分类信息，不允许后续操作！","stepselect_main.php?".ExecTime());
      exit();
   }
   if($arr['issystem']==1)
   {
      ShowMsg("系统内置的枚举分类不能删除！","stepselect_main.php?".ExecTime());
      exit();
   }
   $dsql->ExecuteNoneQuery("Delete From `#@__stepselect` where id='$id'; ");
   $dsql->ExecuteNoneQuery("Delete From `#@__sys_enum` where egroup='{$arr['egroup']}'; ");
   ShowMsg("成功删除一个分类！","stepselect_main.php?".ExecTime());
   exit();
}
else if($action=='delenumAllSel')
{
	if(isset($ids) && is_array($ids))
	{
		$id = join(',', $ids);
		
		$groups = array();
		$dsql->Execute('me', "Select egroup From `#@__sys_enum` where id in($id) group by egroup");
		while($row = $dsql->GetArray('me'))
		{
			$groups[] = $row['egroup'];
		}
		
		$dsql->ExecuteNoneQuery("Delete From `#@__sys_enum` where id in($id); ");
		
		//更新缓存
		foreach($groups as $egropu) {
			WriteEnumsCache($egroup);
		}
		
		ShowMsg("成功删除选中的枚举分类！",$ENV_GOBACK_URL);
	}
	else
	{
		ShowMsg("你没选择任何分类！", "-1");
	}
	exit();
}
else if($action=='delenum')
{
		$row = $dsql->GetOne("Select egroup From `#@__sys_enum` where id = '$id' ");
		$dsql->ExecuteNoneQuery("Delete From `#@__sys_enum` where id='{$id}'; ");
		WriteEnumsCache($row['egroup']);
		ShowMsg("成功删除一个枚举！",$ENV_GOBACK_URL);
		exit();
}
/*-----------------
保存类型修改
function __edit_save() { }
------------------*/
else if($action=='edit_save')
{
   if(eregi("[^0-9a-z_-]",$egroup))
   {
			ShowMsg("组名称不能有全角字符或特殊符号！","-1");
			exit();
   }
   $dsql->ExecuteNoneQuery("update `#@__stepselect` set `itemname`='$itemname',`egroup`='$egroup' where id='$id'; ");
   ShowMsg("成功修改一个分类！","stepselect_main.php?".ExecTime());
   exit();
}
/*-----------------
保存新类型
function __addnew_save() { }
------------------*/
else if($action=='addnew_save')
{
		if(eregi("[^0-9a-z_-]",$egroup))
		{
			ShowMsg("组名称不能有全角字符或特殊符号！","-1");
			exit();
		}
		$arr = $dsql->GetOne("Select * from `#@__stepselect` where itemname like '$itemname' Or egroup like '$egroup' ");
		if(is_array($arr))
		{
			ShowMsg("你指定的类别名称或组名称已经存在，不能使用！","stepselect_main.php");
			exit();
		}
		$dsql->ExecuteNoneQuery("Insert into `#@__stepselect`(`itemname`,`egroup`,`issign`,`issystem`) values('$itemname','$egroup','0','0'); ");
		WriteEnumsCache($egroup);
		ShowMsg("成功添加一个分类！","stepselect_main.php?egroup=$egroup");
		exit();
}
/*---------
把旧版全国省市表替换当前地区数据
function __exarea() { }
----------*/
else if($action=='exarea')
{
	$bigtypes = array();
	$dsql->ExecuteNoneQuery("Delete From `#@__sys_enum` where egroup='nativeplace'; ");
	$query = "SELECT * FROM `#@__area` WHERE reid =0 order by id asc";
	$dsql->Execute('me', $query);
	$n = 1;
	while($row = $dsql->GetArray())
	{
		$bigtypes[$row['id']] = $evalue = $disorder = $n * 500;
		$dsql->ExecuteNoneQuery("Insert into `#@__sys_enum`(`ename`,`evalue`,`egroup`,`disorder`,`issign`)
		                         values('{$row['name']}','$evalue','nativeplace','$disorder','0'); ");
    $n++;                                
	}
	$stypes = array();
	foreach($bigtypes as $k=>$v)
	{
		$query = "SELECT * FROM `#@__area` WHERE reid=$k order by id asc";
	  $dsql->Execute('me', $query);
	  $n = 1;
	  while($row = $dsql->GetArray())
	  {
	  	$stypes[$row['id']] = $evalue = $disorder = $v + $n;
	  	$dsql->ExecuteNoneQuery("Insert into `#@__sys_enum`(`ename`,`evalue`,`egroup`,`disorder`,`issign`)
	  	                         values('{$row['name']}','$evalue','nativeplace','$disorder','0'); ");
      $n++; 
	  }
	}
	WriteEnumsCache('nativeplace');
	ShowMsg("成功导入所有旧的地区数据！", "stepselect_main.php?egroup=nativeplace");
	exit();
}
/*--------------------
function __addenum_save() { }
关于二级枚举的说明，为了节省查询速度，二级枚举是通过特殊算法生成的，原理为
凡是能被 500 整除的都是一级枚举，(500 * n) + 1 < em < 500 * (n+1) 为下级枚举
如：1000 的下级枚举对应的值为 1001,1002,1003...1499
对于 issign=1 的，表示这个类别只有一级枚举，则不受上面的算法限制
---------------------*/
else if($action=='addenum_save')
{
   if(empty($ename) || empty($egroup)) {
   	  Showmsg("类别名称或组名称不能为空！","-1");
   	  exit();
   }
   if($issign==1 || $topvalue==0)
   {
      	$enames = explode(',', $ename);
      	foreach($enames as $ename)
      	{
      		$arr = $dsql->GetOne("Select * From `#@__sys_enum` where egroup='$egroup' And (evalue mod 500)=0 order by evalue desc ");
				
					if(!is_array($arr)) $disorder = $evalue = ($issign==1 ? 1 : 500);
					else $disorder = $evalue = $arr['disorder'] + ($issign==1 ? 1 : 500);
				
					$dsql->ExecuteNoneQuery("Insert into `#@__sys_enum`(`ename`,`evalue`,`egroup`,`disorder`,`issign`) 
                                    values('$ename','$evalue','$egroup','$disorder','$issign'); "); 
        }
        WriteEnumsCache($egroup);                                                          
				ShowMsg("成功添加枚举分类！".$dsql->GetError(), $ENV_GOBACK_URL);
				exit();
		}
		else
		{
				$minid = $topvalue;
				$maxid = $topvalue + 500;
				$enames = explode(',', $ename);
      	foreach($enames as $ename)
      	{
					$arr = $dsql->GetOne("Select * From `#@__sys_enum` where egroup='$egroup' And evalue>$minid And evalue<$maxid order by evalue desc ");
					if(!is_array($arr))
					{
						$disorder = $evalue = $minid+1;
					}
					else
					{
						$disorder = $arr['disorder']+1;
						$evalue = $arr['evalue']+1;
					}
					$dsql->ExecuteNoneQuery("Insert into `#@__sys_enum`(`ename`,`evalue`,`egroup`,`disorder`,`issign`) 
                                  values('$ename','$evalue','$egroup','$disorder','$issign'); ");
        }
        WriteEnumsCache($egroup);
				ShowMsg("成功添加枚举分类！", $ENV_GOBACK_URL);
				exit();
		}
}
/*-----------------
修改枚举名称和排序
function __upenum() { }
------------------*/
else if($action=='upenum')
{
	$ename = trim(str_replace(' └─','',$ename));
	$row = $dsql->GetOne("Select egroup From `#@__sys_enum` where id = '$id' ");
	WriteEnumsCache($row['egroup']);
	$dsql->ExecuteNoneQuery("Update `#@__sys_enum` set `ename`='$ename',`disorder`='$disorder' where id='$aid'; ");
	ShowMsg("成功修改一个枚举！", $ENV_GOBACK_URL);
	exit();
}
/*-----------------
更新枚举缓存
function __upallcache() { }
------------------*/
else if($action=='upallcache')
{
	if(!isset($egroup)) $egroup = '';
	WriteEnumsCache($egroup);
	ShowMsg("成更新枚举缓存！", $ENV_GOBACK_URL);
	exit();
}
?>