<?php
//  ------------------------------------------------------------------------ //
// 本模組由 tad 製作
// 製作日期：2007-11-04
// $Id: tadnews_content_block.php,v 1.4 2008/06/25 06:36:39 tad Exp $
// ------------------------------------------------------------------------- //

//區塊主函式 (顯示新聞內容)
function tadnews_content_block_show($options){
	global $xoopsDB,$xoopsModule,$xoopsUser,$xoopsOption;
	
	chk_always_top();
	
	$num=$options[0];
	$p=0;
  $start=$p*$num;
  
  $now=date("Y-m-d",xoops_getUserTimestamp(time()));
  $today=date("Y-m-d H:i:s",xoops_getUserTimestamp(time()));
  
	//取得目前使用者的所屬群組
	if($xoopsUser){
		$User_Groups=$xoopsUser->getGroups();
	}else{
		$User_Groups=array();
	}
	$del_js=b_del_tadnews_js();
	
	$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)){
		//判斷本文之所屬分類是否允許該用戶之所屬群組觀看
		if(!block_chk_cate_power($ncsn,$User_Groups)){
			continue;
		}
		$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' />";
	
	if($options[1]=="list"){
		$block.="<ul>";
	}elseif($options[1]=="all"){
    $block.=$del_js;
	}else{
		$block.="
		<script type='text/javascript' src='".XOOPS_URL."/modules/tadnews/class/jquery.js'></script>
		<script type='text/javascript'>

	  function view_content(p){
			$.ajax({
				type: 'POST',
				url: '".XOOPS_URL."/modules/tadnews/ajax.php',
				data: 'num={$num}&p=' + p,
				success: function(msg){
					$('#msg_results').html(msg);
				}
			});
		}
		</script>


		<div id='msg_results'>
		<table style='width:100%;' class=\"outer\"><tr><th>"._MB_TADNEW_START_DATE."</th><th colspan=2>"._MB_TADNEW_NEWS_TITLE."</th><th>"._MB_TADNEW_POSTER."</th><th>"._MB_TADNEW_NEWS_CATE."</th><th>"._MB_TADNEW_COUNTER."</th></tr>";
	}
	
	$i=2;
	while($all_news=$xoopsDB->fetchArray($result)){
	  foreach($all_news as $k=>$v){
			$$k=$v;
		}
		if(!empty($ncsn) and !in_array($ncsn,$ncsn_ok))continue;
		//判斷本文之所屬分類是否允許該用戶之所屬群組觀看
		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;

		if($options[1]=="list"){
			$block.="<li style='margin-bottom:10px;padding-bottom:3px;border-bottom:1px dotted gray;'>{$always_top_pic}{$prefix_tag} <a href=\"".XOOPS_URL."/modules/tadnews/index.php?nsn={$nsn}\">$news_title</a></li>\n";
		}elseif($options[1]=="all"){
			$fun=block_admin_tool($uid,$nsn,$counter);
			$end_day=($end_day=="0000-00-00 00:00:00")?"":"~ ".$end_day;
			
			if(!empty($passwd) and !in_array($nsn,$_SESSION['have_pass'])){
				$news_content="<form action='".XOOPS_URL."/modules/tadnews/index.php' method='post'><img src='".XOOPS_URL."/modules/tadnews/images/lock.png' align='absmiddle' hspace=4 alt='lock.png'>"._MB_TADNEW_NEWS_NEED_PASSWD."
				<input type='hidden' name='nsn' value='{$nsn}'>
				<input type='password' name='tadnews_passwd'>
				<input type='submit'>
				</form>";
			}else{
				//$files=b_get_news_files($nsn,0);
				//$news_content.=$files;

        if(eregi("--summary--",$news_content)){
				  $news_content=str_replace("<p>--summary--</p>","--summary--",$news_content);
		      $content=explode("--summary--",$news_content);
				}else{
					$content=explode("<div style=\"page-break-after: always;\"><span style=\"display: none;\">&nbsp;</span></div>",$news_content);
				}
				
				
				
				$more=(empty($content[1]))?"":"<p><a href='".XOOPS_URL."/modules/tadnews/index.php?nsn={$nsn}'>"._MB_TADNEW_MORE."...</a></p>";
				$news_content=$content[0].$more;
			}
			
			$block.="
			<div class=\"tadnews_inset\" style=\"clear: both;\">
			<b class=\"b1\"></b><b class=\"b2\"></b><b class=\"b3\"></b><b class=\"b4\"></b>
			<div class=\"boxcontent\">
				<h1>{$prefix_tag} <a href='".XOOPS_URL."/modules/tadnews/index.php?nsn={$nsn}'>{$news_title}</a></h1>
				<div id='news_toolbar'>$fun</div>
				<div id='news_info'><a href='".XOOPS_URL."/userinfo.php?uid={$uid}'>$uid_name</a> - <a href='".XOOPS_URL."/modules/tadnews/index.php?ncsn={$ncsn}'>{$cates[$ncsn]}</a> | $start_day $end_day
				</div>
				<div id='news_content'>$news_content</div>
			</div>
			<b class=\"b4b\"></b><b class=\"b3b\"></b><b class=\"b2b\"></b><b class=\"b1b\"></b>
			</div>
			";
		}else{
			$style=($i%2)?"odd":"even";
			$block.="<tr class=\"{$style}\"><td>$post_date</td><td>{$always_top_pic}</td><td>{$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++;		
		}
	}
	
	if($options[1]=="list"){
		$block.="</ul>";
	}elseif($options[1]=="all"){
		$block.="";
	}else{
		$block.="</table>
		<input type='button' style='float:right;' onClick='view_content(1)' value='".sprintf(_TADNEWS_BLOCK_NEXT,$num)."'></div>";
	}

	return $block;
}

