<?php
/*
	Copyright(c) 2010 Shift Tech Inc. All Rights Reserved.
 	http://www.shift-tech.jp/
*/
require_once(dirname(dirname(dirname(__FILE__)))."/class/CLinqlist.php");
class CSVFile extends CLinqlist
{
	//csvはこの並び順に出力されます。
	var $head=array(
		0=>array(
			'head'=>'対応状態',
			'use' =>true,
			'callback'=>'getInqStatus',
			'part_id'=>0
		),
		1=>array(
			'head'=>'対応日時',
			'use' =>true,
			'callback'=>'getInqResDate',
			'part_id'=>1
		),
		2=>array(
			'head'=>'備考',
			'use' =>true,
			'callback'=>'getInqText',
			'part_id'=>2
		),
		/*3=>array(
			'head'=>'メール',
			'use' =>true,
			'callback'=>'getInqMail',
			'part_id'=>3
		),*/
		3=>array(
			'head'=>'受付日時',
			'use' =>true,
			'callback'=>'getInqDate',
			'part_id'=>3
		)
	);

	function setData($csvdata, $ihea_frm_id=null, $ihea_mail_address=null)
	{
		//▼ヘッダーの作成////////////////
		$where = array();
 	 	$sql = "select th.ihea_id
 	 				  ,td.idet_part_id
					  ,tp.prt_part_label
					  ,td.idet_part_value
					  ,tp.prt_part_type
					  ,tp.prt_part_sub_type
					  ,tp.prt_part_fullname_kanaflg
					  ,tp.prt_part_datetime_fromtoflg
					  ,tp.prt_part_address_zipflg
					  ,tp.prt_part_address_value
					  ,tp.prt_part_datetime_fromtoflg
					  ,tp.prt_part_mail_addressflg
				from   mdl_extMdlFormGnrtr_tbl_inquiry_header as th
					  ,mdl_extMdlFormGnrtr_tbl_inquiry_detail as td
					  ,mdl_extMdlFormGnrtr_tbl_part as tp
 	 		    where  td.idet_ihea_id = th.ihea_id
 	 		    and	   td.idet_part_id = tp.prt_part_id
 	 		    and	   td.idet_frm_id  = tp.prt_frm_id
 	 		    and	   tp.prt_part_type not in('caps', 'fileupload', 'line', 'guidetext')
 	 		    and    th.ihea_active = 1";
		if ($ihea_frm_id) {
			$where[] = $ihea_frm_id;
			$sql	.= " and td.idet_frm_id = ?";
		}
		if ($ihea_mail_address) {
			$where[] = "%" . trim($ihea_mail_address) . "%";
			$sql	.= " and ihea_mail_address Like ?";
		}
 	 	$sql .= " order by th.ihea_responseflg
 	 					  ,th.ihea_request_date DESC
 	 					  ,td.idet_ihea_id desc
 	 					  ,tp.prt_part_order ";
		$result = $this->db->prepare($sql, $where);
		if ($result) {
			$rows=0;
			while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
				if($row["prt_part_type"]=="name"){
				//名前
					if($row["prt_part_fullname_kanaflg"]==1){
					//カナ有り
						if($row["prt_part_sub_type"]=="nameseparate"){
						//姓名分割
							$head["id".$row["idet_part_id"]."_1"]=strip_tags($row["prt_part_label"])."1";
							$head["id".$row["idet_part_id"]."_2"]=strip_tags($row["prt_part_label"])."2";
							$head["id".$row["idet_part_id"]."_3"]=strip_tags($row["prt_part_label"])."3";
							$head["id".$row["idet_part_id"]."_4"]=strip_tags($row["prt_part_label"])."4";

							$_data = explode(UNITS_REPLACE_FLG, $row["idet_part_value"]);
							if(count($_data)==4){
								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"]=$_data[0];
								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_2"]=$_data[1];
								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_3"]=$_data[2];
								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_4"]=$_data[3];
							}else{
								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"]=$_data[0];
								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_2"]="";
								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_3"]=$_data[1];
								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_4"]="";
							}
						}else{
						//姓名分割しない
							$head["id".$row["idet_part_id"]."_1"]=strip_tags($row["prt_part_label"])."1";
							$head["id".$row["idet_part_id"]."_2"]=strip_tags($row["prt_part_label"])."2";

							$_data = explode(UNITS_REPLACE_FLG, $row["idet_part_value"]);
							if(count($_data)<=2){
								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"] = $_data[0];
								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_2"] = $_data[1];
							}else{
								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"] = $_data[0]." ".$_data[1];
								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_2"] = $_data[2]." ".$_data[3];
							}

						}
					}else{
					//カナ無し
						if($row["prt_part_sub_type"]=="nameseparate"){
						//姓名分割
							$head["id".$row["idet_part_id"]."_1"]=strip_tags($row["prt_part_label"])."1";
							$head["id".$row["idet_part_id"]."_2"]=strip_tags($row["prt_part_label"])."2";

							$_data = explode(UNITS_REPLACE_FLG, $row["idet_part_value"]);
							$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"] = $_data[0];
							$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_2"] = $_data[1];
						}else{
						//姓名分割しない
							$head["id".$row["idet_part_id"]]=strip_tags($row["prt_part_label"]."1");
							$_data = explode(UNITS_REPLACE_FLG, $row["idet_part_value"]);

							$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]] = $_data[0];
						}
					}
				}else if($row["prt_part_type"]=="address"){
				//住所
					$_no=1;
					if($row["prt_part_address_zipflg"]==1){
					//郵便番号有り
						if($row["prt_part_sub_type"]=="addressseparate"){
						//分割
							$_data = explode(UNITS_REPLACE_FLG, $row["idet_part_value"]);
							$uses=explode(UNITS_REPLACE_FLG,$row["prt_part_address_value"]);

							$head["id".$row["idet_part_id"]."_1"]=strip_tags($row["prt_part_label"]).$_no++;

							$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"] = $_data[0];
							if($uses[0]==1){
								$head["id".$row["idet_part_id"]."_2"]=strip_tags($row["prt_part_label"]).$_no++;

								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_2"] = $_data[1];
							}
							if($uses[1]==1){
								$head["id".$row["idet_part_id"]."_3"]=strip_tags($row["prt_part_label"]).$_no++;

								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_3"] = $_data[2];
							}
							if($uses[2]==1){
								$head["id".$row["idet_part_id"]."_4"]=strip_tags($row["prt_part_label"]).$_no++;

								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_4"] = $_data[3];
							}
							if($uses[3]==1){
								$head["id".$row["idet_part_id"]."_5"]=strip_tags($row["prt_part_label"]).$_no++;

								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_5"] = $_data[4];
							}


						}else{
						//分割無し
							$head["id".$row["idet_part_id"]."_1"]=strip_tags($row["prt_part_label"]).$_no++;
							$head["id".$row["idet_part_id"]."_2"]=strip_tags($row["prt_part_label"]).$_no++;

							$_data = explode(UNITS_REPLACE_FLG, $row["idet_part_value"]);
							$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"] = $_data[0];
							$_data[0]=null;
							$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_2"] = implode("", $_data);
						}
					}else{
					//郵便番号無し
						if($row["prt_part_sub_type"]=="addressseparate"){
						//分割
							$_data = explode(UNITS_REPLACE_FLG, $row["idet_part_value"]);
							$uses=explode(UNITS_REPLACE_FLG,$row["prt_part_address_value"]);
							if($uses[0]==1){
								$head["id".$row["idet_part_id"]."_1"]=strip_tags($row["prt_part_label"]).$_no++;

								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"] = $_data[1];
							}
							if($uses[1]==1){
								$head["id".$row["idet_part_id"]."_2"]=strip_tags($row["prt_part_label"]).$_no++;

								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_2"] = $_data[2];
							}
							if($uses[2]==1){
								$head["id".$row["idet_part_id"]."_3"]=strip_tags($row["prt_part_label"]).$_no++;

								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_3"] = $_data[3];
							}
							if($uses[3]==1){
								$head["id".$row["idet_part_id"]."_4"]=strip_tags($row["prt_part_label"]).$_no++;

								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_4"] = $_data[4];
							}
						}else{
						//分割無し
							$_data = explode(UNITS_REPLACE_FLG, $row["idet_part_value"]);
							$head["id".$row["idet_part_id"]]=strip_tags($row["prt_part_label"]).$_no++;
							$_data[0]=null;

							$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]] = implode("",$_data);
						}
					}
				}else if($row["prt_part_type"]=="datetime"){
				//日時
					$_no=1;
					$_data = explode(UNITS_REPLACE_FLG, $row["idet_part_value"]);
					if(count($_data)==6 || count($_data)==3){
						//ヘッダーセット
						if($row["prt_part_datetime_fromtoflg"]==1){
						//From-Toの時
							$head["id".$row["idet_part_id"]."_1"]=strip_tags($row["prt_part_label"]).$_no++;
							$head["id".$row["idet_part_id"]."_2"]=strip_tags($row["prt_part_label"]).$_no++;
						}else{
							$head["id".$row["idet_part_id"]."_1"]=strip_tags($row["prt_part_label"]).$_no++;
						}
						//データセット
						if($row["prt_part_sub_type"]=="datetime"){
							if($row["prt_part_datetime_fromtoflg"]==1){
							//From-Toの時
								$day = $_data[0];
								$hm = $_data[1]."時".$_data[2]."分";
								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"].=$day." ".$hm;

								$day = $_data[3];
								$hm = $_data[4]."時".$_data[5]."分";
								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_2"].=$day." ".$hm;
							}else{
								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"]=$_data[0]." ".$_data[1]."時".$_data[2]."分";
							}
						}else{
							if($row["prt_part_datetime_fromtoflg"]==1){
							//From-To 日 or 時
								if($row["prt_part_sub_type"]=="date"){//日
									$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"]=$_data[0];
									$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_2"]=$_data[3];
								}else{//時
									$_hm = "";
									if($_data[1]!="") $_hm = $_data[1]."時".$_data[2]."分";
									$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"]=$_hm;
									$_hm = "";
									if($_data[4]!="") $_hm = $_data[4]."時".$_data[5]."分";
									$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_2"]=$_hm;
								}
							}else{
							//From
								if($row["prt_part_sub_type"]=="date"){//日
									$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"]=$_data[0];
								}else{//時
									$_hm = "";
									if($_data[1]!="") $_hm = $_data[1]."時".$_data[2]."分";
									$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"]=$_hm;
								}
							}
						}
					}else
					//以下旧データ形式
					if($row["prt_part_sub_type"]=="datetime"){
					//日時
						if($row["prt_part_datetime_fromtoflg"]==1){
						//From-Toの時
							$head["id".$row["idet_part_id"]."_1"]=strip_tags($row["prt_part_label"]).$_no++;
							$head["id".$row["idet_part_id"]."_2"]=strip_tags($row["prt_part_label"]).$_no++;

							$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"]=$_data[0]." ".$_data[1]."時".$_data[2]."分";
							$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_2"]=$_data[3]." ".$_data[4]."時".$_data[5]."分";
						}else{
						//Fromのみ
							$head["id".$row["idet_part_id"]."_1"]=strip_tags($row["prt_part_label"]).$_no++;

							$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"]=$_data[0]." ".$_data[1]."時".$_data[2]."分";
						}
					}else if($row["prt_part_sub_type"]=="date"){
					//日のみ
						if($row["prt_part_datetime_fromtoflg"]==1){
						//From-Toの時
							$head["id".$row["idet_part_id"]."_1"]=strip_tags($row["prt_part_label"]).$_no++;
							$head["id".$row["idet_part_id"]."_2"]=strip_tags($row["prt_part_label"]).$_no++;

							$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"]=$_data[0];
							$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_2"]=$_data[1];
						}else{
						//Fromのみ
							$head["id".$row["idet_part_id"]."_1"]=strip_tags($row["prt_part_label"]);

							$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"]=$_data[0];
						}
					}else if($row["prt_part_sub_type"]=="time"){
					//時のみ
						if($row["prt_part_datetime_fromtoflg"]==1){
						//From-Toの時
							$head["id".$row["idet_part_id"]."_1"]=strip_tags($row["prt_part_label"]).$_no++;
							$head["id".$row["idet_part_id"]."_2"]=strip_tags($row["prt_part_label"]).$_no++;

							$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"]=$_data[0]."時".$_data[1]."分";
							$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_2"]=$_data[2]."時".$_data[3]."分";
						}else{
						//Fromのみ
							$head["id".$row["idet_part_id"]."_1"]=strip_tags($row["prt_part_label"]);

							if(preg_match("/^\d{4}-(\d|\d{2})-(\d|\d{2})$/", $_data[0])){
								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"]=$_data[0];
							}else{
								$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"]=$_data[0]."時".$_data[1]."分";
							}
						}
					}
				}else if($row["prt_part_type"]=="mail"){
				//メールアドレス
					$_no=1;
					$mail = explode(UNITS_REPLACE_FLG, $row["prt_part_mail_addressflg"]);
					$_data = explode(UNITS_REPLACE_FLG, $row["idet_part_value"]);
					if($mail[0]==1 || count($mail)==1){
						$head["id".$row["idet_part_id"]."_1"]=strip_tags($row["prt_part_label"]).$_no++;

						$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"]=$_data[0];
					}
					if($mail[1]==1){
						$head["id".$row["idet_part_id"]."_1"]=strip_tags($row["prt_part_label"]).$_no++;

						$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_1"]=$_data[1];
					}
					if($mail[2]==1){
						$head["id".$row["idet_part_id"]."_2"]=strip_tags($row["prt_part_label"]).$_no++;

						$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]."_2"]=$_data[2];
					}
				}else{
					if($row["prt_part_type"]=="text"){
						$row["idet_part_value"] = str_replace(UNITS_REPLACE_FLG, "\n", $row["idet_part_value"]);
					}else if($row["prt_part_type"]=="telphone"){
						$row["idet_part_value"] = str_replace(UNITS_REPLACE_FLG, "-", $row["idet_part_value"]);
					}else if($row["prt_part_type"]=="select"){
						$row["idet_part_value"] = str_replace(UNITS_REPLACE_FLG, ",", $row["idet_part_value"]);
					}
					$head["id".$row["idet_part_id"]]=strip_tags($row["prt_part_label"]);
					$this->data[$row["ihea_id"]]["id".$row["idet_part_id"]]=$row["idet_part_value"];
				}
			}

			foreach($head as $k => $v){
				$this->head[]=array(
					"head"=>$v,
					"part_id"=>$k//preg_replace("/_[0-9]$/", "", $k)
				);
			}
			//▲ヘッダーの作成////////////////
			//▼出力データの作成/////////////
			foreach($this->data as $k=>$v){
				foreach($csvdata as $kk=>$vv){
					if($k==$vv[0]){
						$this->data[$k] = array_merge(array($vv[1],$vv[2],$vv[3],$vv[5]) ,$this->data[$k]);

					}
				}
			}

			$outPutData=array();
			//ヘッダーセット
			foreach($this->head as $col => $v){
				$outPutData[0][]=$v['head'];
			}

			$rows=1;
			foreach($this->data as $k=>$v){
				foreach($this->head as $kk => $vv){
					$outPutData[$rows][] = !isset($v[$vv["part_id"]]) || is_null($v[$vv["part_id"]])? "" : $v[$vv["part_id"]];
				}
				$rows++;
			}

			$len = count($outPutData);
			$count = 0;
			foreach($outPutData as $k => $row){
				$output.=$this->putcsvfromarray($row);
				$count++;
				if($count < $len){
					$output.="\n";
				}
			}
			//var_dump($outPutData);
			//▲出力データの作成/////////////
			$this->downloadCsv($output, 'inquiry.csv');
		}
	}

	function perform($data)
	{
		if(!is_null($this->data)){
			$outPutData=array();
			//ヘッダーセット
			foreach($this->head as $col => $v){
				if($v['use']){
					if(isset($this->data[0][$col]) || $this->data[0][$col]==null ){
						$outPutData[0][]=$v['head'];
					}
				}
			}
			//データ行セット
			$rowCount=1;
			foreach($this->data as $k => $row){
				foreach($this->head as $col => $val){
					if($val['use']){
						$outPutData[$rowCount][]= isset($val['callback']) ? $this->$val['callback']($row[$col],$row) : $row[$col];
					}
				}
				$rowCount++;
			}

			$len = count($outPutData);
			$count = 0;
			foreach($outPutData as $k => $row){
				$output.=$this->putcsvfromarray($row);
				$count++;
				if($count < $len){
					$output.="\n";
				}
			}
			$this->downloadCsv($output, 'customer.csv');
		}
        return 'saas-admin/csv.tpl';
	}

	/**
	 * Array→csv出力
	 * @param $output
	 * @param $in_enc
	 * @param $out_enc
	 */
	function putcsvfromarray($output, $in_enc="UTF-8", $out_enc="SJIS"){
		$data=array();
		foreach($output as $value){
			$a=array('"'=>'""');
			$data[]='"'.strtr(mb_convert_encoding($value,$out_enc,$in_enc),$a).'"';
		}
		return implode(",",$data);
	}

	/**
	 * csv出力
	 * @param $data
	 * @param $filename
	 */
	function downloadCsv($data, $filename=null){
		if(!isset($filename) || $filename==""){
			$filename = 'undefined.csv';
		}
		$filesize = strlen($data);
		header("Pragma: public");
		header("Content-Disposition: attachment; filename={$filename}");
		header("Content-Length:{$filesize}");
		header("Content-Type: application/octet-stream");
		echo $data;
		exit;
	}
}
?>