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

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

class partlistAct extends CLpartlist
{
	function partlistAct(&$bsmactlr)
	{
		$this->__construct($bsmactlr);
	}

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

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

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

		//ボタン
		$this->setPrevButtons();

		//frm_defaultflg
		$this->bsmactlr->view->assign('frm_defaultflg', $_SESSION['frm_defaultflg']);
		$this->bsmactlr->view->assign('frm_id', $_SESSION['frm_id']);

		//javascirptパース
		$this->bsmactlr->view->assign('MODULES_PATH', MODULES_PATH);

		//選択肢値のデータ
		$SelectValue = $this->getPartSelectValue($_SESSION['frm_id']);
		//プレビューのデータ
		$data = $this->getPartData($_SESSION['frm_id']);
		$js = $this->setJs($data);
		$this->bsmactlr->view->assign('jsdatetime', $js);
		//Elements設定
		$this->setPartElements($data, $SelectValue, &$this->_form);

		$this->bsmactlr->view->assign('SelectValue', $SelectValue);
		$this->bsmactlr->view->assign('tplData', $data);

		//処理
		$req = isset($_POST['buttons']) ? $_POST['buttons'] : array();

		//保存ボタン
		if(isset($req['BTNsave']) && $req['BTNsave'] != null) {
				$file_flag = "";
				for($i=0; $i<count($data); $i++) {
					if ($data[$i]['prt_part_type'] == 'fileupload' && $data[$i]['prt_part_fileupload_limitflg'] == 1) {
						$file_name = 'file'.$data[$i]['prt_part_id'];
						$upfile_name = explode(".", $_FILES[$file_name]["name"]);

						//ファイルアップロードの値がある場合
						if (isset($upfile_name[0]) && $upfile_name[0] != null) {
							$file_type = null;

							if($data[$i]['prt_part_fileupload_filetype1'] == '1'){
								$file_type = $file_type."&nbsp;pdf&nbsp;";
								if (isset($upfile_name[1]) && $upfile_name[1] == "pdf") {
									$file_flag = 1;
								}
							}

							if($data[$i]['prt_part_fileupload_filetype2'] == '1'){
								$file_type = $file_type."&nbsp;xls&nbsp;";
								if (isset($upfile_name[1]) && $upfile_name[1] == "xls") {
									$file_flag = 1;
								}
							}

							if($data[$i]['prt_part_fileupload_filetype3'] == '1'){
								$file_type = $file_type."&nbsp;doc&nbsp;";
								if (isset($upfile_name[1]) && $upfile_name[1] == "doc") {
									$file_flag = 1;
								}
							}

							if($data[$i]['prt_part_fileupload_filetype4'] == '1'){
								$file_type = $file_type."&nbsp;ppt&nbsp;";
								if (isset($upfile_name[1]) && $upfile_name[1] == "ppt") {
									$file_flag = 1;
								}
							}

							if($data[$i]['prt_part_fileupload_filetype5'] == '1'){
								$file_type = $file_type."&nbsp;jpeg&nbsp;";
								if (isset($upfile_name[1]) && $upfile_name[1] == "jpeg") {
									$file_flag = 1;
								}
							}

							if($data[$i]['prt_part_fileupload_filetype6'] == '1'){
								$file_type = $file_type."&nbsp;png&nbsp;";
								if (isset($upfile_name[1]) && $upfile_name[1] == "png") {
									$file_flag = 1;
								}
							}

							if($data[$i]['prt_part_fileupload_filetype7'] == '1'){
								$file_type = $file_type."&nbsp;zip&nbsp;";
								if (isset($upfile_name[1]) && $upfile_name[1] == "zip") {
									$file_flag = 1;
								}
							}

							if (isset($data[$i]['prt_part_fileupload_subfiletype'])) {
								$sub_type = explode(";", $data[$i]['prt_part_fileupload_subfiletype']);

								foreach ($sub_type as $key => $val) {
									$file_type = $file_type."&nbsp;".$val."&nbsp;";

									if (isset($upfile_name[1]) && $upfile_name[1] == $val) {
										$file_flag = 1;
									}
								}
							}

							if ($file_flag != 1) {
								$this->_form->setElementError("","・「".$data[$i]['prt_part_label']."」できるファイル形式は".$file_type."ファイルとなっております。");
							}
						}
					}
				}

				//フォームの公開状態フラグを設定（0..未公開 1..公開）
				$this->updateFrmActflg($_SESSION['frm_id'], 1);
				header('location:../frmlist/index.php');
				exit();
		}

		//キャンセルボタン
		if(isset($req['BTNcancel']) && $req['BTNcancel'] != null) {
			header('location:../partlist/index.php?frm_id='.$_SESSION['frm_id'].'&frm_defaultflg='.$_SESSION['frm_defaultflg']);
			exit();
		}

		//登録エラー時
		if(!empty($this->_form->_errors)) {
			$this->bsmactlr->view->assign('ERROR',$this->_form->_errors);
			foreach($this->_form->_errors as $key => $val) {
				$this->_form->updateElementAttr($key, array("class"=>"err"));
			}
		}

		$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/partlist/".$tpl_file));
	}
}

$frm = new partlistAct($bsmactlr);
$frm->action();
?>