<?php 
class CActionIssue extends CActionBase
{
	function doAction()
	{	
		$ret = 0;
		$job = $_POST["job"];
		GetCookie();
		
		switch($job)
		{
			case "add":
				$ret = $this->Add();
			break;
			case "work":
				$ret = $this->Work();
			break;
			case "mod":
				$ret = $this->Mod();
			break;
			case "del":
				$ret = $this->Del();
			break;
			case "attach":
				$ret = $this->Attach();
			break;
			case "tran":
				$ret = $this->Tran();
			break;
		}
		
		return $ret;
	}
	
	function Add()
	{
		global $UserInfo, $FileInfo;	
		
		$parent_id = $_POST["parent_id"];
		$project_id = $_POST["project_id"];
		$issue_type = $_POST["issue_type"];  
		$issue_title = str_replace("'", "''", stripslashes($_POST["issue_title"])); 
		$issue_priority = $_POST["issue_priority"];  
		$issue_serverity = $_POST["issue_serverity"];  
		$issue_action = $_POST["issue_action"];  
		$project_module = $_POST["project_module"];  
		$action_user = $_POST["action_user"];  
		$exp_time = $_POST["exp_time"];  
		$issue_content = str_replace("'", "''", $_POST["issue_content"]);
		$action_users = split(",", $_POST["action_users"]);
		
		//检查是否选择处理用户
		if(sizeof($action_users)==1 && $action_users[0]==0)
			return "<script> alert('请选择处理用户'); </script>";
		
		//1. 找出对应动作的下一状态
		require_once("../module/module.base.php");
		require_once("../module/module.flow_action.php");
		$action = new CModuleFlowAction();
		$action->InitModule($issue_action);
		if($action->module == '')
			return "<script> alert('创建事务出现异常,找不到对应动作状态:".$issue_action."'); </script>";
		$status_id = $action->module->dst_status_id;
		
		//2. 上传文件
		$file_error = $_FILES["attach_file"]["error"];
		if($file_error == UPLOAD_ERR_OK)
		{
			$file_name = $_FILES["attach_file"]["name"];
			$file_tmp = $_FILES["attach_file"]["tmp_name"];
			$file_size = $_FILES["attach_file"]["size"];
			$file_type = strtolower(substr($file_name, strlen($file_name)-3, 3));
			require_once("../common/class.upload.php");
			$up = new CUpload();
			if(!$up->Execute($file_tmp, $FileInfo["save_path"],$file_size, $file_type))
					return "<script> alert('".$up->GetError()."'); </script>";
		}
		
		//3. 添加事务信息
		$issue_id = $this->db->GetMaxField('tb_issue', 'issue_id')+1;
		$sql = "insert into tb_issue (";
		$sql = $sql . "issue_id,";
		$sql = $sql . "parent_issue_id,";        
		$sql = $sql . "issue_type_id,";   
		$sql = $sql . "project_id,";      
		$sql = $sql . "issue_title,";     
		$sql = $sql . "issue_content,";   
		$sql = $sql . "status_id,";       
		$sql = $sql . "create_user,";     
		$sql = $sql . "module_id,";       
		$sql = $sql . "pripority_id,";    
		$sql = $sql . "serverity_id,";    
		$sql = $sql . "create_time,";     
		$sql = $sql . "exp_time)";       
		$sql = $sql . " values (";
		$sql = $sql . $issue_id.",";
		$sql = $sql . $parent_id.",";
		$sql = $sql . $issue_type.",";
		$sql = $sql . $project_id.",";
		$sql = $sql . "'".$issue_title."',";
		$sql = $sql . "'".$issue_content."',";
		$sql = $sql . $status_id.",";
		$sql = $sql . $UserInfo["UID"].",";
		$sql = $sql . $project_module.",";
		$sql = $sql . $issue_priority.",";
		$sql = $sql . $issue_serverity.",";
		$sql = $sql . "now(),";
		$sql = $sql . "'".$exp_time."')";
		$this->db->ExeSql($sql);
	
		//4. 添加文件信息
		if($file_error == UPLOAD_ERR_OK)
		{
			$attach_id = $this->db->GetMaxField('tb_issue_attach', 'attach_id')+1;
			$sql = "insert into tb_issue_attach (attach_id, issue_id, attach_name, attach_type, file_name, file_size, user_id, create_time) values (";
			$sql = $sql. $attach_id . ",";
			$sql = $sql. $issue_id . ",";
			$sql = $sql. "'".$file_name . "',";
			$sql = $sql. "'".$up->file_type . "',";
			$sql = $sql. "'".$up->file_name . "',";
			$sql = $sql. $file_size . ",";
			$sql = $sql. $UserInfo["UID"].",";
			$sql = $sql . "now() )";
			$this->db->ExeSql($sql);
		}
	
		//5. 添加处理人
		$action_code = $this->db->GetMaxField('tb_issue_action_user', 'action_code')+1;
		for($i=0;$i<sizeof($action_users);$i++)
		{
			if($action_users[$i] == 0) continue;
			$sql = "insert into tb_issue_action_user (action_code, issue_id, status_id, action_user_id, action_time) values (";
			$sql = $sql . $action_code .",";
			$sql = $sql . $issue_id .",";
			$sql = $sql . $status_id .",";
			$sql = $sql . $action_users[$i] .",";
			$sql = $sql . "now() )";
			$this->db->ExeSql($sql);
			
			//6. 通知处理人
			$this->Notice($action_users[$i], $action->module->action_name, $issue_type, $issue_title, $issue_id);
		}
				
		//7. 跳转反馈页面
		$echo_str = "<div style=\"padding-left:200;\" class=\"big\"><br>事务添加成功&nbsp;&nbsp; <a href=\"index.php?job=issue_create\">继续创建新事物</a><br><br>";
		$echo_str = $echo_str . "查看事务详细信息:<a href=\"index.php?job=issue_info&issue_id=".$issue_id."\">".$issue_title."</a><br><br><br>";
		$echo_str = $echo_str . "</div>";
		return "<script>parent.document.getElementById('form_new_issue').innerHTML='".$echo_str."';</script>";
	}
	
