<?php
/*
	Copyright(c) 2010 Shift Tech inc. All Rights Reserved.
 	http://www.shift-tech.jp/
*/
//ログインチェック、設定ファイル読み込み、Smarty Viewの生成を行う
require_once (MODULES_PATH.'/extMdlFormGnrtr/admin/common/fileUpload.php');
require_once (LIB_PATH.'/common/unitCommon.php');
require_once (MODULES_PATH.'/extMdlFormGnrtr/class/CLpartcommon.php');
require_once (MODULES_PATH.'/extMdlFormGnrtr/class/CLbaseform.php');

if(!defined("UNITS_REPLACE_FLG")){
	define('UNITS_REPLACE_FLG', '%%%');
	define('UNITS_ENTER_FLG', "\r\n");
	define('UNITS_DOCUMENT_ROOT_PATH', BASE_PATH);
}

class extMdlFormGnrtrForm extends unitCommon
{
	var $_table = "extunit_extMdlFormGnrtr_bForm";		//公開用テーブル名
	var $_tmptable = "extunit_extMdlFormGnrtr_bForm_tmp";	//下書き用テーブル名
	var $columns = array(
					array("column"=>"pageu_code","conv"=>"n","tags"=>false),		//ユニット登録コード
					array("column"=>"btm_padding","conv"=>"","tags"=>false),        //下方スペース
					array("column"=>"ufrm_frm_id","conv"=>"","tags"=>false)
					);

	function extMdlFormGnrtrForm()
	{
		$this->__construct();
	}

	function __construct()
	{
		unitCommon::__construct();
		//CLpartcommon::__construct();
	}

