<?php
//お約束の呪文/////////////////////////////////
//ログインチェック、設定ファイル読み込み、Smarty Viewの生成を行う
require_once("../../../../../app_path.php");
require_once(APP_PATH . '/BSMAdminController.php');
//require_once("../common/CSVFile.php");
require_once("../common/csv.php");
$bsmactlr = new BSMAdminController();
///////////////////////////////////////////////

//コントローラ部分
//好きに書いてよし！

//Modelクラスを読み込み
//DBからのデータ読み込み、書き込み、大半の処理はこのModelクラスに書いてください。
require_once("../../class/CLinqlist.php");

class inqlistAct extends CLinqlist
{
	function inqlistAct(&$bsmactlr)
	{
		$this->__construct($bsmactlr);
	}

	function __construct(&$bsmactlr)
	{
		CLinqlist::__construct();
		$this->bsmactlr = $bsmactlr;

		//管理権限によるメニュー表示制御
		$this->setAuth($this->bsmactlr->view);
	}

	function action()
	{
		$tpl_file = "index.tpl";
		if (isset($_REQUEST["frm_id"]) && $_REQUEST["frm_id"]!=null) {
			$_SESSION['ihea_frm_id'] = $_REQUEST["frm_id"];
		}

		if($_REQUEST["p"] == null){
			unset($_SESSION['UserSearch']);
		}

		//Form設定
		$this->setElements();

		//選択タイプ集計ボタン
		$this->bsmactlr->view->assign('exist_select', $this->existPartSelect($_SESSION['ihea_frm_id']));
		$this->bsmactlr->view->assign('frm_id', $_SESSION['ihea_frm_id']);


		$this->_form->setDefaults($_SESSION['UserSearch']);
		//「表示」ボタンクリック時
		if (isset($_REQUEST['BTNsearch']) && $_REQUEST['BTNsearch']!=null) {
			$_SESSION['UserSearch']['ihea_mail_address'] = isset($_REQUEST['ihea_mail_address']) ? $_REQUEST['ihea_mail_address'] : "";
			$_SESSION['UserSearch']['ihea_responseflg'] = isset($_REQUEST['ihea_responseflg']) ? $_REQUEST['ihea_responseflg'] : "";

			$_SESSION['UserSearch']['ihea_request_date_to'] = isset($_REQUEST['ihea_request_date_to']) ? $_REQUEST['ihea_request_date_to'] : "";
			$_SESSION['UserSearch']['ihea_request_date_from'] = isset($_REQUEST['ihea_request_date_from']) ? $_REQUEST['ihea_request_date_from'] : "";

		//「CSV書き出す」ボタンクリック時
		} elseif (isset($_REQUEST['BTNcsv']) && $_REQUEST['BTNcsv']!=null) {
			$post		  = $this->_form->getSubmitValues();
		  	//$_csv		  = new CSV();
		  	//$title_data   = $this->gettitleData($_SESSION['ihea_frm_id'], $post["ihea_mail_address"]);
		  	$csv_data	  = $this->getcsvData($_SESSION['ihea_frm_id'], $post["ihea_mail_address"]);
		  	//$countcsv	  = count($csv_data);
		  	//$counttitle = count($title_data);
		  	//$value_data = $this->getvalueData($_SESSION['ihea_frm_id'], $post["ihea_mail_address"]);
		  	//CSV出力データを編集
			$csv = new CSVFile();
			$csv->setData($csv_data, $_SESSION['ihea_frm_id'], $post["ihea_mail_address"]);
			exit;
			/*
		  	for ($i = 0; $i < $countcsv; $i++) {
			  	for ($y = 0, $k = 6; $y < $counttitle; $y++, $k++) {
				  	$val = $csv_data[$i][0].$title_data[$y]['prt_part_id'];
				  	if (array_key_exists($val, $value_data)) {
				  			$idet_part_value = "";
							switch ($value_data[$val]['prt_part_type']) {
								//テキスト
								case "text":
									if($value_data[$val]['prt_part_sub_type'] == 'single'){
										$idet_part_value = $value_data[$val]['idet_part_value'];
									}else{
										$textArea = '"'.str_replace(UNITS_REPLACE_FLG,"\n",$value_data[$val]['idet_part_value']).'"';
										$idet_part_value =$textArea;
									}
									break;
								//選択肢
								case "select":
									$idet_part_value = ereg_replace(UNITS_REPLACE_FLG, "　", $value_data[$val]['idet_part_value']);
									break;
								//名前
								case "name":
									//分割時
									if ($value_data[$val]['prt_part_sub_type'] == "nameseparate") {
										if($value_data[$val]['prt_part_fullname_kanaflg'] == '1'){
											//フリガナが有
											$name_value	 = explode(UNITS_REPLACE_FLG, $value_data[$val]['idet_part_value']);
											if(isset($name_value[0]) && $name_value[0] != ''){
												$idet_part_value = $name_value[0];
											}
											if(isset($name_value[1]) && $name_value[1] != ''){
												if(isset($name_value[0]) && $name_value[0] != ''){
													$idet_part_value .= "　";
												}
												$idet_part_value .= $name_value[1];
											}
											if(isset($name_value[2]) && $name_value[2] != ''){
												if(isset($name_value[0]) && $name_value[0] != ''
												|| isset($name_value[1]) && $name_value[1] != ''){
													$idet_part_value .= "  ";
												}
												$idet_part_value .= $name_value[2];
											}
											if(isset($name_value[3]) && $name_value[3] != ''){
												if(isset($name_value[2]) && $name_value[2] != ''){
													$idet_part_value .= "　";
												}
												$idet_part_value .= $name_value[3];
											}
										}else{
											//フリガナが無
											$idet_part_value = ereg_replace(UNITS_REPLACE_FLG, "　", $value_data[$val]['idet_part_value']);
										}
									} else {
										$idet_part_value = ereg_replace(UNITS_REPLACE_FLG, "　", $value_data[$val]['idet_part_value']);
									}
									break;
								case "address":
									if ($value_data[$val]['prt_part_sub_type'] == "addressseparate") {
										$address_value	 = explode(UNITS_REPLACE_FLG, $value_data[$val]['idet_part_value']);
										if (isset($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] != ''){
												$idet_part_value .= "" ;
											}
											$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 .= "";
											}
											$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 .= "";
											}
											$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 .= "";
											}
											$idet_part_value .= $address_value[4];
										}
									} else {
										$address_value	 = explode(UNITS_REPLACE_FLG, $value_data[$val]['idet_part_value']);
										if (isset($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] != ''){
												$idet_part_value .= "" ;
											}
											$idet_part_value .= $address_value[1];
										}
									}
									break;
								case "datetime":
									if ($value_data[$val]['prt_part_datetime_fromtoflg'] == 1) {
										$date_value	 = explode(UNITS_REPLACE_FLG, $value_data[$val]['idet_part_value']);
										if ($value_data[$val]['prt_part_sub_type'] == 'date') {
											if(isset($date_value[0]) && $date_value[0] != ''){
												$idet_part_value = $date_value[0];
											}
											if(isset($date_value[1]) && $date_value[1] != ''){
												if(isset($date_value[0]) && $date_value[0] != ''){
													$idet_part_value .= "　～　";
												}
												$idet_part_value .= $date_value[1];
											}
										}else if ($value_data[$val]['prt_part_sub_type'] == 'time') {
											if(isset($date_value[0]) && $date_value[0] != ''){
												$idet_part_value = $date_value[0];
											}
											if(isset($date_value[1]) && $date_value[1] != ''){
												if(isset($date_value[0]) && $date_value[0] != ''){
													$idet_part_value .= "時";
												}
												if($date_value[1]=="0"){
													$idet_part_value .= "00分";
												}else{
													$idet_part_value .= $date_value[1]."分";
												}
											}
											if(isset($date_value[2]) && $date_value[2] != ''){
												if(isset($date_value[0]) && $date_value[0] != ''
												|| isset($date_value[1]) && $date_value[1] != ''){
													$idet_part_value .= "　～　";
												}
												$idet_part_value .= $date_value[2];
											}
											if(isset($date_value[3]) && $date_value[3] != ''){
												if(isset($date_value[2]) && $date_value[2] != ''){
													$idet_part_value .= "時";
												}
												if($date_value[3]=="0"){
													$idet_part_value .= "00分";
												}else{
													$idet_part_value .= $date_value[3]."分";
												}
											}

										}else if ($value_data[$val]['prt_part_sub_type'] == 'datetime') {
											if(isset($date_value[0]) && $date_value[0] != ''){
												$idet_part_value = $date_value[0];
											}
											if(isset($date_value[1]) && $date_value[1] != ''){
												if(isset($date_value[0]) && $date_value[0] != ''){
													$idet_part_value .= "　";
												}
												$idet_part_value .= $date_value[1];
											}
											if(isset($date_value[2]) && $date_value[2] != ''){
												if(isset($date_value[1]) && $date_value[1] != ''){
													$idet_part_value .= "時";
												}
												if($date_value[2]=="0"){
													$idet_part_value .= "00分";
												}else{
													$idet_part_value .= $date_value[2]."分";
												}
											}
											if(isset($date_value[3]) && $date_value[3] != ''){
												if(isset($date_value[0]) && $date_value[0] != ''
												  || isset($date_value[1]) && $date_value[1] != ''
												  || isset($date_value[2]) && $date_value[2] != ''){
													$idet_part_value .= "　～　";
												}
												$idet_part_value .= $date_value[3];
											}
											if(isset($date_value[4]) && $date_value[4] != ''){
												if(isset($date_value[3]) && $date_value[3] != ''){
													$idet_part_value .= "　";
												}
												$idet_part_value .= $date_value[4];
											}
											if(isset($date_value[5]) && $date_value[5] != ''){
												if(isset($date_value[4]) && $date_value[4] != ''){
													$idet_part_value .= "時";
												}
												if($date_value[5]=="0"){
													$idet_part_value .= "00分";
												}else{
													$idet_part_value .= $date_value[5]."分";
												}
											}
										}
									} else {
										if ($value_data[$val]['prt_part_sub_type'] == 'date') {
											$date_value	 = explode(UNITS_REPLACE_FLG, $value_data[$val]['idet_part_value']);
											if(isset($date_value[0]) && $date_value[0] != ''){
												$idet_part_value = $date_value[0];
											}
										}else if ($value_data[$val]['prt_part_sub_type'] == 'time') {
											$date_value	 = explode(UNITS_REPLACE_FLG, $value_data[$val]['idet_part_value']);
											if(isset($date_value[0]) && $date_value[0] != ''){
												$idet_part_value = $date_value[0];
											}
											if(isset($date_value[1]) && $date_value[1] != ''){
												if(isset($date_value[0]) && $date_value[0] != ''){
													$idet_part_value .= "時";
												}
												if($date_value[1]=="0"){
													$idet_part_value .= "00分";
												}else{
													$idet_part_value .= $date_value[1]."分";
												}
											}
										}else if ($value_data[$val]['prt_part_sub_type'] == 'datetime') {
											$date_value	 = explode(UNITS_REPLACE_FLG, $value_data[$val]['idet_part_value']);
											if(isset($date_value[0]) && $date_value[0] != ''){
												$idet_part_value = $date_value[0];
											}
											if(isset($date_value[1]) && $date_value[1] != ''){
												if(isset($date_value[0]) && $date_value[0] != ''){
													$idet_part_value .= "　";
												}
												$idet_part_value .= $date_value[1];
											}
											if(isset($date_value[2]) && $date_value[2] != ''){
												if(isset($date_value[1]) && $date_value[1] != ''){
													$idet_part_value .= "時";
												}
												if($date_value[2]=="0"){
													$idet_part_value .= "00分";
												}else{
													$idet_part_value .= $date_value[2]."分";
												}

											}
										}
									}
									break;
								case "telphone":
									if ($value_data[$val]['prt_part_sub_type'] == "threepart") {
										$telphone_value	 = explode(UNITS_REPLACE_FLG, $value_data[$val]['idet_part_value']);
										$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 = "　".$value_data[$val]['idet_part_value'];
									}
									break;
								case "web":
									$idet_part_value = $value_data[$val]['idet_part_value'];
									break;
								case "mail":
									$idet_part_value = ereg_replace(UNITS_REPLACE_FLG, "　", $value_data[$val]['idet_part_value']);
									break;
								default:
									break;
							}
							$csv_data[$i][$k] = $idet_part_value;
					  	} else {
						  	$csv_data[$i][$k] = "";
						}
			  		}
				}
				//print_r($csv_data);
		  		for ($i = 0; $i < $countcsv; $i++) {
		  			unset($csv_data[$i][0]);
			  	}

			  	$title = array('対応状態', '対応日時', '備考', 'メール', '日時'); //csvタイトルセット

		  		foreach($title_data as $key => $val){
				  	$title[] = $val['prt_part_label'];
			  	}

			  	$frm_name = $this->getFormName($_SESSION['ihea_frm_id']);
			  	$code		= "utf-8";
			  	$_csv->csv_output($title, $csv_data, $code);
			  	exit;
			*/
		  	}

			$rtn  = array();
			//一覧画面データ取得;
			$ihea_mail_address = isset($_SESSION['UserSearch']['ihea_mail_address']) ? $_SESSION['UserSearch']['ihea_mail_address'] : "";
			$ihea_responseflg = isset($_SESSION['UserSearch']['ihea_responseflg']) ? $_SESSION['UserSearch']['ihea_responseflg'] : "";

			$ihea_request_date['to'] = isset($_SESSION['UserSearch']['ihea_request_date_to']) ? $_SESSION['UserSearch']['ihea_request_date_to'] : "";
			$ihea_request_date['from'] = isset($_SESSION['UserSearch']['ihea_request_date_from']) ? $_SESSION['UserSearch']['ihea_request_date_from'] : "";

			$list = $this->getData($_SESSION['ihea_frm_id'], $ihea_mail_address,$ihea_responseflg,$ihea_request_date);

			//PCメール、携帯メールを分割
			foreach($list as $key => $row){
				$list[$key][mail] = explode("　",$row[ihea_mail_address]);
			}

			$p = isset($_REQUEST['p']) ? $_REQUEST['p'] : 1;
			$rtn = $this->formPager(AD_PAGER_LIMIT, $p, $list, $method='GET', $urlvar='p');

			$this->bsmactlr->view->assign('csv_download','./index.php?'.'BTNcsv='.urlencode("CSVで書き出す"));
			$this->bsmactlr->view->assign('p', $p);
			$this->bsmactlr->view->assign('list', $rtn["pagedata"]);
			$this->bsmactlr->view->assign('Pages', $rtn["pageNavi"]["all"]);

			$renderer = new HTML_QuickForm_Renderer_ArraySmarty($this->bsmactlr->view);
			$this->_form->accept($renderer);
			$this->bsmactlr->view->assign('form', $renderer->toArray());

			//set template file
			$this->bsmactlr->view->assign("MENU", $this->_tpl_path."/admin/common/menu.tpl");
			$this->bsmactlr->setHeadhtml('<link href="../css/imported.css" rel="stylesheet" type="text/css" media="all">');
			$this->bsmactlr->DisplayPage($this->bsmactlr->setTpl("/extMdlFormGnrtr/admin/inqlist/".$tpl_file));
		}
	}

$frm = new inqlistAct($bsmactlr);
$frm->action();

?>