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

include_once(XOOPS_ROOT_PATH."/modules/tadnews/up_file.php");

//時間處理，$ifempty 若為空，要傳回甚麼 now
function check_time($date="",$ifempty=""){
	if(empty($date['date'])){
		$day=($ifempty=="now")?date("Y-m-d H:i:s"):"";
	}else{
		$day=strtotime($date['date']) + $date['time'];
		$day=date("Y-m-d H:i:s",$day);
	}
	return $day;
}


//前置字串下拉選單
function prefix_tag_menu($prefix_tag=""){
	global $xoopsDB,$xoopsModuleConfig;
	
	$tags=explode(";",$xoopsModuleConfig['prefix_tag']);
	$option="";
	foreach($tags as $opt){
	  $txt=html_entity_decode($opt);
	  $selected=($prefix_tag==$opt)?"selected":"";
		$opt=(get_magic_quotes_gpc())?$opt:addslashes($opt);
		$option.="<option value=\"{$opt}\" $selected>{$txt}</option>";
	}

  $select = "<select name='prefix_tag'><option></option>$option</select>";
  return $select;
}

//tad_news編輯表單
function tad_news_form($nsn=""){
	global $xoopsDB,$xoopsUser;

	include_once(XOOPS_ROOT_PATH."/class/xoopsformloader.php");

	$nscn_arr=chk_cate_post_power("post");
	if(empty($nscn_arr)) redirect_header("index.php",3, _MA_TADNEW_NO_ADMIN_POWER."<br>".implode(";",$nscn_arr));

	//抓取預設值
	if(!empty($nsn)){
		$DBV=get_tad_news($nsn,true);
	}else{
		$DBV=array();
	}

	//預設值設定
	
	$nsn=(!isset($DBV['nsn']))?"":$DBV['nsn'];
	$ncsn=(!isset($DBV['ncsn']))?"":$DBV['ncsn'];
	$news_title=(!isset($DBV['news_title']))?"":$DBV['news_title'];
	$news_content=(!isset($DBV['news_content']))?"":$DBV['news_content'];
	$start_day=(!isset($DBV['start_day']) or $DBV['start_day']=="0000-00-00 00:00:00")?date("Y/m/d H:i:s",xoops_getUserTimestamp(time())):str_replace("-","/",$DBV['start_day']);
	$end_day=(!isset($DBV['end_day']) or $DBV['end_day']=="0000-00-00 00:00:00")?"":str_replace("-","/",$DBV['end_day']);
	$enable=(!isset($DBV['enable']))?"":$DBV['enable'];
	$uid=(!isset($DBV['uid']))?$xoopsUser->getVar('uid'):$DBV['uid'];

	$passwd=(!isset($DBV['passwd']))?"":$DBV['passwd'];
	$enable_group=(!isset($DBV['enable_group']))?"":explode(",",$DBV['enable_group']);
	
	$prefix_tag=(!isset($DBV['prefix_tag']))?"":$DBV['prefix_tag'];
	$always_top=(!isset($DBV['always_top']))?"0":$DBV['always_top'];
	$always_top_date=(!isset($DBV['always_top_date']))?"":$DBV['always_top_date'];
	
	
	$always_top_checked=($always_top=='1')?"checked":"";
	
	
	$SelectGroup_name = new XoopsFormSelectGroup("", "enable_group", false, $enable_group, 3, true);
	$SelectGroup_name->addOption("", _MA_TADNEW_ALL_OK, false);
	 
	$enable_group = $SelectGroup_name->render();
	
	//前置字串選單
	$prefix_tag_menu=prefix_tag_menu($prefix_tag);
	
	//類別選單
	$cate_select=get_tad_news_cate_option(0,0,$ncsn);
	
		
	$start_date_form="<input type='text' value='{$start_day}' size='15' name='start_day' id='start_day'  onClick=\"displayCalendar(document.myForm.start_day,'yyyy/mm/dd hh:ii',this,true)\">
	<img src=\"".XOOPS_URL."/modules/tadnews/images/img.gif\" onClick=\"displayCalendar(document.myForm.start_day,'yyyy/mm/dd hh:ii',this,true)\" align='absmiddle'>";
	
	
	$end_date_form="<input type='text' value='{$end_day}' size='15' name='end_day' id='end_day' onClick=\"displayCalendar(document.myForm.end_day,'yyyy/mm/dd hh:ii',this,true)\">
	<img src=\"".XOOPS_URL."/modules/tadnews/images/img.gif\" onClick=\"displayCalendar(document.myForm.end_day,'yyyy/mm/dd hh:ii',this,true)\" align='absmiddle'>";
	
	include(XOOPS_ROOT_PATH."/modules/tadnews/class/fckeditor/fckeditor.php") ;
	$oFCKeditor = new FCKeditor('news_content') ;
	$oFCKeditor->BasePath	= XOOPS_URL."/modules/tadnews/class/fckeditor/" ;
	$oFCKeditor->Config['AutoDetectLanguage']=false;
	$oFCKeditor->Config['DefaultLanguage']		= 'zh' ;
	$oFCKeditor->ToolbarSet ='my';
	$oFCKeditor->Width = '100%' ;
	$oFCKeditor->Height = '300' ;
	$oFCKeditor->Value =$news_content;
	$editor=$oFCKeditor->CreateHtml() ;
	
	$op=(empty($nsn))?"insert_tad_news":"update_tad_news";
	//$op="replace_tad_news";
	
	
	$main="

	<link type=\"text/css\" rel=\"stylesheet\" href=\"".XOOPS_URL."/modules/tadnews/class/dhtmlgoodies_calendar/dhtmlgoodies_calendar.css\" media=\"screen\"></LINK>

	<link type=\"text/css\" rel=\"stylesheet\" href=\"".XOOPS_URL."/modules/tadnews/class/bt-0.9.1/jquery.bt.css\" media=\"screen\"></LINK>

	<script type='text/javascript' src='".XOOPS_URL."/modules/tadnews/class/jquery.js'></script>
	<script type='text/javascript' src='".XOOPS_URL."/modules/tadnews/class/bt-0.9.1/jquery.bt.min.js'></script>
	
	<script src='".XOOPS_URL."/modules/tadnews/class/multiple-file-upload/jquery.MultiFile.js'></script>


	
	<SCRIPT type=\"text/javascript\" src=\"".XOOPS_URL."/modules/tadnews/class/dhtmlgoodies_calendar/dhtmlgoodies_calendar.php\"></script>
	<script type=\"text/javascript\">

	
	$(document).ready(function() {
	
		/*
		$('#enable_group').bt('"._MA_TADNEW_CAN_READ_NEWS_GROUP_TEXT."', {trigger: 'click', positions: 'right'});
		$('#start_day').bt('"._MA_TADNEW_START_DATE_TEXT."', {trigger: 'click', positions: 'top'});
		$('#end_day').bt('"._MA_TADNEW_END_DATE_TEXT."', {trigger: 'click', positions: 'top'});

		$('#ncsn').bt('"._MA_TADNEW_NEWS_CATE_TEXT."', {trigger: 'click', positions: 'top'});
		$('#news_title_input').bt('"._MA_TADNEW_NEWS_TITLE_TEXT."', {trigger: 'click', positions: 'top'});
		$('#passwd').bt('"._MA_TADNEW_NEWS_PASSWD_TEXT."', {trigger: 'click', positions: 'right'});

		$('#enable1').bt('"._MA_TADNEW_NEWS_ENABLE1_TEXT."', {trigger: 'click', positions: 'top'});
		$('#enable0').bt('"._MA_TADNEW_NEWS_ENABLE0_TEXT."', {trigger: 'click', positions: 'top'});
	  */
	
		$('#input_form').hide();
		if(!$always_top){
		$('#top_date_input').hide();
		}
		
		$('#show_input_form').click(function() {
			if ($('#input_form').is(':visible')) {
	       $('#input_form').slideUp();
			} else{
	       $('#input_form').slideDown();
			}
		});
		
		$('#always_top').click(function() {
			if ($('#top_date_input').is(':visible')) {
	       $('#top_date_input').fadeOut();
			} else{
	       $('#top_date_input').fadeIn();
			}
		});
		
		
	});
	</script>
  <form action='{$_SERVER['PHP_SELF']}' method='post' id='myForm' name='myForm' enctype='multipart/form-data'>
  <table class='form_tbl' style='width:100%'>
  <tr><td class='col' nowrap>"._MA_TADNEW_NEWS_CATE."<br>
  <select name='ncsn' id='ncsn' size=1>
		$cate_select
	</select>
	<input type='hidden' name='nsn' value='{$nsn}'>
	</td>
	<td class='col'>"._MA_TADNEW_PREFIX_TAG."<br>$prefix_tag_menu</td>
	<td class='col' style='width:100%'>"._MA_TADNEW_NEWS_TITLE."<br><input type='text' name='news_title' id='news_title_input' style='width:100%' value='{$news_title}' ></td></tr>
	
	<tr>
	<td colspan='4'>$editor</td></tr>


	<tr><td colspan=4>
	
	<div id='input_form'>
		<table class='tbl' style='width:100%'>
		<tr>
		
		<td nowrap><input type='checkbox' name='always_top' id='always_top' value='1' $always_top_checked> "._MA_TADNEW_ALWAYS_TOP."</td>
		<td><span id='top_date_input'>
		<input type='text' name='always_top_date'  id='always_top_date' size=16 value='{$always_top_date}' onClick=\"displayCalendar(document.myForm.always_top_date,'yyyy/mm/dd hh:ii',this,true);\"><img src=\"".XOOPS_URL."/modules/tadnews/images/img.gif\" onClick=\"displayCalendar(document.myForm.always_top_date,'yyyy/mm/dd hh:ii',this,true)\" align='absmiddle'></span></td>
		<td rowspan=6 valign='top'>"._MA_TADNEW_NEWS_FILES."<input type='file' name='upfile[]' class='multi'>".list_del_file("nsn",$nsn)."</td>
		</tr>

		<tr><td nowrap>"._MA_TADNEW_START_DATE."</td>
		<td>{$start_date_form}</td>
		</tr>
		
		<tr>
		<td nowrap>"._MA_TADNEW_END_DATE."</td>
		<td>{$end_date_form}</td>
		</tr>

		<tr>
		<td  nowrap>"._MA_TADNEW_NEWS_PASSWD."</td>
		<td><input type='text' name='passwd' id='passwd' size='10' value='{$passwd}'></td>
		</tr>

		<tr>
		<td nowrap>"._MA_TADNEW_NEWS_ENABLE."</td>
		<td>
		<input type='radio' name='enable' value='1' id='enable1' ".chk($enable,'1',1).">"._MA_TADNEW_NEWS_ENABLE_OK."
		<input type='radio' name='enable' value='0' id='enable0' ".chk($enable,'0').">"._MA_TADNEW_NEWS_UNABLE."
		</td>
		</tr>

		<tr>
		<td nowrap>"._MA_TADNEW_CAN_READ_NEWS_GROUP."</td>
		<td>
			$enable_group
		</td>
		</tr>
		</table>
	</div>

	</td></tr>
	
	
  <tr><td class='bar' colspan='4'>
	<input type='hidden' name='uid' value='{$uid}'>
  <input type='hidden' name='op' value='{$op}'>
  <input type='button' value='"._MA_TADNEW_ADV_SETUP."' id='show_input_form'>
  <input type='submit' value='"._MA_TADNEW_SAVE_NEWS."'></td></tr>
  </table>
  </form>";
  
  $main=div_3d(_MA_TADNEW_ADD_NEWS,$main,"raised","width:100%");

	return $main;
}