	//出力データセット処理（必須）
	function setDisplayData()
	{
		//１：入力画面　２：確認画面　３：完了画面
		if(isset($_REQUEST['gmode']) && is_numeric($_REQUEST['gmode'])){
			$this->_gmode = $_REQUEST['gmode'];
		}else{
			$this->_gmode = 1;
			unset($_SESSION['postData']);
		}

		//ページ表示変数
		$d = $this->getRequest('d');
		$c = $this->getRequest('c');
		$p = $this->getRequest('p');
		$_prev = ($this->getRequest('prev')) ? '&prev=1&mprev=1' : '';

		$form_name = 'form'.$this->_pageu_code;
		$this->_view->assign('form_name', $form_name);
		$this->_view->assign('MODULES_PATH', MODULES_PATH);
		$_form = new HTML_QuickForm($form_name,'post',BINGOTOPURL.'/index.php?p='.$p.'&d='.$d.'&c='.$c.$_prev,'',null,true);
		$this->viewForm($_form);

		$_form->setDefaults($_SESSION['postData']);

		if ($this->getRequest('bContact_bBtnConf')!=null) {
		//確認ボタンクリック
			if($_form->validate()){
				$_SESSION['postData'] = $_form->getSubmitValues();
				$data = CLpartcommon::getPartData($this->getUnitFormid());
				//Eメールの二重登録制御のチェック
				$ret = $this->mail_register_check($data, $_SESSION['postData']);
				if ($ret == false) {
					$_form->setElementError('', "・ご入力いただきましたアドレスは既に受付されています。");
				}else{
					//ファイルをtmpフォルダに複製
					$fileinfo = $this->uploadFileTotmpdir($data,$_form);
					unset($_SESSION['fileinfo']);
					$_SESSION['fileinfo'] = $fileinfo;

					//アップロードファイルの拡張子のチェック
					$ret = $this->fileload_filetype_check($data, $fileinfo);
					$ret2= $this->fileload_security_check($data, $fileinfo);
					if($ret != null){
						$_form->setElementError('', $ret);
					}else{
						//確認画面でウエブサイドリングで表示される
						unset($_SESSION['webinfo']);
						$webinfo = $this->web_link_set($data, $_SESSION['postData']);
						$_SESSION['webinfo'] = $webinfo;
						header('Location:'.BINGOTOPURL.'/index.php?gmode=2&p=confirm&d='.$d.'&c='.$c.$_prev);
						exit;
					}
				}
			}
		} elseif ($this->getRequest('bContact_bBtnBack')!=null) {
		//戻るボタン
			$_SESSION['postData'] = $_form->getSubmitValues();
			header('Location:'.BINGOTOPURL.'/index.php?gmode=1&d='.$d.'&c='.$c.$_prev);
			exit;
		} elseif ($this->getRequest('bContact_bBtnSend')!=null) {
		//送信ボタン
			$_SESSION['postData'] = $_form->getSubmitValues();
			$this->db->query('BEGIN');
			//パーツ「mail」
			$mail_part_value = null;
			if($this->setUnitData($_SESSION['postData']) == false){
				$_form->setElementError('', '・データの保存に失敗しました。お手数ですがお電話にてご連絡頂くか、しばらく経ってから送信をお願いいたします。');
			}else{
		    	$ret = $this->sendMail($_SESSION['postData']);
				if ($ret == false) {
					$_form->setElementError('', '・メールの送信に失敗しました。お手数ですがお電話にてご連絡頂くか、しばらく経ってから送信をお願いいたします。');
				} else {
					$this->db->query('COMMIT');
					$this->_view->assign('message', $this->_data['bctt_send']);
					//header('Location:'.BINGOTOPURL.'/index.php?gmode=3&p=fin&d='.$d.'&c='.$c.$_prev);
					unset($_SESSION['capcheck']);
					header('Location:'.BINGOTOPURL.'/index.php?gmode=3&p=fin&d='.$d.'&c='.$c.$_prev);
					exit;
				}
			}
		} else{
			if ($this->_gmode == 1) {
				unset($_SESSION['pcode']);
			}
			if ($this->_gmode == 2) {
				$_SESSION['postData'] = $_SESSION['postData'];
				//アップロードした元ファイルの名前を表示
				$this->_view->assign('fileinfo', $_SESSION['fileinfo']);
				//webリンク表示
				$this->_view->assign('webinfo', $_SESSION['webinfo']);
				$_form->setDefaults($_SESSION['postData']);
				$_form->freeze();
			}
		}

		//エラー時
		if (!empty($_form->_errors)) {
			$this->_view->assign('ERROR',$_form->_errors);
			foreach($_form->_errors as $key => $val) {
				if(preg_match("/.*(zipcode|tel|checkbox|time|timeto).*/",$key)){
					$_form = $this->setAttr($_form,$key,array("class","err"));
					$_form->updateElementAttr($key, array("class"=>"err"));
					if(preg_match("/.*開始日時、又は終了日時に未入力があります。$/",$val)){
						$_form = $this->setAttr($_form,str_replace("time","timeto",$key),array("class","err"));
					}
				}else{
					$_form->updateElementAttr($key, array("class"=>"err"));
				}
			}
		}

		$renderer = new HTML_QuickForm_Renderer_ArraySmarty($this->_view);
		$_form->accept($renderer);
		$this->_view->assign('form_formg',$renderer->toArray());
		$this->_view->assign('gmode', $this->_gmode);
		$data = $this->getDetail();
		if (count($data) == 0) {
			if(isset($_SESSION['pcode'])){
				return true;
			}
			return null;
		} else {
			$_SESSION['pcode'] = $this->_pageu_code;
			return $data;
		}


//		$this->_data = $this->getDetail();
//		//処理状態判定用
//		//１：入力画面　２：確認画面　３：完了画面
//		$gmode = (isset($_SESSION['gmode'])? $_SESSION['gmode'] : 1);
//
//		//ページ表示変数
//		$d = $this->getRequest('d');
//		$c = $this->getRequest('c');
//		$p = $this->getRequest('p');
//
//		//ページが指定されていない場合は、ステップ１とする
//		if ($p == null) {
//			$gmode = 1;
//			$_SESSION['gmode'] = 1;
//		}
//
//		//フォーム名
//		$form_name = 'form'.$this->_pageu_code;
//		$this->_view->assign('form_name', $form_name);
//
//		//javascriptパース
//		$this->_view->assign('MODULES_PATH', MODULES_PATH);
//
//		$_prev = ($this->_prev) ? '&prev=1&mprev=1' : '';
//		$_form = new HTML_QuickForm($form_name,'post',BINGOTOPURL.'/index.php?p='.$p.'&d='.$d.'&c='.$c.$_prev,'',null,true);
//		$this->viewForm($_form);
//
//		if(empty($_POST) && $_REQUEST['f'] != 1){
//			unset($_SESSION['postData']);
//		}
//		$_form->setDefaults($_SESSION['postData']);
//
//		//確認ボタンクリック時
//		if ($this->getRequest('bContact_bBtnConf')!=null) {
//			if ($_form->validate()){
//				//画面で入力した値を取得
//				unset($_SESSION['postData']);
//				$_SESSION['postData'] = $_form->getSubmitValues();
//				$data = CLpartcommon::getPartData($this->getUnitFormid());
//
//				//Eメールの二重登録制御のチェック
//				$ret = $this->mail_register_check($data, $_SESSION['postData']);
//				if ($ret == false) {
//					$_form->setElementError('', "・ご入力いただきましたアドレスは既に受付されています。");
//				}else{
//					//ファイルをtmpフォルダに複製
//					$fileinfo = $this->uploadFileTotmpdir($data,$_form);
//					unset($_SESSION['fileinfo']);
//					$_SESSION['fileinfo'] = $fileinfo;
//
//					//アップロードファイルの拡張子のチェック
//					$ret = $this->fileload_filetype_check($data, $fileinfo);
//					$ret2= $this->fileload_security_check($data, $fileinfo);
//					if($ret != null){
//						$_form->setElementError('', $ret);
//					}else{
//						//確認画面でウエブサイドリングで表示される
//						unset($_SESSION['webinfo']);
//						$webinfo = $this->web_link_set($data, $_SESSION['postData']);
//						$_SESSION['webinfo'] = $webinfo;
//
//						$gmode = 2;
//						$_SESSION['gmode'] = $gmode;
//						$_SESSION['pcode'] = $this->_pageu_code;
//						$_form->freeze();
//						//header('Location:'.BINGOTOPURL.'/index.php?gmode=2&p=confirm&d='.$d.'&c='.$c.$_prev);
//						//header('Location:'.BINGOTOPURL.'/index.php?p=confirm&d='.$d.'&c='.$c.$_prev);
//					}
//				}
//			}
//		//戻るボタンクリック時
//		} elseif ($this->getRequest('bContact_bBtnBack')!=null) {
//			$gmode = 1;
//			$_SESSION['gmode'] = $gmode;
//			header('Location:'.BINGOTOPURL.'/index.php?d='.$d.'&c='.$c.$_prev."&f=1");
//		//送信ボタンクリック時
//		} elseif ($this->getRequest('bContact_bBtnSend')!=null) {
//			$this->db->query('BEGIN');
//			//パーツ「mail」
//			$mail_part_value = null;
//			//print_r($_SESSION['postData']);
//			$this->setUnitData($_SESSION['postData']);
//		    $ret = $this->sendMail($_SESSION['postData']);
//
//		   	$this->db->query('COMMIT');
//			if ($ret == false) {
//				$_form->setElementError('', '・メールの送信に失敗しました。お手数ですがお電話にてご連絡頂くか、しばらく経ってから送信をお願いいたします。');
//			} else {
//				$this->_view->assign('message', $this->_data['bctt_send']);
//				//header('Location:'.BINGOTOPURL.'/index.php?gmode=3&p=fin&d='.$d.'&c='.$c.$_prev);
//				unset($_SESSION['capcheck']);
//				$gmode=3;
//				$_SESSION['gmode'] = $gmode;
//				if($_REQUEST['protocol']=='ssl'){
//					header('Location:'.WWW_SSL.'/index.php?p=fin&d='.$d.'&c='.$c.$_prev);
//				}else{
//					header('Location:'.WWW.'/index.php?p=fin&d='.$d.'&c='.$c.$_prev);
//				}
//			}
//		}else {
//			if ($gmode == 2) {
//				$_form->freeze();
//				//セッション切れ
//				if (!isset($_SESSION['postData'])) {
//					header('Location:'.BINGOTOPURL.'/index.php?d='.$d.'&c='.$c.$_prev);
//				}
//			}
//		}
//
//		$this->_view->assign('gmode', $gmode);
//		$_form->addElement('hidden','gmode', $gmode);
//		//$_form->addElement('hidden','pcode', $this -> _pageu_code);
//
//
//		if($gmode == 2) {
//			//確認画面でアップロードした元ファイル名前で表示される
//			$this->_view->assign('fileinfo', $_SESSION['fileinfo']);
//			//ウエブサイド場合には、リングで表示される
//			$this->_view->assign('webinfo', $_SESSION['webinfo']);
//		}
//
//		if($gmode == 3){
//			$this->_pageu_code = $_SESSION['pcode'];
//		}
//
//		//エラー時
//		if (!empty($_form->_errors)) {
//			$this->_view->assign('ERROR',$_form->_errors);
//			foreach($_form->_errors as $key => $val) {
//				if(preg_match("/.*(zipcode|tel|checkbox|time|timeto).*/",$key)){
//					$_form = $this->setAttr($_form,$key,array("class","err"));
//				}else{
//					$_form->updateElementAttr($key, array("class"=>"err"));
//				}
//			}
//		}
//
//		$renderer = new HTML_QuickForm_Renderer_ArraySmarty($this->_view);
//		$_form->accept($renderer);
//		$this->_view->assign('form_formg',$renderer->toArray());
//		//データがない場合はNULLを返す
//		if ($this->getUnitFormid() == null) {
//			return null;
//			//return true;
//		}
//		return $this->_data;
//		//return $data;
	}

	/**
	 * QuickFormのグループ要素への属性追加用
	 *@param object $form QFオブジェクト
	 *@param string $element 要素名
	 *@param array  $param 追加する属性
	 *@return $form QFオブジェクト
	 */
	function setAttr($form,$element,$param){
		$ele = $form->_elements;
		foreach($ele as $k => $v){
			if(isset($v->_name) && $v->_name == $element){
				foreach($v->_elements as $k2 => $v2){
					$v2->_attributes[$param[0]] = $param[1];
				}
			}
		}
		return $form;
	}

