<?php
require_once(dirname(__FILE__)."/config.php");
$db = new DedeSql(false);
if($action == 'add')
{
	$name = trim($name);
	if($name == '' ) {
		ShowMsg('小分类名称不能为空，将返回小分类管理页面','sectors.php');
	}
	$disorder = intval($disorder);
	$disorder = max(0, $disorder);
	$description = trim($description);
	$sql = "insert into #@__smalltypes (name, disorder, description) values ('$name', $disorder, '$description');";
	$db->SetQuery($sql);
	if($db->ExecuteNoneQuery())
	{
		ShowMsg('添加小分类成功，将返回小分类管理页面','smalltype.php');
	}else
	{
		ShowMsg('更新小分类失败，将返回小分类管理页面','smalltype.php');
	}

}elseif($action == 'update')
{
	$errinfo = '';
	foreach($disorders as $key => $disorder)
	{
		$sql = "update #@__smalltypes set disorder=$disorder, name='$names[$key]', description='$descriptions[$key]' where id=$key";
		$db->SetQuery($sql);
		if(!$db->ExecuteNoneQuery())
		{
			$errinfo .= $sql."\n";
		}
	}
	if(trim($errinfo)  != '' )
	{
		ShowMsg($errinfo,'smalltype.php');
	}else
	{
		ShowMsg('更新小分类成功，将返回小分类管理页面','smalltype.php');
	}

}elseif($action == 'delete')
{
		$id = intval($id);
		if($id < 1) {
			ShowMsg('小分类编号不正确，将返回小分类管理页面','sectors.php');
		}else {
			$sql = "delete from #@__smalltypes where id=$id";
			$db->SetQuery($sql);
			if($db->ExecuteNoneQuery())
			{
				if($types != '')
				{
					$sql = "select ID, smalltypes from #@__arctype where ID in ($types)";
					$db->SetQuery($sql);
					$db->Execute();
					while($row = $db->GetArray()){
						$row['smalltypes'] = explode(',',$row['smalltypes']);
						foreach($row['smalltypes'] as $key => $value)
						{
							if($value == $id){
								unset($row['smalltypes'][$key]);
							}
						}
						$smalltypes = implode(',',$row['smalltypes']);

						$sql = "update #@__arctype set smalltypes='$smalltypes' where ID=$row[ID];";
						$db->SetQuery($sql);
						if($db->ExecuteNoneQuery())
						{
							ShowMsg('删除小分类成功，将返回小分类管理页面', 'smalltype.php');
							exit;
						}

					}
				}
				ShowMsg('删除小分类失败，将返回小分类管理页面 ','smalltype.php');
				exit;
			}else
			{
				ShowMsg('删除小分类失败，将返回小分类管理页面 ','smalltype.php');
				exit;
			}
		}
}else{
	$smalltypes = array();
	$sql = "select typename, ID, smalltypes from #@__arctype where smalltypes<>''";
	$db->SetQuery($sql);
	$db->Execute();
	$typesinfo = $types =array();
	while($row = $db->GetArray())
	{
		$row['smalltypes'] = explode(',',$row['smalltypes']);
		foreach($row['smalltypes'] as $smalltypeid)
		{
			$typesinfo[$smalltypeid][] = $row['typename'];
			$types[$smalltypeid][] = $row['ID'];
		}

	}

	$sql = "select * from #@__smalltypes order by disorder asc, id asc";
	$db->SetQuery($sql);
	$db->Execute();
	while($smalltype = $db->GetArray())
	{
		$smalltype['types'] = $smalltype['relatetype'] = '';
		if(is_array($typesinfo[$smalltype['id']]))
		{
			$smalltype['relatetype'] = implode(', ', $typesinfo[$smalltype['id']]);
			$smalltype['types'] = urlencode(implode(', ', $types[$smalltype['id']]));
		}
		$smalltypes[] = $smalltype;
	}
	include(dirname(__FILE__)."/templets/smalltype.htm");
}












?>