<?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($_POST['date'])){
	$all=archive($_POST['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",strtotime($max));
	$end=date("Y-m",strtotime($min));

  $main="
   <script type='text/javascript' src='".XOOPS_URL."/modules/tadnews/class/jquery.js'></script>
	<script type='text/javascript'>

	$(document).ready(function(){
    view_archive('{$now}');
  });
  
  function view_archive(chk_date){
		$.ajax({
			type: 'POST',
			url: '{$_SERVER['PHP_SELF']}',
			data: 'date=' + chk_date,
			success: function(msg){
				$('#msg_results').html(msg);
			}
		});
	}
	</script>
	<form>
	<table>
	<tr><td style='width:100px;' valign='top'>
	<select size=20 onClick='view_archive(this.value)'>
	<option value='{$now}'>".str_replace("-",""._MD_TADNEW_YEAR." ",$now)._MD_TADNEW_MONTH."</option>
	";

  $i=1;
	if($end!=$now){
		while($date!=$end){
	    $date=date("Y-m",strtotime("{$now}-01 -{$i} months"));
	    $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,b.not_news 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 a.start_day like '{$date}%' order by a.start_day desc";
	
	//return $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>".str_replace("-",""._MD_TADNEW_YEAR." ",$date)._MD_TADNEW_MONTH._MA_TADNEW_NEWS_TITLE."</th><th nowrap>"._MD_TADNEW_FILES."</th></tr>";

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

		if($not_news=="1")continue;

		//判斷本文及所屬分類是否允許該用戶之所屬群組觀看
		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 style='width:150px;'>$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';

?>