	/**
	* お問い合わせフォームの設定
	*
	* @author Nobuo Nishino
	* @param object $form QFオブジェクト
	*/
	function viewForm (&$form)
	{
		$form->applyFilter('__ALL__','trim');
		$form->setJsWarnings('下記の項目が不正です','');
		if ($this->getUnitFormid() != null) {
			$frm_id = $this->getUnitFormid();
		}else if($_SESSION['frm_id']){
			$frm_id = $_SESSION['frm_id'];
		}
		$_SESSION['frm_id'] = $frm_id;

		$data   = CLpartcommon::getPartData($frm_id);
		$selectData = CLpartcommon::getPartSelectValue($frm_id);
		$js = CLpartcommon::setJs($data);
		CLpartcommon::setPartElements($data, $selectData, &$form);
		if(!empty($count)){
			$this -> _view -> assign('count',$count);
		}
		$_SESSION['data'] = $data;
		$this->_view->assign('SelectValue', $selectData);
		$this->_view->assign('tplData', $data);
		$this->_view->assign('jsdatetime', $js);
	}

	/**
	* メール送信
	*
	* @author Nobuo Nishino
	* @param array $data 送信データ
	*/
	function sendMail($postData)
	{
		require_once (LIB_PATH.'/common/commonMail.php');
		$frm_id = $_SESSION['frm_id'];
		$SelectValue = CLpartcommon::getPartSelectValue($frm_id);
		//$SelectValue = CLpartcommon::getPartSelectValue($this->getUnitFormid());
		$formData = $this->getFormDetail();
		$to_mail = explode(',', $formData['frm_mail_address']);

		if (!is_array($to_mail) && $to_mail!=null) {
			$to_mail = array($to_mail);
		}
		if (is_array($to_mail)) {
			$data = $_SESSION["data"];
			//$data = CLpartcommon::getPartData($this->getUnitFormid());
			//ヘッダー
			$msg = $formData['frm_mail_header'].'\n\n';

			for ($i=0; $i<count($data); $i++) {
				$idet_part_value = "";
				//ラベルのタグを除去
				$label = strip_tags($data[$i]['prt_part_label']);
				if ($data[$i]['prt_part_sub_type'] == 'single') {
					//single
					//$idet_part_value = '['.$data[$i]['prt_part_label'].']\n'.$postData['single'.$data[$i]['prt_part_id']].'\n';
					$idet_part_value = '['.$label.']\n'.$postData['single'.$data[$i]['prt_part_id']].'\n';
				} else if ($data[$i]['prt_part_sub_type'] == 'multiply') {
					//multiply
					//$idet_part_value = '['.$data[$i]['prt_part_label'].']\n'.ereg_replace(UNITS_REPLACE_FLG, UNITS_REPLACE_FLG, $postData['multiply'.$data[$i]['prt_part_id']]).'\n';
					$idet_part_value = '['.$label.']\n'.ereg_replace(UNITS_REPLACE_FLG, UNITS_REPLACE_FLG, $postData['multiply'.$data[$i]['prt_part_id']]).'\n';
				} else if ($data[$i]['prt_part_sub_type'] == 'radiobutton') {
					//radiobutton
					//$idet_part_value = '['.$data[$i]['prt_part_label'].']\n'.$postData['radio'.$data[$i]['prt_part_id']].'\n';
					$idet_part_value = '['.$label.']\n'.$postData['radio'.$data[$i]['prt_part_id']].'\n';
				} else if ($data[$i]['prt_part_sub_type'] == 'checkbox') {
					//checkbox
					//チェックボックスの選択状態
					$Checkvalue = '';
					$CheckSelectFlg = $postData['checkbox'.$data[$i]['prt_part_id']];
					//チェックボックスのラベル
					//$idet_part_value .='['.$data[$i]['prt_part_label'].']\n';
					$idet_part_value .='['.$label.']\n';
					for($j=0; $j<count($SelectValue); $j++) {
						if($SelectValue[$j]['prt_part_id'] == $data[$i]['prt_part_id']) {
							$select = explode("%%%",$SelectValue[$j]["prt_part_select_value"]);
							$replaceflg=0;
							foreach($select as $k => $v){
								if($postData["checkbox".$SelectValue[$j]['prt_part_id']]["checkbox".$k]==1){
									if($replaceflg > 0){
										$idet_part_value .= ",";
									}
									$idet_part_value .= $v;
									$replaceflg = 1;
								}
							}
						}
					}
					$idet_part_value .='\n';
					/*
					for($j=0; $j<count($SelectValue); $j++) {
						if ($SelectValue[$j]['prt_part_id'] == $data[$i]['prt_part_id']) {
							for ($a=0; $a<count($SelectValue[$j])-4; $a++) {
								if($CheckSelectFlg['checkbox'.$a] == 1){
									$Checkvalue .= $SelectValue[$j][$a];
									if($a < count($SelectValue[$j])-5){
										$Checkvalue .= ', ';
									}
								}
							}
						}
					}
					*/
				} else if ($data[$i]['prt_part_sub_type'] == 'dropdown') {
					//dropdown
					//$idet_part_value = '['.$data[$i]['prt_part_label'].']\n'.$postData['dropdown'.$data[$i]['prt_part_id']].'\n';
					$idet_part_value = '['.$label.']\n'.$postData['dropdown'.$data[$i]['prt_part_id']].'\n';
				} else if ($data[$i]['prt_part_sub_type'] == 'nametogether') {
					//nametogether
					//$idet_part_value = '['.$data[$i]['prt_part_label'].']\n';
					$idet_part_value = '['.$label.']\n';
					$name_value0 = $postData['nametogether'.$data[$i]['prt_part_id']];
					$name_value1 = $postData['nametogether_kana'.$data[$i]['prt_part_id']];

					if(isset($name_value0) && $name_value0 != ''){
						$idet_part_value .=$name_value0.'\n';
					}
					if(isset($name_value1) && $name_value1 != ''){
						$idet_part_value .= "\n[フリガナ]\n" . $name_value1."\n";
					}
				} else if ($data[$i]['prt_part_sub_type'] == 'nameseparate') {
					//nameseparate
					//$idet_part_value = '['.$data[$i]['prt_part_label'].']\n';
					$idet_part_value = '['.$label.']\n';
					$name[] = $postData['nameseparate1'.$data[$i]['prt_part_id']];
					$name[]= $postData['nameseparate2'.$data[$i]['prt_part_id']];
					$name_kana[] = $postData['nameseparate_kana1'.$data[$i]['prt_part_id']];
					$name_kana[] = $postData['nameseparate_kana2'.$data[$i]['prt_part_id']];
					if(!empty($name[0]) || !empty($name[1])){
						$idet_part_value .= $name[0]." ".$name[1]."\n";
					}

					if(!empty($name_kana[0]) || !empty($name_kana[1])){
						$idet_part_value .= "\n[フリガナ]\n";
						$idet_part_value .= $name_kana[0]." ".$name_kana[1]."\n";
					}
					unset($name);
					unset($name_kana);
				} else if ($data[$i]['prt_part_sub_type'] == 'addresstogether') {
					//addresstogether
					//$idet_part_value =  '['.$data[$i]['prt_part_label'].']\n';
					$idet_part_value =  '['.$label.']\n';
					$address_value0 = $postData['zipcode'.$data[$i]['prt_part_id']]['zip1']."-".$postData['zipcode'.$data[$i]['prt_part_id']]['zip2'];
					$address_value1 = $postData['addresstogether'.$data[$i]['prt_part_id']];

					if (isset($address_value0) && $address_value0 != '-' && $address_value0 != '') {
						$idet_part_value .= "〒" . $address_value0.'\n';
					}
					if (isset($address_value1) && $address_value1 != '') {
						if(isset($address_value0) && $address_value0 != ''){
							$idet_part_value .= "" ;
						}
						$idet_part_value .= $address_value1.'\n';
					}
				} else if ($data[$i]['prt_part_sub_type'] == 'addressseparate') {
					//$idet_part_value = '['.$data[$i]['prt_part_label'].']\n';
					$idet_part_value = '['.$label.']\n';
					$address_value0 = $postData['zipcode'.$data[$i]['prt_part_id']]['zip1']."-".$postData['zipcode'.$data[$i]['prt_part_id']]['zip2'];
					$address_value1 = $postData['smmbr_pref'.$data[$i]['prt_part_id']];
					$address_value2 = $postData['smmbr_address2'.$data[$i]['prt_part_id']];
					$address_value3	= $postData['smmbr_address3'.$data[$i]['prt_part_id']];
					$address_value4	= $postData['smmbr_address4'.$data[$i]['prt_part_id']];

					if (isset($address_value0) && $address_value0 != '-' && $address_value0 != '') {
						$idet_part_value .= "〒" . $address_value0.'\n';
					}
					if (isset($address_value1) && $address_value1 != '') {
						if(isset($address_value0) && $address_value0 != ''){
							$idet_part_value .= "" ;
						}
						$idet_part_value .= $address_value1;
					}
					if (isset($address_value2) && $address_value2 != '') {
						if(isset($address_value0) && $address_value0 != ''
						|| isset($address_value1) && $address_value1 != ''){
							$idet_part_value .= "";
						}
						$idet_part_value .= $address_value2;
					}
					if (isset($address_value3) && $address_value3 != '') {
						if(isset($address_value0) && $address_value0 != ''
						|| isset($address_value1) && $address_value1 != ''
						|| isset($address_value2) && $address_value2 != ''){
							$idet_part_value .= "";
						}
						$idet_part_value .= $address_value3;
					}
					if (isset($address_value4) && $address_value4 != '') {
						if(isset($address_value0) && $address_value0 != ''
						|| isset($address_value1) && $address_value1 != ''
						|| isset($address_value2) && $address_value2 != ''
						|| isset($address_value3) && $address_value3 != ''){
							$idet_part_value .= "";
						}
						$idet_part_value .= $address_value4;
					}
					$idet_part_value .= '\n';
				} else if ($data[$i]['prt_part_sub_type'] == 'date') {
					//date
					//$idet_part_value =  '['.$data[$i]['prt_part_label'].']\n'.$postData['date'.$data[$i]['prt_part_id']];
					$idet_part_value =  '['.$label.']\n'.$postData['date'.$data[$i]['prt_part_id']];
					//「FROM～TO」フラグが１場合
					if ($data[$i]['prt_part_datetime_fromtoflg'] == "1"){
						$idet_part_value .= '　～　' . $postData['dateto'.$data[$i]['prt_part_id']];
					}
					$idet_part_value .= '\n';
				} else if ($data[$i]['prt_part_sub_type'] == 'time') {
					//time
					//$idet_part_value =  '['.$data[$i]['prt_part_label'].']\n';
					$idet_part_value =  '['.$label.']\n';

					$hour = $postData['time'.$data[$i]['prt_part_id']]['hour'];
					if($hour != ''){
						$idet_part_value .= $hour.'時';
					}
					$minute = $postData['time'.$data[$i]['prt_part_id']]['minute'];
					if($minute != ''){
						$idet_part_value .= ($minute == "0" ? "00分" : $minute."分");
					}
					//「FROM～TO」フラグが１場合
					if ($data[$i]['prt_part_datetime_fromtoflg'] == "1"){
						//$idet_part_value .= '　～　' . $postData['hourto'.$data[$i]['prt_part_id']].'時';

						$hourto = $postData['timeto'.$data[$i]['prt_part_id']]['hour'];
						$minuteto = $postData['timeto'.$data[$i]['prt_part_id']]['minute'];

						if($hourto != ''){
							$idet_part_value .= '　～　' . $hourto.'時';
						}
						if($minuteto != ''){
							$idet_part_value .= ($minuteto == "0" ? "00分" : $minuteto."分");
						}
					}
					$idet_part_value .='\n';
				} else if ($data[$i]['prt_part_sub_type'] == 'datetime') {
					//datetime
					//$idet_part_value =  '['.$data[$i]['prt_part_label'].']\n';
					$idet_part_value =  '['.$label.']\n';
					$datetime = $postData['datetime'.$data[$i]['prt_part_id']];
					$hour     = $postData['time'.$data[$i]['prt_part_id']]['hour'];
					$minute   = $postData['time'.$data[$i]['prt_part_id']]['minute'];

					if($datetime != ''){
						$idet_part_value .= $datetime;
					}
					if($hour != ''){
						$idet_part_value .= '　' . $hour.'時';
					}
					if($minute != ''){
						$idet_part_value .= ($minute == "0" ? "00分" : $minute."分");
					}

					//「FROM～TO」フラグが１場合
					if ($data[$i]['prt_part_datetime_fromtoflg'] == "1"){
					   	/*
						$idet_part_value .= '　～　' . $postData['datetimeto'.$data[$i]['prt_part_id']].'　'
					   				   		. $postData['hourto'.$data[$i]['prt_part_id']].'時';
						*/
						$datetimeto = $postData['datetimeto'.$data[$i]['prt_part_id']];
						$hourto     = $postData['timeto'.$data[$i]['prt_part_id']]['hour'];
						$minuteto   = $postData['timeto'.$data[$i]['prt_part_id']]['minute'];

						if($datetimeto != ''){
							$idet_part_value .= '　～　' . $datetimeto;
						}
						if($hourto != ''){
							$idet_part_value .= '　' . $hourto.'時';
						}
						if($minuteto != ''){
							$idet_part_value .= ($minuteto == "0" ? "00分" : $minuteto."分");
						}
					}
					$idet_part_value .='\n';
				} else if ($data[$i]['prt_part_sub_type'] == 'onepart') {
					//onepart
					//$idet_part_value =  '['.$data[$i]['prt_part_label'].']\n'.$postData['onepart'.$data[$i]['prt_part_id']].'\n';
					$idet_part_value =  '['.$label.']\n'.$postData['onepart'.$data[$i]['prt_part_id']].'\n';
				} else if ($data[$i]['prt_part_sub_type'] == 'threepart') {
					//threepart

					//$idet_part_value = '['.$data[$i]['prt_part_label'].']\n';
					$idet_part_value = '['.$label.']\n';
					$telphone_value0 = $postData['tel'.$data[$i]['prt_part_id']][0];
					$telphone_value1 = $postData['tel'.$data[$i]['prt_part_id']][1];
					$telphone_value2 = $postData['tel'.$data[$i]['prt_part_id']][2];
					$idet_part_value .= $telphone_value0."-".$telphone_value1."-".$telphone_value2;
					$idet_part_value .='\n';
				} else if ($data[$i]['prt_part_type'] == 'web') {
					//web
					//$idet_part_value = '['.$data[$i]['prt_part_label'].']\n'.trim($postData['web'.$data[$i]['prt_part_id']].'\n');
					$idet_part_value = '['.$label.']\n'.trim($postData['web'.$data[$i]['prt_part_id']].'\n');
				} else if ($data[$i]['prt_part_type'] == 'mail') {
					//mail
					$idet_part_value = '['.$label. ']\n';
					if($postData['allmail'.$data[$i]['prt_part_id']] != ""){
						$idet_part_value .= 'Ｅメール：'.$postData['allmail'.$data[$i]['prt_part_id']]."\n";
					}
					if($postData['pcmail'.$data[$i]['prt_part_id']] != ""){
						$idet_part_value .= 'PCメール：'.$postData['pcmail'.$data[$i]['prt_part_id']]."\n";
					}
					if($postData['mobilemail'.$data[$i]['prt_part_id']] != ""){
						$idet_part_value .='携帯メール：'.$postData['mobilemail'.$data[$i]['prt_part_id']]."\n";
					}

					//送信者
					if($postData['allmail'.$data[$i]['prt_part_id']] != null){
						$from .= $postData['allmail'.$data[$i]['prt_part_id']].',';
					}else if($postData['pcmail'.$data[$i]['prt_part_id']] != null){
						$from .= $postData['pcmail'.$data[$i]['prt_part_id']].',';
					}else if($postData['mobilemail'.$data[$i]['prt_part_id']] != null){
						$from .= $postData['mobilemail'.$data[$i]['prt_part_id']].',';
					}
				} else if ($data[$i]['prt_part_type'] == 'fileupload') {
					//fileupload
					//アップロードしたファイルの名前を送信
					if(isset($_SESSION["fileinfo"]) && is_array($_SESSION["fileinfo"]) && count($_SESSION["fileinfo"])>0) {
						foreach($_SESSION['fileinfo'] as $key=>$val) {
							if($key == $data[$i]['prt_part_id']){
								//$idet_part_value .=  '['.$data[$i]['prt_part_label'].']\n'.$val['original_filename'].'\n';
								$idet_part_value .=  '['.$label.']\n'.$val['original_filename'].'\n';
								break;
							}
						}
					}
				}
				$idet_part_value = str_replace("'", "'", $idet_part_value);
				//改行で表示
				if($data[$i]['prt_part_type'] != 'guidetext' && $data[$i]['prt_part_type'] != 'line'){
					$msg .= $idet_part_value.'\n';
				}
			}
			//フッター
			$msg .= $formData['frm_mail_footer'].'\n';

			//送信者（メールが必須でない場合は・・・）
			$froms=explode(',', $from);
			$_from = ($from !=null) ? $froms[0] : ADMIN_MAIL;

			//フォーム管理者へメール送信 $val=管理者  $from=ユーザのアドレス or 管理人
			foreach ($to_mail as $val) {
				if ($val!=null) {
					$ret = commonMail::sendMail($val, $formData['frm_mail_subject'], $msg, $_from);
					if ($ret == false) {
						return false;
					}
				}
			}

			//入力アドレス(共用 or PC) へ送信
			$from_mail = explode(',', $from);
			foreach ($from_mail as $val) {
				if ($val != null && $val != ADMIN_MAIL ) {
					$ret = commonMail::sendMail($val, $formData['frm_mail_subject'], $msg, ADMIN_MAIL);
					if ($ret == false) {
						return false;
					}
				}
			}
			return true;
		} else {
			$this->db->query('ROLLBACK');
			return false;
		}
	}