	function Work()
	{
		global $UserInfo, $FileInfo;	
		$issue_id = $_POST["issue_id"];
		$action_id = $_POST["action_id"];
		$module_id = $_POST["project_module"];
		$src_status_id = $_POST["src_status_id"];
		$status_id = $_POST["status_id"];
		$action_user = $_POST["action_user"];
		$issue_content = str_replace("'", "''", $_POST["issue_content"]); 
		$log_id = $this->db->GetMaxField('tb_issue_log', 'log_id')+1;
		
		//1.上传附件
		$file_error = $_FILES["attach_file"]["error"];
		if($file_error == UPLOAD_ERR_OK)
		{
			$file_name = $_FILES["attach_file"]["name"];
			$file_tmp = $_FILES["attach_file"]["tmp_name"];
			$file_size = $_FILES["attach_file"]["size"];
			$file_type = strtolower(substr($file_name, strlen($file_name)-3, 3));
			require_once("../common/class.upload.php");
			$up = new CUpload();
			if(!$up->Execute($file_tmp, $FileInfo["save_path"],$file_size, $file_type))
					return "<script> alert('".$up->GetError()."'); </script>";
					
			$attach_id = $this->db->GetMaxField('tb_issue_attach', 'attach_id')+1;
			$sql = "insert into tb_issue_attach (attach_id, issue_id, log_id, attach_name, attach_type, file_name, file_size, user_id, create_time) values (";
			$sql = $sql. $attach_id . ",";
			$sql = $sql. $issue_id . ",";
			$sql = $sql. $log_id . ",";
			$sql = $sql. "'".$file_name . "',";
			$sql = $sql. "'".$up->file_type . "',";
			$sql = $sql. "'".$up->file_name . "',";
			$sql = $sql. $file_size . ",";
			$sql = $sql. $UserInfo["UID"].",";
			$sql = $sql . "now() )";
			$this->db->ExeSql($sql);
		}
		
		//2 添加事务日志
		$sql = "insert into tb_issue_log (log_id, issue_id, status_id, user_id, action_id, work_log, work_time ) values (";
		$sql = $sql . $log_id . ",";
		$sql = $sql . $issue_id . ",";
		$sql = $sql . $status_id . ",";
		$sql = $sql . $UserInfo['UID'] . ",";
		$sql = $sql . $action_id . ",";
		$sql = $sql . "'" . $issue_content . "',";		
		$sql = $sql . "now())";
		$this->db->ExeSql($sql);
		
		//3 更新当前处理人动作
		$sql = "update tb_issue_action_user set action_id=".$action_id.", action_time=now() where issue_id=".$issue_id." and status_id=".$src_status_id." and action_user_id=".$UserInfo['UID']." and action_id=0";
		$this->db->ExeSql($sql);
		
		//4 更新事务状态和添加新处理人
		require_once("../module/module.base.php");
		require_once("../module/module.issue.php");
		$issue = new CModuleIssue();
		$issue->Init($issue_id);
		
		//判断事务的处理人是否全部处理完毕
		if($issue->modules[0]->ActionUserCount() == 0) 
		{		
			//事务下一状态
			$sql = "update tb_issue set status_id=".$status_id.", process_user=".$UserInfo['UID'].", module_id=".$module_id." where issue_id=".$issue_id;
			$this->db->ExeSql($sql);
		
			//事务下一状态处理人
			$action_users = split(",",$_POST["action_users"]);
			$action_code = $this->db->GetMaxField('tb_issue_action_user', 'action_code')+1;
			for($i=0; $i<sizeof($action_users);$i++)
			{
					if($action_users[$i] == 0) continue;
				
					$sql = "insert into tb_issue_action_user (action_code, issue_id, status_id, action_user_id) values (";
					$sql = $sql . $action_code . ",";
					$sql = $sql . $issue_id . ",";
					$sql = $sql . $status_id . ",";
					$sql = $sql . $action_users[$i] . ")";
					$this->db->ExeSql($sql);
			}
		}
		
		//6. 通知处理人
		require_once("../module/module.base.php");
		require_once("../module/module.flow_action.php");
		$action = new CModuleFlowAction();
		$action->InitModule($action_id);
		if($action->module->DstIsClose())
		{
			$sql = "update tb_issue set is_close=1, close_time=now() where issue_id=".$issue_id;
			$this->db->ExeSql($sql);
		}
		
		if(sizeof($action_users)>0 && $action_users[0] != 0) //指定了明确处理人
		{
			for($i=0; $i<sizeof($action_users);$i++)
				$this->Notice($action_users[$i], $action->module->action_name, $issue->modules[0]->issue_type_id, $issue->modules[0]->issue_title, $issue_id);
				
			//分配问题时 额外通知黄圣洁,邓迎雪
			if($action->module->action_name == '分配')
			{
				$this->Notice(46, $action->module->action_name, $issue->modules[0]->issue_type_id, $issue->modules[0]->issue_title, $issue_id);
				$this->Notice(47, $action->module->action_name, $issue->modules[0]->issue_type_id, $issue->modules[0]->issue_title, $issue_id);
			}
		}
		else
		{
			if($action->module->DstIsClose()) //目标状态是关闭状态
			{
					$notice_creater=false;
					
					//取前一状态的处理人
					$users = $this->GetPrevActionUser($issue_id, $action_code);
					for($i=0; $i<sizeof($users); $i++)
					{
						$this->Notice($users[$i], $action->module->action_name, $issue->modules[0]->issue_type_id, $issue->modules[0]->issue_title, $issue_id);
						if($users[$i] == $issue->modules[0]->create_user)
							$notice_creater = true;
					}
					//并通知创建者
					if(!$notice_creater)
						$this->Notice($issue->modules[0]->create_user, $action->module->action_name, $issue->modules[0]->issue_type_id, $issue->modules[0]->issue_title, $issue_id);
			}	
		}
		
		return "<script> parent.CloseDialog(1); </script>";
	}
	
