<?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 parteditAct extends CLpartlist
{

	function parteditAct(&$bsmactlr)
	{
		$this->__construct($bsmactlr);
	}

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

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

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

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

		//エラーチェック
		$this->setEditRules($_REQUEST['prt_part_type']);

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

		//falg
		$flag = "";

		//パーツタイプ
		$this->bsmactlr->view->assign('part_type',$_REQUEST['prt_part_type']);
		$this->bsmactlr->view->assign('prt_part_sub_type',$_REQUEST['prt_part_sub_type']);
		$this->bsmactlr->view->assign('prt_part_fileupload_limitflg',$_REQUEST['prt_part_fileupload_limitflg']);
		$this->bsmactlr->view->assign('frm_defaultflg',$_REQUEST['frm_defaultflg']);

		//パーツサブタイプのデータ
		$list = $this->getData($_REQUEST["prt_frm_id"], $_REQUEST["prt_part_id"]);
		
		if($_SESSION['extFrmGnrtAdmin_token']!=$_REQUEST["token"]){
			$_SESSION['formg_tokenError']=1;
			$this->putTokenError();
			exit;
		}

		//処理
		$req = isset($_POST['buttons']) ? $_POST['buttons'] : array();
		//保存ボタン
		if(isset($req['BTNsubmit']) && $req['BTNsubmit'] != null) {
			$post = $this->_form->getSubmitValues();
			if(!$this->check_token($post['token'])){
				$this->putTokenError();
			}
			if($this->_form->validate()){
				$data = array();

				//アップロード制御が限定する時、ファイル拡張子エラーチェックの設定
				if($this->setTypeControlRules($post)){
					$part_type = $post['prt_part_type'];

					//パーツの設定値を取得
					if ($part_type == 'guidetext'){                                                     //guidetext
						$data["prt_part_guidetext_value"] = $post['prt_part_guidetext_value'];
					} else if ($part_type == 'text'){                                                   //text
						if(isset($post['sub_type_single'])&& $post['sub_type_single'] == "single"){
							$text = "single";
							$data["prt_part_guidetext_value"] = $post['text_size'];
						}else if(isset($post['sub_type_multiply'])&& $post['sub_type_multiply'] == "multiply"){
							$text = "multiply";
							$data["prt_part_guidetext_value"] ='%%%'.$post['col_size'].'%%%'.$post['row_size'];
						}
						$data["prt_part_sub_type"] = $text;
						$data["prt_part_mustflg"]  = $post['prt_part_mustflg'];
						$data["prt_part_ime_mode"]  = $post['prt_part_ime_mode'];

					} else if ($part_type == 'select'){                                                 //select
						$data["prt_part_sub_type"]     = $post['select'];
						$data["prt_part_mustflg"]	   = $post['prt_part_mustflg'];
						//入力区切り符号は"ENTERキー"を"%%%"に変えたら、"%%%"で保存します。
						$data["prt_part_select_value"] = ereg_replace(UNITS_ENTER_FLG, UNITS_REPLACE_FLG, $post['prt_part_select_value']);
						$data["prt_part_select_row"] = $post['prt_part_select_row'];
					} else if ($part_type == 'name'){                                                   //name
						if(isset($post['sub_type_nametogether'])&& $post['sub_type_nametogether'] == "nametogether"){
							$name = "nametogether";
						}else if(isset($post['sub_type_nameseparate'])&& $post['sub_type_nameseparate'] == "nameseparate"){
							$name = "nameseparate";
						}
						$data["prt_part_sub_type"]         = $name;
						$data["prt_part_mustflg"]          = $post['prt_part_mustflg'];
						$data["prt_part_fullname_kanaflg"] = $post['prt_part_fullname_kanaflg'];
					} else if ($part_type == 'address'){                                                //address
						if(isset($post['sub_type_addresstogether'])&& $post['sub_type_addresstogether'] == "addresstogether"){
							$address = "addresstogether";
						}else if(isset($post['sub_type_addressseparate'])&& $post['sub_type_addressseparate'] == "addressseparate"){
							$address = "addressseparate";
						}
						$data["prt_part_sub_type"] = $address;
						$data["prt_part_address_zipflg"] = $post['prt_part_address_zipflg'];

						$data["prt_part_mustflg"] = 	   $post['prt_part_mustflg_zip']   .UNITS_REPLACE_FLG.
														   $post['prt_part_mustflg1']      .UNITS_REPLACE_FLG.
														   $post['prt_part_mustflg2']      .UNITS_REPLACE_FLG.
														   $post['prt_part_mustflg3']	   .UNITS_REPLACE_FLG.
														   $post['prt_part_mustflg4']	   .UNITS_REPLACE_FLG.
														   $post['prt_part_mustflg_adr'];

						$data["prt_part_address_value"]  = $post['prt_part_address_value1'].UNITS_REPLACE_FLG.
													   	   $post['prt_part_address_value2'].UNITS_REPLACE_FLG.
														   $post['prt_part_address_value3'].UNITS_REPLACE_FLG.
														   $post['prt_part_address_value4'];
					} else if ($part_type == 'datetime'){                                              //datetime
						$data["prt_part_sub_type"]			 = $post['datetime'];
						$data["prt_part_mustflg"]			 = $post['prt_part_mustflg'];
						$data["prt_part_datetime_interval"]  = $post['prt_part_datetime_interval'];
						$data["prt_part_datetime_defaultflg"] = $post['prt_part_datetime_defaultflg'];
						$data["prt_part_datetime_fromtoflg"] = $post['prt_part_datetime_fromtoflg'];
					} else if ($part_type == 'telphone'){                                              //telphone
						if(isset($post['sub_type_onepart'])&& $post['sub_type_onepart'] == "onepart"){
							$telphone = "onepart";
						}else if(isset($post['sub_type_threepart'])&& $post['sub_type_threepart'] == "threepart"){
							$telphone = "threepart";
						}
						$data["prt_part_sub_type"] = $telphone;
						$data["prt_part_mustflg"]  = $post['prt_part_mustflg'];
					} else if ($part_type == 'web'){                                                   //web
						$data["prt_part_mustflg"]      = $post['prt_part_mustflg'];
						$data["prt_part_web_guideflg"] = $post['prt_part_web_guideflg'];
						$data["prt_part_web_linkflg"]  = $post['prt_part_web_linkflg'];
					} else if ($part_type == 'mail'){												//mail
						$data["prt_part_mustflg"]         = $post['prt_part_mustflg_all'].UNITS_REPLACE_FLG.
															$post['prt_part_mustflg_pc'].UNITS_REPLACE_FLG.
													        $post['prt_part_mustflg_mobile'];
						$data["prt_part_mail_addressflg"] = $post['all_mail'].UNITS_REPLACE_FLG.
															$post['pc_mail'].UNITS_REPLACE_FLG.
															$post['mobile_mail'];
						$data["prt_part_mail_registerflg"] = ($post['prt_part_mail_registerflg']==""?0:1);
					} else if ($part_type == 'fileupload'){                                            //fileupload
						$data["prt_part_mustflg"] = $post['prt_part_mustflg'];
						$data["prt_part_fileupload_limitflg"] = $post['limit'];
						$data["prt_part_fileupload_filetype"] = $post['pdf']        .UNITS_REPLACE_FLG.
																$post['excel']      .UNITS_REPLACE_FLG.
																$post['word']       .UNITS_REPLACE_FLG.
																//$post['Power_Point'].UNITS_REPLACE_FLG.
																//$post['jpeg']       .UNITS_REPLACE_FLG.
																$post['jpg']       .UNITS_REPLACE_FLG.
																$post['gif']       .UNITS_REPLACE_FLG.
																$post['png']        .UNITS_REPLACE_FLG.
																//$post['zip'];
						//入力区切り符号は"ENTERキー"を"%%%"に変えたら、"%%%"で保存します。
						$data["prt_part_fileupload_subfiletype"] = ereg_replace(UNITS_ENTER_FLG, UNITS_REPLACE_FLG, $post['prt_part_fileupload_subfiletype']);
					} else if ($part_type == 'line'){                                                  //line
						$data["prt_part_line_size"]  = $post['prt_part_line_size'];
						//$data["prt_part_line_color"] = $post['prt_part_line_color'];
						if(isset($post['prt_part_line_color']) && $post['prt_part_line_color']!=null){
							$data["prt_part_line_color"] = '#'.$post['prt_part_line_color'];
						} else {
							$data["prt_part_line_color"] = '#000000';
						}
					}

					$data["prt_part_label"]   			= $_REQUEST['prt_part_label'];
					$data["prt_part_comment"] 			= $_REQUEST['prt_part_comment'];
					$data["prt_part_comment_color"] 	= $_REQUEST['prt_part_comment_color'];
					$data["prt_part_comment_position"]  = $_REQUEST['prt_part_comment_position'];
					$data["prt_part_comment_color"]     = '#'.$_REQUEST['prt_part_comment_color'];
					$data["prt_frm_id"]  	  			= $_REQUEST['prt_frm_id'];
					$data["prt_part_id"]      			= $_REQUEST['prt_part_id'];
					$data["prt_u_date"]       			= 'now';

					//タグの除去
					$denyTags=array(
						'href=javascript' => 'href=',
						'href=\'javascript' => 'href=\'',
						'href="javascript' => 'href="',
						'href=script' => 'href=',
						'href=\'script' => 'href=\'',
						'href="script' => 'href="'
					);
					foreach($data as $k => $v){
						$data[$k] = strip_tags($v,'<br><a><em><b><p><div><span>');
						$data[$k] = strtr($data[$k], $denyTags);
						if(empty($data[$k])){
							$data[$k]=null;
						}
					}

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

					if(!$rtn) {
						$this->clrToken();
						$this->bsmactlr->view->assign('CLOSESCRIPTFLG','1');
					}
				}else{
					$flag = "validateError";
				}
			} else {
				$flag = "validateError";
			}
		}

		//エラー
		if ($flag == "validateError" ) {
			$part_type = $post['prt_part_type'];
			if ($part_type == 'telphone'){
				//電話エラーがある
				if(isset($post['sub_type_onepart'])&& $post['sub_type_onepart'] == "onepart"){
					$prt_part_type = "onepart";
				}else if(isset($post['sub_type_threepart'])&& $post['sub_type_threepart'] == "threepart"){
					$prt_part_type = "threepart";
				}
			} else if ($part_type == 'fileupload'){
				if(isset($post['limit'])&& $post['limit'] == "1"){
					$prt_part_type = "limitYes";
				}else{
					$prt_part_type = "limitNo";
				}
			} else if ($part_type == 'address'){
				if(isset($post['sub_type_addresstogether'])&& $post['sub_type_addresstogether'] == "addresstogether"){
					$prt_part_type = "addresstogether";
				}else if(isset($post['sub_type_addressseparate'])&& $post['sub_type_addressseparate'] == "addressseparate"){
					$prt_part_type = "addressseparate";
				}
			}
			$this->bsmactlr->view->assign('prt_part_sub_type',$prt_part_type);
		}else{
			//パーツの値を設定
			$list['prt_part_comment_color'] = str_replace("#","",$list['prt_part_comment_color']);
			$list['prt_part_line_color'] = str_replace("#","",$list['prt_part_line_color']);

			if ($_REQUEST['prt_part_type'] == 'address') {                                         //address
				$address_mustflg = explode(UNITS_REPLACE_FLG, $list['prt_part_mustflg']);
				$address_value   = explode(UNITS_REPLACE_FLG, $list['prt_part_address_value']);

				$addressData = array('prt_frm_id' 					=> $list['prt_frm_id'],
									  'prt_part_id' 				=> $list['prt_part_id'],
									  'prt_part_type' 				=> $list['prt_part_type'],
				                      'prt_part_label' 				=> $list['prt_part_label'],
				                      'prt_part_comment' 			=> $list['prt_part_comment'],
									  'prt_part_comment_color'   	=> str_replace("#","",$list['prt_part_comment_color']),
									  'sub_type_addresstogether'	=> $list['prt_part_sub_type'],
									  'sub_type_addressseparate' 	=> $list['prt_part_sub_type'],
									  'prt_part_address_zipflg' 	=> $list['prt_part_address_zipflg'],
									  'prt_part_mustflg_zip' 		=> $address_mustflg[0],
									  'prt_part_mustflg1' 			=> $address_mustflg[1],
									  'prt_part_mustflg2' 			=> $address_mustflg[2],
									  'prt_part_mustflg3' 			=> $address_mustflg[3],
									  'prt_part_mustflg4' 			=> $address_mustflg[4],
									  'prt_part_mustflg_adr' 		=> $address_mustflg[5],
									  'prt_part_address_value1' 	=> $address_value[0],
									  'prt_part_address_value2' 	=> $address_value[1],
									  'prt_part_address_value3' 	=> $address_value[2],
									  'prt_part_address_value4' 	=> $address_value[3]
								);

				$this->_form->setConstants($addressData);

			} else if ($_REQUEST['prt_part_type'] == 'fileupload') {                              //fileupload
				$fileupType   = explode(UNITS_REPLACE_FLG, $list['prt_part_fileupload_filetype']);
				//入力区切り符号は"ENTERキー"を"%%%"に変えたら、"%%%"で保存します。
				$list['prt_part_fileupload_subfiletype'] = ereg_replace(UNITS_REPLACE_FLG, UNITS_ENTER_FLG,$list['prt_part_fileupload_subfiletype']);
				$fileupData = array('prt_frm_id' 						=> $list['prt_frm_id'],
							  		  'prt_part_id' 					=> $list['prt_part_id'],
								  	  'prt_part_type' 					=> $list['prt_part_type'],
									  'prt_part_label' 		            => $list['prt_part_label'],
									  'prt_part_comment' 		        => $list['prt_part_comment'],
									  'prt_part_comment_color'          => $list['prt_part_comment_color'],
								      'limit' 							=> $list['prt_part_fileupload_limitflg'],
								 	  'prt_part_mustflg'				=> $list['prt_part_mustflg'],
									  'prt_part_fileupload_subfiletype'	=> $list['prt_part_fileupload_subfiletype'],
								      'pdf' 							=> $fileupType[0],
								      'excel' 							=> $fileupType[1],
								      'word' 							=> $fileupType[2],
									  //'Power_Point'						=> $fileupType[3],
									  //'jpeg' 							=> $fileupType[4],
									  'jpg' 							=> $fileupType[3],
									  'gif' 							=> $fileupType[4],
									  'png' 							=> $fileupType[5],
									  //'zip' 							=> $fileupType[5]
								);

				$this->_form->setConstants($fileupData);

			} else if ($_REQUEST['prt_part_type'] == 'mail') {                                    //mail
				$mustType   = explode(UNITS_REPLACE_FLG, $list['prt_part_mustflg']);
				$mailType   = explode(UNITS_REPLACE_FLG, $list['prt_part_mail_addressflg']);
				$prt_part_mail_registerflg = ($list['prt_part_mail_registerflg'] == 1 ? 1 : 0);
				$mailData = array('prt_frm_id' 						=> $list['prt_frm_id'],
							  		  'prt_part_id' 				=> $list['prt_part_id'],
								  	  'prt_part_type' 				=> $list['prt_part_type'],
									  'prt_part_label' 		    	=> $list['prt_part_label'],
									  'prt_part_comment' 			=> $list['prt_part_comment'],
				                      'prt_part_comment_color'      => $list['prt_part_comment_color'],
									  'prt_part_mail_registerflg' 	=> $prt_part_mail_registerflg,
									  'prt_part_mustflg_all' 		=> $mustType[0],
								      'prt_part_mustflg_pc' 		=> $mustType[1],
								      'prt_part_mustflg_mobile' 	=> $mustType[2],
								      'all_mail' 					=> $mailType[0],
								      'pc_mail' 					=> $mailType[1],
									  'mobile_mail'					=> $mailType[2]
								);
				$this->_form->setConstants($mailData);

			} else if ($_REQUEST['prt_part_type'] == 'select') {                                 //select
				//入力区切り符号は"ENTERキー"を"%%%"に変えたら、"%%%"で保存します。
				$list['prt_part_select_value'] = ereg_replace(UNITS_REPLACE_FLG, UNITS_ENTER_FLG, $list['prt_part_select_value']);
				$selectData = array('prt_frm_id' 			  => $list['prt_frm_id'],
						  		    'prt_part_id' 			  => $list['prt_part_id'],
							  	    'prt_part_type' 		  => $list['prt_part_type'],
								    'select' 				  => $list['prt_part_sub_type'],
							        'prt_part_select_value'   => $list['prt_part_select_value'],
									'prt_part_select_row'     => $list['prt_part_select_row'],
									'prt_part_label' 		  => $list['prt_part_label'],
									'prt_part_comment' 		  => $list['prt_part_comment'],
								    'prt_part_comment_color'  => $list['prt_part_comment_color']
								);

				$this->_form->setConstants($selectData);

			} else if ($_REQUEST['prt_part_type'] == 'telphone') {                              //telphone
				$telephoneData = array('prt_frm_id'           => $list['prt_frm_id'],
						  		    'prt_part_id' 	          => $list['prt_part_id'],
							  	    'prt_part_type'           => $list['prt_part_type'],
								    'sub_type_onepart' 		  => $list['prt_part_sub_type'],
									'sub_type_threepart' 	  => $list['prt_part_sub_type'],
				                    'prt_part_label'          => $list['prt_part_label'],
									'prt_part_comment'        => $list['prt_part_comment'],
									'prt_part_comment_color'  => $list['prt_part_comment_color']
								);

				$this->_form->setConstants($telephoneData);

			} else if ($_REQUEST['prt_part_type'] == 'text') {                                 //text
				$size = explode('%%%',$list['prt_part_guidetext_value']);
				$textData = array('prt_frm_id'    	             => $list['prt_frm_id'],
						  		       'prt_part_id'             => $list['prt_part_id'],
							  	       'prt_part_type'           => $list['prt_part_type'],
									   'sub_type_single'         => $list['prt_part_sub_type'],
								       'sub_type_multiply' 		 => $list['prt_part_sub_type'],
									   'prt_part_ime_mode' 		 => $list['prt_part_ime_mode'],
									   'prt_part_label'          => $list['prt_part_label'],
									   'prt_part_comment'        => $list['prt_part_comment'],
									   'prt_part_comment_color'  => $list['prt_part_comment_color'],
									   'text_size'  => $size[0],
									   'col_size'  => $size[1],
									   'row_size'  => $size[2]
								);
				$this->_form->setConstants($textData);

			} else if ($_REQUEST['prt_part_type'] == 'name') {                                 //name
				$textData = array('prt_frm_id'    				=> $list['prt_frm_id'],
						  		  'prt_part_id'   				=> $list['prt_part_id'],
							  	  'prt_part_type' 				=> $list['prt_part_type'],
								  'sub_type_nametogether' 		=> $list['prt_part_sub_type'],
								  'sub_type_nameseparate' 		=> $list['prt_part_sub_type'],
				 				  'prt_part_fullname_kanaflg' 	=> $list['prt_part_fullname_kanaflg'],
								  'prt_part_label' 		        => $list['prt_part_label'],
								  'prt_part_comment' 		    => $list['prt_part_comment'],
								  'prt_part_comment_color'      => $list['prt_part_comment_color']
								);

				$this->_form->setConstants($textData);

			} else if ($_REQUEST['prt_part_type'] == 'datetime') {                             //datetime
				$textData = array('prt_frm_id'    				=> $list['prt_frm_id'],
						  		  'prt_part_id'   				=> $list['prt_part_id'],
							  	  'prt_part_type' 				=> $list['prt_part_type'],
								  'datetime' 		  			=> $list['prt_part_sub_type'],
				 				  'prt_part_datetime_interval' 	=> $list['prt_part_datetime_interval'],
								  'prt_part_datetime_defaultflg'=> $list['prt_part_datetime_defaultflgl'],
								  'prt_part_datetime_fromtoflg' => $list['prt_part_datetime_fromtoflg'],
								  'prt_part_label' 		        => $list['prt_part_label'],
								  'prt_part_comment' 		    => $list['prt_part_comment'],
								  'prt_part_comment_color'      => $list['prt_part_comment_color']
								);

				$this->_form->setConstants($textData);
			}
			$this->_form->setConstants($list);
			
			$token = $this->getToken();
			$this->_form->setDefaults(array('token'=>$token));
		}
		$this->bsmactlr->view->assign('prt_part_type', $_REQUEST['prt_part_type']);

		//登録エラー時
		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());
		$this->bsmactlr->view->display(MODULES_PATH."/extMdlFormGnrtr/admin/partlist/".$tpl_file);
	}
}

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