<?php
	!defined('DIRCMS_MANAGE') && exit('Access Denied!');
	$r=cache_read('module.cache.php',DIRCMS_ROOT.'data/cache_module/');
	$r=$r['system']['roleid']; 
	if(!in_array($_roleid,$r) && $_userid!=ADMIN_FOUNDERS)showmsg_nourl('您没有该操作的权限!');
	$action=empty($action)?'manage':trim($action);
	$c=new cache();
	switch($action)
	{
		case 'manage':
			if(isset($do_submit)) 
			{
				if(!$id)showmsg('请选择需要操作的地区!');
				foreach($id as $val)
				{
					switch($do)
					{
						case 'delete':
							$db->mysql_delete(DB_PRE.'area',intval($val));
							cache_delete('area'.$val.'.cache.php',DIRCMS_ROOT.'data/cache/');
							break;
						default:
							$db->update(DB_PRE.'area',array('area'=>$area[$val],'orderby'=>$orderby[$val]),'id='.intval($val));
							break;
					}
				}
				$c->cache_area();
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=area&action=manage');
			}
			function tree ($parentid=0)
			{
				global $db;
				static $flag=0;
				$r=$db->fetch_all("SELECT * FROM ".DB_PRE."area WHERE parentid=".intval($parentid));
				if($r)foreach($r as $_r)
				{
					echo '<tr>
			<td width="40" align="center"><input type="checkbox" name="id[]" value="'.$_r['id'].'" class="checkbox" /></td>
			<td width="80" align="center"><input type="text" size="6" name="orderby['.$_r['id'].']" value="'.$_r['orderby'].'" /></td>
			<td width="60" align="center">'.$_r['id'].'</td>
			<td width="240" align="center">'.str_repeat('&nbsp;',$_r['grade']*5).'└<input type="text" size="25" name="area['.$_r['id'].']" value="'.$_r['area'].'" /></td>
			<td width="100" align="center">'.$_r['grade'].'级地区 <a href="javascript:void(0);" onclick="javascript:poptemplate(\'area\',\'parentid\','.$_r['id'].',\''.$_r['parentid'].'\',\'area\');" title="设置上级地区"><u>设置</u></a></td>
			<td width="40" align="center"><a href="'.ADMIN_FILE.'?file=area&action=delete&id='.$_r['id'].'"><u>删除</u></a></td></tr>';
					tree($_r['id']);
				}
			}
			$area=array();
			$r=cache_read('all_area.cache.php',DIRCMS_ROOT.'data/cache/');
			if($r)foreach($r as $_r)
			{
				if($_r['parentid']==0)$area[]=$_r;
			}
			include admin_tlp('area_manage');			
			break;
		case 'add':
			if(isset($do_submit)) 
			{
				if(!$newarea['area'])showmsg('地区名称不能为空!');
				$area=explode("\r\n",$newarea['area']);
				$r=$db->fetch_one("SELECT grade FROM ".DB_PRE."area WHERE ".DB_PRE."area.id=".intval($newarea['parentid']));
				$newarea['grade']=$r['grade']+1;
				foreach($area as $val)
				{
					$newarea['area']=$val;
					$db->insert(DB_PRE."area",$newarea);
				}
				$c->cache_area();
				showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=area&action=manage');
			}
			require_once(DIRCMS_ROOT.'include/select.class.php');
			$s=new select();
			$select=$s->getSelect('newarea[parentid]','无（作为一级地区）',DB_PRE.'area',true,'area');
			include admin_tlp('area_add');
			break;
		case 'delete':
			$db->mysql_delete(DB_PRE.'area',intval($id));
			$db->mysql_delete(DB_PRE.'area',intval($id),'parentid');
			$c->cache_area();
			cache_delete('area'.$id.'.cache.php',DIRCMS_ROOT.'data/cache/');
			showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=area&action=manage');
			break;
		case 'setparentid':
			$r=$db->fetch_one("SELECT grade FROM ".DB_PRE."area WHERE ".DB_PRE."area.id=".intval($parentid));
			$grade=$r['grade']+1;
			$db->update(DB_PRE.'area',array('parentid'=>intval($parentid),'grade'=>$grade),'id='.intval($id));
			$c->cache_area();
			showmsg($LANG['MANAGE_SUCEESS'],ADMIN_FILE.'?file=area&action=manage');
	}
?>