	/*
	 * unit dataを登録
	 *
	 */
	function setUnitData($postData)
	{
		$ihead_id = null;
//		$frm_id = $this->getUnitFormid();
		$frm_id = $_SESSION["frm_id"];
		$data = CLpartcommon::getPartData($frm_id);
		$SelectValue = CLpartcommon::getPartSelectValue($frm_id);
		//$this->db->query('BEGIN');

		//tbl_inquiry_headerにデータを登録
		$sql = "insert into mdl_extMdlFormGnrtr_tbl_inquiry_header(ihea_frm_id,
											   ihea_mail_address,
											   ihea_request_date,
											   ihea_responseflg,
											   ihea_c_date,
											   ihea_u_date)
									    values(".$frm_id.",
											   '$mail_part_value',
											   now(),
											   0,
											   now(),
											   now())";

		if($this->db->query($sql)) {
			//ihea_idを取得
//			$sql = "select max(ihea_id) as ihea_id_new
//			        from   mdl_extMdlFormGnrtr_tbl_inquiry_header";
//			$ihead_id = $this->db->getOne($sql);
			$ihead_id = $this->currval("mdl_extmdlformgnrtr_tbl_inquiry_header","ihea_id");
		} else {
			$this->db->query('ROLLBACK');
			return false;
		}

