<?php
require_once ('CLbaseform.php');

class CLfrmlist extends baseForm
{
	function CLfrmlist()
	{
		$this->__construct();
	}

	function __construct()
	{
		baseForm::__construct();
		$this->_form = new HTML_QuickForm('form','post',$_SERVER['PHP_SELF'],'',null,true);
		$this->db = new DataBase();
	}

	/**
	 * フォーム情報取得
	 * @param   null
	 * @return	array
	 */
	function getList() {
		$data = array();
		$sql = "select frm_id
		 			  ,frm_name
		 			  ,frm_actflg
		 			  ,frm_defaultflg
		 			  ,frm_actflg
		 			  ,frm_order
		        from   mdl_extMdlFormGnrtr_tbl_form
		        where  frm_active = 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[$row["frm_id"]] = $row;
				}
			}
		}
 		return $data;
	}

	/**
	 * フォーム複製の名称を設定
	 * @param   null
	 * @return	array
	 */
	function getFrmName() {
		$data	= array();
		$frm_id = $_REQUEST["frm_id"];
		/*
		$sql = "select frm_name
			    from   mdl_extMdlFormGnrtr_tbl_form
			    where  frm_id = ".$frm_id."
			    and	   frm_active = 1";

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

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

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

	/**
	 * フォーム情報複製
	 * @param   null
	 * @return	array
	 */
	function copyData($copy=false) {
		$frm_id		= $_REQUEST["frm_id"];
		$frm_id_new = "";
		$this->db->query('BEGIN');
		//tbl_formにデータを入力
		/*
		$sql = "insert into mdl_extMdlFormGnrtr_tbl_form(frm_name
									 ,frm_mail_subject
									 ,frm_mail_address
									 ,frm_mail_header
									 ,frm_mail_footer
									 ,frm_mail_finishing
									 ,frm_defaultflg
									 ,frm_order
									 ,frm_c_date
									 ,frm_u_date)
							 (select  frm_name
									 ,frm_mail_subject
									 ,frm_mail_address
									 ,frm_mail_header
									 ,frm_mail_footer
									 ,frm_mail_finishing
									 ,0
									 ,frm_order
									 ,now()
									 ,now()
							  from    mdl_extMdlFormGnrtr_tbl_form
							  where   frm_id	 = ".$frm_id."
							  and	  frm_active = 1)";
		//,frm_defaultflg
		if($this->db->query($sql)) {
		*/
		$sql = "insert into mdl_extMdlFormGnrtr_tbl_form(frm_name
									 ,frm_mail_subject
									 ,frm_mail_address
									 ,frm_mail_header
									 ,frm_mail_footer
									 ,frm_mail_finishing
									 ,frm_defaultflg
									 ,frm_order
									 ,frm_c_date
									 ,frm_u_date)
							 (select  frm_name
									 ,frm_mail_subject
									 ,frm_mail_address
									 ,frm_mail_header
									 ,frm_mail_footer
									 ,frm_mail_finishing
									 ,0
									 ,frm_order
									 ,now()
									 ,now()
							  from    mdl_extMdlFormGnrtr_tbl_form
							  where   frm_id	 = ?
							  and	  frm_active = 1)";
		//,frm_defaultflg
		if($this->db->prepare($sql, array($frm_id))) {
			//新しいfrm_idを取得
			$sql = "select max(frm_id) as frm_id_new
			        from   mdl_extMdlFormGnrtr_tbl_form";
			$result = $this->db->query($sql);
			if ($result) {
				while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
					$frm_id_new = $row['frm_id_new'];
				}
			}
		} else {
			$this->db->query('ROLLBACK');
			return false;
		}
		//新しいフォーム名称を更新

		//フォーム名称の重複チェック
		$_frm_id = $frm_id_new;
		$sql = "select frm_name from mdl_extMdlFormGnrtr_tbl_form where frm_id =".$frm_id;
		$frm_name = $this -> db -> getOne($sql);
		while(true){
			$sql = "select count(frm_name) from mdl_extMdlFormGnrtr_tbl_form where frm_name = '".$frm_name.$_frm_id."'";
			$result = $this -> db -> getOne($sql);
			if(PEAR::isError($result)){
				die($result -> getMessage());
			}else if($result == 0){
				break;
			}
			$_frm_id++;
		}

		/*
		$sql = "update mdl_extMdlFormGnrtr_tbl_form set frm_name =";
		if(DBTYPE=='pgsql'){
			$sql .= "frm_name || ".$_frm_id;
		}else if(DBTYPE=='mysql'){
			$sql .= "concat(frm_name,{$_frm_id})";
		}
		$sql .=" where frm_id = ".$frm_id_new;
		if (!$this->db->query($sql)) {
		*/
		$sql = "update mdl_extMdlFormGnrtr_tbl_form set frm_name =";
		if(DBTYPE=='pgsql'){
			$sql .= "frm_name || ?";
		}else if(DBTYPE=='mysql'){
			$sql .= "concat(frm_name, ?)";
		}
		$sql .=" where frm_id = ?";
		if (!$this->db->prepare($sql, array($_frm_id, $frm_id_new))) {
			$this->db->query('ROLLBACK');
			return false;
		}


		//tbl_partにデータを入力
		/*
		$sql= "insert into  mdl_extMdlFormGnrtr_tbl_part(prt_frm_id
									 ,prt_part_type
									 ,prt_part_sub_type
									 ,prt_part_label
									 ,prt_part_comment
									 ,prt_part_comment_position
									 ,prt_part_comment_color
									 ,prt_part_mustflg
									 ,prt_part_order
									 ,prt_part_guidetext_value
									 ,prt_part_ime_mode
									 ,prt_part_select_value
									 ,prt_part_select_row
									 ,prt_part_fullname_kanaflg
									 ,prt_part_address_zipflg
									 ,prt_part_address_value
									 ,prt_part_datetime_interval
									 ,prt_part_datetime_defaultflg
									 ,prt_part_datetime_fromtoflg
									 ,prt_part_web_guideflg
									 ,prt_part_web_linkflg
									 ,prt_part_mail_addressflg
									 ,prt_part_fileupload_limitflg
									 ,prt_part_fileupload_filetype
									 ,prt_part_fileupload_subfiletype
									 ,prt_part_line_size
									 ,prt_part_line_color
									 ,prt_c_date
									 ,prt_u_date)
							 (select  $frm_id_new
									 ,prt_part_type
									 ,prt_part_sub_type
									 ,prt_part_label
									 ,prt_part_comment
									 ,prt_part_comment_position
									 ,prt_part_comment_color
									 ,prt_part_mustflg
									 ,prt_part_order
									 ,prt_part_guidetext_value
									 ,prt_part_ime_mode
									 ,prt_part_select_value
									 ,prt_part_select_row
									 ,prt_part_fullname_kanaflg
									 ,prt_part_address_zipflg
									 ,prt_part_address_value
									 ,prt_part_datetime_interval
									 ,prt_part_datetime_defaultflg
									 ,prt_part_datetime_fromtoflg
									 ,prt_part_web_guideflg
									 ,prt_part_web_linkflg
									 ,prt_part_mail_addressflg
									 ,prt_part_fileupload_limitflg
									 ,prt_part_fileupload_filetype
									 ,prt_part_fileupload_subfiletype
									 ,prt_part_line_size
									 ,prt_part_line_color
									 ,now()
									 ,now()
							  from    mdl_extMdlFormGnrtr_tbl_part
							  where   prt_frm_id	  = ".$frm_id."
							  and	  prt_part_active = 1)";
		if (!$this->db->query($sql)) {
		*/
		$sql= "insert into  mdl_extMdlFormGnrtr_tbl_part(prt_frm_id
									 ,prt_part_type
									 ,prt_part_sub_type
									 ,prt_part_label
									 ,prt_part_comment
									 ,prt_part_comment_position
									 ,prt_part_comment_color
									 ,prt_part_mustflg
									 ,prt_part_order
									 ,prt_part_guidetext_value
									 ,prt_part_ime_mode
									 ,prt_part_select_value
									 ,prt_part_select_row
									 ,prt_part_fullname_kanaflg
									 ,prt_part_address_zipflg
									 ,prt_part_address_value
									 ,prt_part_datetime_interval
									 ,prt_part_datetime_defaultflg
									 ,prt_part_datetime_fromtoflg
									 ,prt_part_web_guideflg
									 ,prt_part_web_linkflg
									 ,prt_part_mail_addressflg
									 ,prt_part_mail_registerflg
									 ,prt_part_fileupload_limitflg
									 ,prt_part_fileupload_filetype
									 ,prt_part_fileupload_subfiletype
									 ,prt_part_line_size
									 ,prt_part_line_color
									 ,prt_c_date
									 ,prt_u_date)
							 (select  $frm_id_new
									 ,prt_part_type
									 ,prt_part_sub_type
									 ,prt_part_label
									 ,prt_part_comment
									 ,prt_part_comment_position
									 ,prt_part_comment_color
									 ,prt_part_mustflg
									 ,prt_part_order
									 ,prt_part_guidetext_value
									 ,prt_part_ime_mode
									 ,prt_part_select_value
									 ,prt_part_select_row
									 ,prt_part_fullname_kanaflg
									 ,prt_part_address_zipflg
									 ,prt_part_address_value
									 ,prt_part_datetime_interval
									 ,prt_part_datetime_defaultflg
									 ,prt_part_datetime_fromtoflg
									 ,prt_part_web_guideflg
									 ,prt_part_web_linkflg
									 ,prt_part_mail_addressflg
									 ,prt_part_mail_registerflg
									 ,prt_part_fileupload_limitflg
									 ,prt_part_fileupload_filetype
									 ,prt_part_fileupload_subfiletype
									 ,prt_part_line_size
									 ,prt_part_line_color
									 ,now()
									 ,now()
							  from    mdl_extMdlFormGnrtr_tbl_part
							  where   prt_frm_id	  = ?
							  and	  prt_part_active = 1)";
		if (!$this->db->prepare($sql, array($frm_id))) {
			$this->db->query('ROLLBACK');
			return false;
		}

		//問い合わせ一覧複製あり
		/*
		if($copy){
			$sql = "insert into mdl_extMdlFormGnrtr_tbl_inquiry_header(
						ihea_frm_id,
						ihea_mail_address,
						ihea_request_date,
						ihea_responseflg,
						ihea_response_date,
						ihea_response_comment,
						ihea_response_user,
						ihea_c_date,
						ihea_u_date,
						ihea_active)
					select "
						.$frm_id_new.",
						ihea_mail_address,
						ihea_request_date,
						ihea_responseflg,
						ihea_response_date,
						ihea_response_comment,
						ihea_response_user,
						now(),
						now(),
						ihea_active
					from mdl_extMdlFormGnrtr_tbl_inquiry_header
					where
							ihea_frm_id =".$frm_id;

			if (!$this->db->query($sql)) {
				$this->db->query('ROLLBACK');
				return false;
			}
			*/
			if($copy){
			$sql = "insert into mdl_extMdlFormGnrtr_tbl_inquiry_header(
						ihea_frm_id,
						ihea_mail_address,
						ihea_request_date,
						ihea_responseflg,
						ihea_response_date,
						ihea_response_comment,
						ihea_response_user,
						ihea_c_date,
						ihea_u_date,
						ihea_active)
					select "
						.$frm_id_new.",
						ihea_mail_address,
						ihea_request_date,
						ihea_responseflg,
						ihea_response_date,
						ihea_response_comment,
						ihea_response_user,
						now(),
						now(),
						ihea_active
					from mdl_extMdlFormGnrtr_tbl_inquiry_header
					where
							ihea_frm_id = ?";

			if (!$this->db->prepare($sql, array($frm_id))) {
				$this->db->query('ROLLBACK');
				return false;
			}
			/*
			$sql = "insert into mdl_extMdlFormGnrtr_tbl_inquiry_detail
					(select
						idet_ihea_id, ".$frm_id_new.", idet_part_id, idet_part_value, idet_c_date, idet_u_date
					from
						mdl_extMdlFormGnrtr_tbl_inquiry_detail
					where
						idet_frm_id = ".$frm_id.")";
			if (!$this->db->query($sql)) {
				$this->db->query('ROLLBACK');
				return false;
			}
			*/
			$sql = "insert into mdl_extMdlFormGnrtr_tbl_inquiry_detail
					(select
						idet_ihea_id, ".$frm_id_new.", idet_part_id, idet_part_value, idet_c_date, idet_u_date
					from
						mdl_extMdlFormGnrtr_tbl_inquiry_detail
					where
						idet_frm_id = ?)";
			if (!$this->db->prepare($sql, array($frm_id))) {
				$this->db->query('ROLLBACK');
				return false;
			}

			//新しいid取得
			$sql = "select ihea_id from mdl_extMdlFormGnrtr_tbl_inquiry_header where ihea_frm_id =".$frm_id_new;
			$result = $this -> db -> query($sql);

			if($result == null){
				$this->db->query('ROLLBACK');
				return false;
			}

			while($row = $result -> fetchRow(DB_FETCHMODE_ASSOC)){
				$new_ihea_id[]=$row['ihea_id'];
			}

			//古いid取得
			/*
			$sql = "select ihea_id from mdl_extMdlFormGnrtr_tbl_inquiry_header where ihea_frm_id =".$frm_id;
			$result = $this -> db -> query($sql);
			*/
			$sql = "select ihea_id from mdl_extMdlFormGnrtr_tbl_inquiry_header where ihea_frm_id =?";
			$result = $this -> db -> prepare($sql, array($frm_id));

			if($result == null){
				$this->db->query('ROLLBACK');
				return false;
			}

			while($row = $result -> fetchRow(DB_FETCHMODE_ASSOC)){
				$old_ihea_id[]=$row['ihea_id'];
			}

			//新しいパーツid取得
			$sql = "select prt_part_id from mdl_extMdlFormGnrtr_tbl_part where prt_frm_id =".$frm_id_new;
			$result = $this -> db -> query($sql);

			if($result == null){
				$this->db->query('ROLLBACK');
				return false;
			}

			while($row = $result -> fetchRow(DB_FETCHMODE_ASSOC)){
				$new_part_id[]=$row['prt_part_id'];
			}


			//古いパーツid取得
			/*
			$sql = "select prt_part_id from mdl_extMdlFormGnrtr_tbl_part where prt_frm_id =".$frm_id." and prt_part_active = 1";
			$result = $this -> db -> query($sql);
			*/
			$sql = "select prt_part_id from mdl_extMdlFormGnrtr_tbl_part where prt_frm_id = ? and prt_part_active = 1";
			$result = $this -> db -> prepare($sql, array($frm_id));

			if($result == null){
				$this->db->query('ROLLBACK');
				return false;
			}

			while($row = $result -> fetchRow(DB_FETCHMODE_ASSOC)){
				$old_part_id[]=$row['prt_part_id'];
			}

			for($i=0; $i < count($new_ihea_id); $i++){
				/*
				$sql = "update mdl_extMdlFormGnrtr_tbl_inquiry_detail
							set
								idet_ihea_id =".$new_ihea_id[$i]."
							where
								idet_ihea_id =".$old_ihea_id[$i]."
							and
								idet_frm_id =".$frm_id_new;

				if (!$this->db->query($sql)) {
					$this->db->query('ROLLBACK');
					return false;
				}
				*/
				$sql = "update mdl_extMdlFormGnrtr_tbl_inquiry_detail
							set
								idet_ihea_id = ?
							where
								idet_ihea_id = ?
							and
								idet_frm_id = ?";

				if (!$this->db->prepare($sql, array($new_ihea_id[$i], $old_ihea_id[$i], $frm_id_new))) {
					$this->db->query('ROLLBACK');
					return false;
				}
			}

			for($i=0; $i < count($new_ihea_id); $i++){
				for($j=0; $j < count($new_part_id); $j++){
					/*
					$sql = "update mdl_extMdlFormGnrtr_tbl_inquiry_detail
								set
									idet_part_id =".$new_part_id[$j]."
								where
									idet_ihea_id =".$new_ihea_id[$i]."
								and
									idet_frm_id =".$frm_id_new."
								and
									idet_part_id=".$old_part_id[$j];
					if (!$this->db->query($sql)) {
						$this->db->query('ROLLBACK');
						return false;
					}
					*/
					$sql = "update mdl_extMdlFormGnrtr_tbl_inquiry_detail
								set
									idet_part_id = ?
								where
									idet_ihea_id = ?
								and
									idet_frm_id = ?
								and
									idet_part_id= ?";
					if (!$this->db->prepare($sql, array($new_part_id[$j], $new_ihea_id[$i], $frm_id_new, $old_part_id[$j]))) {
						$this->db->query('ROLLBACK');
						return false;
					}
				}
			}
		}
		$this->db->query('COMMIT');

		return true;
	}
	/**
	 * 未対応件数取得用
	 * @param $frmid
	 * @return int
	 */
	function getUnCorrespondenceNumber($frmid = null){
		if($frmid == null){
			return false;
		}
		$sql = "select count(*) from mdl_extMdlFormGnrtr_tbl_inquiry_header where ihea_frm_id = {$frmid} and ihea_responseflg = 0 and ihea_active = 1";
		return $this -> db -> getOne($sql,$frmid);
	}

	/**
	 * お問い合わせ件数取得用
	 * @param $frmid
	 * @return int
	 */
	function getActiveInquiriesCount($frmid = null){
		if($frmid == null){
			return false;
		}
		$sql = "select count(*) from mdl_extMdlFormGnrtr_tbl_inquiry_header where ihea_frm_id = {$frmid} and ihea_active = 1";
		return $this -> db -> getOne($sql,$frmid);
	}
}
?>