<?php
class makehtml_channelAction extends security_login {
	
	function action(){
		ModuleLimit(__CLASS__);
		set_time_limit(0);
		$miceSession = new session();
		$miceSession->set('makehtml','miceCMS');

		global $conn;
		$category=new model_Category();
		$category->setSubsection('WHERE type LIKE \'article%\' AND isvisible=0 ORDER BY path ASC');
		$results=$category->doSelect();
		$allid='';

		echo '<html xmlns="http://www.w3.org/1999/xhtml"><head>';
		echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
		echo '<base href="'.MICE_URL.MICE_TPL.'/admin/" />';
		echo '<link href="css/Default.css" rel="stylesheet" type="text/css" />';
		echo '<link href="css/fieldset.css" rel="stylesheet" type="text/css">';
		echo '<script language="JavaScript">var baseurl = \''.MICE_URL.MICE_TPL.'/admin/\';</script>';
		echo '<script type="text/javascript" src="js/Main.js"></script>';
		echo '</head><body>';
		echo '<table width="100%" border="0" cellspacing="6" cellpadding="0" style="border-collapse: separate; border-spacing: 6px;"><tr valign="top"><td><table width="100%" border="0" cellspacing="0" cellpadding="0" class="blockTable"><tr><td width="100%" style="padding:6px 10px;" class="blockTd">&nbsp;</td></tr><tr><td style="padding-top:0px;padding-left:6px;padding-right:6px;padding-bottom:8px;">';
		echo '<table width="99%" border="0" align="center" cellpadding="0" cellspacing="0">  <tr>		<td width="860" align="center" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#D5D5D5">      <tr>        <td height="28" bgcolor="#F9F9F9">&nbsp;<span class="STYLE7">更新分类封面HTML文件</span></td>      </tr>      <tr>        <td height="28" bgcolor="#FFFFFF"><table width="100%" border="0" cellspacing="0" cellpadding="0"><form id="form1" name="form1" method="post" action="">';
		
		echo '<tr><td height="28" class="bottomline" width="300">&nbsp;选择分类</td><td height="28" class="bottomline">';
		echo "<select name='path' id='path'><option value='all'>所有分类</option>";
		foreach($results as $crs){
			$checked='';
			if($_POST['path']==$crs->getCategoryId()){
				$checked='selected';
			}
			$allid=$allid.','.$crs->getCategoryId();
			echo "<option value='".$crs->getCategoryId()."' ".$checked.">".StrRepeat("─",$crs->getDepth()-1).$crs->getTitle()."</option>";
		}
		echo "</select>";
		echo '</td></tr>';
		
		$checked='';
		$checked1='';
		if($_POST['parent']==1){
			$checked='checked';
		}elseif($_POST['parent']==2){
			$checked1='checked';
		}
		echo '<tr><td height="28" class="bottomline" width="300">&nbsp;是否更新上级分类</td><td height="28" class="bottomline">';
		echo "<input name='parent' id='parent' type='radio' value='0' checked/>只更新此分类&nbsp;&nbsp;<input name='parent' id='parent' type='radio' value='1' ".$checked."/>更新此分类及此分类的所有上级分类&nbsp;&nbsp;<input name='parent' id='parent' type='radio' value='2' ".$checked1."/>更新此分类及此分类的所有下级分类";
		echo '</td></tr>';

		$checked2='';
		if($_POST['ishtml']){
			$checked2='checked';
		}

		echo '<tr><td height="28" class="bottomline" width="300">&nbsp;只更新未生成HTML的分类、<br>&nbsp;分类属性或分类文档做过改变的分类</td><td height="28" class="bottomline">';
		echo "<input name='ishtml' id='ishtml' type='radio' value='0' checked/>否&nbsp;&nbsp;<input name='ishtml' id='ishtml' type='radio' value='1' ".$checked2."/>是";
		echo '</td></tr>';

		echo '<tr><td height="28" class="bottomline" width="300">&nbsp;</td><td height="28" class="bottomline">';
		echo '</td></tr>';

		echo '<tr><td height="28" class="bottomline" width="300">&nbsp;</td><td height="28" class="bottomline">';
		echo '<input type="submit" name="Submit" value="开始生成HTML" /><br>(如果一次生成的分类过多,系统可能会较长时间没有反应,请耐心等待,不要做其它操作)';
		echo '</td></tr></form>';


		$miceSession = new session();
		if($_POST['path']){
			if($_POST['path']=='all'){
				$Cids=explode(',',$allid);
			}else{
				if($_POST['parent']==1){
					$category0=new model_Category();
					$category0->setSubsection('WHERE type LIKE \'article%\' AND isvisible=0 AND category_id='.$_POST['path']);
					$CategoryRS0=$category0->doSelectOne();
					$Cids=explode(',',$CategoryRS0->getPath());
				}elseif($_POST['parent']==2){
					$category0=new model_Category();
					$category0->setSubsection('WHERE type LIKE \'article%\' AND isvisible=0 AND path like \'%,'.$_POST['path'].',%\'');
					$CategoryRS0=$category0->doSelect();
					
					$searchpath=','.$_POST['path'].',';
					$paths=$searchpath;
					foreach($CategoryRS0 as $category){
						$pathlen=strpos($category->getPath(),$searchpath);
						$paths.=substr($category->getPath(),$pathlen+strlen($searchpath));
					}
					$Cids=explode(',',$paths);
				}else{
					$Cids[0]=$_POST['path'];
				}
			}
			
			$categoryname='';
			$ishtmlsql='';
			if($_POST['ishtml']){
				$ishtmlsql=' AND ishtml=0';
			}
			foreach($Cids as $CategoryId){
				if(is_numeric($CategoryId)){
					$category=new model_Category();
					$category->setSubsection('WHERE type LIKE \'article%\' AND isvisible=0'.$ishtmlsql.' AND category_id='.$CategoryId.' ORDER BY path');
					$CategoryRS=$category->doSelectOne();

					if($CategoryRS){
						$categoryname.=$CategoryRS->getTitle();
						$content='';
						if($CategoryRS->getUrl()){
							//跳转
							$content='<html><body><form action="'.MiceRestoreQuote('micefun',$CategoryRS->getUrl()).'"  method="post" id="link" name="link"  target="_blank"  style="display:none;"></form><Script LANGUAGE="JavaScript">document.getElementById("link").submit();history.back(-1);</script></body></html>';
						}

					$miceSession = new session();
					$limit='';
					if($miceSession->get('mu__grouplevel')<$CategoryRS->getUsergroupId() AND $CategoryRS->getUsergroupId()>0){
						$limit='<body><iframe src="'.MICE_URL.'index.php/limit/0/'.$CategoryRS->getUsergroupId().'/0"  style="width:0px; height:0px;" frameborder="0"></iframe>';
					}

					$smarty = new Smarty();
					$smarty->load_filter('pre','micePreCompileIndex');
					$smarty->assign('CategoryRS',$CategoryRS);
					$smarty->assign('CategoryId',$CategoryId);
					
					//分类目录
					$lpath=$CategoryId;
					if(strlen($CategoryRS->getFilename())){
						$lpath=$CategoryRS->getFilename();
					}

					$categoryname.='&nbsp;&nbsp;&nbsp;<a href="'.MICE_URL.'channel/'.$lpath.MICE_HTML_EXT.'" target="_blank">浏览此分类封面……</a><br>';

					//将内容写入至一个静态文件
					if($content==''){
						$content = $smarty->fetch('index/channel.php'); 
						if($limit!=''){
							$content = str_replace("<body>", $limit, $content);
						}
					}
					$fp = fopen(MICE_DIR.'channel/'.$lpath.MICE_HTML_EXT, 'w'); 
					fwrite($fp, $content); 
					fclose($fp);

					//对已生成html的栏目做标记
					if(!$CategoryRS->getIshtml()){
						$category->setIshtml(1);
						$category->doUpdate();
					}
				}
			  }
			}
		}
		if(!$categoryname){
			$categoryname='本次操作没有更新任何分类';
		}
		echo '<tr><td height="28" class="bottomline" width="300">&nbsp;生成结果</td><td height="28" class="bottomline"><br>';
		echo $categoryname;
		echo '<br></td></tr></form>';
		echo '</table></td></tr></table></td></tr></table><br></td></tr></table></td></tr></form></table></body></html>';
		$miceSession->set('makehtml','');
	}
}
?>