<?php
require_once 'datasets_classes/_base.class.php';
class datasets extends datasets_base {

function dispatch_index() {
	//$this->assign_search_form();
	$this->display_record();
	switch($this->QuictyStatus) {
		case 'INSERT':
			$datasets_id = $this->datasets['id'];
			$fields = $this->new_data_set('fields');
			$insert_set = array(
				'datasets_id'=>$datasets_id,
				'type'=>'id',
				'hqf_type'=>'id',
				'name'=>'id',
				'properties'=>serialize(array('name'=>'id','type'=>'id')),
				'sort_flag'=>1,
				'seq'=>1);
			$fields->insert_table($insert_set);
			redirect('/datasets/edit?id='.$datasets_id);
		case 'UPDATE':
			$datasets_id = $_POST['id'];
			redirect('/datasets/edit?id='.$datasets_id);
			break;
		case 'DELETE':
			$fields = $this->new_data_set('fields');
			$fields->delete('where datasets_id='.$_POST['id']);
			redirect('/info?id='.$_POST['application_id']);
			break;
	}
	if($_REQUEST['id']) {
		$datasets_id = $_REQUEST['id'];
		$fields = $this->new_data_set('fields');
		$fieldset = $fields->select_table('where datasets_id='.$datasets_id.' order by seq');
		$this->assign('table_view',$fields->display_table_value($fieldset));
		
		if(count($fieldset)>0) {
			require_once 'includes/datasets_form.class.php';
			$datasets_form = new datasets_form();
			$new_form = $datasets_form->make_sample($datasets_id);
			$this->assign_form('new_fields',$new_form);
			$write_conf_form = $datasets_form->make_write_conf($datasets_id);
			$this->assign('write_conf',$write_conf_form);
			$create_form = $datasets_form->make_create_table($datasets_id);
			$this->assign('create_form',$create_form);
			$create_template = $datasets_form->make_create_template($datasets_id);
			$this->assign('create_template',$create_template);
		}
	}
	
	$this->assign('application_id',$this->datasets['application_id']);
	$this->add_pathlist('/info?id='.$this->datasets['application_id'],'アプリ');
	$this->page['name'] = 'データセット';
	$this->add_pathlist('',$this->page['name']);
	$this->assign('page',$this->page);
	return $this->display($this->current_template);
}


function dispatch_delete() {
	if($this->QuictyStatus=='DELETE') {
		require_once 'includes/datasets_conf/delete.class.php';
		$datasets_id = $_REQUEST['id'];
		$datasets_conf = new datasets_conf_delete();
		$result = $datasets_conf->delete_conf($datasets_id);
	}
	
	$this->display_record();

	if($this->QuictyStatus=='DELETE') {
		redirect('/info?id='.$_POST['application_id']);
	}
	$this->page['message'] = nl2br($result['message']);
	$this->page['name'] = 'データセットと設定ファイルの削除';
	$this->assign('page',$this->page);
	return $this->display($this->current_template);
}


function dispatch_write_conf() {
	require_once 'includes/datasets_conf/write.class.php';
	$datasets_id = $_POST['id'];
	$datasets_conf = new datasets_conf_write();
	$result = $datasets_conf->write_conf($datasets_id);
	$this->page['message'] = nl2br($result['message']);
	$this->page['name'] = 'テーブル関連設定ファイルの保存';
	$this->assign('page',$this->page);
	return $this->display($this->current_template);
}

function dispatch_create_table() {
	require_once 'includes/datasets_conf/create_table.class.php';
	$datasets_id = $_POST['id'];
	$datasets_conf = new datasets_conf_create_table();
	$result = $datasets_conf->create_table($datasets_id,$_REQUEST['create']);
	$this->page['message'] = nl2br($result['message']);
	$this->page['name'] = 'テーブルの作成';
	$this->assign('page',$this->page);
	return $this->display($this->current_template);
}

function dispatch_create_template() {
	require_once 'includes/datasets_conf/create_template.class.php';
	$datasets_id = $_POST['id'];
	$options['Quicty_modules'] = $this->Quicty_modules;
	$datasets_conf = new datasets_conf_create_template($options);
	$result = $datasets_conf->create_template($datasets_id);
	$this->page['message'] = nl2br($result['message']);
	$this->page['name'] = 'フォーム/テーブル表示テンプレートの作成';
	$this->assign('page',$this->page);
	return $this->display($this->current_template);
}

} // end of dataset class
?>