<?
define('SOFTPUBS_IN', true);
require_once(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH.'includes/header.php');
this_channel(CHANNEL_SUPPORT_ID);

if($_POST['name']&&$_POST['email']&&$_POST['department']&&$_POST['leave_word']){
	save_leave_word();
}
$support_article_res		=	$db_obj->getRecords("SELECT title,article_id,last_update FROM {$db_obj->db_pre}article WHERE  is_support=1");
$support_article_res		=	format_art_list($support_article_res);

$leave_word_setting		=	storage_dencode($glo_config_arr['leave_word_setting']['value']);

foreach($leave_word_setting['type'] as $key=>$val){
	$leave_word_setting['type'][$key]['name']			=	htmlspecialchars($val['name'],ENT_QUOTES);
	$leave_word_setting['type'][$key]['return_tpl']	=	htmlspecialchars($val['return_tpl'],ENT_QUOTES);
}

function save_leave_word()
{
	global $db_obj,$glo_config_arr,$_LANG,$glo_this_channel,$glo_lang,$leave_word_setting;		
	if($leave_word_setting['is_close']){
		return false;
	}	
	
	require_once(ROOT_PATH.'admin/lang/'.$glo_lang.'.php');
	$leave_word_setting		=		storage_dencode($glo_config_arr['leave_word_setting']['value']);
	$name				=	$_POST['name'];
	$email			=	$_POST['email'];
	$department		=	$leave_word_setting['type'][$_POST['department']]?$_POST['department']:1;
	$leave_word		=	$_POST['leave_word'];
	if($_FILES['attachment']['name']&&$leave_word_setting['is_attachment']){
		require_once(ROOT_PATH.'includes/glo_upload.php');
		if($_FILES['attachment']['error']==0){
			$upload_obj			=	new glo_upload();
			$attachment			=	$upload_obj->upload_file($_FILES['attachment'],'leave_word_attachment','');			
		}else{
			$attachment			=	'';
		}
	}else{
		$attachment			=	'';
	}
	if($db_obj->addRecord("{$db_obj->db_pre}leave_word",array(
		'name'			=>	$name,
		'email'			=>	$email,
		'type'			=>	$department,
		'content'	=>	$leave_word,
		'attachment'	=>	$attachment,
		'time'			=>	time(),	
		))
	){
			if($leave_word_setting['mail_to_admin']){
				$title		=		str_replace('$1',$name,$_ADMIN_LANG['leave_word_mail_to_admin_title']);
				$email_setting		=		storage_dencode($glo_config_arr['email_setting']['value']);
				$leave_word			=		nl2br($leave_word);
				
				$leave_word			=		str_replace(array('$1','$2','$3','$4'),array($glo_config_arr['admin_name']['value'],$name,$email,$leave_word),$_ADMIN_LANG['leave_word_mail_to_admin_desc']);
				if($email_setting['type']=='smtp'){
					$smtp_arr	=	$email_setting['smtp'];
				}else{
					$smtp_arr	=	array();
				}
				sendmail($glo_config_arr['admin_email']['value'],$title,$leave_word,$email,$name,$email_setting['port'],$smtp_arr,$email_setting['reply'],array(UPLOAD_DIR.$attachment));
			}	
			if($leave_word_setting['type'][$department]['return_tpl']){
				$glo_this_channel['content']    =     str_replace(array('{$name}','{$email}','{$attachment}'),array($name,$email,$attachment),$leave_word_setting['type'][$department]['return_tpl']);	
			}else{
				 $glo_this_channel['content']    =     $_LANG['leave_word_success'];
			}
	}else{
		$glo_this_channel['content']			=		str_replace('$1',$glo_config_arr['admin_email']['value'],$_LANG['leave_word_error']);
	}
	$glo_this_channel['content']				=		htmlspecialchars($glo_this_channel['content'],ENT_QUOTES);
}

$leave_word_type					=	array();				
foreach($leave_word_setting['type'] as $key=>$val){
	$leave_word_type[$key]		=	$val['name'];
}
$is_attachment				=	$leave_word_setting['is_attachment'];

parse_template('support.html');