//新增資料到tad_news中
function insert_tad_news(){
	global $xoopsDB,$xoopsUser;
	$uid=$xoopsUser->getVar('uid');

 	if(empty($_POST['enable_group']) or in_array("",$_POST['enable_group'])){
    $enable_group="";
	}else{
		$enable_group=implode(",",$_POST['enable_group']);
	}

	
	$myts =& MyTextSanitizer::getInstance();
	$news_title=$myts->addSlashes($_POST['news_title']);
	$news_content=$myts->addSlashes($_POST['news_content']);
  $always_top=(empty($_POST['always_top']))?"0":"1";
	  
	$sql = "insert into ".$xoopsDB->prefix("tad_news")." (ncsn,news_title,news_content,start_day,end_day,enable,uid,passwd,enable_group,prefix_tag,always_top,always_top_date) values('{$_POST['ncsn']}','{$news_title}','{$news_content}','{$_POST['start_day']}','{$_POST['end_day']}','{$_POST['enable']}','{$uid}','{$_POST['passwd']}','{$enable_group}','{$_POST['prefix_tag']}','{$always_top}','{$_POST['always_top_date']}')";
	$xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	
	//取得最後新增資料的流水編號
	$nsn=$xoopsDB->getInsertId();
	
	//處理上傳的檔案
	upload_file("nsn",$nsn);

  $cate=get_tad_news_cate($_POST['ncsn']);
  $page=($cate['not_news']=='1')?"page":"index";
	header("location: ".XOOPS_URL."/modules/tadnews/{$page}.php?nsn={$nsn}");
	exit;
	return $nsn;
}


