<?php
require_once 'fields_classes/_base.class.php';
class fields extends fields_base {

function dispatch_index() {
	if($_POST['command']=='add_rule') {
		$this->QuictyStatus = 'UPDATE';
	}
	$this->display_record();
	switch($this->QuictyStatus) {
		case 'START':
			$this->page['name'] = 'フィールドの追加';
			break;
		case 'EDIT':
			$this->page['name'] = 'フィールドの編集';
			break;
		case 'INSERT':
		case 'UPDATE':
			if(is_array($this->posted_properties)) {
				$properties = serialize($this->posted_properties);
				$update_set = array('properties'=>$properties);
				$field_id = $_POST['id'] ? $_POST['id'] : $this->fields['id'];
				$this->form->update_table($update_set,'where id='.$field_id);
			}
			if($_POST['command']=='add_rule') {
				redirect('/fields/add_rule?id='.$field_id);
			} else {
				redirect('/datasets/edit?id='.$this->fields['datasets_id']);
			}
			break;
		case 'DELETE_CONFIRM':
			$this->page['name'] = 'フィールドの削除';
			break;
		case 'DELETE':
			redirect('/datasets/edit?id='.$_POST['datasets_id']);
			break;
		case 'BROWSE':
			$this->page['name'] = 'フィールドの表示';
			break;
	}
	$this->assign('page',$this->page);
	return $this->display($this->current_template,$this->cache_id);
}

function dispatch_add() {
	//$this->assign_search_form();
	$form = new HTML_QuickForm('fields','POST',$this->page['base_url'].'/fields/');
	foreach($this->field_kind as $prop) {
		$name = $prop['name'];
		$description = $this->field_desc[$name];
		if($description) {
			$label = (strlen($name)>16)
				? substr($name,0,16).'<br />'.substr($name,16)
				: $name;
			$form->addElement('radio','type',$label,'　'.$description,$name);
		}
	}
	$form->setDefaults(array('type'=>'text'));
	$form->addElement('hidden','datasets_id',$_GET['datasets_id']);
	$form->addElement('hidden','mode','add');
	$form->addElement('submit','submit','追加');
	$renderer = new HTML_QuickForm_Renderer_ArraySmarty($form);
	$form->accept($renderer);
	$fields = $renderer->toArray();
	foreach($fields['type'] as $key=>$attributes) {
		$fields[' '.$key] = $attributes;
	}
	unset($fields['type']);
	$this->assign('input_form',$fields);

	$this->page['name'] = 'フィールドを追加';
	$this->assign('page',$this->page);
	return $this->display($this->current_template);
}

function dispatch_add_rule() {
	
	$form = new HTML_QuickForm('rules','GET',$this->page['base_url'].'/fields/edit');
	foreach($this->rule_kind as $key=>$prop) {
		$name = $prop['name'];
		$description = $prop['description'];
		if($description) {
			$form->addElement('radio','rule',$description,$name,$key);
		}
	}
	$form->setDefaults(array('rule'=>16));
	$form->addElement('hidden','id',$_GET['id']);
	//$form->addElement('hidden','datasets_id',$this->datasets['id']);
	$form->addElement('submit','submit','追加');
	$renderer = new HTML_QuickForm_Renderer_ArraySmarty($form);
	$form->accept($renderer);
	$rules = $renderer->toArray();
	foreach($rules['rule'] as $key=>$attributes) {
		$rules[' '.$key] = $attributes;
	}
	unset($rules['rule']);
	$this->assign('input_form',$rules);

	$this->page['name'] = 'ルールを追加';
	$this->assign('page',$this->page);
	return $this->display($this->current_template);
}


function dispatch_batch_update() {
	require_once 'fields_classes/list_options.class.php';
	$base_url = quicty_base_url();
	$this->list = $this->new_data_set('fields');
	$options = new fields_list_options($this);
	$pager_option = $options->get_pager_option('datasets_id='.$_REQUEST['datasets_id']);
	$this->list->bind_pager($pager_option);
	
	$fields = $options->make_select_fields();
	//$sort_condition = $options->make_sort_condition();

	$this->list->hide_in_batch_list(array('hqf_type','virtual','datasets_id','properties','memo'));
	$this->list->freeze_in_batch_list(array('type','name','label'));
	$this->list->set_button_status('use_confirm',false);
	$this->list->bind_button_control();
	$table_data = $this->list->automatic_batch_handler(' order by seq',$fields);

	if($this->QuictyStatus=='UPDATE') {
		redirect('/datasets/edit?id='.$_POST['datasets_id']);
	}
	
	$update_form['display_cmd'] = $base_url.'/fields/edit';
	$update_form['conditions']['datasets_id'] = $_REQUEST['datasets_id'];
	$update_form['even_color'] = '#fee';	
	$this->assign_batch_form('table_view',$table_data,$max_column=6,$update_form);

	$navi = $this->list->get_pager_links();
	$this->assign('pager_navi',$navi['all']);

	$upper_path = '一覧ページ';
	$this->add_pathlist('/fields/',$upper_path,false);
	$this->add_pathlist('',$this->page['name']);
	$this->page['name'] = 'フィールド';
	$this->assign('page',$this->page);
	return $this->display($this->current_template);
}

} // end of fields class
?>