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


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

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

class inqpreAct extends CLinqpre
{
	function inqpreAct(&$bsmactlr)
	{
		$this->__construct($bsmactlr);
	}

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

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

	function action()
	{
		$tpl_file = "index.tpl";

		//Form設定
		$this->setElements();
		//ボタン
		$this->setButtons();
		$req = isset($_POST['buttons']) ? $_POST['buttons'] : array();
        $p = isset($_REQUEST['p']) ? $_REQUEST['p'] : 1;
		//「一覧に戻る」ボタンクリック時
		if (isset($req['BTNList'])&& $req['BTNList']!=null) {
			header('location:../inqlist/index.php?p='.$p);
			exit();
		//「保存」ボタンクリック時
		} else if (isset($req['BTNsave']) && $req['BTNsave'] != null) {
			$post = $this->_form->getSubmitValues();

			//対応済み場合
			$headData["ihea_id"] = $post['ihea_id'];
			if ($post['ihea_responseflg']) {
				$headData["ihea_responseflg"] = $post['ihea_responseflg'];
			} else {
				$headData["ihea_responseflg"] = 0;
			}
			$headData["ihea_response_comment"] = $post["ihea_response_comment"];
			$headData["ihea_response_user"]	   = $_SESSION["usr_info"]["usr_code"];
			if ($post['ihea_responseflg']) {
				$headData["ihea_response_date"]	   = "now()";
			} else {
				$headData["ihea_response_date"]	   = null;
			}
			$headData["ihea_u_date"]		   = "now()";

			$rtn = $this->doSave($headData);
			if ($rtn == null) {
				header('location:../inqlist/index.php?p='.$p);
			} else {
				$this->_form->setElementError("", $rtn);
			}
		//削除ボタンをクリック時
		} else if(isset($req['BTNdelete']) && $req['BTNdelete'] != null) {
		       $ihea_id		= $_REQUEST["ihea_id"];
			   $data = array('ihea_active' => '0',
						     'ihea_u_date' => 'now');
			   $this->db->query('BEGIN');
			   $this->db->update("mdl_extMdlFormGnrtr_tbl_inquiry_header", $data, "ihea_id=".$ihea_id);
			   if ($this->db->update("mdl_extMdlFormGnrtr_tbl_inquiry_header", $data, "ihea_id=".$ihea_id)) {
						$this->db->query('COMMIT');
						$this->_form->setElementError(""," 削除しました");
						header('location:../inqlist/index.php?p='.$p);
				} else {
						$this->_form->setElementError(""," 削除に失敗しました");
						return false;
				}
		//ファイルをダウンロードクリック時
		}else if(!empty($_GET['hid']) && !empty($_GET['fid'])){
			$filename = "";
			//$file;
			$sql = "select idet_part_value from mdl_extMdlFormGnrtr_tbl_inquiry_detail where idet_ihea_id = ? and idet_part_id = ?";
			$where = array($_GET['hid'],$_GET['fid']);
			$result = $this -> db -> prepare($sql,$where);
			if($result){
				while($row = $result -> fetchRow(DB_FETCHMODE_ASSOC)){
					$file = $row['idet_part_value'];
				}
			}else{
				$this->_form->setElementError(""," ファイルのダウンロードに失敗しました。");
				return false;
			}
			$file = explode("%%%",$file);
			//$filepath = '../../../../../global-image/units/upfiles/'.$file[1];
			$filepath = PIC_PATH."/units/upfiles/".$file[1];
			$ext = strtolower(array_pop(explode(".",$file[0])));

			if($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png' || $ext == 'bmp' || $ext == 'gif'){
				$replace = array('script','java','javascript');
				$resource = str_replace($replace,"",$resource);
			}

			if(strstr($_SERVER['HTTP_USER_AGENT'],'Safari')){
				$filename = $file[1];
			}else{
				$filename = $file[0];
			}

			mb_convert_variables("SJIS","auto",$filename);
			$resource = file_get_contents($filepath);
			header("Cache-Control: public");
			header("Pragma: public");
			header('Content-Type: '.$this->get_mine_type($ext).';charset=Shift-JIS');
			header("Content-Disposition: attachment; filename=".$filename);
			print $resource;
			exit;

		} else {
			//何もクリックされていない場合は情報取得
			//DBデータ取得
			$list = $this->getData($_REQUEST["ihea_id"], $_REQUEST["ihea_frm_id"]);
			$this->bsmactlr->view->assign('list', $list);
			$this->bsmactlr->view->assign('ihea_id',$_REQUEST["ihea_id"]);
			$this->_form->setConstants(array('ihea_response_comment'=>$_REQUEST["ihea_response_comment"]));
		}

		//print_r($list);
		$renderer = new HTML_QuickForm_Renderer_ArraySmarty($this->bsmactlr->view);
		$this->_form->accept($renderer);
		$this->bsmactlr->view->assign('form', $renderer->toArray());
        $this->bsmactlr->view->assign('p', $p);
		//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/inqpre/".$tpl_file));
	}

	function get_mine_type($ext){
		switch($ext){
			case "js" :
                return "application/x-javascript";

            case "json" :
                return "application/json";

            case "jpg" :
            case "jpeg" :
            case "jpe" :
            case "png" :
            case "gif" :
            case "bmp" :
            case "tiff" :
                return "image/".$ext;

            case "css" :
                return "text/css";

            case "xml" :
                return "application/xml";

            case "doc" :
            case "docx" :
                return "application/msword";

            case "xls" :
            case "xlt" :
            case "xlm" :
            case "xld" :
            case "xla" :
            case "xlc" :
            case "xlw" :
            case "xll" :
                return "application/vnd.ms-excel";

            case "ppt" :
            case "pps" :
                return "application/vnd.ms-powerpoint";

            case "rtf" :
                return "application/rtf";

            case "pdf" :
                return "application/pdf";

            case "html" :
            case "htm" :
            case "php" :
                return "text/html";

            case "txt" :
                return "text/plain";

            case "mpeg" :
            case "mpg" :
            case "mpe" :
                return "video/mpeg";

            case "mp3" :
                return "audio/mpeg3";

            case "wav" :
                return "audio/wav";

            case "aiff" :
            case "aif" :
                return "audio/aiff";

            case "avi" :
                return "video/msvideo";

            case "wmv" :
                return "video/x-ms-wmv";

            case "mov" :
                return "video/quicktime";

            case "zip" :
                return "application/zip";

            case "tar" :
                return "application/x-tar";

            case "swf" :
                return "application/x-shockwave-flash";
            default :
            	return "unknown/" .trim($ext);
        }
	}
}

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

?>