<?php
/*
 * Copyright(C). D-cube,Inc. All Rights Reserved.
 */

/*
 * @file		CategoryEntry.class.php
 * @brief		カテゴリの新規登録クラス
 * @date		2006.12.19
 */

require_once(CLASS_DIR."/form/CategoryInputForm.class.php");
require_once(CLASS_DIR."/adodb/GroupManager.class.php");
require_once(CLASS_DIR."/adodb/CategoryManager.class.php");

class CategoryEntry extends CategoryInputForm
{
	function makeForm()
	{
		$this->assign("TOPIC_PATH","<a href=\"./index.php?mode=3\">bitcube管理TOP</a> &gt; <a href=\"./index.php?mode=15&group_code=".$_GET["group_code"]."\">カテゴリ一覧</a> &gt; カテゴリ新規登録");
		
		$dao = new GroupManager();
		$group_array = $dao->getGroupList();
		$this->assign("group_array",$group_array);
		
		$this->current_group = $dao->getGroupInfo($_GET["group_code"]);
		if(!$this->current_group)
			$this->error("更新セット情報の取得に失敗しました。");
		$this->assign("current_group",$this->current_group);
		
		switch($_POST["state"])
		{
			case 1 :{$this->input();break;}		// 入力
			case 2 :{$this->confirm();break;}	// 確認
			case 3 :{$this->complete();break;}	// 完了
			default:{$this->input();break;}		// 入力
		}
	}
	
	// 入力
	function input()
	{
		$this->assign("state",2);
		$this->assign("name","");
		$this->template = "CategoryEntryInput.tpl";
	}
	
	// 確認
	function confirm()
	{
		$this->setCategoryName($_POST["name"]);
		$form_error_array = $this->check();
		
		if((count($form_error_array) > 0) || ($_POST["back"]))
		{
			$this->assign("state",2);
			$this->assign("error_array",$form_error_array);
			$this->template = "CategoryEntryInput.tpl";
		}
		else
		{
			$this->assign("state",3);
			$this->assign("message_array","下記の内容で登録します。よろしければ「登録」ボタンを押してください。<br />修正をする場合は「編集」ボタンよりお戻りください。");
			$this->template = "CategoryEntryConfirm.tpl";
		}
		
		$this->assign("name",$_POST["name"]);
	}
	
	// 完了
	function complete()
	{
		$record = array();
		$record["group_code"] = $_GET["group_code"];
		$record["category_name"] = $_POST["name"];
		
		$category = new CategoryManager();
		$category->StartTrans();
		$category->Lock();
		$category->insCategory($record);
		if(!$category->CompleteTrans())
			$this->error("カテゴリの新規登録に失敗しました。");
		
		$send_vars["mode"] = 15;
		$send_vars["group_code"] = $this->current_group["group_code"];
		redirect(CURRENT_HTTP_SERVER.MANAGER."/index.php",$send_vars);
	}
	
	function render()
	{
		$this->makeForm();
		$this->renderForm();
	}
}
