<?php
/*
	[Office 515158] (C) 2009-2014 天生创想 Inc.
	$Id: mod_workclass mobile $
*/
(!defined('IN_TOA') || !defined('IN_ADMIN')) && exit('Access Denied!');
empty($do) && $do = 'list';
if(getGP('modid','G')!=''){
	$modid=getGP('modid','G');
}else{
	$modid=getGP('modid','P');
}
$typeid=getGP('typeid','G');
$tplid=getGP('tplid','G');
$tid=getGP('tid','G');
if ($do == 'list') {
	if($tplid==''){
		global $_CACHE;
		get_cache('workclass_type');
		include_once('template/worklist.php');
	}else{
		$workid=getGP('workid','G','int');
		if($workid!=''){
			$tpl = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."workclass_template  WHERE tplid = '".$tplid."'  ");
			$work = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."workclass  WHERE id = '".$workid."'  ");
			//获取当前流程
			if(getGP('perid','G','int')!=''){
				$flow['perid']=getGP('perid','G','int');
				$flow['flowid']=getGP('flowid','G','int');
			}else{
				$sql = "SELECT perid,flowid FROM ".DB_TABLEPRE."workclass_personnel  WHERE workid = '".$workid."' and (pertype=0 or pertype=4) order by perid desc";
				$flow = $db->fetch_one_array($sql);
			}
			if($tpl['tplkey']!=''){
				include_once('template/workclass_add.php');
			}else{
				echo '无效的类型！';
			}
		}else{
			$workclass = array();
			$workclass_db = array();
			$tpl = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."workclass_template  WHERE tplid = '".$tplid."'  ");
			//写入主表信息
			$workclass['number']=get_date('YmdHis',PHP_TIME);
			$workclass['typeid']=$typeid;
			$workclass['tplid']=$tplid;
			$workclass['title']=trim($tpl["title"]).'(No:'.get_date('YmdHis',PHP_TIME).')'.get_realname($_USER->id);
			$workclass['uid']=$_USER->id;
			$workclass['type']=0;
			$workclass['date']=get_date('Y-m-d H:i:s',PHP_TIME);
			insert_db('workclass',$workclass);
			$workid=$db->insert_id();
			if($tpl['tplkey']!=4 && $tpl['tplkey']!=5){
				//写入数据表
				$workclass_db = array(
						'workid' => $workid,
						'tplid' => $tplid,
						'typeid' => $typeid,
						'type'=>1
					);
				insert_db('wd_'.$tplid,$workclass_db);
			}
			//处理流程
			$sql = "SELECT * FROM ".DB_TABLEPRE."workclass_flow  WHERE tplid = '".$tplid."' and flownum=1 and typeid= '".$typeid."' order by fid asc";
			$flow = $db->fetch_one_array($sql);
			$personnel1 = array(
				'name' => get_realname($_USER->id),
				'uid' =>$_USER->id,
				'designationdate' =>get_date('Y-m-d H:i:s',PHP_TIME),
				'pertype' =>0,
				'workid' => $workid,
				'typeid' =>$typeid,
				'flowid' => $flow['fid'],
				'appkey' => $flow['flowkey2'],
				'appkey1' => $flow['flowkey3'],
				'flowdatetype' => 0,
				'flowdate' => get_date('Y-m-d H:i:s',PHP_TIME)
				);
			insert_db('workclass_personnel',$personnel1);
			//更新LOG
			$content=serialize($workclass);
			$title='新建工作流程';
			get_logadd($id,$content,$title,35,$_USER->id);
			header('Location: index.php?ac=workadd&fileurl=plugin&tplid='.$tplid.'&typeid='.$typeid.'&workid='.$workid.'&modid='.$modid.'');
		}
		
	}

}elseif ($do == 'add') {
	mod_workflow_update();
	mod_workform_update();
	mod_workform_view_update();
	//更新LOG
	$title='保存流程数据';
	get_logadd($id,'',$title,35,$_USER->id);
	header('Location: index.php?ac=workadd&fileurl=plugin&tplid='.$tplid.'&typeid='.$typeid.'&flowid='.getGP('flowid','P','int').'&perid='.getGP('perid','P','int').'&error=true&workid='.getGP('workid','P','int'));
}elseif ($do == 'fileadd') {
	$workid = getGP('workid','G','int');
	if($workid!=''){
		$fileoffice=array();
		$fileoffice['filename'] = unescape(getGP('title','G'));
		$fileoffice['date'] = get_date('Y-m-d H:i:s',PHP_TIME);
		$fileoffice['fileaddr'] = str_replace('../','',getGP('annex','G'));
		$fileoffice['uid'] = $_USER->id;
		$fileoffice['officetype'] = 3;
		$fileoffice['filetype'] = 2;
		$fileoffice['fileid'] = 0;
		$fileoffice['number'] = $workid;
		insert_db('fileoffice',$fileoffice);
		$id=$db->insert_id();
		if($id!=''){
			echo 'true';
			exit;
		}
	}
	echo 'false';
	exit;
}
?>