//更新tad_news某一筆資料
function update_tad_news($nsn=""){
	global $xoopsDB,$xoopsUser;
	$uid=$xoopsUser->getVar('uid');
	
	//確認有管理員或本人才能管理
	$news=get_tad_news($nsn);
	if(!chk_who($news['uid'])){
		redirect_header($_SERVER['PHP_SELF'],3, _MA_TADNEW_NO_ADMIN_POWER);
	}
	
	//送出後，取得時間值的方法
	if(empty($_POST['enable_group']) or in_array("",$_POST['enable_group'])){
    $enable_group="";
	}else{
		$enable_group=implode(",",$_POST['enable_group']);
	}
	
	$myts =& MyTextSanitizer::getInstance();
	$news_title=$myts->addSlashes($_POST['news_title']);
	$news_content=$myts->addSlashes($_POST['news_content']);
	$always_top=(empty($_POST['always_top']))?"0":"1";
	
	$sql = "update ".$xoopsDB->prefix("tad_news")." set  ncsn = '{$_POST['ncsn']}', news_title = '{$news_title}', news_content = '{$news_content}', start_day = '{$_POST['start_day']}', end_day = '{$_POST['end_day']}', enable = '{$_POST['enable']}', passwd = '{$_POST['passwd']}', enable_group = '{$enable_group}',prefix_tag='{$_POST['prefix_tag']}',always_top='{$always_top}',always_top_date='{$_POST['always_top_date']}' where nsn='$nsn'";
	$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	
	//處理上傳的檔案
	upload_file("nsn",$nsn);
	
	return $nsn;
}



//取得tad_news所有資料陣列
function get_tad_news_all(){
	global $xoopsDB;
	$sql = "select * from ".$xoopsDB->prefix("tad_news");
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3,  mysql_error());
	$data=$xoopsDB->fetchArray($result);
	return $data;
}

?>
