<?php
require_once(dirname(__FILE__)."/config.php");
require_once(DEDEINC."/datalistcp.class.php");
CheckPurview('plus_Mail');
if(!isset($dopost)) $dopost = '';
if($dopost=="add"){
	$dsql->SetQuery("SELECT id,typename FROM `#@__mail_type` ORDER BY id");
  $dsql->Execute();
	$btypes = Array();
	while($row = $dsql->GetArray())
	{
	  $btypes[$row['id']] = $row['typename'];
	}
	require_once(DEDEADMIN."/templets/mail_title_add.htm");	
}elseif($dopost=="edit"){
	$dsql->SetQuery("SELECT id,typename FROM `#@__mail_type` ORDER BY id");
  $dsql->Execute();
	$btypes = Array();
	while($row = $dsql->GetArray())
	{
	  $btypes[$row['id']] = $row['typename'];
	}
  $row=$dsql->GetOne("SELECT * FROM `#@__mail_title` WHERE id=$id");
  require_once(DEDEADMIN."/templets/mail_title_edit.htm");	
}elseif($dopost=="addsave"){
	if(!preg_match('/[0-9]/',$period)){
		ShowMsg("期刊號只能為數字!","-1");
		exit();
	}
	if($typeid=="0"){
		ShowMsg("請選擇類別!","-1");
		exit();
	}
	if($title==''){
		ShowMsg("請填寫信息標題!","-1");
		exit();
	}
	if($message==''){
		ShowMsg("請填寫內容!","-1");
		exit();
	}
	$message = stripslashes($message);
  $pattern="/\\".$cfg_medias_dir."/";
	$message =preg_replace($pattern,$cfg_basehost.$cfg_medias_dir,$message);
	$title = cn_substrR(HtmlReplace($title,1),60);
	$addtime=$sendtime = time();
	$writer= $cuserLogin->getUserName();
	$mid=$cuserLogin->getUserID();
	$query = "INSERT INTO #@__mail_title (period,typeid,title,content,addtime,sendtime,writer,mid,state,count) VALUES ('$period','$typeid','$title','$message','$addtime',0,'$writer','$mid',0,0)";
	if(!$dsql->ExecuteNoneQuery($query)){
		ShowMsg("更新數據庫#@__mail_title表時出錯，請檢查！","javascript:;");
	  exit();
	}else{
    ShowMsg("發表期刊成功！","mail_title.php");
		exit();
	}
}elseif($dopost=="editsave"){
	if(!preg_match('/[0-9]/',$period)){
		ShowMsg("期刊號只能為數字!","-1");
		exit();
	}
	if($title==''){
		ShowMsg("請填寫信息標題!","-1");
		exit();
	}
	if($message==''){
		ShowMsg("請填寫內容!","-1");
		exit();
	}
	$message = stripslashes($message);
  $pattern="/\\".$cfg_medias_dir."/";
	$message =preg_replace($pattern,$cfg_basehost.$cfg_medias_dir,$message);
	$title = cn_substrR(HtmlReplace($title,1),60);
	$writer= $cuserLogin->getUserName();
	$mid=$cuserLogin->getUserID();

	$query = "UPDATE #@__mail_title SET period='$period',typeid='$typeid',title='$title',content='$message',writer='$writer',mid='$mid' WHERE id=$id";
	if(!$dsql->ExecuteNoneQuery($query)){
		ShowMsg("更新數據庫#@__mail_title表時出錯，請檢查！","javascript:;");
	  exit();
	}else{
    ShowMsg("編輯期刊成功！","mail_title.php");
		exit();
	}
}elseif($dopost=="delete"){
  $dsql->ExecuteNoneQuery("Delete From `#@__mail_title` where id='$id'");
  ShowMsg("刪除期刊成功！","mail_title.php");
	exit();
}else{
	
	function GetSendTimeMk($mktime){
		if($mktime=="0") return "未發送";
		else return MyDate('Y-m-d H:i:s',$mktime);
	}
	
	$sql  = "SELECT t.*,p.typename FROM `#@__mail_title` AS t LEFT JOIN `#@__mail_type` AS p ON t.typeid=p.id ORDER BY t.id desc";
	$dlist = new DataListCP();
	$dlist->SetTemplet(DEDEADMIN."/templets/mail_title_main.htm");
	$dlist->SetSource($sql);
	$dlist->display();
}

?>