<?php 
require "../Run.php";
require "../include/getmkurl.fun.php";

echo head('高级一键自动化功能');
if($admin != 1 && $admin !=2 ){
//非法权限
echo '<div class="tip">非法操作：您好，您不具备此权限！</div>';
}else{
if($_GET['act'] == 'cmd'){
	/*命令执行函数*/
	
	
	function autolist(){
	//查询并获取子栏目记录
	$sql = "SELECT cid,cn,url,xuhao,q,siteid,gro,list FROM class WHERE siteid='".WAPID."' AND gro='".P_ID."' ORDER BY xuhao";
    $result = mysql_query($sql);
	//获取栏目数量
	$nums = mysql_num_rows($result);
	if($result && $nums > 0){ 
	//对栏目进行循环遍历
	while(list($cid,$cn,$url,$xuhao,$q,$siteid,$gro,$list) = mysql_fetch_row($result)){
	//是否为显示栏目
			$sql = "UPDATE class SET list='1' WHERE cid='".$cid."'";	
			mysql_query($sql);
			$q_content .= '[div='.$_POST['title_class'].'][class='.$cid.']'.$cn.'[/class][/div][ul='.$_POST['list_class'].']['.$_POST['list_type'].'='.$cid.'_12_1_0_4][/ul]'; 
	   }
	$content = $q_content.$_POST['list_bottom']; 
		if($_GET['cid'] == NULL || $_GET['cid'] == 0){
			$sql = "UPDATE config SET index_bottom='{$content}' WHERE user='".WAPID."'";
				}else{
			$sql = "UPDATE class SET bottom='{$content}' WHERE cid='{$_GET['cid']}' AND siteid='".WAPID."'";
				}	
				$result = mysql_query($sql);
				if($result && mysql_affected_rows()>0){
					return 1;
				}else{
					return 0;
				}	
			}
	}
	
	function class_list($C_ID){
		$sql = "SELECT cid,cn,url,xuhao,q,siteid,gro,list FROM class WHERE siteid='".WAPID."' AND gro='".$C_ID."'";
		$result = mysql_query($sql);
		$nums = mysql_num_rows($result);
		if($result && $nums > 0){ 
			return $nums;
		}else{
			return 0;
		}
	}
	function sitemap(){
		$mkid = get_mkid("bbs/list.php");
		//查询并获取子栏目记录
		$sql = "SELECT cid,cn,url,xuhao,q,siteid,gro,list FROM class WHERE siteid='".WAPID."' AND gro='".P_ID."' ORDER BY xuhao";
		$result = mysql_query($sql);
		if($result && mysql_num_rows($result) > 0){ 
		//对栏目进行循环遍历
		while(list($cid,$cn,$url,$xuhao,$q,$siteid,$gro,$list) = mysql_fetch_row($result)){
				//是否为显示栏目
				mysql_query($sql);
				if(class_list($cid) > 0){
				//如果拥有子栏目
					$class_list_yes[] = $cid;
					$class_list_name[$cid] = $cn;
						$sql = "SELECT cid,cn,url,xuhao,q,siteid,gro,list FROM class WHERE siteid='".WAPID."' AND gro='".$cid."' ORDER BY xuhao";
						$result2 = mysql_query($sql);
						//获取栏目数量
						while(list($classid2,$cn2,$url2,$xuhao2,$q2,$siteid2,$gro2,$list2) = mysql_fetch_row($result2)){
							$class_list[$cid][] = $classid2; 
							$class_listn[$cid][$classid2] = $cn2; 
						}
				}else{
					$class_list_no[] = '[class='.$cid.']'.$cn.'[/class]';
				}
			}
		}
				$no_nums = count($class_list_no);
				$no = 1;
				foreach($class_list_no as $valuem){
					if($no == $no_nums){
						$br = '';
					}else{
						$br = '<br>';
					}
					$one_class .= $valuem.$br; 
				$no++;
				}
				foreach($class_list_yes as $value){
					$two_class .= '[div=title][class='.$value.']'.$class_list_name[$value].'[/class][/div]';
					$nums2 = 0;
					foreach($class_list[$value] as $value2){
					$br2 = $nums2++%2 == 0?'.':'<br>';
					$two_class .= '[class='.$value2.']'.$class_listn[$value][$value2].'[/class]'.$br2;
					}
					$nums2 = 0;
				}
		
		$content = '[div=view][div=title]栏目导航[/div]'.$one_class.'[/div][div=view]'.$two_class.'[/div][div=mylink][back]返回上级[/back]|[index]网站首页[/index][/div]';
			if($_GET['cid'] == NULL || $_GET['cid'] == 0){
				$sql = "UPDATE config SET index_bottom='{$content}' WHERE user='".WAPID."'";
					}else{
						$sql = "UPDATE class SET bottom='{$content}' WHERE cid='{$_GET['cid']}' AND siteid='".WAPID."'";
					}	
				$result = mysql_query($sql);
				if($result && mysql_affected_rows()>0){
					return 1;
				}else{
					return 0;
				}	
			}
	

	
	/*函数结束*/
	define("P_CMD",$_POST['cmd']);
	if($_POST['cid'] == NULL){
		$_POST['cid'] = '0';
	}
	define("P_ID",$_POST['cid']);
	if(P_CMD == 'autolist'){
		if(autolist() == 1){
		echo '<div class="tip">自动排版命令已经执行</div>';
		}else{
		echo '<div class="tip">自动排版命令执行失败</div>';
		}
	}
	if(P_CMD == 'sitemap'){
		if(sitemap() == 1){
			echo '<div class="tip">栏目导航（论坛）排设完成！</div>';
		}else{
			echo '<div class="tip">栏目导航（论坛）排设失败！</div>';
		}
	}
}else{
echo '<div class="title">自动化排版功能</div>';
echo '<form method="post" action="?act=cmd&cid='.$_GET['cid'].'">';
echo '<div class="content">';
	echo '请输入命令<br>';
	echo '<input name="cmd" type="text" value="">';
	echo '<br>';
	echo '请输入栏目ID<br>';
	echo '<input name="cid" type="text" value="'.$_GET['cid'].'">';
	echo '<br><input type="submit" value="执行命令"></form>';
echo '</div>';
	}
}
echo '<div class="mylink"><a href="http://'.HTTP_HOST.'/indexadmin.php?cid='.$_GET['cid'].'">当前排版</a>|<a href="http://'.HTTP_HOST.'/indexadmin.php?cid=0">首页排版</a></div>';
echo footer(1);

?>