	function Notice($action_user, $action_name, $issue_type, $issue_title, $issue_id)
	{
		global $UserInfo;
		
		require_once("../module/module.project_issuetype.php");
		$issueType = new CModuleIssueType();
		$issueType->InitModule($issue_type);
		$type_name = $issueType->module->type_name;
		
		require_once("../module/module.user.php");
		$user = new CModuleUser();
		$user->InitModule($action_user);
		
		$msg = "\n";
		$msg = $msg. "您好,";
		$msg = $msg. $UserInfo['RNAME'].$action_name."了".$type_name.": ";
		$msg = $msg. $issue_title."\n";
		
		$this->SendNotice($user->module->email, $msg, $issue_id);
	}
	
	//获取事务前一动作处理人
	function GetPrevActionUser($issue_id, $action_code)
	{
		$users = array();
		$sql = "select action_code, action_user_id from tb_issue_action_user where issue_id=".$issue_id." and action_code <".$action_code." order by action_code desc";
		$this->db->ExeQuerySql($sql);
		
		$action_code = 0;
		while($this->db->FetchRow())
		{	
				if( $action_code>0 && $action_code!=$this->db->Get("action_code") ) break;
				$users[sizeof($users)] = $this->db->Get("action_user_id");
				$action_code=$this->db->Get("action_code");
		}
		$this->db->FreeResult();
		
		return $users;
	}
	