		//tbl_inquiry_detailにデータを登録
		for ($i=0; $i<count($data); $i++) {
			$part_id = $data[$i]['prt_part_id'];
			$part_value = null;

			if ($data[$i]['prt_part_sub_type'] == 'single') {
				//single
				$part_value = $postData['single'.$data[$i]['prt_part_id']];
			} else if ($data[$i]['prt_part_sub_type'] == 'multiply') {
				//multiply
				$part_value = ereg_replace(UNITS_ENTER_FLG, UNITS_REPLACE_FLG, $postData['multiply'.$data[$i]['prt_part_id']]);
			} else if ($data[$i]['prt_part_sub_type'] == 'radiobutton') {
				//radiobutton
				$part_value = $postData['radio'.$data[$i]['prt_part_id']];
			} else if ($data[$i]['prt_part_sub_type'] == 'checkbox') {
				//checkbox
				//チェックボックスの選択状態
				$CheckSelectFlg = $postData['checkbox'.$data[$i]['prt_part_id']];
				//チェックボックスのラベル
				for($j=0; $j<count($SelectValue); $j++) {
					if($SelectValue[$j]['prt_part_id'] == $data[$i]['prt_part_id']) {
						$select = explode("%%%",$SelectValue[$j]["prt_part_select_value"]);
						$replaceflg=0;
						foreach($select as $k => $v){
							if($postData["checkbox".$SelectValue[$j]['prt_part_id']]["checkbox".$k]==1){
								if($replaceflg > 0){
									$part_value .= UNITS_REPLACE_FLG;
								}
								$part_value .= $v;
								$replaceflg = 1;
							}
						}
					}
				}
				/*
				for($j=0; $j<count($SelectValue); $j++) {
					if ($SelectValue[$j]['prt_15:15 2009/05/08part_id'] == $data[$i]['prt_part_id']) {
						for ($a=0; $a<count($SelectValue[$j])-4; $a++) {
							if($CheckSelectFlg['checkbox'.$a] == 1){
								$part_value .= $SelectValue[$j][$a];
								if($a < count($SelectValue[$j])-5){
									$part_value .= UNITS_REPLACE_FLG;
								}
							}
						}
					}
				}
				*/
			} else if ($data[$i]['prt_part_sub_type'] == 'dropdown') {
				//dropdown
				$part_value = $postData['dropdown'.$data[$i]['prt_part_id']];
			} else if ($data[$i]['prt_part_sub_type'] == 'nametogether') {
			//名前のデータは「姓(同一の時は姓名)%%%名%%%カナ姓(同一の時は姓名)%%%カナ姓」の形式に統一する 2010.12.24
				//nametogether
				$part_value = h($postData['nametogether'.$data[$i]['prt_part_id']]).
							  UNITS_REPLACE_FLG."".
							  UNITS_REPLACE_FLG.h($postData['nametogether_kana'.$data[$i]['prt_part_id']].
							  UNITS_REPLACE_FLG."");
			} else if ($data[$i]['prt_part_sub_type'] == 'nameseparate') {
				//nameseparate
				$part_value = h($postData['nameseparate1'.$data[$i]['prt_part_id']]).UNITS_REPLACE_FLG.
							  h($postData['nameseparate2'.$data[$i]['prt_part_id']]).UNITS_REPLACE_FLG.
							  h($postData['nameseparate_kana1'.$data[$i]['prt_part_id']]).UNITS_REPLACE_FLG.
							  h($postData['nameseparate_kana2'.$data[$i]['prt_part_id']]);
			} else if ($data[$i]['prt_part_sub_type'] == 'addresstogether') {
				//addresstogether
				$part_value = $postData['zipcode'.$data[$i]['prt_part_id']]['zip1']."-".
							  $postData['zipcode'.$data[$i]['prt_part_id']]['zip2'].UNITS_REPLACE_FLG.
							  h($postData['addresstogether'.$data[$i]['prt_part_id']]);

			} else if ($data[$i]['prt_part_sub_type'] == 'addressseparate') {
				$part_value = $postData['zipcode'.$data[$i]['prt_part_id']]['zip1']."-".
							  $postData['zipcode'.$data[$i]['prt_part_id']]['zip2'].UNITS_REPLACE_FLG.
							  $postData['smmbr_pref'.$data[$i]['prt_part_id']].UNITS_REPLACE_FLG.
							  h($postData['smmbr_address2'.$data[$i]['prt_part_id']]).UNITS_REPLACE_FLG.
							  h($postData['smmbr_address3'.$data[$i]['prt_part_id']]).UNITS_REPLACE_FLG.
							  h($postData['smmbr_address4'.$data[$i]['prt_part_id']]);
			} else if ($data[$i]['prt_part_sub_type'] == 'date') {
			//日付/時間パーツのデータは「日%%%時%%%分%%%日（to)%%%時（to)%%%分（to)」の形式に統一する 2010.12.24
			//日付のみ
				$part_value = $postData['date'.$data[$i]['prt_part_id']];
				$part_value.= UNITS_REPLACE_FLG."";//時
				$part_value.= UNITS_REPLACE_FLG."";//分
				$part_value.= UNITS_REPLACE_FLG.$postData['dateto'.$data[$i]['prt_part_id']];//日（to)
				$part_value.= UNITS_REPLACE_FLG."";//時（to)
				$part_value.= UNITS_REPLACE_FLG."";//分（to)
			} else if ($data[$i]['prt_part_sub_type'] == 'time') {
			//時間のみ
				//time
				$part_value = "";
				$part_value = UNITS_REPLACE_FLG.$postData['time'.$data[$i]['prt_part_id']]['hour'];
				$part_value .= UNITS_REPLACE_FLG.$postData['time'.$data[$i]['prt_part_id']]['minute'];
				//「FROM～TO」フラグが１場合
				if ($data[$i]['prt_part_datetime_fromtoflg'] == "1"){
					$part_value .= UNITS_REPLACE_FLG."";
					$part_value .= UNITS_REPLACE_FLG.$postData['timeto'.$data[$i]['prt_part_id']]['hour'];
					$part_value .= UNITS_REPLACE_FLG.$postData['timeto'.$data[$i]['prt_part_id']]['minute'];
				}
			} else if ($data[$i]['prt_part_sub_type'] == 'datetime') {
			//日付＋時間
				$part_value = $postData['datetime'.$data[$i]['prt_part_id']];
				$part_value.= UNITS_REPLACE_FLG.$postData['time'.$data[$i]['prt_part_id']]['hour'];
				$part_value.= UNITS_REPLACE_FLG.$postData['time'.$data[$i]['prt_part_id']]['minute'];
				//「FROM～TO」フラグが１場合
				if ($data[$i]['prt_part_datetime_fromtoflg'] == "1"){
					$part_value .= UNITS_REPLACE_FLG.$postData['datetimeto'.$data[$i]['prt_part_id']].
								   UNITS_REPLACE_FLG.$postData['timeto'.$data[$i]['prt_part_id']]['hour'];
					$part_value .= UNITS_REPLACE_FLG.$postData['timeto'.$data[$i]['prt_part_id']]['minute'];
				}
			} else if ($data[$i]['prt_part_sub_type'] == 'onepart') {
				//onepart
				$part_value = $postData['onepart'.$data[$i]['prt_part_id']];
			} else if ($data[$i]['prt_part_sub_type'] == 'threepart') {
				//threepart
				$part_value = $postData['tel'.$data[$i]['prt_part_id']][0].UNITS_REPLACE_FLG.
							  $postData['tel'.$data[$i]['prt_part_id']][1].UNITS_REPLACE_FLG.
							  $postData['tel'.$data[$i]['prt_part_id']][2];
			} else if ($data[$i]['prt_part_type'] == 'guidetext') {
				//guidetext
				$part_value = $postData['hiddenguidetext'.$data[$i]['prt_part_id']];
			} else if ($data[$i]['prt_part_type'] == 'web') {
				//web
				$part_value = $postData['web'.$data[$i]['prt_part_id']];
			} else if ($data[$i]['prt_part_type'] == 'mail') {
				//mail
				$part_value = $postData['allmail'.$data[$i]['prt_part_id']].UNITS_REPLACE_FLG.
							  $postData['pcmail'.$data[$i]['prt_part_id']].UNITS_REPLACE_FLG.
							  $postData['mobilemail'.$data[$i]['prt_part_id']];

				if ($mail_part_value == null) {
					$mail_part_value = $part_value;
				} else {
					$mail_part_value = $mail_part_value.UNITS_REPLACE_FLG.$part_value;
				}
			} else if ($data[$i]['prt_part_type'] == 'fileupload') {
				//アップロードファイルを保存
				if(isset($_SESSION["fileinfo"]) && is_array($_SESSION["fileinfo"]) && count($_SESSION["fileinfo"])>0) {
					foreach($_SESSION['fileinfo'] as $key=>$val) {
						if($key == $data[$i]['prt_part_id']){
							$part_value = $val['original_filename'].UNITS_REPLACE_FLG.
							              $val['upload_filename'];

							$upfile_path = fileUpload::getFilePath($val['upload_filename']);

							if(isset($upfile_path) && is_array($upfile_path) && count($upfile_path)>0) {
								$rtn = fileUpload::copyImage(UNITS_DOCUMENT_ROOT_PATH.$upfile_path['tmp_path'],$val['upload_filename'],UNITS_DOCUMENT_ROOT_PATH.$upfile_path['real_path']);
								//ファイル名前(拡張子付)によって、ファイルアップロードアドレスを取る時、エラーがある場合
								if (!$rtn) {
									$this->db->query('ROLLBACK');
									return false;
									break;
								}
							}else{
								//元ファイルフォルダー、先ファイルフォルダーが無し場合
								$this->db->query('ROLLBACK');
								return false;
								break;
							}
							break;
						}
					}
				}
			}


			$insertParam = array(
				'idet_ihea_id'=>$ihead_id,
				'idet_frm_id'=>$frm_id,
				'idet_part_id'=>$part_id,
				'idet_part_value'=>$part_value
			);

			if (!$this->db->insert('mdl_extMdlFormGnrtr_tbl_inquiry_detail', $insertParam)) {
				$this->db->query('ROLLBACK');
				return false;
			}
			/*
			$sql_esc=array(
				'\''=>'\'\'',
				'\"'=>'\"\"',
				'\\'=>'\\\\'
			);

			$ihead_id = strtr($ihead_id, $sql_esc);
			$frm_id = strtr($frm_id, $sql_esc);
			$part_id = strtr($part_id, $sql_esc);
			$part_value = strtr($part_value, $sql_esc);

			$sql = "insert into mdl_extMdlFormGnrtr_tbl_inquiry_detail
					values(".$ihead_id.",
						   ".$frm_id.",
						   ".$part_id.",
						   '".$part_value."',
						   now(),
						   now())";

			if (!$this->db->query($sql)) {
				$this->db->query('ROLLBACK');
				return false;
			}
			*/
		}

