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

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

class partaddAct extends CLpartadd
{
	function partaddAct(&$bsmactlr)
	{
		$this->__construct($bsmactlr);
	}

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

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

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

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

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

		$frm_id = $_SESSION['frm_id'];
		$this->bsmactlr->view->assign('frm_id', $_SESSION['frm_id']);
		//処理
		$req = isset($_POST['buttons']) ? $_POST['buttons'] : array();
		
		if($_SESSION['extFrmGnrtAdmin_token']!=$_REQUEST["token"]){
			$_SESSION['formg_tokenError']=1;
			$this->putTokenError();
			exit;
		}

		//保存ボタン
		if(isset($req['BTNadd']) && $req['BTNadd'] != null) {
			$post = $this->_form->getSubmitValues();
			if(!$this->check_token($post['token'])){
				$this->putTokenError();
			}
			//パーツタイプがガイドテキスト・罫線対象外,エラーチェック
			$this->setRules($post['part']);
			if($this->_form->validate()){
				//パーツの表示順の最大値を取得
				$part_order_new = $this->getPartOrder($frm_id);
				$data = array();

				$data["prt_frm_id"] 				= $frm_id;
				$data["prt_part_label"] 			= $post['prt_part_label'];
				$data["prt_part_comment"] 			= $post['prt_part_comment'];
				$data["prt_part_comment_position"] 	= $post['jyuusyo'];
				//追加 BY chenwenhui
				$data["prt_part_comment_color"] 	= $post['prt_part_comment_color'];
				$data["prt_part_line_color"] 	    = '#000000';
				$data["prt_part_type"] 				= $post['part'];
				$data["prt_c_date"] 				= 'now';
				$data["prt_part_order"] 			= $part_order_new;

				//バーツのディフォトの種類
				if ($post['part'] == "text") {
					$data["prt_part_sub_type"] = "single";
				} elseif ($post['part'] == "select") {
					$data["prt_part_sub_type"] = "radiobutton";
				}elseif ($post['part'] == "name") {
					$data["prt_part_sub_type"] = "nametogether";
				}elseif ($post['part'] == "address") {
					$data["prt_part_sub_type"] = "addresstogether";
				}elseif ($post['part'] == "datetime") {
					$data["prt_part_sub_type"] = "date";
				}elseif ($post['part'] == "telphone") {
					$data["prt_part_sub_type"] = "onepart";
				}

				$rtn = $this->doSave($data);

				if($rtn == true) {
					$this->bsmactlr->view->assign('CLOSESCRIPTFLG','1');
				}
			}else{
				//入力制御時、エラーがあれば
				$this->bsmactlr->view->assign('prt_part_sub_type',$post['part']);
			}
		}else{
			$token = $this->getToken();
			$this->_form->setDefaults(array('token'=>$token));
		}
		//登録エラー時
		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->display(MODULES_PATH."/extMdlFormGnrtr/admin/partadd/".$tpl_file);
	}
}

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