<?php
/*
	Copyright(c) 2010 Shift Tech Inc. All Rights Reserved.
 	http://www.shift-tech.jp/

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

require_once (LIB_PATH.'/common/sectionIdxDB.php');

class Admin_admin_contents_Edit extends sectionIdxDB
{
	function prepare($view)
	{
		$this->_view =& $view;
		$this->_form = new HTML_QuickForm('form','post',$_SERVER['PHP_SELF'],'',null,true);
		$this->setElements();
		$this->setButtons();
		$this->setRules();
		return null;
	}

	function perform($view)
	{
		$req = $this->getRequest('buttons');
		if (isset($req['BTNList'])&& $req['BTNList']!=null) {	// 一覧表示がクリックされたとき
			header('location:index.php');
			exit();
		} elseif (isset($req['BTNnext'])&&$req['BTNnext']!=null) {
			if($this->_form->validate())
			{
				$this->_form->freeze();
				$this->setButtons($this->_form->isFrozen()); //ボタンの作り直し
			}
		} else if(isset($req['BTNsave'])&&$req['BTNsave']!=null) {
			if($this->_form->validate()){
				$post = $this->_form->getSubmitValues();
				$rtn = $this->doSave($post);
				if($rtn==null) {
					header('location:index.php');
				} else {
					$this->_form->setElementError("",$rtn);
				}
			}
		//削除ボタンクリック時
		} else if(isset($req['BTNdelete'])&&$req['BTNdelete']!=null) {
			$post = $this->_form->getSubmitValues();
			$rtn = $this->delete($post);
			if($rtn==null) {
				//キャッシュ再構成（サイトマップユニット等のデータを更新するため）
				require_once (LIB_PATH.'/common/unitCache.php');
				$cache = new unitCache();
				$cache->makeCache(&$view);
				header('location:index.php');
			} else {
				$this->_form->setElementError("",$rtn);
			}
		} else {
			//何もクリックされていない場合は情報取得
			if(is_numeric($this->getRequest("sidx_code"))) {
				$record = $this->getData($this->getrequest("sidx_code"),$prev=true);	//	一旦登録情報を読み出しておく
				$ulist = sectionuserDB::getusrlist($this->getrequest("sidx_code"));
				if(is_array($ulist)){
					foreach($ulist as $key=>$val) {
						$record["secusr_usr_codes"][$val["secusr_usr_code"]] = $val["secusr_usr_code"];
					}
				}
			} else {
				//新規登録時のデフォルト設定
				$record = array("sidx_active"=>1,
								"sidx_view"=>1,
								"sidx_menuflg"=>1,
								"sidx_smapflg"=>1,
								"sidx_comment_flg"=>0,
								"sidx_order"=>0);
				//sidx_tpl_codeの取得
				//グローバルメニュー「ホーム」にて当たっているtpl_codeをセットしてあげる。
				$top_data = $this->getData(1);
				if(isset($top_data["sidx_tpl_code"])&&$top_data["sidx_tpl_code"]>0) {
					$record["sidx_tpl_code"]= $top_data["sidx_tpl_code"];
				} else {
					$record["sidx_tpl_code"] = 1;
				}
			}
			$this->_form->setDefaults($record);
		}
		//登録エラー時
		if(!empty($this->_form->_errors)) {
			$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($view);
		$this->_form->accept($renderer);
		$view->assign('form',$renderer->toArray());
		if(isset($record)) {
			$view->assign('usr_code',$record["usr_code"]);
		}
		if($this->_form->isFrozen()) {
			return 'contents/prev.tpl';
		} else {
			return 'contents/edit.tpl';
		}
	}
}
?>