<?php
//  ------------------------------------------------------------------------ //
// 本模組由 tad 製作
// 製作日期：2011-04-15
// $Id:$
// ------------------------------------------------------------------------- //
/*-----------引入檔案區--------------*/
include_once "header.php";
$xoopsOption['template_main'] = "tad_web_discuss_tpl.html";
include_once XOOPS_ROOT_PATH."/header.php";
/*-----------function區--------------*/

//tad_web_discuss編輯表單
function tad_web_discuss_form($DiscussID=""){
	global $xoopsDB,$xoopsUser,$WebID,$isWebOwner,$isClassMem,$isAdmin;

  if(empty($xoopsUser)){
    redirect_header("index.php",3, _MD_TCW_LOGIN_TO_POST);
	}

	//抓取預設值
	if(!empty($DiscussID)){
		$DBV=get_tad_web_discuss($DiscussID);
	}else{
		$DBV=array();
	}

	//預設值設定


	//設定「DiscussID」欄位預設值
	$DiscussID=(!isset($DBV['DiscussID']))?"":$DBV['DiscussID'];

	//設定「ReDiscussID」欄位預設值
	$ReDiscussID=(!isset($DBV['ReDiscussID']))?"":$DBV['ReDiscussID'];

	//設定「uid」欄位預設值
	$uid=(!isset($DBV['uid']))?$xoopsUser->getVar('uid'):$DBV['uid'];

	//設定「DiscussTitle」欄位預設值
	$DiscussTitle=(!isset($DBV['DiscussTitle']))?"":$DBV['DiscussTitle'];

	//設定「DiscussContent」欄位預設值
	$DiscussContent=(!isset($DBV['DiscussContent']))?"":$DBV['DiscussContent'];

	//設定「DiscussDate」欄位預設值
	$DiscussDate=(!isset($DBV['DiscussDate']))?date("Y-m-d H:i:s"):$DBV['DiscussDate'];

	//設定「WebID」欄位預設值
	$WebID=(!isset($DBV['WebID']))?$isWebOwner:$DBV['WebID'];

	//設定「LastTime」欄位預設值
	$LastTime=(!isset($DBV['LastTime']))?date("Y-m-d H:i:s"):$DBV['LastTime'];

	//設定「DisscussCounter」欄位預設值
	$DisscussCounter=(!isset($DBV['DisscussCounter']))?"":$DBV['DisscussCounter'];

	$op=(empty($DiscussID))?"insert_tad_web_discuss":"update_tad_web_discuss";
	//$op="replace_tad_web_discuss";

	if(!file_exists(TADTOOLS_PATH."/formValidator.php")){
   redirect_header("index.php",3, _MD_NEED_TADTOOLS);
  }
  include_once TADTOOLS_PATH."/formValidator.php";
  $formValidator= new formValidator("#myForm",true);
  $formValidator_code=$formValidator->render();

	$main="
	$formValidator_code

	<form action='{$_SERVER['PHP_SELF']}' method='post' id='myForm' enctype='multipart/form-data'>
	<table class='form_tbl' style='width:100%;'>
	<!--標題-->
	<tr><td class='title' nowrap  style='width:60px;'>"._MD_TCW_DISCUSSTITLE."</td>
	<td class='col'><input type='text' name='DiscussTitle' size='20' value='{$DiscussTitle}' id='DiscussTitle' class='validate[required , length[1,255]]'  style='width:100%;'></td></tr>

	<!--內容-->
	<tr><td class='title' nowrap>"._MD_TCW_DISCUSSCONTENT."</td>
	<td class='col'><textarea name='DiscussContent' cols='50' rows=8 id='DiscussContent' class='validate[required , length[10,9999]]' style='width:100%; height:200px;'>{$DiscussContent}</textarea></td></tr>

	<tr><td class='bar' colspan='2'>

	<!--領域-->
	<input type='hidden' name='WebID' value='{$WebID}'>
	
	<!--編號-->
	<input type='hidden' name='DiscussID' value='{$DiscussID}'>

	<!--回覆編號-->
	<input type='hidden' name='ReDiscussID' value='{$ReDiscussID}'>

	<input type='hidden' name='op' value='{$op}'>
	<input type='submit' value='"._MD_TCW_DISCUSS_SUBMIT."'></td></tr>
	</table>
	</form>";


	return $main;
}



