<?php
//  ------------------------------------------------------------------------ //
// 本模組由 tad 製作
// 製作日期：2007-11-04
// $Id: archive.php,v 1.1 2008/04/10 05:31:02 tad Exp $
// ------------------------------------------------------------------------- //

/*-----------引入檔案區--------------*/
include_once "header.php";
include_once XOOPS_ROOT_PATH."/modules/tadnews/up_file.php";

if(!empty($_GET['date'])){
	$all=archive($_GET['date']);
  echo to_utf8($all);
  exit;
}


$xoopsOption['template_main'] = "archive_tpl.html";
/*-----------function區--------------*/

//列出月份
function month_list(){
	global $xoopsDB;
	$sql = "select min(start_day),max(start_day) from ".$xoopsDB->prefix("tad_news")." where enable='1'";
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	list($min,$max)=$xoopsDB->fetchRow($result);

	$now=date("Y-m",tnsday2ts($max));
	$end=date("Y-m",tnsday2ts($min));


  $dd=explode(" ",$max);
  $d=explode("-",$dd[0]);
  $t=explode(":",$dd[1]);

  $i=1;
  $main="
	<script type='text/javascript'>
	var XmlHttp;

	function startRequest(vv){
	  if(window.ActiveXObject){
	   XmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
	  }else if(window.XMLHttpRequest){
			XmlHttp = new XMLHttpRequest();
		}


		XmlHttp.onreadystatechange = handleStateChange;
		XmlHttp.open('GET','{$_SERVER['PHP_SELF']}?date=' + encodeURI(vv),true);
		XmlHttp.send(null);
	}

	function handleStateChange(){
		if(XmlHttp.readyState==4){
			if(XmlHttp.status == 200){
			document.getElementById('msg_results').innerHTML=XmlHttp.responseText;
			}
		}
	}
	</script>
	<form name='aa'>
	<table>
	<tr><td style='width:100px;' valign='top'>
	<select size=20 onClick='startRequest(this.value)'>
	<option value='{$now}'>".str_replace("-",""._MD_TADNEW_YEAR." ",$now)._MD_TADNEW_MONTH."</option>
	";
	if($end!=$now){
		while($date!=$end){
	    $date=date("Y-m",mktime($t[0],$t[1],$t[2],$d['1']-$i,$d['2'],$d['0']));
	    $main.="<option value='{$date}'>".str_replace("-",""._MD_TADNEW_YEAR." ",$date)._MD_TADNEW_MONTH."</option>";
	    $i++;
		}
	}
	$main.="</select>
	</td>
	<td valign='top'><div id='msg_results'></div></td></tr>
	</table>
	</form>";

	$main=div_3d(_MD_TADNEW_ARCHIVE,$main,"corners","width:100%");

	return $main;
}


//分月新聞
function archive($date=""){
	global $xoopsDB,$xoopsModule,$xoopsUser,$xoopsOption;


	$sql = "select a.nsn,a.ncsn,a.news_title,a.start_day,a.enable_group,a.passwd from ".$xoopsDB->prefix("tad_news")." as a left join ".$xoopsDB->prefix("tad_news_cate")." as b on a.ncsn=b.ncsn where a.enable='1'  and b.not_news!='1' and a.start_day like '{$date}%' order by a.start_day desc";
	
	//die($sql);
	
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());

	$all_news="<table id='tbl' style='width:100%'>
	<tr><th>"._MA_TADNEW_START_DATE."</th><th>"._MA_TADNEW_NEWS_TITLE."</th><th nowrap>"._MD_TADNEW_FILES."</th></tr>";

	while(list($nsn,$ncsn,$news_title,$start_day,$enable_group,$passwd)=$xoopsDB->fetchRow($result)){


		//判斷本文及所屬分類是否允許該用戶之所屬群組觀看
		if(!read_power_chk($ncsn,$enable_group)){
			continue;
		}

		if(empty($news_title)){
			continue;
		}
		


		//$files=(empty($passwd))?get_news_files($nsn,2):"";

		$files=(empty($passwd))?show_files("nsn",$nsn,true,"filename"):"";

		$all_news.="<tr><td>$start_day</td><td><a href='index.php?nsn={$nsn}'>{$news_title}</td><td>$files</td></tr>";
	}

	$all_news.="</table>";

  //$data=div_3d(_MD_TADNEW_ARCHIVE,$all_news,"corners");

	return $all_news;
}

/*-----------執行動作判斷區----------*/
$op = (!isset($_REQUEST['op']))? "main":$_REQUEST['op'];

switch($op){


	default:
	$main=month_list();
	break;
}


/*-----------秀出結果區--------------*/
include XOOPS_ROOT_PATH."/header.php";
$xoopsTpl->assign( "css" , "<link rel='stylesheet' type='text/css' media='screen' href='module.css' />") ;
$xoopsTpl->assign( "toolbar" , toolbar($interface_menu)) ;
$xoopsTpl->assign( "content" , $main) ;
include_once XOOPS_ROOT_PATH.'/footer.php';

?>
