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

//tad_web_action編輯表單
function tad_web_action_form($ActionID=""){
	global $xoopsDB,$xoopsUser,$WebID,$isWebOwner,$xoopsTpl;
  if(empty($isWebOwner)){
    redirect_header("index.php",3, _MD_TCW_NOT_OWNER);
  }elseif($isWebOwner!=$WebID){
    redirect_header($_SERVER['PHP_SELF']."?op=tad_web_action_form&WebID=$isWebOwner",3, _MD_TCW_AUTO_TO_HOME);
  }
  
	//抓取預設值
	if(!empty($ActionID)){
		$DBV=get_tad_web_action($ActionID);
	}else{
		$DBV=array();
	}

	//預設值設定


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

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

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

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

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

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

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

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

	$op=(empty($ActionID))?"insert_tad_web_action":"update_tad_web_action";
	//$op="replace_tad_web_action";

	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();

  $xoopsTpl->assign('formValidator_code',$formValidator_code);
  $xoopsTpl->assign('ActionName',$ActionName);
  $xoopsTpl->assign('ActionDesc',$ActionDesc);
  $xoopsTpl->assign('ActionDate',$ActionDate);
  $xoopsTpl->assign('ActionPlace',$ActionPlace);
  $xoopsTpl->assign('list_del_file',upfile::list_del_file("ActionID",$ActionID,true));
  $xoopsTpl->assign('ActionID',$ActionID);
  $xoopsTpl->assign('WebID',$WebID);
  $xoopsTpl->assign('next_op',$op);
  $xoopsTpl->assign('op','tad_web_action_form');


}



//新增資料到tad_web_action中
function insert_tad_web_action(){
	global $xoopsDB,$xoopsUser,$upfile;

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

	$myts =& MyTextSanitizer::getInstance();
	$_POST['ActionName']=$myts->addSlashes($_POST['ActionName']);
	$_POST['ActionDesc']=$myts->addSlashes($_POST['ActionDesc']);
	$_POST['ActionPlace']=$myts->addSlashes($_POST['ActionPlace']);


	$sql = "insert into ".$xoopsDB->prefix("tad_web_action")."
	(`ActionName` , `ActionDesc` , `ActionDate` , `ActionPlace` , `uid` , `WebID` , `ActionCount`)
	values('{$_POST['ActionName']}' , '{$_POST['ActionDesc']}' , '{$_POST['ActionDate']}' , '{$_POST['ActionPlace']}' , '{$uid}' , '{$_POST['WebID']}' , '{$_POST['ActionCount']}')";
	$xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());

	//取得最後新增資料的流水編號
	$ActionID=$xoopsDB->getInsertId();
  $upfile->upload_file('upfile',"ActionID",$ActionID,600);
	return $ActionID;
}

//更新tad_web_action某一筆資料
function update_tad_web_action($ActionID=""){
	global $xoopsDB,$xoopsUser,$isAdmin , $isWebOwner,$upfile;

	$myts =& MyTextSanitizer::getInstance();
	$_POST['ActionName']=$myts->addSlashes($_POST['ActionName']);
	$_POST['ActionDesc']=$myts->addSlashes($_POST['ActionDesc']);
	$_POST['ActionPlace']=$myts->addSlashes($_POST['ActionPlace']);

  $anduid=onlyMine();

	$sql = "update ".$xoopsDB->prefix("tad_web_action")." set
	 `ActionName` = '{$_POST['ActionName']}' ,
	 `ActionDesc` = '{$_POST['ActionDesc']}' ,
	 `ActionDate` = '{$_POST['ActionDate']}' ,
	 `ActionPlace` = '{$_POST['ActionPlace']}'
	where ActionID='$ActionID' $anduid";
	$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
  $upfile->upload_file('upfile',"ActionID",$ActionID,600);
	return $ActionID;
}


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


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

//刪除tad_web_action某筆資料資料
function delete_tad_web_action($ActionID=""){
	global $xoopsDB,$xoopsUser;
  $anduid=onlyMine();
	$sql = "delete from ".$xoopsDB->prefix("tad_web_action")." where ActionID='$ActionID' $anduid";
	$xoopsDB->queryF($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
}

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

	//以下會產生這些變數： $ActionID , $ActionName , $ActionDesc , $ActionDate , $ActionPlace , $uid , $WebID , $ActionCount
	foreach($all as $k=>$v){
		$$k=$v;
	}
		
		
  $pics=$upfile->show_files('upfile',"ActionID",$ActionID,true,false,false,false,$WebID);
  $uid_name=XoopsUser::getUnameFromId($uid,1);


	$xoopsTpl->assign('isMineAction',isMine());
	$xoopsTpl->assign('ActionName',$ActionName);
	$xoopsTpl->assign('ActionDate',$ActionDate);
	$xoopsTpl->assign('ActionPlace',$ActionPlace);
	$xoopsTpl->assign('ActionDesc',nl2br($ActionDesc));
	$xoopsTpl->assign('uid_name',$uid_name);
	$xoopsTpl->assign('ActionCount',$ActionCount);
	$xoopsTpl->assign('pics',$pics);
	$xoopsTpl->assign('op','show_one_tad_web_action');
	$xoopsTpl->assign('ActionID',$ActionID);
	$xoopsTpl->assign('ActionInfo',sprintf(_MD_TCW_INFO,$uid_name,$ActionDate,$ActionCount));
}



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

common_template($interface_menu,$WebID);


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

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

  //更新資料
  case "update_tad_web_action":
  update_tad_web_action($ActionID);
  header("location: {$_SERVER['PHP_SELF']}");
  break;
  //輸入表格
  case "tad_web_action_form":
  tad_web_action_form($ActionID);
  break;

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

  //預設動作
  default:
  if(empty($ActionID)){
  	list_tad_web_action($WebID);
  }else{
  	show_one_tad_web_action($ActionID);
  }
  break;

}

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