		$updateParam = array(
				'ihea_mail_address'=>$mail_part_value
		);

		$sql_esc=array(
			'\''=>'\'\'',
			'\"'=>'\"\"',
			'\\'=>'\\\\'
		);
		$ihead_id = strtr($ihead_id, $sql_esc);
		$frm_id = strtr($frm_id, $sql_esc);
		$where = 'ihea_id='.$ihead_id.' AND ihea_frm_id='.$frm_id;

		if (!$this->db->update('mdl_extMdlFormGnrtr_tbl_inquiry_header', $updateParam, $where)) {
			$this->db->query('ROLLBACK');
			return false;
		}

		//tbl_inquiry_headerのにデータを登録
		/*
		$sql = "update  mdl_extMdlFormGnrtr_tbl_inquiry_header
		        set     ihea_mail_address = '".$mail_part_value."'
		        where   ihea_id = ".$ihead_id."
					and ihea_frm_id	= ".$frm_id;

		if (!$this->db->query($sql)) {
			$this->db->query('ROLLBACK');
			return false;
		}
		*/

		return true;

	}

	//unitのフォームIDを取得
	function getUnitFormid()
	{
		$data = array();
		$sql = "select ufrm_frm_id from ".$this->getTbl()." where pageu_code = ".$this ->_pageu_code;
		$data = $this->db->getOne($sql);
		return $data;
	}

	/*
	 * DBより保存データ取得
	 * 改変可能
	 */
	function getDetail() {
		$rtn =  null;
		$sql = "select * from ".$this->getTbl()." where pageu_code = ?";

		if(isset($this->_pageu_code)&&is_numeric($this->_pageu_code)) {
			$result = $this->db->prepare($sql,$this->_pageu_code);
			if($result){
				$rtn = $result->fetchRow(DB_FETCHMODE_ASSOC);
			}
		}
		return $rtn;
	}

	//フォームIDを取得
	function getFormid()
	{
		$data = array();

		$sql = "select   frm_id,
						 frm_name
		        from     mdl_extMdlFormGnrtr_tbl_form
		        where    frm_active = 1
		        	and  frm_actflg = 1
		        order by frm_defaultflg DESC, frm_order, frm_name";

		$result = $this->db->query($sql);

		if($result != null) {
			//データ取得
			while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
				if (isset($row) && is_array($row) && count($row) > 0) {
					$data = $data + array($row['frm_id'] => $row['frm_name']);
				}
			}
		}
		return $data;
	}

	//フォーム情報を取得
	function getFormDetail()
	{
		$data = array();
		$frm_id= $_SESSION['frm_id'];
		/*
		$sql = "select   frm_id,
						 frm_name,
						 frm_mail_subject,
						 frm_mail_address,
						 frm_mail_header,
						 frm_mail_footer,
						 frm_mail_finishing
		        from     mdl_extMdlFormGnrtr_tbl_form
		        where    frm_id = ".$frm_id."
		        	and  frm_active = 1";

		$result = $this->db->query($sql);
		*/
		$sql = "select   frm_id,
						 frm_name,
						 frm_mail_subject,
						 frm_mail_address,
						 frm_mail_header,
						 frm_mail_footer,
						 frm_mail_finishing
		        from     mdl_extMdlFormGnrtr_tbl_form
		        where    frm_id = ?
		        	and  frm_active = 1";

		$result = $this->db->prepare($sql, array($frm_id));

		if($result != null) {
			//データ取得
			while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
				if (isset($row) && is_array($row) && count($row) > 0) {
					$data = $row;
				}
			}
		}
		return $data;
	}

	//データ下書き保存処理
	function doSave($post)
	{
		$record = null;
		foreach($this->columns as $val) {
			if($post[$val["column"]]!=null){
				$record[$val["column"]] = $post[$val["column"]];
			} else {
				$record[$val["column"]] = null;
			}
		}

		//既存データがあるか確認
		$old_data = $this->getDetail();

		$this->db->query('BEGIN');
		//保存処理開始
		if(is_array($old_data)&& count($old_data)>0) {
			//更新
			if(!$this->db->update($this->getTbl(),$record, "pageu_code=".$record["pageu_code"])) {
				$this->db->query('ROLLBACK');
				return "・データの保存に失敗しました";
			}
		} else { //新規
			if(!$this->db->insert($this->getTbl(),$record)) {
				$this->db->query('ROLLBACK');
				return "・データの保存に失敗しました";
			}
		}

		$this->db->query('COMMIT');
		return null;
	}

	function setDefault()
	{
		$data = $this->getDetail();
		$this->_form->setDefaults($data);
	}

	//フォームエレメントの作成
	function setElements()
	{
		//フォーム名
		$formid = $this->getFormid();
		$this->_form->addElement('select','ufrm_frm_id', null, $formid);
		$this->_form->addElement('hidden','pageu_code', $this->_pageu_code,array("id"=>"pageu_code")); //ページコード
	}

	//フォーム入力エラーチェックの設定
	function setRules()
	{
		$this->_form->applyFilter('__ALL__','trim');
		$this->_form->_submitValues = DataBase::ConvColumnVals($this->_form->_submitValues,$this->columns); //ルールに従った値変換、タグの除去
		$this->_form->setJsWarnings('下記の項目が不正です','');
		$this->_form->addRule('pageu_code','・「ユニットコード」が不明です。コンテンツ管理よりやり直してください','required',null,$this->erchk);
		$this->_form->addRule('ufrm_frm_id','・「フォーム選択」を選択してください。','required',null,$this->erchk);
	}

	//ファイルを保存
	function uploadFileTotmpdir($data,&$form){
		//アップロードしたファイル名（拡張子付）
		$rtnFileName = array();
		for ($i=0; $i<count($data); $i++) {
		 	if ($data[$i]['prt_part_type'] == 'fileupload') {
				//fileupload
				$part_value = $_FILES['file'.$data[$i]['prt_part_id']]["name"];
				$upfile_path = fileUpload::getFilePath($part_value);
				if(isset($upfile_path) && is_array($upfile_path) && count($upfile_path)>0) {
					if (is_uploaded_file($_FILES['file'.$data[$i]['prt_part_id']]["tmp_name"])){
						//ファイル名
						$upfile_convert_name = date("YmdHis").uniqid(rand());
						//ファイル保存
						$upfile_tmp_path = BASE_PATH . $upfile_path['tmp_path'];
						$upfile = $form->getElement('file'.$data[$i]['prt_part_id']);
						$rtn = fileUpload::uploadImage($upfile, $upfile_tmp_path, $upfile_convert_name);
						if (!$rtn) {
							$form->setElementError("","・ファイルの保存に失敗しました");
							break;
						}
						//アップロードしたファイル名（拡張子付）
						$rtnFileName[$data[$i]['prt_part_id']]['upload_filename'] = $rtn;
						//最初ファイル名（拡張子付）
						$rtnFileName[$data[$i]['prt_part_id']]['original_filename'] = $part_value;

						//画像プレビュー用に追加 aminaka===============
						$rtnFileName[$data[$i]['prt_part_id']]['isImage'] = 0;
						$ext = array_pop(explode(".", $part_value));
						if(strtolower($ext)=='jpg' || strtolower($ext)=='jpeg' || strtolower($ext)=='gif'){
							$rtnFileName[$data[$i]['prt_part_id']]['isImage'] = 1;
							$rtnFileName[$data[$i]['prt_part_id']]['size'] = 320;
							$info = getimagesize($upfile_tmp_path."/".$rtn);
							if($info[0] < $rtnFileName[$data[$i]['prt_part_id']]['size']){
								$rtnFileName[$data[$i]['prt_part_id']]['size'] = $info[0];
							}
						}
						//▲=====================================
					}
				}
		 	}
		}
		return $rtnFileName;
	}

	//Eメールの二重登録制御のチェック
	function mail_register_check($data,$postData){
		$mail_flg = true;
		if($data == null || $postData == null){
			return true;
		}
		for ($i=0; $i<count($data); $i++) {
			if ($data[$i]['prt_part_type'] == 'mail' && $data[$i]['prt_part_mail_registerflg'] != '1') {
				//mail
				$allmail_address = $postData['allmail'.$data[$i]['prt_part_id']];
				$pcmail_address = $postData['pcmail'.$data[$i]['prt_part_id']];
				$mobilemail_address = $postData['mobilemail'.$data[$i]['prt_part_id']];

				if($allmail_address != null || $pcmail_address != null || $mobilemail_address != null){
					$sql = "select count(*) as cnt  from   mdl_extMdlFormGnrtr_tbl_inquiry_detail where 1 = 1 ";

					$whrdata = null;
					$whrsql = null;

					if ($allmail_address != null) {
						$whrdata[] = "%{$allmail_address}%";
						$whrsql[] = "idet_part_value LIKE ?";
					}

					if ($pcmail_address != null) {
						$whrdata[] = "%{$pcmail_address}%";
						$whrsql[] = "idet_part_value LIKE ?";
					}

					if ($mobilemail_address != null) {
						$whrdata[] = "%{$mobilemail_address}%";
						$whrsql[] = "idet_part_value LIKE ?";
					}

					$sql .= " and (".implode(" or ", $whrsql).") ";

					if(count($whrdata) > 0) {
						$result = $this->db->prepare($sql,$whrdata);
					} else {
						$result = $this->db->query($sql);
					}
					$rtn=null;
					if($result != null) {
						//データ取得
						$data = array();
						if ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
							if(isset($row) && is_array($row) && count($row) > 0) {
								if(isset($row["cnt"])){
								 	$cnt = $row["cnt"];
									if($cnt > 0){
										$mail_flg = false;
										break;
									}
								}
							}
						}
					}//$result
				}
			}
		}//for
		return $mail_flg;
	}

	//ファイルアップロード拡張子のチェック
	function fileload_filetype_check($data,$fileinfo){
		$file_check_msg = "";
		if($data == null || $fileinfo == null){
			return false;
		}

		for($i=0; $i<count($data); $i++) {
			if ($data[$i]['prt_part_type'] == 'fileupload') {
			    //毎回チェックフラグがリセット
				$file_flag = false;

				$upfile_name = explode(".", $fileinfo[$data[$i]['prt_part_id']]['original_filename']);

				//ファイル拡張子制御がある場合
				if ($data[$i]['prt_part_fileupload_limitflg'] == 1) {
					//ファイルアップロードの値がある場合
					if (isset($upfile_name[0]) && $upfile_name[0] != null) {
						if($data[$i]['prt_part_fileupload_filetype1'] == '1'){
							if (isset($upfile_name[1]) && strtolower($upfile_name[1]) == "pdf") {
								$file_flag = true;
							}
						}//pdf

						if($data[$i]['prt_part_fileupload_filetype2'] == '1'){
							if (isset($upfile_name[1]) && strtolower($upfile_name[1]) == "xls") {
								$file_flag = true;
							}
						}//xls

						if($data[$i]['prt_part_fileupload_filetype3'] == '1'){
							if (isset($upfile_name[1]) && strtolower($upfile_name[1]) == "doc") {
								$file_flag = true;
							}
						}//doc

						if($data[$i]['prt_part_fileupload_filetype4'] == '1'){
						//	if (isset($upfile_name[1]) && $upfile_name[1] == "jpeg") {
							if (isset($upfile_name[1]) && strtolower($upfile_name[1]) == "jpg") {
								$file_flag = true;
							}
						}//jpeg

						if($data[$i]['prt_part_fileupload_filetype5'] == '1'){
						//	if (isset($upfile_name[1]) && $upfile_name[1] == "jpeg") {
							if (isset($upfile_name[1]) && strtolower($upfile_name[1]) == "gif") {
								$file_flag = true;
							}
						}//jpeg

						if($data[$i]['prt_part_fileupload_filetype6'] == '1'){
							if (isset($upfile_name[1]) && strtolower($upfile_name[1]) == "png") {
								$file_flag = true;
							}
						}//png

						if (isset($data[$i]['prt_part_fileupload_subfiletype'])) {
							$sub_type = explode(UNITS_REPLACE_FLG, $data[$i]['prt_part_fileupload_subfiletype']);
							foreach ($sub_type as $key => $val) {
								if (isset($upfile_name[1]) && $upfile_name[1] == $val) {
									$file_flag = true;
								}
							}
						}//subfiletype

						if($file_flag == false){
							$file_check_msg = "・".$data[$i]['prt_part_label']."：".$upfile_name[1]."ファイルはアップロードできません。";
							break;
						}
					}
				}else{
					//ファイル拡張子制御なし場合（実行ファイル(*.exe)は除く）
					if (isset($upfile_name[1]) && $upfile_name[1] == "exe") {
						$file_check_msg = "・「".$data[$i]['prt_part_label']."」ファイル形式は実行ファイル(*.exe)を除きます";
						break;
					}
				}
			}//fileupload
		}//for
		return $file_check_msg;
	}

	function fileload_security_check($data,$fileinfo){
		return true;
	}

	//確認画面でリングで表示される用
	function web_link_set($data,$postData){
		$webinfo = array();
		if($data == null || $postData == null){
			return true;
		}
		for ($i=0; $i<count($data); $i++) {
			if ($data[$i]['prt_part_type'] == 'web') {
				$webinfo[$data[$i]['prt_part_id']]['prt_part_web_linkflg'] = $data[$i]['prt_part_web_linkflg'];
				$webinfo[$data[$i]['prt_part_id']]['web_link_value'] = $postData['web'.$data[$i]['prt_part_id']];
			}
		}//for
		return $webinfo;
	}

	function currval($seq, $pk){
		if(DBTYPE=='mysql'){
			$sql = "select last_insert_id()";
		}else{
			$sql = "select currval('{$seq}_{$pk}_seq')";
		}
		return $this->db->db->getOne($sql);
	}
}

function h($v){
	return htmlspecialchars($v, 3, "UTF-8");
}

?>