//新增資料到tad_web_discuss中
function insert_tad_web_discuss(){
	global $xoopsDB,$xoopsUser;

	//取得使用者編號
	$uid=($xoopsUser)?$xoopsUser->getVar('uid'):"";

	$myts =& MyTextSanitizer::getInstance();
	$_POST['DiscussTitle']=$myts->addSlashes($_POST['DiscussTitle']);
	$_POST['DiscussContent']=$myts->addSlashes($_POST['DiscussContent']);


	$sql = "insert into ".$xoopsDB->prefix("tad_web_discuss")." 	(`ReDiscussID` , `uid` , `DiscussTitle` , `DiscussContent` , `DiscussDate` , `WebID` , `LastTime` , `DisscussCounter`)
	values('{$_POST['ReDiscussID']}' , '{$uid}' , '{$_POST['DiscussTitle']}' , '{$_POST['DiscussContent']}' , now() , '{$_POST['WebID']}' , now() , '{$_POST['DisscussCounter']}')";
	$xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());

	//取得最後新增資料的流水編號
	$DiscussID=$xoopsDB->getInsertId();

	if(!empty($_POST['ReDiscussID'])){
  	$sql = "update ".$xoopsDB->prefix("tad_web_discuss")." set `LastTime` = now()
  	where `DiscussID` = '{$_POST['ReDiscussID']}' or `ReDiscussID` = '{$_POST['ReDiscussID']}'";
  	$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
  }

	if(!empty($_POST['ReDiscussID']))return $_POST['ReDiscussID'];
	return $DiscussID;
}

//更新tad_web_discuss某一筆資料
function update_tad_web_discuss($DiscussID=""){
	global $xoopsDB,$xoopsUser;

	$myts =& MyTextSanitizer::getInstance();
	$_POST['DiscussTitle']=$myts->addSlashes($_POST['DiscussTitle']);
	$_POST['DiscussContent']=$myts->addSlashes($_POST['DiscussContent']);


  $anduid=onlyMine();
  
	$sql = "update ".$xoopsDB->prefix("tad_web_discuss")." set
	 `ReDiscussID` = '{$_POST['ReDiscussID']}' ,
	 `DiscussTitle` = '{$_POST['DiscussTitle']}' ,
	 `DiscussContent` = '{$_POST['DiscussContent']}' ,
	 `WebID` = '{$_POST['WebID']}' ,
	 `LastTime` = now()
	where DiscussID='$DiscussID' $anduid";
	$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());

	return $DiscussID;
}


//取得回覆數量
function get_re_num($DiscussID=""){
	global $xoopsDB,$xoopsUser;
  if(empty($DiscussID)) return 0;
	$sql = "select count(*) from ".$xoopsDB->prefix("tad_web_discuss")." where ReDiscussID='$DiscussID'";
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
  list($counter)=$xoopsDB->fetchRow($result);
  return $counter;
}



//以流水號取得某筆tad_web_discuss資料
function get_tad_web_discuss($DiscussID=""){
	global $xoopsDB;
	if(empty($DiscussID))return;
	$sql = "select * from ".$xoopsDB->prefix("tad_web_discuss")." where DiscussID='$DiscussID'";
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	$data=$xoopsDB->fetchArray($result);
	return $data;
}

//刪除tad_web_discuss某筆資料資料
function delete_tad_web_discuss($DiscussID=""){
	global $xoopsDB,$xoopsUser,$isAdmin,$isWebOwner;
  $uid=$xoopsUser->getVar('uid');
  $anduid=($isAdmin or $isWebOwner)?"":"and uid='$uid'";
	$sql = "delete from ".$xoopsDB->prefix("tad_web_discuss")." where DiscussID='$DiscussID' $anduid";
	$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	
	$sql = "delete from ".$xoopsDB->prefix("tad_web_discuss")." where ReDiscussID='$DiscussID' $anduid";
	$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
}

