#!/usr/bin/perl
#--NetShopOwner-CGI-version:4.2.5.5--

;#
;# support2.cgi
;# [PC]お問い合わせ確認画面
;# Copyright (c) 2004- Increment P Corp. All rights reserved.
;#
;# 2009/09/30 NSO4対応
;#

use strict;
use uselib;
use defnsopath_shop;
use NsoVersionPC 1.00;
use Lib::SiteStatus;
use Lib::Support;

&ReadParse();
my %in    = %main::in;
my %incfn = %main::incfn;
my %inct  = %main::inct;

# インスタンス定義
my $objConfigSystem = Config::NsoSystem->newShop();
my $NsoMisc       = NsoMisc->new();
my $LibSupport   = Lib::Support->new();

# コンフィグレーションのキャッシュ
my %ConfigSystem = $objConfigSystem->cache_config();

my $status_err;

FUNC:{
	my $func = $in{func} || 'main';
	my %function = (
		main       => \&main,
	);
	
	&check_request_method();
	&check_http_referer();
	&check_closing();
	
	if ($function{$func}){
		$function{$func}->();
	}else{
		&main();
	}
}
exit();

;#
;# リクエストメソッド検査
;#
sub check_request_method {
	unless ($ENV{REQUEST_METHOD} eq 'POST'){
		my $Support1URL = $ConfigSystem{base_href}{cgishop}.$ConfigSystem{shop_script_name}{support1};
		print $NsoMisc->redirect( $NsoMisc->getChangedURL({url=>$Support1URL, key=>'AfterCart'}) );
		exit();
	}
}

;#
;# HTTP_REFERER検査
;# 
;#
sub check_http_referer {
	return if( !$ConfigSystem{Default}{Referer}{Check} );
	
	my $cgiroot = $NsoMisc->getChangedURL({url=>$ConfigSystem{base_href}{cgishop}, key=>'AfterCart'});
	my $Support1URL = $ConfigSystem{base_href}{cgishop}.$ConfigSystem{shop_script_name}{support1};
	
	unless ($ENV{HTTP_REFERER} =~ /$cgiroot/i){
		print $NsoMisc->redirect( $NsoMisc->getChangedURL({url=>$Support1URL, key=>'AfterCart'}) );
		exit();
	}
}

;#
;# 開店・閉店状態検査
;#
sub check_closing {
	my $SiteStatus = Lib::SiteStatus->new({ ConfigSystem=>\%ConfigSystem });
	$SiteStatus->CheckClosing({ IgnoreFlag=>1 });
}

;#
;# メイン処理
;#
sub main {

	# 入力内容チェック
	my %Err = &get_page2();
	my %htmls  = ();
	$htmls{status_err} = $status_err;

	# テンプレート置換
	my $system_templ_dir   = $main::nsoDB_path ."/";
	my $system_templ_kind  = "templ";
	my $Data;

	# HTMLタグ無効化
	my %Text = ();
	foreach my $key ( keys( %in) ) {
		$Text{$key} = $NsoMisc->html_encode($in{$key});
	}
	
	# エラーがある場合または修正ボタンを押された場合は再入力
	if ($status_err ne "" or $in{Revise_Flg} ne "" ) {
		$Data = "support1.htmp";
	} else {
		$Data = "support2.htmp";
		# HTMLで表示できるように
		$htmls{Inquiry} = $Text{Inquiry};
		$htmls{Inquiry} =~ s/[\n]/<BR>/ig;
	}

	my %support1 = ();
	my %support3 = ();
	# 質問部分編集
	for (my $i=1; $i <= 4; $i++) {
		# objectにより処理振り分け
		if ($Text{"question$i"."_object"} eq "cmb") {
			$Text{"question$i"."_".substr($Text{"question$i"},0,1) } = "selected";	# selected用
			$support1{"question$i"} = $Text{"question$i"};							# support1へ
			$support3{"question$i"} = substr( $Text{"question$i"}, 2);				# support3へ
		} else {
			$support1{"question$i"} = $Text{"question$i"};
			$support3{"question$i"} = $Text{"question$i"};
		}
	}

	my $NsoTempl = NsoTempl->new({ DataDir=>$system_templ_dir, DirKind=>$system_templ_kind, Data=>$Data });
	my %replace_templ = (
		Err      => \%Err,
		Text     => \%Text,
		htmls    => \%htmls,
		support1 => \%support1,
		support3 => \%support3,
	);
	
	print $NsoMisc->print_header();
	print $NsoTempl->replace_templ(\%replace_templ);
	exit();
}

;#
;# 問合せ確認フォーム
;#
sub get_page2 {
	
	my %forms = ();
	my %errors = ();
	my $errs = 0;
	my @form_keys = qw( Name Mail Subject Inquiry question1 question2 question3 question4);
	
	# フォーム内容検査
	CHECKFORM:{
		my %values = ();
		my %default = (
			%in,
		);
		
		# 半角カナを全角に直す
		foreach my $key (@form_keys){
			$in{$key} = $NsoMisc->j_han2zen($in{$key});
		}
		
		foreach my $key (keys %default){
			$default{$key} = $in{$key} if (defined $in{$key});
		}
		
		my %form_params = $LibSupport->MakeFormParams({ values=>\%values, default=>\%default });
		
		# フォーム入力内容検査
		foreach my $key (@form_keys){
			my $NsoForm = NsoForm->new($form_params{$key});
			my $err_msg = $NsoForm->checkform({ query=>$in{$key} });
			
			if ($err_msg ne ''){
				$errs++;
				$errors{$key} = qq(▼$err_msg<BR>);
			}
		}
	};
	
	# 入力内容に不備がある場合は入力画面を表示して終了
	if ($errs > 0){
		$status_err = q(記入・選択内容に不備があります。修正してください。);
	}else{
		$status_err = q();
	}
	
	return %errors;
}












