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

//區塊主函式 (顯示指定的自訂類別文章)
function tadnews_page($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_page_treemenu{$options[0]}\" class=\"treeview\">
	".block_get_all_page_cate($options[0])."
	</ul>

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

}


//區塊編輯函式
function tadnews_page_edit($options){
	$cate=block_get_all_not_news_cate(0,0,$options[0]);

	$form="
	"._MB_TADNEWS_PAGE_EDIT_BITEM0."
	<select name='options[0]'>
	$cate
	</select>
	<br />
	"._MB_TADNEWS_PAGE_EDIT_BITEM1."
	<INPUT type='text' name='options[1]' value='{$options[1]}'>
	";
	return $form;
}



//取得所有類別標題
if(!function_exists("block_get_all_not_news_cate")){
	function block_get_all_not_news_cate($of_ncsn=0,$level=0,$v=""){
		global $xoopsDB,$xoopsUser,$xoopsModule;

		$left=$level*10;
		$level+=1;

		$option=($of_ncsn)?"":"<option value='0'></option>";
		$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);

		while(list($ncsn,$nc_title)=$xoopsDB->fetchRow($result)){
			$selected=($v==$ncsn)?"selected":"";
			$option.="<option value='{$ncsn}' style='padding-left: {$left}px' $selected>{$nc_title}</option>";
			$option.=block_get_all_not_news_cate($ncsn,$level,$v);
		}
		return $option;
	}
}


//取得所有類別標題
if(!function_exists("block_get_all_page_cate")){
	function block_get_all_page_cate($of_ncsn=0){
		global $xoopsDB;
		$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) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());

		$option="";
		while(list($ncsn,$nc_title)=$xoopsDB->fetchRow($result)){
			$sub=block_chk_pagecate_have_sub($ncsn);
			$have_sub=(sizeof($sub)>0)?true:false;
			$option.="<li>";
			//$option.="<a href='".XOOPS_URL."/modules/tadnews/index.php?ncsn={$ncsn}&kind=page'>{$nc_title}</a>";
			$option.=$nc_title;
				if($have_sub){
					$option.="\n<ul>\n";
					$option.=block_get_all_page_cate($ncsn);
					$option.="</ul>\n";
				}
			$option.="</li>\n";
		}
		$option.=block_chk_page($of_ncsn);
		return $option;
 }
}



//檢查有無子分類
if(!function_exists("block_chk_pagecate_have_sub")){
	function block_chk_pagecate_have_sub($ncsn=0){
		global $xoopsDB;
		$sql = "select ncsn from ".$xoopsDB->prefix("tad_news_cate")." where of_ncsn='{$ncsn}'";
		$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, _MA_TADNEW_DB_SELECT_ERROR1);
		$all=$xoopsDB->fetchArray($result);
		return $all;
	}
}


//檢查該類別文章
if(!function_exists("block_chk_page")){
	function block_chk_page($show_ncsn=0){
		global $xoopsDB;
		$today=date("Y-m-d H:i:s",xoops_getUserTimestamp(time()));
		
		$option="";

		$sql = "select nsn,news_title,start_day,end_day,enable_group,counter from ".$xoopsDB->prefix("tad_news")." where ncsn='{$show_ncsn}' and enable='1' and start_day < '{$today}' and (end_day > '{$today}' or end_day='0000-00-00 00:00:00') order by start_day ";
		$result = $xoopsDB->query($sql);
		$page="";
		while(list($nsn,$news_title,$start_day,$end_day,$enable_group,$counter)=$xoopsDB->fetchRow($result)){
		  $page.="<li><a href='".XOOPS_URL."/modules/tadnews/page.php?nsn={$nsn}'>$news_title</a></li>\n";
		}
		return $page;
 }
}

?>