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

//區塊主函式 (顯示所有新聞的類別)
function tadnews_cate_show($options){
	$path=XOOPS_URL."/modules/tadnews/menu/";

	$block="<script type=\"text/javascript\" src=\"{$path}simpletreemenu.php\">
	/***********************************************
	* Simple Tree Menu- U+00A9. Dynamic Drive DHTML code library (www.dynamicdrive.com)
	* This notice MUST stay intact for legal use
	* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
	***********************************************/
	</script>

	<link rel=\"stylesheet\" type=\"text/css\" href=\"{$path}simpletree.css\" />


<ul id=\"tadnews_treemenu1\" class=\"treeview\">
".block_get_all_news_cate()."
</ul>
	<div align='right'><img src='".XOOPS_URL."/modules/tadnews/images/open.png' alt='"._MB_TADNEW_EXPAND_ALL."' title='"._MB_TADNEW_EXPAND_ALL."' onClick=\"ddtreemenu.flatten('tadnews_treemenu1', 'expand')\">
	<img src='".XOOPS_URL."/modules/tadnews/images/close.png' alt='"._MB_TADNEW_CONTACT_ALL."' title='"._MB_TADNEW_CONTACT_ALL."' onClick=\"ddtreemenu.flatten('tadnews_treemenu1', 'contact')\"></div>

	<script type=\"text/javascript\">
	//ddtreemenu.createTree(treeid, enablepersist, opt_persist_in_days (default is 1))
	ddtreemenu.createTree(\"tadnews_treemenu1\", true)
	</script>";
	return $block;
}

//取得所有類別標題
if(!function_exists("block_get_all_news_cate")){
	function block_get_all_news_cate($of_ncsn=0){
		global $xoopsDB;
		$now=date("Y-m-d",xoops_getUserTimestamp(time()));
		$today=date("Y-m-d H:i:s",xoops_getUserTimestamp(time()));

		$sql="select ncsn,nc_title from ".$xoopsDB->prefix("tad_news_cate")." where not_news!='1' and of_ncsn='$of_ncsn' order by sort";
		$result=$xoopsDB->query($sql);

		if($of_ncsn==0){
			$cates[0]=_MB_TADNEWS_NO_CATE;
			$ncsn_ok[]=0;
		}
		while(list($ncsn,$nc_title)=$xoopsDB->fetchRow($result)){
			$ncsn_ok[]=$ncsn;
			$cates[$ncsn]=$nc_title;
		}
		$ok_cate=implode(",",$ncsn_ok);
		$sql = "select ncsn,count(nsn) from ".$xoopsDB->prefix("tad_news")." where enable='1' and ncsn in($ok_cate) and start_day < '".$today."' and (end_day > '".$today."' or end_day='0000-00-00 00:00:00') group by ncsn";

		$result = $xoopsDB->query($sql);
		while(list($ncsn,$count)=$xoopsDB->fetchRow($result)){
		  $counter[$ncsn]=$count;
		}


		$option="";

		foreach($cates as $ncsn=>$nc_title){
		  $count=(empty($counter[$ncsn]))?0:$counter[$ncsn];
		  if(!empty($ncsn)){
				$have_sub=block_chk_cate_have_sub($ncsn);
			}else{
				$have_sub=false;
			}
			$option.="<li>";
			$option.="<a href='".XOOPS_URL."/modules/tadnews/rss.php?ncsn={$ncsn}'><img src='".XOOPS_URL."/modules/tadnews/images/rss.png' title='rss' alt='rss' align='absmiddle' style='margin-right:3px;'></a><a href='".XOOPS_URL."/modules/tadnews/index.php?ncsn={$ncsn}'>{$nc_title}</a> ({$count})";
				if($have_sub){
					$option.="\n<ul>\n";
				$option.=block_get_all_news_cate($ncsn);
					$option.="</ul>\n";
				}
			$option.="</li>\n";
		}
		return $option;
	}
}

//檢查有無子選項
if(!function_exists("block_chk_cate_have_sub")){
	function block_chk_cate_have_sub($ncsn=0){

		global $xoopsDB;
		$sql = "select ncsn from ".$xoopsDB->prefix("tad_news_cate")." where of_ncsn='{$ncsn}'";
		$result = $xoopsDB->query($sql);

		while(list($sub_ncsn)=$xoopsDB->fetchRow($result)){
			if(!empty($sub_ncsn)){
				return true;
			}
		}

		return false;
	}
}
?>