<?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');
require_once (LIB_PATH.'/common/categoryDB.php');
class Admin_admin_contents_category_Edit extends categoryDB
{
	function prepare($view)
	{
		$this->_form = new HTML_QuickForm('form','post',$_SERVER['PHP_SELF'],'',null,true);
		$this->setElements();
		$this->setRules();
		$this->setButtons();
		if(is_numeric($this->getRequest("sidx_code"))) {
			$this->_sidx_data = sectionIdxDB::getData($this->getrequest("sidx_code"),$prev=true);
		}
		return null;
	}

	function perform($view)
	{
		$req = $this->getRequest('buttons');
		if (isset($req['BTNList'])&& $req['BTNList']!=null) {	// 一覧表示がクリックされたとき
			header('location:index.php?sidx_code='.$this->getRequest("sidx_code"));
			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?sidx_code='.$this->getRequest("sidx_code"));
				} 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?sidx_code='.$this->getRequest("sidx_code"));
			} else {
				$this->_form->setElementError("",$rtn);
			}
		} else {
			//何もクリックされていない場合は情報取得
			if(is_numeric($this->getRequest("cat_code"))) {
				$record = $this->getData($this->getRequest("cat_code"),$prev=true);
			} else {
				//新規登録時のデフォルト設定
				$record = array("cat_active"=>1,
								"cat_order"=>10,
								"cat_menuflg"=>1,
								"cat_smapflg"=>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());
		$view->assign('sidx_data',$this->_sidx_data);

		if($this->_form->isFrozen()) {
			return 'contents/category/prev.tpl';
		} else {
			return 'contents/category/edit.tpl';
		}
	}
}
?>