//以流水號秀出某筆tad_web_discuss資料內容
function show_one_tad_web_discuss($DiscussID=""){
	global $xoopsDB,$isClassMem,$xoopsUser,$isAdmin;
	if(empty($DiscussID)){
		return;
	}else{
		$DiscussID=intval($DiscussID);
	}
	
	add_tad_web_discuss_counter($DiscussID);
	
	$sql = "select * from ".$xoopsDB->prefix("tad_web_discuss")." where DiscussID='{$DiscussID}'";
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	$all=$xoopsDB->fetchArray($result);

	//以下會產生這些變數： $DiscussID , $ReDiscussID , $uid , $DiscussTitle , $DiscussContent , $DiscussDate , $WebID , $LastTime , $DisscussCounter
	foreach($all as $k=>$v){
		$$k=$v;
	}
	$mainDiscussTitle=$DiscussTitle;
	$DiscussContent=nl2br($DiscussContent);
  $mainDiscussContent="
  	<script>
  	function delete_tad_web_discuss_func(DiscussID){
  		var sure = window.confirm('"._TAD_DEL_CONFIRM."');
  		if (!sure)	return;
  		location.href=\"{$_SERVER['PHP_SELF']}?op=delete_tad_web_discuss&DiscussID=\" + DiscussID;
  	}
	</script><div style='line-height:180%;'>{$DiscussContent}</div>";
	
	$fun=(isMine($uid))?"
	<a href='{$_SERVER['PHP_SELF']}?op=tad_web_discuss_form&DiscussID=$DiscussID' class='link_button_r'>"._TAD_EDIT."</a>
	<a href=\"javascript:delete_tad_web_discuss_func($DiscussID);\" class='link_button_r'>"._TAD_DEL."</a>
	":"";

  if($xoopsUser){
  	$form="
  	<form action='{$_SERVER['PHP_SELF']}' method='post' id='myForm' enctype='multipart/form-data'>
  	<input type='hidden' name='DiscussTitle' value='Re:{$DiscussTitle}' id='DiscussTitle'>
  	<table style='width:100%;'>
  	<!--內容-->
  	<tr>
  	<td class='col'><textarea name='DiscussContent' cols='50' rows=8 id='DiscussContent' class='validate[required , length[10,9999]]' style='width:100%; height:80px;'></textarea></td><td style='width:100px;text-align:center;'>

  	<!--領域-->
  	<input type='hidden' name='WebID' value='{$WebID}'>

  	<!--回覆編號-->
  	<input type='hidden' name='ReDiscussID' value='{$DiscussID}'>


  	<input type='hidden' name='op' value='insert_tad_web_discuss'>
  	<input type='submit' value='"._MD_TCW_DISCUSS_TO_REPLY."'>
    </td></tr>
  	</table>
  	</form>";
	}

  //回覆的留言
	$re=get_re($DiscussID);


  $uid_name=XoopsUser::getUnameFromId($uid,1);
  
  $info=sprintf(_MD_TCW_INFO,$uid_name,$DiscussDate,$DisscussCounter);
	
	$logo="<a href='discuss.php?WebID=$WebID'><img src='images/discuss.png' alt='discuss.png, 5.9kB' title='Discuss' border='0' height='49' width='127'></a>";
	

	//raised,corners,inset
	$main=$logo.$content.$re.$form;

	return $main;
}


