<?php 
class CActionConfigFlow extends CActionBase
{
	function doAction()
	{
		$ret = 0;
		$flow_id = $_POST["flow_id"];
		$job = $_POST["job"];
		if($job=="add" && $flow_id!=0) $job="mod";
		
		switch($job)
		{
			case "add":
				$ret = $this->Add();
			break;
			case "mod":
				$ret = $this->Mod();
			break;
			case "del":
				$ret = $this->Del();
			break;
		}
		
		return $ret;
	}	
	
	function Add()
	{
		$json_str = $_POST["json_str"];
		$obj = json_decode($json_str);
		
		//保存流程基本信息
		$flow_id = $this->db->GetMaxField('tb_flow', 'flow_id')+1;
		$sql = "insert into tb_flow (flow_id, flow_name, create_time) values (";
		$sql = $sql . $flow_id . ",";
		$sql = $sql . "'".$obj->flow_name."',";
		$sql = $sql . "now())";
		$this->db->ExeSql($sql);
		
		//保存流程状态信息
		$status_id = $this->db->GetMaxField('tb_flow_status', 'status_id')+1;
		for($i=0; $i<sizeof($obj->states);$i++)
		{
			$sql = "insert into tb_flow_status (status_id, flow_id, chart_state_id, state_name, state_type, state_title, coord_left, coord_top) values (";
			$sql = $sql . $status_id.",";		
			$sql = $sql . $flow_id.",";
			$sql = $sql . "'".$obj->states[$i]->state_id."',";
			$sql = $sql . "'".$obj->states[$i]->state_name."',";
			$sql = $sql . $obj->states[$i]->state_type.",";
			$sql = $sql . "'".$obj->states[$i]->state_title."',";
			$sql = $sql . $obj->states[$i]->coord_left.",";
			$sql = $sql . $obj->states[$i]->coord_top;
			$sql = $sql . ")";
			$this->db->ExeSql($sql);
			$status_id++;
		}
		
		//保存流程动作信息
		$action_id = $this->db->GetMaxField('tb_flow_action', 'action_id')+1;
		for($i=0; $i<sizeof($obj->actions);$i++)
		{
			$sql = "insert into tb_flow_action (action_id, flow_id, chart_action_id, action_name, action_title, src_status, dst_status, points, coord_left, coord_top) values (";
			$sql = $sql . $action_id . ",";
			$sql = $sql . $flow_id.",";
			$sql = $sql . "'".$obj->actions[$i]->action_id."',";
			$sql = $sql . "'".$obj->actions[$i]->action_name."',";
			$sql = $sql . "'".$obj->actions[$i]->action_title."',";
			$sql = $sql . "'".$obj->actions[$i]->src_state."',";
			$sql = $sql . "'".$obj->actions[$i]->dst_state."',";
			$sql = $sql . "'".$obj->actions[$i]->points."',";
			$sql = $sql . $obj->actions[$i]->coord_left.",";
			$sql = $sql . $obj->actions[$i]->coord_top;
			$sql = $sql . ")";
			
			$this->db->ExeSql($sql);
			$action_id++;				
		}
		
		return $flow_id;
	}
	
	function Mod()
	{
		$json_str = $_POST["json_str"];
		$obj = json_decode($json_str);
		
		//保存流程基本信息
		$sql = "update tb_flow set flow_name='".$obj->flow_name."', update_time=now() where flow_id=".$obj->flow_id;
		$this->db->ExeSql($sql);
		
		//保存流程状态信息
		$status_id = $this->db->GetMaxField('tb_flow_status', 'status_id')+1;
		
		$sql = "delete from tb_flow_status where flow_id=".$obj->flow_id;
		$this->db->ExeSql($sql);
		
		for($i=0; $i<sizeof($obj->states);$i++)
		{
			$id = $obj->states[$i]->db_id;
			if($id==0) $id = $status_id++;
			
			$sql = "insert into tb_flow_status (status_id, flow_id, chart_state_id, state_name, state_type, state_title, coord_left, coord_top) values (";
			$sql = $sql . $id.",";	
			$sql = $sql . $obj->flow_id.",";
			$sql = $sql . "'".$obj->states[$i]->state_id."',";
			$sql = $sql . "'".$obj->states[$i]->state_name."',";
			$sql = $sql . $obj->states[$i]->state_type.",";
			$sql = $sql . "'".$obj->states[$i]->state_title."',";
			$sql = $sql . $obj->states[$i]->coord_left.",";
			$sql = $sql . $obj->states[$i]->coord_top;
			$sql = $sql . ")";
			
			$this->db->ExeSql($sql);
		}
		
		//保存流程动作信息
		$action_id = $this->db->GetMaxField('tb_flow_action', 'action_id')+1;
		$sql = "delete from tb_flow_action where flow_id=".$obj->flow_id;
		$this->db->ExeSql($sql);
		
		for($i=0; $i<sizeof($obj->actions);$i++)
		{
			$id = $obj->actions[$i]->db_id;
			if($id==0) $id = $action_id++;
			
			$sql = "insert into tb_flow_action (action_id, flow_id, chart_action_id, action_name, action_title, src_status, dst_status, points, coord_left, coord_top) values (";
			$sql = $sql . $id . ",";
			$sql = $sql . $obj->flow_id.",";
			$sql = $sql . "'".$obj->actions[$i]->action_id."',";
			$sql = $sql . "'".$obj->actions[$i]->action_name."',";
			$sql = $sql . "'".$obj->actions[$i]->action_title."',";
			$sql = $sql . "'".$obj->actions[$i]->src_state."',";
			$sql = $sql . "'".$obj->actions[$i]->dst_state."',";
			$sql = $sql . "'".$obj->actions[$i]->points."',";
			$sql = $sql . $obj->actions[$i]->coord_left.",";
			$sql = $sql . $obj->actions[$i]->coord_top;
			$sql = $sql . ")";
			
			$this->db->ExeSql($sql);
		}
		
		return 0;
	}
	
	function Del()
	{
		$flow_id = $_POST["flow_id"];
		$sql = "delete from tb_flow_status where flow_id=".$flow_id;
		$this->db->ExeSql($sql);
		$sql = "delete from tb_flow_action where flow_id=".$flow_id;
		$this->db->ExeSql($sql);
		$sql = " delete from tb_flow where flow_id=".$flow_id;
		$this->db->ExeSql($sql);
		return 0;
	}
	
};
?>
