#!/usr/bin/perl
########## ########## ########## ########## ##########
#  entry_ng.cgi(recruit)
#  MARS FLAG Corporation.
#  M.Nishida write
#  Update 2018.12.12
########## ########## ########## ########## ##########
use Encode;
require '../../../lib/common.pl';

########## ########## ########## ########## ##########
# Define Data
########## ########## ########## ########## ##########
$MAIL_FROM = 'recruit@marsflag.com';
$MAIL_CC   = 'recruit@marsflag.com';
#$MAIL_CC   = 'nishida@marsflag.com';

$SUBJECT = '[MARS FLAG] 新卒採用(2020年卒)エントリー完了のお知らせ';

$FORM_TMPL     = '../../../tmpl/new_graduate_form.tmpl';
$CHECK_TMPL    = '../../../tmpl/new_graduate_check.tmpl';
$THANKS_URL    = 'https://www.marsflag.com/company/recruit/thanks_ng.html';
$SENDMAIL_PATH = '/usr/sbin/sendmail';
$LOG_PATH      = '../../../data/recruit.txt';

@ITEM_SORT = ('last', 'first', 'school', 'specialty', 'email', 'agree');
%ITEMS = ('last'      => '氏名(姓)を入力してください。',
          'first'     => '氏名(名)を入力してください。',
          'school'    => '在籍校を入力してください。',
          'specialty' => '学部/学科を入力してください。',
          'email'     => 'E-mailを入力してください。',
          'email_ng'  => 'E-mailを正しく入力してください。',
          'agree'     => '個人情報の取り扱いにご同意願います。');

$ERROR_VIEW = ' style="background:#FFD5CC"';

########## ########## ########## ########## ##########
## Main Source
########## ########## ########## ########## ##########
%in = &ReceiveData();  # CGIデータ取得
%out = %in;
$kind = $in{'k'};  # なし:新規 / c:確認 / s:エントリー

if($kind eq 'c'){
  # 問い合わせ項目チェック
  foreach $item (@ITEM_SORT){
    if($in{$item} eq ''){
      $out{'error'} .= $ITEMS{$item} . "<br />\n";
      $out{'err_' . $item} = $ERROR_VIEW;
    }elsif($item eq 'email' && $in{'email'} !~ /^([a-zA-Z0-9\.\-\/_]{1,})@([a-zA-Z0-9\.\-\/_]{1,})\.([a-zA-Z0-9\.\-\/_]{1,})$/){
      $out{'error'} .= $ITEMS{$item . '_ng'} . "<br />\n";
      $out{'err_email'} = $ERROR_VIEW;
    }

    $out{$item} =~ s/"/”/g;
    $out{$item} =~ s/</＜/g;
    $out{$item} =~ s/>/＞/g;
    $out{$item} =~ s/&/＆/g;
  }

  if($out{'error'} eq ''){
    &PrintPage($CHECK_TMPL, %out);  # 確認ページを表示
  }else{
    &PrintPage($FORM_TMPL, %out);  # 入力ページを表示
  }
}elsif($kind eq 's'){
  # 受け付け日時取得
  ($sec, $min, $hour, $day, $month, $year) = localtime(time);
  $date = sprintf("%04d.%02d.%02d %02d:%02d", $year+1900, $month+1, $day, $hour, $min);
  $sdate = sprintf("%04d.%02d.%02d %02d:%02d:%02d", $year+1900, $month+1, $day, $hour, $min, $sec);

  # ロギング
  $logline = "$sdate\t$in{'last'}\t$in{'first'}\t$in{'school'}\t$in{'specialty'}\t$in{'email'}";
  &WriteLog($LOG_PATH, $logline);

  # メール送信
  $subject = Encode::encode('MIME-Header-ISO_2022_JP', $SUBJECT);
  $text  = "このメールはマーズフラッグからの自動返信メールです。\n";
  $text .= "----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n";
  $text .= "\n";
  $text .= "$in{'school'} $in{'last'} $in{'first'}さま\n";
  $text .= "\n";
  $text .= "このたびは、マーズフラッグ 新卒採用(2020年卒)にエントリーいただき、\n";
  $text .= "ありがとうございました。\n";
  $text .= "以下の内容にて、エントリーを承りましたので、ご連絡いたします。\n";
  $text .= "\n";
  $text .= "[エントリー情報]\n";
  $text .= "・受け付け日時   : $date\n";
  $text .= "・氏名           : $in{'last'} $in{'first'}\n";
  $text .= "・在籍校         : $in{'school'}\n";
  $text .= "・学部/学科      : $in{'specialty'}\n";
  $text .= "・E-mail         : $in{'email'}\n";
  $text .= "\n";
  $text .= "新卒採用担当からのメールでのご連絡をお待ちください。\n";
  $text .= "\n";
  $text .= "\n";
  $text .= "-- \n";
  $text .= "新卒採用担当  recruit\@marsflag.com\n";
  $text .= "株式会社マーズフラッグ\n";
  $text .= "\n";
  $text .= "160-0022 東京都新宿区新宿2-16-6 新宿イーストスクエアビル3F\n";
  $text .= "http://www.marsflag.com/\n";
  $text .= "----------------------------\n";
  $text .= "このメール (添付ファイル等を含む) は、宛先として意図した特定の相手に\n";
  $text .= "送信したものであり、秘匿特権の対象になる情報が含まれています。\n";
  $text .= "もし、意図した相手以外の方が受信された場合は、甚だお手数ではございますが、\n";
  $text .= "このメールを破棄して頂くとともに、このメールについて一切の開示、複写、配布、\n";
  $text .= "その他の利用、または記載内容に基づくいかなる行動もお控え下さいますように、\n";
  $text .= "謹んでお願い申し上げます。\n";

  open(MAIL,"| $SENDMAIL_PATH -t -i") || die 'sendmail error';
  print MAIL "From: $MAIL_FROM\n";
  print MAIL "To: $in{'email'}\n";
  print MAIL "Cc: $MAIL_CC\n";
  print MAIL "Subject: $subject\n";
  print MAIL "Content-Transfer-Encoding: 7bit\n";
  print MAIL "Content-Type: text/plain;\n\n";
  print MAIL "$text";
  close(MAIL);

  print "Location: $THANKS_URL\n";
  print "\n";
}else{
  &PrintPage($FORM_TMPL, %out);  # 入力ページを表示
}

exit;