	function Mod()
	{
		global $UserInfo, $Config;
		$issue_id = $_POST["issue_id"];
		$status_id= $_POST["issue_status"];
		$serverity = $_POST["serverity"];
		$priority = $_POST["priority"];
		$action_user = $_POST["action_user"];
		$exp_time = $_POST["exp_time"]; 
		$issue_content = str_replace("'", "''", $_POST["issue_content"]); 
		$issue_title = str_replace("'", "''", $_POST["issue_title"]); 
		
		$sql = "update tb_issue set status_id=".$status_id.", serverity_id=".$serverity.", pripority_id=".$priority.", exp_time='".$exp_time."', issue_content='".$issue_content."' where issue_id=".$issue_id;
		$this->db->ExeSql($sql);
		
		$sql = "delete from tb_issue_action_user where issue_id=".$issue_id." and action_id=0";
		$this->db->ExeSql($sql);
		
		//判断是否关闭
		require_once("../module/module.base.php");
		require_once("../module/module.status.php");
		$status = new CModuleStatus();
		$status->InitModule($status_id);
		if($status->module->state_type == $Config['state_type_close'])
		{
			$sql = "update tb_issue set is_close=1, close_time=now() where issue_id=".$issue_id;
			$this->db->ExeSql($sql);			
		}
		else
		{
			$sql = "update tb_issue set is_close=0, close_time='' where issue_id=".$issue_id;
			$this->db->ExeSql($sql);	
			$action_code = $this->db->GetMaxField('tb_issue_action_user', 'action_code')+1;
			$sql = "insert into tb_issue_action_user (action_code, issue_id, status_id, action_user_id) values (";
			$sql = $sql . $action_code . ",";
			$sql = $sql . $issue_id . ",";
			$sql = $sql . $status_id . ",";
			$sql = $sql . $action_user . ")";
			$this->db->ExeSql($sql);
		}
	
		
		$comment_id = $this->db->GetMaxField('tb_issue_comment', 'comment_id')+1;
		$sql = "insert into tb_issue_comment (comment_id, issue_id, issue_comment, user_id, comment_time) values (";
		$sql = $sql . $comment_id.",";
		$sql = $sql . $issue_id.",";
		$sql = $sql . "'".$UserInfo['RNAME']."对事务作了修改',";
		$sql = $sql . $UserInfo["UID"].",";
		$sql = $sql ."now())";
		$this->db->ExeSql($sql);
		
		//发送修改通知
		require_once("../module/module.user.php");
		$user = new CModuleUser();
		$user->InitModule($action_user);
		
		$msg = "\n";
		$msg = $msg. "您好,";
		$msg = $msg. $UserInfo['RNAME']."修改了: ".$issue_title."\n";		
		$this->SendNotice($user->module->ip, $msg, $issue_id);
		
		return "<script> parent.CloseDialog(1); </script>";
	}
	
	function Del()
	{
		$issue_id = $_POST["issue_id"];
		$sql = "update tb_issue set is_delete=1 where issue_id=".$issue_id;
		$this->db->ExeSql($sql);
	}
	
	//添加附件
	function Attach()
	{
		global $UserInfo, $FileInfo;
		$issue_id = $_POST["issue_id"];
		$file_error = $_FILES["attach_file"]["error"];
		if($file_error == UPLOAD_ERR_OK)
		{
			$file_name = $_FILES["attach_file"]["name"];
			$file_tmp = $_FILES["attach_file"]["tmp_name"];
			$file_size = $_FILES["attach_file"]["size"];
			$file_type = strtolower(substr($file_name, strlen($file_name)-3, 3));
			require_once("../common/class.upload.php");
			$up = new CUpload();
			if(!$up->Execute($file_tmp, $FileInfo["save_path"],$file_size, $file_type))
					return "<script> alert('".$up->GetError()."'); </script>";
					
			$attach_id = $this->db->GetMaxField('tb_issue_attach', 'attach_id')+1;
			$sql = "insert into tb_issue_attach (attach_id, issue_id, log_id, attach_name, attach_type, file_name, file_size, user_id, create_time) values (";
			$sql = $sql. $attach_id . ",";
			$sql = $sql. $issue_id . ",";
			$sql = $sql. "0,";
			$sql = $sql. "'".$file_name . "',";
			$sql = $sql. "'".$up->file_type . "',";
			$sql = $sql. "'".$up->file_name . "',";
			$sql = $sql. $file_size . ",";
			$sql = $sql. $UserInfo["UID"].",";
			$sql = $sql . "now() )";
			$this->db->ExeSql($sql);
		}
		
		return "<script> alert('附件上传成功'); parent.CloseDialog(1); </script>";
	}
	
	//转移事务
	function Tran()
	{
		global $UserInfo;
		$issue_id = $_POST["issue_id"];
		$project_id = $_POST["project_id"];
		$issue_type = $_POST["issue_type"];
		
		$sql = "delete from tb_issue_action_user where issue_id=".$issue_id." and action_id=0";
		$this->db->ExeSql($sql);
		
		$action_code = $this->db->GetMaxField('tb_issue_action_user', 'action_code')+1;
		$sql = "insert into tb_issue_action_user (action_code, issue_id, status_id, action_user_id) values (";
		$sql = $sql . $action_code . ",";
		$sql = $sql . $issue_id . ",";
		$sql = $sql . "0,";
		$sql = $sql . $UserInfo['UID'] . ")";
		$this->db->ExeSql($sql);
		
		$sql = "update tb_issue set project_id=".$project_id.", issue_type_id=".$issue_type.", module_id=0 where issue_id=".$issue_id;
		$this->db->ExeSql($sql);
	}
};
?>
