<?php
//  ------------------------------------------------------------------------ //
// 本模組由 tad 製作
// 製作日期：2008-06-25
// $Id: function.php,v 1.1 2008/05/14 01:22:08 tad Exp $
// ------------------------------------------------------------------------- //

/*-----------引入檔案區--------------*/
include "../../../include/cp_header.php";
include "../function.php";
include_once XOOPS_ROOT_PATH."/Frameworks/art/functions.php";
include_once XOOPS_ROOT_PATH."/Frameworks/art/functions.admin.php";

/*-----------function區--------------*/
//列出所有tad_form_main資料
function mail_form_main($ofsn=""){
	global $xoopsDB,$xoopsModule,$xoopsConfig,$xoopsUser;
	$MDIR=$xoopsModule->getVar('dirname');
	
	$form=get_tad_form_main($ofsn);
	
	$tag="";
	$sql = "select csn,title from ".$xoopsDB->prefix("tad_form_col")." where ofsn='$ofsn'";
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	while(list($csn,$title)=$xoopsDB->fetchRow($result)){
	  $tag.="{{$title}}<br>";
	}
	

	include(XOOPS_ROOT_PATH."/modules/tad_form/class/fckeditor/fckeditor.php") ;
	$oFCKeditor = new FCKeditor('content') ;
	$oFCKeditor->BasePath	= XOOPS_URL."/modules/tad_form/class/fckeditor/" ;
	$oFCKeditor->Config['AutoDetectLanguage']=false;
	$oFCKeditor->Config['DefaultLanguage']		= 'zh' ;
	$oFCKeditor->ToolbarSet ='my';
	$oFCKeditor->Width = '600' ;
	$oFCKeditor->Height = '250' ;
	$oFCKeditor->Value ="";
	$editor=$oFCKeditor->CreateHtml() ;



	$sql = "select man_name,email from ".$xoopsDB->prefix("tad_form_fill")." where ofsn='{$ofsn}'";
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());


	$data="<form action='{$_SERVER['PHP_SELF']}' method='post' id='myForm'>
	<table id='tbl'><tr><td colspan=3>
  <input type='hidden' name='email[".$xoopsUser->getVar('uname')."]' value='{$xoopsConfig['adminmail']}'>
	<input type='checkbox' name='test' value='1' checked>test";
	
	while(list($man_name,$email)=$xoopsDB->fetchRow($result)){
    $data.="<input type='checkbox' name='email[$man_name]' value='$email' checked>$man_name ";
	}
	$data.="
	</td></tr>
	<tr>
	<td rowspan=2>$tag</td><td nowrap>"._MA_TADFORM_MAIL_TITLE."</td><td><input type='text' name='title' size=50 style='width:100%;' value='".sprintf(_MA_TADFORM_MAIL_TITLE_VAL,$form['title'])."'></td></tr>
	<tr><td colspan=2>$editor</td></tr>
	<tr><td colspan=3 class='bar'>
  <input type='hidden' name='op' value='send'>
  <input type='hidden' name='ofsn' value='$ofsn'>
	<input type='submit' value='"._MA_TADFORM_SEND."'></td></tr>
	</tbody>
	</table>
	</form>";
	
	$data=div_3d("",$data,"corners");
	
	return $data;
}


//立即寄出
function send_all($ofsn){
	global $xoopsConfig,$xoopsDB;
	$xoopsMailer =& getMailer();
	$xoopsMailer->multimailer->ContentType="text/html";
	$xoopsMailer->addHeaders("MIME-Version: 1.0");
	
	$sql = "select csn,title from ".$xoopsDB->prefix("tad_form_col")." where ofsn='$ofsn'";
	$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
	while(list($csn,$title)=$xoopsDB->fetchRow($result)){
	  $tag[$csn]="{{$title}}";
	}

	foreach($_POST['email'] as $man_name=>$mail){
		$content= $_POST['content'];
	  $ans="";
	  $sql = "select a.csn,a.val from ".$xoopsDB->prefix("tad_form_value")." as a left join ".$xoopsDB->prefix("tad_form_fill")." as b on a.ssn=b.ssn where b.man_name='$man_name' and b.`ofsn`='{$ofsn}'";
		$result = $xoopsDB->query($sql) or redirect_header($_SERVER['PHP_SELF'],3, mysql_error());
		while(list($csn,$val)=$xoopsDB->fetchRow($result)){
      $ans[$csn]=$val;
		}

    foreach($tag as $csn=>$title){
			$content=str_replace($title,$ans[$csn],$content);
		}
		
  	if($_POST['test']=='1'){
			$main.="to: {$mail} <div style='border:1px solid gray;padding:10px;'>{$content}</div>";
			$xoopsMailer->sendMail("tad0616@gmail.com", $_POST['title'], $content,$headers);
		}else{
      if($xoopsMailer->sendMail($mail, $_POST['title'], $content,$headers)){
	      $main.="{$mail} "._MD_TADFORM_SEND_OK."<br>";
			}else{
	      $main.="{$mail} "._MD_TADFORM_SEND_ERROR."<br>";
			}
		}
		
	}
	return $main;

}
/*-----------執行動作判斷區----------*/
$op = (!isset($_REQUEST['op']))? "main":$_REQUEST['op'];
$ofsn = (!isset($_REQUEST['ofsn']))? "":intval($_REQUEST['ofsn']);

switch($op){

	case "send":
	$main=send_all($ofsn);
	break;

	//預設動作
	default:
	$main=mail_form_main($ofsn);
	break;

}

/*-----------秀出結果區--------------*/
xoops_cp_header();
echo "<link rel='stylesheet' type='text/css' media='screen' href='../module.css' />";
loadModuleAdminMenu(0);
echo $main;
xoops_cp_footer();

?>
