<?php
include_once "../../mainfile.php";
include_once "function.php";

$now=date("Y-m-d",xoops_getUserTimestamp(time()));
$today=date("Y-m-d H:i:s",xoops_getUserTimestamp(time()));

include_once(XOOPS_ROOT_PATH."/modules/tadnews/language/{$xoopsConfig[language]}/blocks.php");


$options[1]="table";

$num=(!empty($_POST['num']))?$_POST['num']:10;

$p=(empty($_POST['p']))?0:$_POST['p'];
$b=$_POST['p']-1;
$n=$p+1;

$start=$p*$num;

if($start <= 0 )$start=0;

//取得目前使用者的所屬群組
if($xoopsUser){
	$User_Groups=$xoopsUser->getGroups();
}else{
	$User_Groups=array();
}

$sql="select ncsn,nc_title from ".$xoopsDB->prefix("tad_news_cate")." where not_news!='1'";
$result=$xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
while(list($ncsn,$nc_title)=$xoopsDB->fetchRow($result)){
	$ncsn_ok[]=$ncsn;
	$cates[$ncsn]=$nc_title;
}



$top_order=($options[1]!="all")?" `always_top` desc ,":"";
$ok_cate=implode(",",$ncsn_ok);
$where_cate=(empty($ok_cate))?"and ncsn='0'":"and (ncsn in($ok_cate) or ncsn='0')";

$sql = "select * from ".$xoopsDB->prefix("tad_news")." where enable='1' $where_cate and start_day < '{$today}' and (end_day > '{$today}' or end_day='0000-00-00 00:00:00') order by $top_order start_day desc limit {$start} , {$num}";



$result = $xoopsDB->query($sql) or redirect_header(XOOPS_URL,3,mysql_error());
$block="<link rel='stylesheet' type='text/css' media='screen' href='".XOOPS_URL."/modules/tadnews/module.css' />";


$block.="<table class=\"outer\"><tr><th>"._MB_TADNEW_START_DATE."</th><th>"._MB_TADNEW_NEWS_TITLE."</th><th>"._MB_TADNEW_POSTER."</th><th>"._MB_TADNEW_NEWS_CATE."</th><th>"._MB_TADNEW_COUNTER."</th></tr>";


$i=2;

$total=0;
while($all_news=$xoopsDB->fetchArray($result)){
	  foreach($all_news as $k=>$v){
			$$k=$v;
		}
	//判斷本文之所屬分類是否允許該用戶之所屬群組觀看
	if(!block_chk_cate_power($ncsn,$User_Groups)){
		continue;
	}

	//判斷本文是否允許該用戶之所屬群組觀看
	if(!empty($enable_group)){
		$ok=false;
		$news_enable_group=explode(",",$enable_group);
		foreach($User_Groups as $gid){
			if(in_array($gid,$news_enable_group)){
				$ok=true;
			}
		}
		if(!$ok)continue;
	}

	$uid_name=XoopsUser::getUnameFromId($uid,0);

	$post_date=substr($start_day,0,10);
	$today_pic=($now==$post_date)?"<img src='".XOOPS_URL."/modules/tadnews/images/today.gif' alt='"._MB_TADNEW_TODAY_NEWS."' title='"._MB_TADNEW_TODAY_NEWS."' hspace=3 align='absmiddle'>":"";

	$always_top_pic=($always_top=='1')?"<img src='".XOOPS_URL."/modules/tadnews/images/top.gif' alt='"._MB_TADNEW_ALWAYS_TOP."' title='"._MB_TADNEW_ALWAYS_TOP."' hspace=3 align='absmiddle'>":$today_pic;



	$style=($i%2)?"odd":"even";
	$block.="<tr class=\"{$style}\"><td>$post_date</td><td>{$always_top_pic}{$prefix_tag} <a href=\"".XOOPS_URL."/modules/tadnews/index.php?nsn={$nsn}\">$news_title</a></td><td><a href=\"".XOOPS_URL."/userinfo.php?uid={$uid}\">$uid_name</a></td><td><a href=\"".XOOPS_URL."/modules/tadnews/index.php?ncsn={$ncsn}\">{$cates[$ncsn]}</a></td><td>$counter</td></tr>";
	$i++;
  $total++;
}


$b_button=($b < 0)?"":"<input type='button'  onClick='view_content({$b})' value='".sprintf(_TADNEWS_BLOCK_BACK,$num)."'>";

$n_button=($total < $num)?"":"<input type='button' style='float:right;' onClick='view_content({$n})' value='".sprintf(_TADNEWS_BLOCK_NEXT,$num)."'>";

$block.="</table>
$n_button
$b_button
</div>";


echo to_utf8($block);

//取得所有類別標題
function block_get_all_cate(){
	global $xoopsDB;
	$sql = "select ncsn,nc_title from ".$xoopsDB->prefix("tad_news_cate")." order by sort";
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());

	while(list($ncsn,$nc_title)=$xoopsDB->fetchRow($result)){
		$data[$ncsn]=$nc_title;
	}
	return $data;
}



//判斷本文之所屬分類是否允許該用戶之所屬群組觀看
function block_chk_cate_power($ncsn="",$User_Groups=array()){
	$cate=block_get_tad_news_cate($ncsn);
	$enable_group=$cate['enable_group'];
	if(!empty($enable_group)){
		$ok=false;
		$cate_enable_group=explode(",",$enable_group);
		foreach($User_Groups as $gid){
			if(in_array($gid,$cate_enable_group)){
				return true;
			}
		}
	}else{
		return true;
	}
	return false;
}


//以流水號取得某筆tad_news_cate資料
function block_get_tad_news_cate($ncsn=""){
	global $xoopsDB;
	if(empty($ncsn))return;
	$sql = "select * from ".$xoopsDB->prefix("tad_news_cate")." where ncsn='$ncsn'";
	$result = $xoopsDB->query($sql) or redirect_header(XOOPS_URL,3,mysql_error());
	$data=$xoopsDB->fetchArray($result);
	return $data;
}


?>
