<?php
include "../../mainfile.php";

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

//找出所有分類
$sql="select ncsn,nc_title,enable_group,enable_post_group,cate_pic from ".$xoopsDB->prefix("tad_news_cate")." where not_news!='1'";

$result=$xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],10, mysql_error()."<br>$sql");

$cates_enable_post_group[]="";

while(list($ncsn,$nc_title,$enable_group,$enable_post_group,$cate_pic)=$xoopsDB->fetchRow($result)){
  //只有可讀的分類才納入
	$cate_read_poewr=chk_cate_power($ncsn,$User_Groups,$enable_group,"read");
	if($cate_read_poewr){
		$ncsn_ok[]=$ncsn; //權限內可觀看群組
		$cates[$ncsn]=$nc_title;
		$cates_enable_post_group[$ncsn]=$enable_post_group;
		$cates_enable_group[$ncsn]=$enable_group;
		$cates_pic[$ncsn]=$cate_pic;
	}

}


$ok_cate=implode(",",$ncsn_ok);

//假如沒有指定觀看分類
$where_cate=(empty($ok_cate))?"and ncsn='0'":"and (ncsn in($ok_cate) or ncsn='0')";
$today=date("Y-m-d H:i:s");
$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 always_top desc , start_day desc";

			//PageBar(資料數, 每頁顯示幾筆資料, 最多顯示幾個頁數選項);
			$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],10, mysql_error()."<br>$sql");
			$total=$xoopsDB->getRowsNum($result);

			$navbar = new PageBar($total, 10, 10);
			$mybar = $navbar->makeBar();
			$bar= sprintf(_MD_TADNEW_TOOLBAR,$mybar['total'],$mybar['current'])."{$mybar['left']}{$mybar['center']}{$mybar['right']}";
			$sql.=$mybar['sql'];

$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());

$all_news="";
while($news=$xoopsDB->fetchArray($result)){

	foreach($news as $k=>$v){
		$$k=$v;
	}

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

	//新聞資訊列
	$end_day=($end_day=="0000-00-00 00:00:00")?"":"~ ".$end_day;


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

	$news_title=(empty($news_title))?_MD_TADNEWS_NO_TITLE:$news_title;
	$cate_name=(empty($cates[$ncsn]))?_MD_TADNEWS_NO_CATE:$cates[$ncsn];

	$post_date=substr($start_day,0,10);
	$today_pic=get_news_pic($always_top,$start_day);


	$g_txt=txt_to_group_name($enable_group,_MA_TADNEW_ALL_OK);


  $all_news.="<tr><td nowrap><a href='".XOOPS_URL."/modules/tadnews/index.php?ncsn={$ncsn}' target='_blank'>{$cate_name}</a></td><td width=70%>{$today_pic}{$prefix_tag} <a href='".XOOPS_URL."/modules/tadnews/index.php?nsn={$nsn}' target='_blank'>{$news_title}</a></td><td nowrap><a href='".XOOPS_URL."/userinfo.php?uid={$uid}' target='_blank'>$uid_name</a></td><td nowrap>$post_date</td><td nowrap>$counter</td></tr>";


}

$all_news="
<style>
/* 表格樣式 */
table#tbl{
	width: auto;
	padding: 8px;
	border-collapse: collapse;
}

/* 標題儲存格樣式 */
table#tbl th {
	border-bottom:1px solid rgb(192,192,192);
	background-color: rgb(102,153,255);
	text-align:center;
	vertical-align: middle;
	font-size:12px;
	text-decoration:none;
}

/* 儲存格樣式 */
table#tbl td {
	padding: 5px;
	border-bottom:1px solid rgb(192,192,192);
	background-color: rgb(255,255,255);
	vertical-align: middle;
	font-size:12px;
	text-decoration:none;
}


/* 儲存格樣式 */
table#tbl.dot td {
	padding: 6px;
	border-bottom:1px dotted rgb(192,192,192);
	background-color: rgb(255,255,255);
	vertical-align: middle;
}

