<?php
require_once ('CLbaseform.php');

class CLpartadd extends baseForm
{
	function CLpartadd()
	{
		$this->__construct();
	}

	function __construct()
	{
		baseForm::__construct();
		$this->_form = new HTML_QuickForm('form','post',$_SERVER['PHP_SELF'],'',null,true);
		$this->db = new DataBase();
	}

	/**
	 * Smarty Element 設定
	 * @param
	 * @return
	 */
	function setElements()
	{
		$this->_form->addElement('hidden','token', null);
		
		//パーツのラベル
		$this->_form->addElement('text','prt_part_label', '', array('size'=>20,'maxlength'=>'100','id'=>'prt_part_label'));

		//パーツのコメント
		$this->_form->addElement('text','prt_part_comment', '', array('size'=>20,'maxlength'=>'100','id'=>'prt_part_comment'));

		//色の値
		$this->_form->addElement('text','prt_part_comment_color', '', array('size'=>9,'maxlength'=>'7','id'=>'prt_part_comment_color'));

		//表示位置
		$item[]=$this->_form->addElement('radio', null, null, '上', 'top');
		$item[]=$this->_form->addElement('radio', null, null, '右横', 'right');
		$item[]=$this->_form->addElement('radio', null, null, '下', 'bottom');
		$this->_form->addGroup($item, 'jyuusyo', null, '&nbsp;&nbsp;');
		$this->_form->setDefaults(array('jyuusyo' => 'right'));

		//パーツ
		$part[]=$this->_form->addElement('radio', null, null, '名前', 'name', array('onclick' => 'refreshStyle("name");', 'id' => 'sub_type_name'));
		$part[]=$this->_form->addElement('radio', null, null, '住所', 'address', array('onclick' => 'refreshStyle("address");', 'id' => 'sub_type_address'));
		$part[]=$this->_form->addElement('radio', null, null, 'TEL', 'telphone', array('onclick' => 'refreshStyle("telphone");', 'id' => 'sub_type_telphone'));
		$part[]=$this->_form->addElement('radio', null, null, 'メールアドレス', 'mail', array('onclick' => 'refreshStyle("mail");', 'id' => 'sub_type_mail'));
		$part[]=$this->_form->addElement('radio', null, null, 'URL', 'web', array('onclick' => 'refreshStyle("web");', 'id' => 'sub_type_web'));
		$part[]=$this->_form->addElement('radio', null, null, '自由入力', 'text', array('onclick' => 'refreshStyle("text");', 'id' => 'sub_type_text'));
		$part[]=$this->_form->addElement('radio', null, null, '択一・複数選択', 'select', array('onclick' => 'refreshStyle("select");', 'id' => 'sub_type_select'));
		$part[]=$this->_form->addElement('radio', null, null, '日付/時間', 'datetime', array('onclick' => 'refreshStyle("datetime");', 'id' => 'sub_type_datetime'));
		$part[]=$this->_form->addElement('radio', null, null, 'ファイルアップロード', 'fileupload', array('onclick' => 'refreshStyle("fileupload");', 'id' => 'sub_type_fileupload'));
		$sql ="select count(prt_part_type) from mdl_extMdlFormGnrtr_tbl_part
				where
				prt_frm_id = '{$_REQUEST['prt_frm_id']}'
				and
				prt_part_type = 'caps'
				and
				prt_part_active=1";
		if($this->db->getOne($sql) == 0){
			$part[]=$this->_form->addElement('radio', null, null, '画像認証', 'caps', array('onclick' => 'refreshStyle("caps");', 'id' => 'sub_type_caps'));
		}
		$part[]=$this->_form->addElement('radio', null, null, '案内文', 'guidetext', array('onclick' => 'refreshStyle("guidetext");', 'id' => 'sub_type_guidetext'));
		$part[]=$this->_form->addElement('radio', null, null, '罫線', 'line', array('onclick' => 'refreshStyle("line");', 'id' => 'sub_type_line'));
		$this->_form->addGroup($part,'part',null,'<br>');
		$this->_form->setDefaults(array('part' => 'text'));
	}

	/**
	 * フォーム入力エラーチェックの設定
	 * @param
	 * @return
	 */
	function setRules($part=null)
	{
		$this->_form->applyFilter('__ALL__','trim');
		$this->_form->setJsWarnings('下記の項目が不正です', '');
		//パーツタイプが罫線対象外
		if($part != 'line'){
			$this->_form->addRule('prt_part_label', '・「項目名」を入力してください', 'required', null);
		}
	}

	/**
	 * ボタン作成
	 * @param
	 * @return
	 */
	function setButtons()
	{
		$buttons = array();
		$buttons[] = $this->_form->createElement('button','BTNcancel',' キャンセル ',array('id'=>'BTNcancel', 'class'=>'bsmBtnCancel', 'onClick'=>'parent.closeBox();'));
		$buttons[] = $this->_form->createElement('submit', 'BTNadd', '保存する', array('id' => 'BTNadd', 'class'=>'bsmBtnSaveUnit'));
		$this->_form->addGroup($buttons, "buttons", null, '&nbsp; &nbsp;');
	}

	/**
	 * パーツの表示順の最大値を取得
	 * @param   $prt_frm_id
	 * @return	array
	 */
	function getPartOrder($prt_frm_id=null) {
		$part_order_new ="";
		/*
		$sql = "select max(prt_part_order) as part_order_new
		        from   mdl_extMdlFormGnrtr_tbl_part
		        where  prt_frm_id =".$prt_frm_id;
		$result = $this->db->query($sql);
		*/
		$sql = "select max(prt_part_order) as part_order_new
		        from   mdl_extMdlFormGnrtr_tbl_part
		        where  prt_frm_id =?";
		$result = $this->db->prepare($sql, array($prt_frm_id));
		
		if ($result) {
			while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
				$part_order_new = $row['part_order_new'] + 1;
			}
		}

		return $part_order_new;
	}

	/**
	 * データ保存
	 * @param   $record
	 * @return	array
	 */
	function doSave($record)
	{
		return $this->insert($record);
	}

	/**
	 * 新規登録
	 * @param   $record
	 * @return	null or err_str
	 */
	function insert($record)
	{
		$this->db->query('BEGIN');
		if($this->db->insert("mdl_extMdlFormGnrtr_tbl_part",$record)) {
			$this->db->query('COMMIT');
			return true;
		}
		$this->db->query('ROLLBACK');
		return "保存に失敗しました";
	}
}
?>