<?php
require_once ('CLbaseform.php');
//require_once ('../admin/common/fileUpload.php');//質問がある
require_once (MODULES_PATH.'/extMdlFormGnrtr/admin/common/fileUpload.php');
class CLinqpre extends baseForm
{
	function CLinqpre()
	{
		$this->__construct();
	}

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

	/**
	 * Smarty Element 設定
	 * @param
	 * @return
	 */
	function setElements()
	{
		//フォーム作成
		$this->_form = new HTML_QuickForm('form', 'post', $_SERVER['PHP_SELF'], '', null, true);

		$this->_form->addElement('hidden', 'ihea_id', $_REQUEST["ihea_id"]);
		//対応完了状態の初期値をセット
		if ($_REQUEST["ihea_responseflg"] == 1) {
			$chk = array('checked' => true);
		} else {
			$chk = array();
		}
		$this->_form->addElement('checkbox', 'ihea_responseflg'.'', '', '', $chk);
		$this->_form->addElement('textarea', 'ihea_response_comment', null, array('cols' => 80, 'rows' => 4, 'id' => 'ihea_response_comment'));
	}

	/**
	 * ボタン作成
	 * @param
	 * @return
	 */
	function setButtons()
	{
		//ボタン作成
		$buttons = array();
		$buttons[] = $this->_form->createElement('submit', 'BTNList', ' 一覧に戻る ', array('id' => 'BTNList', 'class' => 'bsmBtnListBack'));
		$buttons[] = $this->_form->createElement('submit', 'BTNsave', ' 保存 ', array('id' => 'BTNsave', 'class' => 'bBtnSave'));
		$buttons[] = $this->_form->createElement('submit', 'BTNdelete', ' 削除', array('id' => 'BTNdelete', 'class' => 'bsmBtnDelete', 'onclick'=>"return confirm('本当に削除してよろしいですか？')"));
		$this->_form->addGroup($buttons, "buttons", null, '&nbsp; &nbsp;');
	}