//回覆的留言
function get_re($DiscussID=""){
	global $xoopsDB,$isClassMem;
  if(empty($DiscussID))return;
	$sql = "select * from ".$xoopsDB->prefix("tad_web_discuss")." where ReDiscussID='$DiscussID' order by DiscussDate";
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());

	$re_data="";

	while($all=$xoopsDB->fetchArray($result)){
	  //以下會產生這些變數： $DiscussID , $ReDiscussID , $uid , $DiscussTitle , $DiscussContent , $DiscussDate , $WebID , $LastTime , $DisscussCounter
    foreach($all as $k=>$v){
      $$k=$v;
    }
    $uid_name=XoopsUser::getUnameFromId($uid,1);
    
    $pic=get_pic_file("MemPic",$uid,"1","thumb");
    $pic=empty($pic)?"images/nobody.png":$pic;
    
  	$fun=(isMine($uid))?"<div style='font-size:12px;'>
  	<a href='{$_SERVER['PHP_SELF']}?op=tad_web_discuss_form&DiscussID=$DiscussID'>"._TAD_EDIT."</a> | <a href=\"javascript:delete_tad_web_discuss_func($DiscussID);\">"._TAD_DEL."</a>
  	</div>":"";
    
    $DiscussContent=nl2br($DiscussContent);
    $DiscussContent=bubble($DiscussContent);
    $re_data.="<tr><td style='line-height:180%;'>
    $DiscussContent
    <img src='$pic' alt='{$uid_name}"._MD_TCW_DISCUSS_REPLY."' style='margin:0px 15px 0px 30px;float:left;'>
    <div style='line-height:1.5em;'>
      <div>{$uid_name}</div><div style='font-size:12px;'>$DiscussDate</div>{$fun}
    </div>
    <div style='clean:both;'></div>
    </td></tr>";
  }
  
  $re="";
  if(!empty($re_data)){
    $re="
    <link href='bubble.css' rel='stylesheet' type='text/css'>
  	<table>
  	$re_data
  	</table>
    ";
  }
  return $re;
}



function bubble($content=""){
  $main="<div class='xsnazzy'>
  <b class='xb1'></b><b class='xb2'></b><b class='xb3'></b><b class='xb4'></b><b class='xb5'></b><b class='xb6'></b><b class='xb7'></b>
  <div class='xboxcontent'>
  <p>$content</p>
  </div>
  <b class='xb7'></b><b class='xb6'></b><b class='xb5'></b><b class='xb4'></b><b class='xb3'></b><b class='xb2'></b><b class='xb1'></b>
  <em></em><span></span>
  </div>";
  return $main;
}

//新增tad_web_discuss計數器
function add_tad_web_discuss_counter($DiscussID=''){
	global $xoopsDB;
	$sql = "update ".$xoopsDB->prefix("tad_web_discuss")." set `DisscussCounter`=`DisscussCounter`+1 where `DiscussID`='{$DiscussID}'";
	$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
}


/*-----------執行動作判斷區----------*/
$op=(empty($_REQUEST['op']))?"":$_REQUEST['op'];
$DiscussID=(empty($_REQUEST['DiscussID']))?"":intval($_REQUEST['DiscussID']);

common_template($interface_menu,$WebID);

switch($op){
  //替換資料
  case "replace_tad_web_discuss":
  replace_tad_web_discuss();
  header("location: {$_SERVER['PHP_SELF']}");
  break;

  //新增資料
  case "insert_tad_web_discuss":
  $DiscussID=insert_tad_web_discuss();
  header("location: {$_SERVER['PHP_SELF']}?DiscussID=$DiscussID&WebID=$WebID");
  break;

  //更新資料
  case "update_tad_web_discuss":
  update_tad_web_discuss($DiscussID);
  header("location: {$_SERVER['PHP_SELF']}");
  break;
  //輸入表格
  case "tad_web_discuss_form":
  $main=tad_web_discuss_form($DiscussID);
  break;

  //刪除資料
  case "delete_tad_web_discuss":
  delete_tad_web_discuss($DiscussID);
  header("location: {$_SERVER['PHP_SELF']}");
  break;

  //預設動作
  default:
  if(empty($DiscussID)){
  	$main=list_tad_web_discuss($WebID);
  	//$main.=tad_web_discuss_form($DiscussID);
  }else{
  	$main=show_one_tad_web_discuss($DiscussID);
  }
  break;

}

/*-----------秀出結果區--------------*/
include_once XOOPS_ROOT_PATH.'/footer.php';
?>