//區塊編輯函式
function tadnews_content_block_edit($options){
	$chked1_0=($options[1]=="table")?"checked":"";
	$chked1_1=($options[1]=="list")?"checked":"";
	$chked1_2=($options[1]=="all")?"checked":"";

	$form="
	"._MB_TADNEW_TADNEWS_CONTENT_BLOCK_EDIT_BITEM0."
	<INPUT type='text' name='options[0]' value='{$options[0]}'><br />
	"._MB_TADNEW_TADNEWS_CONTENT_BLOCK_EDIT_BITEM1."
	<INPUT type='radio' $chked1_0 name='options[1]' value='table'>"._MB_TADNEW_SHOW_TABLE."
	<INPUT type='radio' $chked1_1 name='options[1]' value='list'>"._MB_TADNEW_SHOW_LIST."
	<INPUT type='radio' $chked1_2 name='options[1]' value='all'>"._MB_TADNEW_SHOW_ALL."
	";
	return $form;
}



if(!function_exists("block_chk_cate_power")){
	//判斷本文之所屬分類是否允許該用戶之所屬群組觀看
	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;
	}
}


if(!function_exists("block_get_tad_news_cate")){
	//以流水號取得某筆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;
	}
}


if(!function_exists("block_admin_tool")){
	//編輯工具
	function block_admin_tool($uid,$nsn,$counter=""){
		global $xoopsUser,$xoopsModule;

		if($xoopsUser){
			$uuid=$xoopsUser->getVar('uid');
			$modhandler = &xoops_gethandler('module');
		  $xoopsModule = &$modhandler->getByDirname("tadnews");
			$module_id = $xoopsModule->getVar('mid');
	    $isAdmin=$xoopsUser->isAdmin($module_id);
		}else{
			$uuid="";
			$isAdmin="";
		}

		//不是本人或網管就不秀出工具
		if($uid!=$uuid and !$isAdmin)return "";


		$fun="<div>
		<a href='".XOOPS_URL."/modules/tadnews/post.php'>"._MB_TADNEW_ADD."</a> |
		<a href='".XOOPS_URL."/modules/tadnews/post.php?op=tad_news_form&nsn=$nsn'>"._MB_TADNEW_EDIT."</a> |
		<a href=\"javascript:delete_tad_news_func($nsn);\">"._MB_TADNEW_DEL."</a> |
		"._MB_TADNEW_HOT."{$counter}
		</div>";
		return $fun;
	}
}


if(!function_exists("b_del_tadnews_js")){
	//刪除的js
	function b_del_tadnews_js(){
		$js="<script>
		function delete_tad_news_func(nsn){
			var sure = window.confirm('"._MB_TADNEW_SURE_DEL."');
			if (!sure)	return;
			location.href=\"".XOOPS_URL."/modules/tadnews/index.php?op=delete_tad_news&nsn=\" + nsn;
		}
		</script>";
		return $js;
	}
}


if(!function_exists("chk_always_top")){
	//取得附檔
	function chk_always_top(){
		global $xoopsDB,$xoopsUser;
    $now=date("Y-m-d H:i:s" , xoops_getUserTimestamp(time()));
		$sql="update ".$xoopsDB->prefix("tad_news")." set always_top='0' WHERE always_top_date <='{$now}' and always_top_date!='0000-00-00 00:00:00'";
		$xoopsDB->queryF($sql);
	}
}
?>