	/**
	 * お問い合わせデータを取得
	 * @param	idet_ihea_id
	 * @param	idet_frm_id
	 * @return	array
	 */
	function getData($idet_ihea_id = null, $idet_frm_id = null) {
		$data  = array();
		$where = array();
 	 	$sql = "select td.idet_part_id
 	 				  ,td.idet_part_value
 	 				  ,tp.prt_part_label
 	 				  ,tp.prt_part_mustflg
 	 				  ,tp.prt_part_type
 	 				  ,tp.prt_part_sub_type
 	 				  ,tp.prt_part_datetime_fromtoflg
 	 				  ,tp.prt_part_line_color
 	 				  ,tp.prt_part_line_size
 	 				  ,tp.prt_part_fullname_kanaflg
 	 		    from   mdl_extMdlFormGnrtr_tbl_inquiry_detail as td
 	 		    	  ,mdl_extMdlFormGnrtr_tbl_part as tp
 	 		    where  td.idet_frm_id  = tp.prt_frm_id
 	 		    and	   td.idet_part_id = tp.prt_part_id ";
		if ($idet_ihea_id) {
			$where[] = $idet_ihea_id;
			$sql	.= " and td.idet_ihea_id = ?";
		}
		if ($idet_frm_id) {
			$where[] = $idet_frm_id;
			$sql	.= " and td.idet_frm_id = ?";
		}
 	 	$sql .= " order by tp.prt_part_order ";

		$result = $this->db->prepare($sql, $where);
		if ($result) {
			while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
				if(isset($row) && is_array($row) && count($row) > 0) {
					$mustflg = explode(UNITS_REPLACE_FLG, $row['prt_part_mustflg']);
					if($row['prt_part_type']=="guidetext" || $row['prt_part_type']=="line" || $row['prt_part_type']=="caps"){
						continue;
					}
					$idet_part_value = "";
					switch ($row['prt_part_type']) {
						case "guidetext":
							$idet_part_value = $row['idet_part_value'];
							break;
						case "text":
							if($row['prt_part_sub_type'] == "single"){
								$idet_part_value = htmlspecialchars($row['idet_part_value']);
							}else{
								$idet_part_value = ereg_replace(UNITS_REPLACE_FLG, "<br>", htmlspecialchars($row['idet_part_value']));
							}
							break;
						case "select":
							$idet_part_value = ereg_replace(UNITS_REPLACE_FLG, "　", $row['idet_part_value']);
							break;
						case "name":
							if($row['prt_part_fullname_kanaflg'] == '1'){
								//フリガナが有
								$row['prt_part_label'] .="<br/>フリガナ";
								$name_value	 = explode(UNITS_REPLACE_FLG, $row['idet_part_value']);
								$idet_part_value = $name_value[0];
								if(count($name_value) >= 4){
									if($name_value[1]!=""){
										$idet_part_value .= "&nbsp;&nbsp;".$name_value[1];
									}
									$idet_part_value .= "<br />";

									if($name_value[2]!=""){
										$idet_part_value .= $name_value[2];
									}

									if($name_value[3]!=""){
										$idet_part_value .= "&nbsp;&nbsp;".$name_value[3];
									}
								}else{//旧ロジック
									//分割しない
									$idet_part_value .= "<br />";
									if($name_value[1]!=""){
										$idet_part_value .= "&nbsp;&nbsp;".$name_value[1];
									}
								}
								unset($name_value);
							}else{
								//フリガナが無
								$idet_part_value = ereg_replace(UNITS_REPLACE_FLG, "&nbsp;&nbsp;", $row['idet_part_value']);
							}
							break;
						case "address":
							if ($row['prt_part_sub_type'] == "addressseparate") {
								$address_value	 = explode(UNITS_REPLACE_FLG, $row['idet_part_value']);
								if (isset($address_value[0]) && $address_value[0] != '-' && $address_value[0] != '') {
									$idet_part_value = "〒" . $address_value[0];
								}
								if (isset($address_value[1]) && $address_value[1] != '') {
									if(isset($address_value[0]) && $address_value[0] != '-' && $address_value[0] != ''){
										$idet_part_value .= "<br>" ;
									}
									$idet_part_value .= $address_value[1];
								}
								if (isset($address_value[2]) && $address_value[2] != '') {
									if(isset($address_value[0]) && $address_value[0] != ''
									|| isset($address_value[1]) && $address_value[1] != ''){
										$idet_part_value .= "<br>";
									}
									$idet_part_value .= $address_value[2];
								}
								if (isset($address_value[3]) && $address_value[3] != '') {
									if(isset($address_value[0]) && $address_value[0] != ''
									|| isset($address_value[1]) && $address_value[1] != ''
									|| isset($address_value[2]) && $address_value[2] != ''){
										$idet_part_value .= "<br>";
									}
									$idet_part_value .= $address_value[3];
								}
								if (isset($address_value[4]) && $address_value[4] != '') {
									if(isset($address_value[0]) && $address_value[0] != ''
									|| isset($address_value[1]) && $address_value[1] != ''
									|| isset($address_value[2]) && $address_value[2] != ''
									|| isset($address_value[3]) && $address_value[3] != ''){
										$idet_part_value .= "<br>";
									}
									$idet_part_value .= $address_value[4];
								}
							} else {
								$address_value	 = explode(UNITS_REPLACE_FLG, $row['idet_part_value']);
								if (isset($address_value[0]) && $address_value[0] != '-' && $address_value[0] != '') {
									$idet_part_value = "〒" . $address_value[0];
								}
								if (isset($address_value[1]) && $address_value[1] != '') {
									if(isset($address_value[0]) && $address_value[0] != '-' && $address_value[0] != ''){
										$idet_part_value .= "　" ;
									}
									$idet_part_value .= $address_value[1];
								}
							}
							break;
						case "datetime":
							$date_value = array();
							$date = explode(UNITS_REPLACE_FLG, $row['idet_part_value']);
							if(count($date)==6 || count($date)==3){//新ロジック 2010.12.24
								$idet_part_value="";
								if($date[0]!=""){
									$idet_part_value .= $date[0];
								}
								if($date[1]!=""){
									if($date[0]!="") $idet_part_value .= "&nbsp;";
									$idet_part_value .= sprintf("%02d",$date[1])."時".sprintf("%02d",$date[2])."分";
								}

								if($date[3]!="" || $date[4]!="" || $date[5]!=""){
									$idet_part_value .= "&nbsp;～&nbsp;";
								}

								if($date[3]!=""){
									$idet_part_value .= $date[3];
								}
								if($date[4]!=""){
									if($date[3]!="") $idet_part_value .= "&nbsp;";
									$idet_part_value .= sprintf("%02d",$date[4])."時".sprintf("%02d",$date[5])."分";
								}

							}else{//旧ロジック
								foreach($date as $d){
									if(strlen($d) >= 3){
										$date_value['ymd'][] = $d;
									}else if(strlen($d) < 3){
										$date_value['time'][] = $d;
									}
								}

								if(isset($date_value['ymd'][0]) && $date_value['ymd'][0] != ""){
									$date['ymd'] = $date_value['ymd'][0]." ";
								}

								if(isset($date_value['ymd'][1]) && $date_value['ymd'][1] != ""){
									$date['to_ymd'] = $date_value['ymd'][1]." ";
								}

								if(isset($date_value['time'][0]) && $date_value['time'][0] != ""){
									$date['hour'] =strlen($date_value['time'][0]) == "1" ? "0".$date_value['time'][0]."時" : $date_value['time'][0]."時";
								}

								if(isset($date_value['time'][1]) && $date_value['time'][1] != ""){
									$date['minute'] = strlen($date_value['time'][1]) == "1" ? "0".$date_value['time'][1]."分" : $date_value['time'][1]."分";
								}

								if(isset($date_value['time'][2]) && $date_value['time'][2] != ""){
									$date['to_hour'] = strlen($date_value['time'][2]) == "1" ? "0".$date_value['time'][2]."時" : $date_value['time'][2]."時";
								}

								if(isset($date_value['time'][3]) && $date_value['time'][3] != ""){
									$date['to_minute'] = strlen($date_value['time'][3]) == "1" ? "0".$date_value['time'][3]."分" : $date_value['time'][3]."分";
								}

								if(empty($date['to_ymd']) && empty($date['to_hour'])){
									//fromの時
									$idet_part_value = $date['ymd'].$date['hour'].$date['minute'];
								}else{
									//from-toの時
									$idet_part_value = $date['ymd'].$date['hour'].$date['minute']."&nbsp;～&nbsp;".$date['to_ymd'].$date['to_hour'].$date['to_minute'];
								}
							}
							unset($date);
							break;
						case "telphone":
							if ($row['prt_part_sub_type'] == "threepart") {
								$telphone_value	 = explode(UNITS_REPLACE_FLG, $row['idet_part_value']);
								//$idet_part_value = $date_value[0] . "　" . $date_value[1] . ":" . $date_value[2];
								$idet_part_value = "";
								if(isset($telphone_value[0]) && $telphone_value[0] != ''){
									$idet_part_value .= $telphone_value[0];
								}
								if(isset($telphone_value[1]) && $telphone_value[1] != ''){
									if(isset($telphone_value[0]) && $telphone_value[0] != ''){
										$idet_part_value .= "-";
									}
									$idet_part_value .= $telphone_value[1];
								}
								if(isset($telphone_value[2]) && $telphone_value[2] != ''){
									if(isset($telphone_value[0]) && $telphone_value[0] != ''
									|| isset($telphone_value[1]) && $telphone_value[1] != ''){
										$idet_part_value .= "-";
									}
									$idet_part_value .= $telphone_value[2];
								}
							} else {
								$idet_part_value = $row['idet_part_value'];
							}
							break;
						case "web":
							$idet_part_value = $row['idet_part_value'];
							break;
						case "mail":
							$mail_value	 = explode(UNITS_REPLACE_FLG, $row['idet_part_value']);
							if(isset($mail_value[0]) && $mail_value[0] != ''){
								$idet_part_value =  $mail_value[0];
							}
							if(isset($mail_value[1]) && $mail_value[1] != ''){
								if(isset($mail_value[0]) && $mail_value[0] != ''){
									$idet_part_value .= "<br>";
								}
								$idet_part_value .= '「ＰＣメール」' . $mail_value[1];
							}

							if(isset($mail_value[2]) && $mail_value[2] != ''){
								if( (isset($mail_value[0]) && $mail_value[0] != '') || (isset($mail_value[1]) && $mail_value[1] != '') ){
									$idet_part_value .= "<br>";
								}
								$idet_part_value .= '「携帯メール」' . $mail_value[2];
							}
							break;
						case "fileupload":
							$idet_part_value = $row['idet_part_value'];
							if($idet_part_value != null && $idet_part_value != ''){
								$filename = explode(UNITS_REPLACE_FLG, $idet_part_value);
								$file_path = fileUpload::getFilePath($filename[1]);
								if($file_path != null && $file_path != '') {
									$idet_part_value = $file_path['real_path'] . '/' . $filename[1];
								}
							}
							break;
						case "line":
							$idet_part_value = $row['idet_part_value'];
							break;
						default:
							break;
					}
					$idet_part_value = $this->textAdjust($idet_part_value);
					$data[] = array('idet_part_id'		  		=> $row['idet_part_id'],
									'idet_part_value'	  		=> $idet_part_value,
									'prt_part_label'	  		=> $row['prt_part_label'],
									'prt_part_type'		  		=> $row['prt_part_type'],
									'prt_part_line_color' 		=> $row['prt_part_line_color'],
									'prt_part_line_size'  		=> $row['prt_part_line_size'],
									'prt_part_sub_type'			=> $row['prt_part_sub_type'],
 									'prt_part_mustflg' 			=> $mustflg[0],
									'prt_part_mustflg1' 		=> $mustflg[1],
    								'prt_part_mustflg2' 		=> $mustflg[2],
    								'prt_part_mustflg3' 		=> $mustflg[3],
    								'prt_part_mustflg4' 		=> $mustflg[4],
    								'prt_part_mustflg_adr'		=> $mustflg[5]
									);
				}//if
			}//while
		}
		return $data;
	}

	/**
	 * お問い合わせデータを保存
	 * @param	data
	 * @return	null or err_str
	 */
	function doSave($data) {
		$this->db->query('BEGIN');
		if ($this->db->update("mdl_extMdlFormGnrtr_tbl_inquiry_header", $data, "ihea_id = " . $data["ihea_id"])) {
			$this->db->query('COMMIT');
			return null;
		}
		$this->db->query('ROLLBACK');
		return "・保存に失敗しました";
	}

}
?>