<?php

/*
 * モジュールが持つテーブル、モジュール付属ユニットが持つテーブルの生成を行う
 * @param tbl_extmoduleのマスタ情報
 */
function module_install($param, $db) {
    $UNITS_REPLACE_FLG = '%%%';
	$sql = array();
	switch(DBTYPE) {
		case "pgsql":
			$sql[] = "CREATE TABLE mdl_extMdlFormGnrtr_tbl_form
							(
								frm_id					serial,
								frm_name			    text not null,
								frm_mail_subject		text,
								frm_mail_address		text,
								frm_mail_header		    text,
								frm_mail_footer		    text,
								frm_mail_finishing		text,
								frm_actflg				smallint default 0,
								frm_defaultflg			smallint default 0,
								frm_active			    smallint default 1,
								frm_order			    smallint default 0,
								frm_c_date		        timestamptz,
								frm_u_date				timestamptz,
								primary key (frm_id)
							);";
			$sql[] = "CREATE TABLE mdl_extMdlFormGnrtr_tbl_part
							(
								prt_part_id		        	serial,
								prt_frm_id					integer,
								prt_part_type	        	varchar(100),
								prt_part_sub_type			varchar(100),
								prt_part_label          	varchar(100),
								prt_part_comment			text,
								prt_part_comment_position   varchar(100),
								prt_part_comment_color      varchar(100),
								prt_part_mustflg			text,
								prt_part_order				integer default 0,
								prt_part_ime_mode    		varchar(50),
								prt_part_guidetext_value    text,
								prt_part_select_value		text,
								prt_part_select_row			varchar(10),
								prt_part_fullname_kanaflg	smallint default 0,
								prt_part_address_zipflg		smallint default 0,
								prt_part_address_value		text,
								prt_part_datetime_interval	varchar(50),
								prt_part_datetime_defaultflg smallint default 0,
								prt_part_datetime_fromtoflg	smallint default 0,
								prt_part_web_guideflg		smallint default 0,
								prt_part_web_linkflg		smallint default 0,
								prt_part_mail_addressflg	text,
                                prt_part_mail_registerflg   smallint default 0,
								prt_part_fileupload_limitflg	smallint default 0,
								prt_part_fileupload_filetype	text,
								prt_part_fileupload_subfiletype	text,
								prt_part_line_size				integer,
								prt_part_line_color				varchar(50),
								prt_part_active					smallint default 1,
								prt_c_date					timestamptz,
								prt_u_date					timestamptz,
								primary key (prt_part_id)
							);";
			$sql[] = "CREATE TABLE mdl_extMdlFormGnrtr_tbl_inquiry_detail
							(
								idet_ihea_id			integer,
								idet_frm_id				integer,
								idet_part_id		    integer,
								idet_part_value         text,
								idet_c_date		        timestamptz,
								idet_u_date				timestamptz,
								primary key (idet_ihea_id,idet_frm_id,idet_part_id)
							);";
			$sql[] = "CREATE TABLE mdl_extMdlFormGnrtr_tbl_inquiry_header
							(
								ihea_id					serial,
								ihea_frm_id				integer,
								ihea_mail_address		text,
								ihea_request_date       timestamptz,
								ihea_responseflg        smallint default 0,
								ihea_response_date      timestamptz,
								ihea_response_comment	text,
								ihea_response_user	    varchar(100),
								ihea_active             smallint default 1,
								ihea_c_date		        timestamptz,
								ihea_u_date				timestamptz,
								primary key (ihea_id)
							);";
			$sql[] = "CREATE TABLE mdl_extMdlFormGnrtr_tbl_form_user
							(
								frm_usr_id				integer,
								primary key (frm_usr_id)
							);";
			$sql[] = "CREATE TABLE extunit_extMdlFormGnrtr_bForm
							(
								pageu_code integer NOT NULL,
								ufrm_frm_id integer,
								btm_padding smallint DEFAULT 2,
								primary key (pageu_code)
							);";
			$sql[] = "CREATE TABLE extunit_extMdlFormGnrtr_bForm_tmp
							(
								pageu_code integer NOT NULL,
								ufrm_frm_id integer,
								btm_padding smallint DEFAULT 2,
								primary key (pageu_code)
							);";
			break;
		case "mysql":
			$sql[] = "CREATE TABLE mdl_extMdlFormGnrtr_tbl_form
							(
								frm_id					int auto_increment,
								frm_name			    text not null,
								frm_mail_subject		text,
								frm_mail_address		text,
								frm_mail_header		    text,
								frm_mail_footer		    text,
								frm_mail_finishing		text,
								frm_actflg				smallint default 0,
								frm_defaultflg			smallint default 0,
								frm_active			    smallint default 1,
								frm_order			    smallint default 0,
								frm_c_date		        datetime,
								frm_u_date				datetime,
								primary key (frm_id)
							)TYPE=InnoDB DEFAULT CHARSET=UTF8;";
			$sql[] = "CREATE TABLE mdl_extMdlFormGnrtr_tbl_part
							(
								prt_part_id		        	int auto_increment,
								prt_frm_id					integer,
								prt_part_type	        	varchar(100),
								prt_part_sub_type			varchar(100),
								prt_part_label          	varchar(100),
								prt_part_comment			text,
								prt_part_comment_position   varchar(100),
								prt_part_comment_color      varchar(100),
								prt_part_mustflg			text,
								prt_part_order				integer default 0,
								prt_part_ime_mode    		varchar(50),
								prt_part_guidetext_value    text,
								prt_part_select_value		text,
								prt_part_select_row			varchar(10),
								prt_part_fullname_kanaflg	smallint default 0,
								prt_part_address_zipflg		smallint default 0,
								prt_part_address_value		text,
								prt_part_datetime_interval	varchar(50),
								prt_part_datetime_defaultflg smallint default 0,
								prt_part_datetime_fromtoflg	smallint default 0,
								prt_part_web_guideflg		smallint default 0,
								prt_part_web_linkflg		smallint default 0,
								prt_part_mail_addressflg	text,
                                prt_part_mail_registerflg   smallint default 0,
								prt_part_fileupload_limitflg	smallint default 0,
								prt_part_fileupload_filetype	text,
								prt_part_fileupload_subfiletype	text,
								prt_part_line_size				integer,
								prt_part_line_color				varchar(50),
								prt_part_active					smallint default 1,
								prt_c_date					datetime,
								prt_u_date					datetime,
								primary key (prt_part_id)
							)TYPE=InnoDB DEFAULT CHARSET=UTF8;";
			$sql[] = "CREATE TABLE mdl_extMdlFormGnrtr_tbl_inquiry_detail
							(
								idet_ihea_id			integer,
								idet_frm_id				integer,
								idet_part_id		    integer,
								idet_part_value         text,
								idet_c_date		        datetime,
								idet_u_date				datetime,
								primary key (idet_ihea_id,idet_frm_id,idet_part_id)
							)TYPE=InnoDB DEFAULT CHARSET=UTF8;";
			$sql[] = "CREATE TABLE mdl_extMdlFormGnrtr_tbl_inquiry_header
							(
								ihea_id					int auto_increment,
								ihea_frm_id				integer,
								ihea_mail_address		text,
								ihea_request_date       datetime,
								ihea_responseflg        smallint default 0,
								ihea_response_date      datetime,
								ihea_response_comment	text,
								ihea_response_user	    varchar(100),
								ihea_active             smallint default 1,
								ihea_c_date		        datetime,
								ihea_u_date				datetime,
								primary key (ihea_id)
							)TYPE=InnoDB DEFAULT CHARSET=UTF8;";
			$sql[] = "CREATE TABLE mdl_extMdlFormGnrtr_tbl_form_user
							(
								frm_usr_id				integer,
								primary key (frm_usr_id)
							)TYPE=InnoDB DEFAULT CHARSET=UTF8;";
			$sql[] = "CREATE TABLE extunit_extMdlFormGnrtr_bForm
							(
								pageu_code integer NOT NULL,
								ufrm_frm_id integer,
								btm_padding smallint DEFAULT 2,
								primary key (pageu_code)
							)TYPE=InnoDB DEFAULT CHARSET=UTF8;";
			$sql[] = "CREATE TABLE extunit_extMdlFormGnrtr_bForm_tmp
							(
								pageu_code integer NOT NULL,
								ufrm_frm_id integer,
								btm_padding smallint DEFAULT 2,
								primary key (pageu_code)
							)TYPE=InnoDB DEFAULT CHARSET=UTF8;";
			//mysql 5.5対応
			if(isset($db->dbversion) && version_compare($db->dbversion,"5.0.0", ">=")){
			//mysql5.0以上の時はTYPEをENGINEに置換え
				foreach((array)$sql as $k => $v){
					$sql[$k] = str_replace('TYPE=InnoDB','ENGINE = InnoDB', $v);
				}
			}
			break;
		default:
			return false;
			break;
	}
	//sqlの実行
	if(!is_array($sql)||!count($sql)>0) {
		//テーブルのないモジュール、ユニットは存在させない
		return false;
	}
	$db->query('BEGIN');
	foreach($sql as $key => $val) {
		if(!$db->query($val)) {
			$db->query('ROLLBACK');
			return false;
		}
	}

	$tbl_form_frm_id = $db->getKey("mdl_extMdlFormGnrtr_tbl_form_frm_id");
	//デフォルトフォームの初期データの挿入
	$record = array("frm_id"=>$tbl_form_frm_id,
					"frm_name"=>'お問い合わせ　',
					"frm_mail_subject"=>'お問い合わせ',
					"frm_mail_address"=>$_SESSION['usr_info']['usr_id'],
					"frm_mail_header"=>'お問い合わせ有難う御座います。
下記内容で受付をいたしました。',
					"frm_mail_footer"=>'今後ともよろしくお願いいたします。',
					"frm_mail_finishing"=>null,
					"frm_actflg"=>0,
					"frm_defaultflg"=>1,
					"frm_active"=>1,
					"frm_c_date"=>"now()",
					"frm_u_date"=>"now()"
					);

	if(!$db->insert("mdl_extMdlFormGnrtr_tbl_form",$record)) {
		$db->query('ROLLBACK');
		return false;
	}

	//デフォルトフォームの初期パーツデータの挿入
	//氏名
	$record = array("prt_part_id"						=>$db->getKey("mdl_extMdlFormGnrtr_tbl_part_prt_part_id"),
					"prt_frm_id"						=>$tbl_form_frm_id,
					"prt_part_type"						=>'name',
					"prt_part_sub_type"					=>'nametogether',
					"prt_part_label"					=>'氏名',
					"prt_part_comment"					=>null,
					"prt_part_comment_position"			=>null,
					"prt_part_comment_color"			=>null,
					"prt_part_mustflg"					=>'1',
					"prt_part_order"					=>'1',
					"prt_part_guidetext_value"			=>null,
					"prt_part_select_value"				=>null,
					"prt_part_fullname_kanaflg"			=>null,
					"prt_part_address_zipflg"			=>0,
					"prt_part_address_value"			=>null,
					"prt_part_datetime_interval"		=>null,
					"prt_part_datetime_fromtoflg"		=>0,
					"prt_part_web_guideflg"				=>0,
					"prt_part_web_linkflg"				=>0,
					"prt_part_mail_addressflg"			=>null,
					"prt_part_mail_registerflg"			=>0,
					"prt_part_fileupload_limitflg"		=>0,
					"prt_part_fileupload_filetype"		=>null,
					"prt_part_fileupload_subfiletype"	=>null,
					"prt_part_line_size"				=>null,
					"prt_part_line_color"				=>null,
					"prt_part_active"					=>1,
					"prt_c_date"						=>"now()",
					"prt_u_date"						=>"now()"
					);

	if(!$db->insert("mdl_extMdlFormGnrtr_tbl_part",$record)) {
		$db->query('ROLLBACK');
		return false;
	}

	//デフォルトフォームの初期パーツデータの挿入
	//住所
	$record = array("prt_part_id"						=>$db->getKey("mdl_extMdlFormGnrtr_tbl_part_prt_part_id"),
					"prt_frm_id"						=>$tbl_form_frm_id,
					"prt_part_type"						=>'address',
					"prt_part_sub_type"					=>'addressseparate',
					"prt_part_label"					=>'住所',
					"prt_part_comment"					=>null,
					"prt_part_comment_position"			=>null,
					"prt_part_comment_color"			=>null,
					"prt_part_mustflg"					=>$UNITS_REPLACE_FLG . $UNITS_REPLACE_FLG . $UNITS_REPLACE_FLG . $UNITS_REPLACE_FLG . $UNITS_REPLACE_FLG,
					"prt_part_order"					=>'2',
					"prt_part_guidetext_value"			=>null,
					"prt_part_select_value"				=>null,
					"prt_part_fullname_kanaflg"			=>0,
					"prt_part_address_zipflg"			=>1,
					"prt_part_address_value"			=>'1' . $UNITS_REPLACE_FLG . '1' . $UNITS_REPLACE_FLG . '1' . $UNITS_REPLACE_FLG . '1',
					"prt_part_datetime_interval"		=>null,
					"prt_part_datetime_fromtoflg"		=>0,
					"prt_part_web_guideflg"				=>0,
					"prt_part_web_linkflg"				=>0,
					"prt_part_mail_addressflg"			=>null,
					"prt_part_mail_registerflg"			=>0,
					"prt_part_fileupload_limitflg"		=>0,
					"prt_part_fileupload_filetype"		=>null,
					"prt_part_fileupload_subfiletype"	=>null,
					"prt_part_line_size"				=>null,
					"prt_part_line_color"				=>null,
					"prt_part_active"					=>1,
					"prt_c_date"						=>"now()",
					"prt_u_date"						=>"now()"
					);

	if(!$db->insert("mdl_extMdlFormGnrtr_tbl_part",$record)) {
		$db->query('ROLLBACK');
		return false;
	}

	//デフォルトフォームの初期パーツデータの挿入
	//電話番号
	$record = array("prt_part_id"						=>$db->getKey("mdl_extMdlFormGnrtr_tbl_part_prt_part_id"),
					"prt_frm_id"						=>$tbl_form_frm_id,
					"prt_part_type"						=>'telphone',
					"prt_part_sub_type"					=>'onepart',
					"prt_part_label"					=>'TEL',
					"prt_part_comment"					=>'（例: 0465-66-2282）',
					"prt_part_comment_position"			=>'right',
					"prt_part_comment_color"			=>'#000000',
					"prt_part_mustflg"					=>null,
					"prt_part_order"					=>'3',
					"prt_part_guidetext_value"			=>null,
					"prt_part_select_value"				=>null,
					"prt_part_fullname_kanaflg"			=>0,
					"prt_part_address_zipflg"			=>0,
					"prt_part_address_value"			=>null,
					"prt_part_datetime_interval"		=>null,
					"prt_part_datetime_fromtoflg"		=>0,
					"prt_part_web_guideflg"				=>0,
					"prt_part_web_linkflg"				=>0,
					"prt_part_mail_addressflg"			=>null,
					"prt_part_mail_registerflg"			=>0,
					"prt_part_fileupload_limitflg"		=>0,
					"prt_part_fileupload_filetype"		=>null,
					"prt_part_fileupload_subfiletype"	=>null,
					"prt_part_line_size"				=>null,
					"prt_part_line_color"				=>null,
					"prt_part_active"					=>1,
					"prt_c_date"						=>"now()",
					"prt_u_date"						=>"now()"
					);

	if(!$db->insert("mdl_extMdlFormGnrtr_tbl_part",$record)) {
		$db->query('ROLLBACK');
		return false;
	}

	//デフォルトフォームの初期パーツデータの挿入
	//メールアドレス
	$record = array("prt_part_id"						=>$db->getKey("mdl_extMdlFormGnrtr_tbl_part_prt_part_id"),
					"prt_frm_id"						=>$tbl_form_frm_id,
					"prt_part_type"						=>'mail',
					"prt_part_sub_type"					=>null,
					"prt_part_label"					=>'Eメール',
					"prt_part_comment"					=>null,
					"prt_part_comment_position"			=>'right',
					"prt_part_comment_color"			=>'#000000',
					"prt_part_mustflg"					=>'1',
					"prt_part_order"					=>'4',
					"prt_part_guidetext_value"			=>null,
					"prt_part_select_value"				=>null,
					"prt_part_fullname_kanaflg"			=>0,
					"prt_part_address_zipflg"			=>0,
					"prt_part_address_value"			=>null,
					"prt_part_datetime_interval"		=>null,
					"prt_part_datetime_fromtoflg"		=>0,
					"prt_part_web_guideflg"				=>0,
					"prt_part_web_linkflg"				=>0,
					"prt_part_mail_addressflg"			=>'1',
					"prt_part_mail_registerflg"			=>1,
					"prt_part_fileupload_limitflg"		=>0,
					"prt_part_fileupload_filetype"		=>null,
					"prt_part_fileupload_subfiletype"	=>null,
					"prt_part_line_size"				=>null,
					"prt_part_line_color"				=>null,
					"prt_part_active"					=>1,
					"prt_c_date"						=>"now()",
					"prt_u_date"						=>"now()"
					);

	if(!$db->insert("mdl_extMdlFormGnrtr_tbl_part",$record)) {
		$db->query('ROLLBACK');
		return false;
	}

	//デフォルトフォームの初期パーツデータの挿入
	//お問い合せ内容
	$record = array("prt_part_id"						=>$db->getKey("mdl_extMdlFormGnrtr_tbl_part_prt_part_id"),
					"prt_frm_id"						=>$tbl_form_frm_id,
					"prt_part_type"						=>'text',
					"prt_part_sub_type"					=>'multiply',
					"prt_part_label"					=>'お問い合せ内容',
					"prt_part_comment"					=>null,
					"prt_part_comment_position"			=>'right',
					"prt_part_comment_color"			=>'#000000',
					"prt_part_mustflg"					=>'1',
					"prt_part_order"					=>'5',
					"prt_part_guidetext_value"			=>null,
					"prt_part_select_value"				=>null,
					"prt_part_fullname_kanaflg"			=>0,
					"prt_part_address_zipflg"			=>0,
					"prt_part_address_value"			=>null,
					"prt_part_datetime_interval"		=>null,
					"prt_part_datetime_fromtoflg"		=>0,
					"prt_part_web_guideflg"				=>0,
					"prt_part_web_linkflg"				=>0,
					"prt_part_mail_addressflg"			=>null,
					"prt_part_mail_registerflg"			=>0,
					"prt_part_fileupload_limitflg"		=>0,
					"prt_part_fileupload_filetype"		=>null,
					"prt_part_fileupload_subfiletype"	=>null,
					"prt_part_line_size"				=>null,
					"prt_part_line_color"				=>null,
					"prt_part_active"					=>1,
					"prt_c_date"						=>"now()",
					"prt_u_date"						=>"now()"
					);

	if(!$db->insert("mdl_extMdlFormGnrtr_tbl_part",$record)) {
		$db->query('ROLLBACK');
		return false;
	}

	$db->query('COMMIT');
	return true;
}
?>