/* 儲存格樣式 */
table#tbl td.noline {
	border-bottom:0px solid rgb(192,192,192);
}

table#tbl td.bar {
	text-align: center;
	color: rgb(0,0,153);
	background-color: rgb(192,224,127);
}

a{
	text-decoration:none;
}
</style>
<table id='tbl' style='width:100%;'><tr><th nowrap>"._MA_TADNEW_NEWS_CATE."</th><th nowrap>"._MA_TADNEW_NEWS_TITLE."</th><th nowrap>"._MD_TADNEW_POSTER."</th><th nowrap>"._MA_TADNEW_START_DATE."</th><th nowrap>"._MD_TADNEW_HOT."</th></tr>$all_news
<tr><td colspan=5 class='bar'>$bar</td></tr></table>";



header("Content-Type: text/html");

echo "<html><head><title>". htmlspecialchars($xoopsConfig['sitename'])."</title>
      <meta name='HandheldFriendly' content='True' />
      <meta name='PalmComputingPlatform' content='True' />
      </head>
      <body>
			$all_news
			</body></html>";

//判斷本文之所屬分類是否允許該用戶之所屬群組觀看或發佈
function chk_cate_power($ncsn="",$User_Groups="",$enable_group="",$kind="read"){
	global $xoopsDB,$xoopsUser;

	if(!empty($enable_group)){
		$ok=false;
		$cate_enable_group=explode(",",$enable_group);
		if(in_array("",$cate_enable_group)){
      return true;
		}

		foreach($User_Groups as $gid){
			if(in_array($gid,$cate_enable_group)){
				return true;
			}
		}
	}else{
		return true;
	}
	return false;
}

//判斷本文是否允許該用戶之所屬群組觀看或發佈
function chk_news_power($enable_group="",$User_Groups=""){
	global $xoopsDB,$xoopsUser;

	if(empty($enable_group))return true;

	$news_enable_group=explode(",",$enable_group);
	foreach($User_Groups as $gid){
		if(in_array($gid,$news_enable_group)){
			return true;
		}
	}

	return false;
}

	//置頂圖示
function get_news_pic($always_top="",$post_date=""){
	$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'>":get_today_pic($post_date);
	return $always_top_pic;
}

