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

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

class frmlistAct extends CLfrmlist
{
	function frmlistAct(&$bsmactlr)
	{
		$this->__construct($bsmactlr);
	}

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

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

	function action()
	{
		$tpl_file = "index.tpl";
		//フォームジェネレータの利用権限のチェック
		if( $_SESSION["form_generator_kanri"] == 0 || $_SESSION["form_generator_kanri"] == null){
			header('location:../index.php');
			exit();
		}
		//公開・非公開ボタン
		if (isset($_REQUEST['bsmBtnAct']) && $_REQUEST['bsmBtnAct'] != null) {
			if(!$this->check_token($_POST['token'])){
				$this->putTokenError();
			}
			if(!$this -> updateFrmActflg($_REQUEST['frm_id'],$_REQUEST['frm_actflg'])){
				$error[] = ($_REQUEST['frm_actflg'] == 0 ? "公開に失敗しました。" : "非公開に失敗しました。");
				$this -> bsmactlr -> view -> assign('ERROR',$error);
			}
			$this->clrToken();
		}else if (isset($_REQUEST['bsmBtnOrder']) && $_REQUEST['bsmBtnOrder'] != null) {
			if($this->updateOrder($_POST)){
				echo 1;
				exit;
			}
			echo 0;
			exit;
		}

		$list = $this->getList();
		foreach($list as $k => $v){
			$list[$k]['unCorrespondenceNumber'] = CLfrmlist::getUnCorrespondenceNumber($v['frm_id']);
			$list[$k]['active_inquiries'] = CLfrmlist::getActiveInquiriesCount($v['frm_id']);
			$param=array("class"=>"w2 rt orders","maxlength"=>"3");
			if($v["frm_defaultflg"]==1) $param["disabled"]="disabled";
			$this->_form->setDefaults(array("frm_order[".$v["frm_id"]."]"=>$v["frm_order"]));
			$this->_form->addElement("text", "frm_order[".$v["frm_id"]."]", null, $param);
		}

		$renderer = new HTML_QuickForm_Renderer_ArraySmarty($this->bsmactlr->view);
		$this->_form->accept($renderer);

		$this->bsmactlr->view->assign('form',$renderer->toArray());
		$this->bsmactlr->view->assign('tpllist', $list);
		$this->bsmactlr->view->assign('token', $this->getToken());

		//set template file
		$this->bsmactlr->setHeadhtml('<link href="../css/imported.css" rel="stylesheet" type="text/css" media="all">');
		$this->bsmactlr->DisplayPage($this->bsmactlr->setTpl("/extMdlFormGnrtr/admin/frmlist/".$tpl_file));
	}

	function updateFrmActflg($frm_id=null, $frmActflg=null) {
		if(isset($frm_id) && $frm_id != null) {
			$data["frm_actflg"] = ($frmActflg == '0' ? '1' : '0');
			$frm_actflg = ($frmActflg == '0' ? '1' : '0');
			$frm_u_date = date('Y-m-d H:i:s');

			$sql = "UPDATE mdl_extMdlFormGnrtr_tbl_form SET frm_actflg=?, frm_u_date=? WHERE frm_id=?";
			$value=array((int)$frm_actflg, $frm_u_date, (int)$frm_id);
			$this->db->query("BEGIN");
			if($this->db->prepare($sql, $value)){
				$this->db->query("COMMIT");
				return true;
			}
			$this->db->query("ROLLBACK");
		}

		return false;
	}

	function updateOrder($post) {
		if(isset($post["frm_order"]) && is_array($post["frm_order"])){
			$this->db->query("BEGIN");
			foreach($post["frm_order"] as $id => $order){
				$values=array($order , $id);
				$sql="UPDATE mdl_extMdlFormGnrtr_tbl_form set frm_order=? WHERE frm_id=?";
				if(!$this->db->prepare($sql, $values)){
					$this->db->query("ROLLBACK");
					return false;
				}
			}
			$this->db->query("COMMIT");
			return true;
		}
		return false;
	}
}

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