//今日文章
function get_today_pic($post_date=""){
	$today_pic=(date("Y-m-d")==$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'>":"";
	return $today_pic;
}

//把字串換成群組
function txt_to_group_name($enable_group="",$default_txt="",$syb="<br />"){
	$groups_array=get_all_groups();
	if(empty($enable_group)){
    $g_txt=$default_txt;
	}else{
	  $gs=explode(",",$enable_group);
	  $g_txt="";
	  foreach($gs as $gid){
    	$g_txt.=$groups_array[$gid]."{$syb}";
		}
	}
	return $g_txt;
}

//取得所有群組
function get_all_groups(){
	global $xoopsDB;
	$sql = "select groupid,name from ".$xoopsDB->prefix("groups")."";
	$result = $xoopsDB->query($sql);
	while(list($groupid,$name)=$xoopsDB->fetchRow($result)){
		$data[$groupid]=$name;
	}
	return $data;
}


//分頁物件
class PageBar{
	// 目前所在頁碼
	var $current;
	// 所有的資料數量 (rows)
	var $total;
	// 每頁顯示幾筆資料
	var $limit;
	// 目前在第幾層的頁數選項？
	var $pCurrent;
	// 總共分成幾頁？
	var $pTotal;
	// 每一層最多有幾個頁數選項可供選擇，如：3 = {[1][2][3]}
	var $pLimit;
	var $prev;
	var $next;
	var $prev_layer = ' ';
	var $next_layer = ' ';
	var $first;
	var $last;
	var $bottons = array();
	// 要使用的 URL 頁數參數名？
	var $url_page = "g2p";
	// 要使用的 URL 讀取時間參數名？
	var $url_loadtime = "loadtime";
	// 會使用到的 URL 變數名，給 process_query() 過濾用的。
	var $used_query = array();
	// 目前頁數顏色
	var $act_color = "#990000";
	var $query_str; // 存放 URL 參數列

	function PageBar($total, $limit, $page_limit){
		$mydirname = basename( dirname( __FILE__ ) ) ;
		$this->prev = "<img src='".XOOPS_URL."/modules/{$mydirname}/images/1leftarrow.gif' alt='"._MD_TADNEW_BACK_PAGE."' align='absmiddle' hspace=3>"._MD_TADNEW_BACK_PAGE;
		$this->next = "<img src='".XOOPS_URL."/modules/{$mydirname}/images/1rightarrow.gif' alt='"._MD_TADNEW_NEXT_PAGE."' align='absmiddle' hspace=3>"._MD_TADNEW_NEXT_PAGE;
		$this->first = "<img src='".XOOPS_URL."/modules/{$mydirname}/images/2leftarrow.gif' alt='"._MD_TADNEW_FIRST_PAGE."' align='absmiddle' hspace=3>"._MD_TADNEW_FIRST_PAGE;
		$this->last = "<img src='".XOOPS_URL."/modules/{$mydirname}/images/2rightarrow.gif' alt='"._MD_TADNEW_LAST_PAGE."' align='absmiddle' hspace=3>"._MD_TADNEW_LAST_PAGE;

		$this->limit = $limit;
		$this->total = $total;
		$this->pLimit = $page_limit;
	}

	function init(){
		$this->used_query = array($this->url_page, $this->url_loadtime);
		$this->query_str = $this->processQuery($this->used_query);
		$this->glue = ($this->query_str == "")?'?':
		'&';
		$this->current = (isset($_GET["$this->url_page"]))? $_GET["$this->url_page"]:
		1;
		$this->pTotal = ceil($this->total / $this->limit);
		$this->pCurrent = ceil($this->current / $this->pLimit);
	}

	//初始設定
	function set($active_color = "none", $buttons = "none"){
		if ($active_color != "none"){
			$this->act_color = $active_color;
		}

		if ($buttons != "none"){
			$this->buttons = $buttons;
			$this->prev = $this->buttons['prev'];
			$this->next = $this->buttons['next'];
			$this->prev_layer = $this->buttons['prev_layer'];
			$this->next_layer = $this->buttons['next_layer'];
			$this->first = $this->buttons['first'];
			$this->last = $this->buttons['last'];
		}
	}

	// 處理 URL 的參數，過濾會使用到的變數名稱
	function processQuery($used_query){
		// 將 URL 字串分離成二維陣列
		$vars = explode("&", $_SERVER['QUERY_STRING']);
		for($i = 0; $i < count($vars); $i++){
			$var[$i] = explode("=", $vars[$i]);
		}

		// 過濾要使用的 URL 變數名稱
		for($i = 0; $i < count($var); $i++){
			for($j = 0; $j < count($used_query); $j++){
				if (isset($var[$i][0]) && $var[$i][0] == $used_query[$j]) $var[$i] = array();
			}
		}

		// 合併變數名與變數值
		for($i = 0; $i < count($var); $i++){
			$vars[$i] = implode("=", $var[$i]);
		}

		// 合併為一完整的 URL 字串
		$processed_query = "";
		for($i = 0; $i < count($vars); $i++){
			$glue = ($processed_query == "")?'?':
			'&';
			// 開頭第一個是 '?' 其餘的才是 '&'
			if ($vars[$i] != "") $processed_query .= $glue.$vars[$i];
		}
		return $processed_query;
	}

	// 製作 sql 的 query 字串 (LIMIT)
	function sqlQuery(){
		$row_start = ($this->current * $this->limit) - $this->limit;
		$sql_query = " LIMIT {$row_start}, {$this->limit}";
		return $sql_query;
	}


	// 製作 bar
	function makeBar($url_page = "none"){
		if ($url_page != "none"){
			$this->url_page = $url_page;
		}
		$this->init();

		// 取得目前時間
		$loadtime = '&loadtime='.time();

		// 取得目前頁框(層)的第一個頁數啟始值，如 6 7 8 9 10 = 6
		$i = ($this->pCurrent * $this->pLimit) - ($this->pLimit - 1);

		$bar_center = "";
		while ($i <= $this->pTotal && $i <= ($this->pCurrent * $this->pLimit)){
			if ($i == $this->current){
				$bar_center = "{$bar_center}<font color='{$this->act_color}'>[{$i}]</font>";
			}else{
				$bar_center .= " <a href='{$_SERVER['PHP_SELF']}{$this->query_str}{$this->glue}{$this->url_page}={$i}{$loadtime}'' title='{$i}'>{$i}</a> ";
			}
			$i++;
		}
		$bar_center = $bar_center . "";

		// 往前跳一頁
		if ($this->current <= 1){
			$bar_left = " {$this->prev} ";
			$bar_first = " {$this->first} ";
		}	else{
			$i = $this->current-1;
			$bar_left = " <a href='{$_SERVER['PHP_SELF']}{$this->query_str}{$this->glue}{$this->url_page}={$i}{$loadtime}' title='"._MD_TADNEW_BACK_PAGE."'>{$this->prev}</a> ";
			$bar_first = " <a href='{$_SERVER['PHP_SELF']}{$this->query_str}{$this->glue}{$this->url_page}=1{$loadtime}' title='"._MD_TADNEW_FIRST_PAGE."'>{$this->first}</a> ";
		}

		// 往後跳一頁
		if ($this->current >= $this->pTotal){
			$bar_right = " {$this->next} ";
			$bar_last = " {$this->last} ";
		}	else{
			$i = $this->current + 1;
			$bar_right = " <a href='{$_SERVER['PHP_SELF']}{$this->query_str}{$this->glue}{$this->url_page}={$i}{$loadtime}' title='"._MD_TADNEW_NEXT_PAGE."'>{$this->next}</a> ";
			$bar_last = " <a href='{$_SERVER['PHP_SELF']}{$this->query_str}{$this->glue}{$this->url_page}={$this->pTotal}{$loadtime}' title='"._MD_TADNEW_LAST_PAGE."'>{$this->last}</a> ";
		}

		// 往前跳一整個頁框(層)
		if (($this->current - $this->pLimit) < 1){
			$bar_l = " {$this->prev_layer} ";
		}	else{
			$i = $this->current - $this->pLimit;
			$bar_l = " <a href='{$_SERVER['PHP_SELF']}{$this->query_str}{$this->glue}{$this->url_page}={$i}{$loadtime}' title='".sprintf($this->pLimit,_MD_TADNEW_GO_BACK_PAGE)."'>{$this->prev_layer}</a> ";
		}

		//往後跳一整個頁框(層)
		if (($this->current + $this->pLimit) > $this->pTotal){
			$bar_r = " {$this->next_layer} ";
		}	else{
			$i = $this->current + $this->pLimit;
			$bar_r = " <a href='{$_SERVER['PHP_SELF']}{$this->query_str}{$this->glue}{$this->url_page}={$i}{$loadtime}' title='".sprintf($this->pLimit,_MD_TADNEW_GO_NEXT_PAGE)."'>{$this->next_layer}</a> ";
		}

		$page_bar['center'] = $bar_center;
		$page_bar['left'] = $bar_first . $bar_l . $bar_left;
		$page_bar['right'] = $bar_right . $bar_r . $bar_last;
		$page_bar['current'] = $this->current;
		$page_bar['total'] = $this->pTotal;
		$page_bar['sql'] = $this->sqlQuery();
		return $page_bar;
	}

}
?>
