<?php
/*
	[Office 515158] (C) 2009-2014 天生创想 Inc.
	$Id: common.class.php mobile $
*/
!defined('IN_TOA') && exit('Access Denied!');
//日期控件
function mob_date($name,$inputname,$y,$m,$d,$type){
	$ds=date('j',mktime(0,0,1,($m==12?1:$m+1),1,($m==12?$y+1:$y))-24*3600);
	$html='<fieldset data-role="controlgroup" data-type="horizontal">';
		//$html.='<legend>'.$name.':</legend>';
		$html.='<label for="select-h-1a">年</label>';
		$html.='<select id="select-h-1a" name="'.$inputname.'[]">';
			for($i=get_date('Y',PHP_TIME)-3;$i<=get_date('Y',PHP_TIME)+3;$i++){
				$html.='<option value="'.$i.'"';
				if(trim($y)==trim($i)){
					$html.=' selected="selected"';
				}
				$html.='>'.$i.'</option> ';
			}
		$html.='</select>';
		$html.='<label for="select-h-1b">月</label>';
		$html.='<select id="select-h-1b" name="'.$inputname.'[]">';
			for($i=1;$i<=12;$i++){
				if($i<10){
					$i='0'.$i;
				}
				$html.='<option value="'.$i.'"';
				if(trim($m)==trim($i)){
					$html.=' selected="selected"';
				}
				$html.='>'.$i.'</option> ';
			}
		$html.='</select>';
		$html.='<label for="select-h-1c">日</label>';
		$html.='<select id="select-h-1c" name="'.$inputname.'[]">';
			for($i=1;$i<=$ds;$i++){
				if($i<10){
					$i='0'.$i;
				}
				$html.='<option value="'.$i.'"';
				if(trim($d)==trim($i)){
					$html.=' selected="selected"';
				}
				$html.='>'.$i.'</option> ';
			}
		$html.='</select>';
		
	$html.='</fieldset>';
	return $html;
}
//接收日期
function mob_date_post($inputvalue){
	foreach ($inputvalue as $arrsave) {
		$inputvalues.=$arrsave.'-';
	}
	return substr($inputvalues, 0, -1);
}
//复选控件人员
function mob_select_user($name,$inputname,$value=''){
	$html='';
	$html.='<select name="'.$inputname.'[]" id="'.$inputname.'" multiple="multiple"  data-native-menu="false">';
	$html.='<option>'.$name.'</option>';
	global $db;
	$query = $db->query("SELECT a.id,b.name FROM ".DB_TABLEPRE."user a,".DB_TABLEPRE."user_view b where a.id=b.uid ORDER BY a.numbers Asc");
	while ($row = $db->fetch_array($query)) {
		$html.='<option value="'.trim($row['name']).'"';
		if(sizeof(explode($row['name'].',',$value.','))>1){
			$html.=' selected="selected"';
		}
		$html.='>'.trim($row['name']).'</option>
				';
	}
	$html.='</select>';
    return $html;
}
//接收复选框
function mob_select_post($inputvalue){
	foreach ($inputvalue as $arrsave) {
		$inputvalues.=$arrsave.',';
	}
	return substr($inputvalues, 0, -1);
}
//单选控件人员
function mob_radio_user($name,$inputname,$value='',$type=''){
	if($type!=''){
		$users="'".str_replace(",","','",$type)."'";
		$wheresql .= " AND b.name in(".$users.")";
	}
	
	$html='';
	$html.='<select id="'.$inputname.'" ';
	$html.=' name="'.$inputname.'" data-native-menu="false"> ';
	$html.='<option value="">'.$name.'</option>';
	global $db;
	$query = $db->query("SELECT a.id,b.name FROM ".DB_TABLEPRE."user a,".DB_TABLEPRE."user_view b where a.id=b.uid ".$wheresql." ORDER BY a.numbers Asc");
	while ($row = $db->fetch_array($query)) {
		$html.='<option value="'.$row['id'].'"';
		if($row['id']==$value){
			$html.=' selected="selected"';
		}
		$html.='>'.$row['name'].'</option>';
	}
	$html.='</select>';
    return $html;
}
//附件/图片，直接读到个人文件柜内容
function mob_radio_pic($name,$inputname,$value=''){
	$html='';
	$html.='<select id="'.$inputname.'" ';
	$html.=' name="'.$inputname.'" data-native-menu="false"> ';
	$html.='<option value="">'.$name.'</option>';
	global $db;
	$query = $db->query("SELECT title,annex FROM ".DB_TABLEPRE."document where type=1 ORDER BY id desc LIMIT 50");
	while ($row = $db->fetch_array($query)) {
		$html.='<option value="'.$row['annex'].'"';
		if($row['annex']==$value){
			$html.=' selected="selected"';
		}
		$html.='>'.$row['title'].'</option>';
	}
	$html.='</select>';
    return $html;
}
//宏控件
function mob_magnificent($name='',$inputname='',$value='',$magnificent){
	$html='';
	$html.='<select id="'.$inputname.'" ';
	$html.=' name="'.$inputname.'" data-native-menu="false"> ';
	$html.='<option value="">'.$name.'</option>';
	global $db;
	global $_CACHE;
	get_cache('magnificent');
	$query = $db->query("SELECT ".$_CACHE['magnificent'][$magnificent]['name'].",".$_CACHE['magnificent'][$magnificent]['value']." FROM ".DB_TABLEPRE.$_CACHE['magnificent'][$magnificent]['table']."  ORDER BY ".$_CACHE['magnificent'][$magnificent]['order']." Asc LIMIT 50");
	while ($row = $db->fetch_array($query)) {
		$html.='<option value="'.$row[$_CACHE['magnificent'][$magnificent]['value']].'"';
		if($row[$_CACHE['magnificent'][$magnificent]['value']]==$value){
			$html.='  selected="selected"';
		}
		$html.='>'.$row[$_CACHE['magnificent'][$magnificent]['name']].'</option>';
	}
	$html.='</select>';
    return $html;
}
function mob_radio($name,$inputname,$value='',$radiovalue){
	$html='';
	$html.='<select id="'.$inputname.'" ';
	$html.=' name="'.$inputname.'" data-native-menu="false"> ';
	$html.='<option value="">'.$name.'</option>';
	$inputvaluenum=explode('|',$radiovalue); 
	for($i=0;$i<sizeof($inputvaluenum);$i++){
		$html.='<option value="'.$inputvaluenum[$i].'"';
		if($inputvaluenum[$i]==$value){
			$html.=' selected="selected"';
		}
		$html.='>'.$inputvaluenum[$i].'</option>';
	}
	$html.='</select>';
    return $html;
}
function mob_select($name,$inputname,$value='',$radiovalue){
	$html='';
	$html.='<select name="'.$inputname.'[]" id="'.$inputname.'" multiple="multiple"  data-native-menu="false">';
	$html.='<option>'.$name.'</option>';
	$inputvaluenum=explode('|',$radiovalue); 
	for($i=0;$i<sizeof($inputvaluenum);$i++){
		$html.='<option value="'.trim($inputvaluenum[$i]).'"';
		if(sizeof(explode(trim($inputvaluenum[$i]),$value))>1){
			$html.=' selected="selected"';
		}
		$html.='>'.trim($inputvaluenum[$i]).'</option>';
	}
	$html.='</select>';
    return $html;
}
function work_pertype($type){
	switch ($type)
	{
		case 0:
		  echo "未审批";
		  break;
		case 1:
		  echo "通过";
		  break;
		case 2:
		  echo "拒绝";
		  break;
		case 3:
		  echo "退回";
		  break;
		case 4:
		  echo "等待审批";
		  break;
		case 5:
		  echo "结束";
		  break;
		default:
		  echo "错误类型";
	}
	return ;
}
function work_pertype_log($type){
	switch ($type)
	{
		case 0:
		  echo "未审批";
		  break;
		case 1:
		  echo "通过";
		  break;
		case 2:
		  echo "拒绝";
		  break;
		case 3:
		  echo "退回";
		  break;
		default:
		  echo "错误类型";
	}
	return ;
}
function mod_workflow_update(){
	global $db;
	global $_USER;
	$workclass = array();
	//$workclass['numberview']=check_str(getGP('numberview','P'));
	$workclass['number']=check_str(getGP('number','P'));
	$workclass['title']=check_str(getGP('title','P'));
	update_db('workclass',$workclass, array('id' =>getGP('workid','P','int'),'uid'=>$_USER->id));
}
//更新表单信息
function mod_workform_update(){
	global $db;
	global $_USER;
	$workform = array();
	$workid=getGP('workid','P','int');
	$tplid=getGP('tplid','G','int');
	$typeid=getGP('typeid','G','int');
	$flowid=getGP('flowid','P','int');
	$formkey=public_value('formkey','workclass_flow','fid='.$flowid);
	if(sizeof(explode('toa',$formkey))>1){
	$query = $db->query("SELECT * FROM ".DB_TABLEPRE."workclass_from where tplid='".$tplid."' and typeid='".$typeid."' and inputtype!=6 ORDER BY fromid Asc");
	while ($row = $db->fetch_array($query)) {
		if(sizeof(explode('"'.$row['inputname'].'"',$formkey))>1){
			if($row['inputtype1']=='4'){
				$inputvalues='';
				$inputvalue=getGP(''.$row["inputname"].'','P','array');
				foreach ($inputvalue as $arrsave) {
					$inputvalues.=$arrsave.'|';
				}
				$workform[$row["inputname"]]=substr($inputvalues, 0, -1);
			}elseif($row['inputtype']=='3'){
				$inputvalue=getGP(''.$row["inputname"].'','P','array');
				$workform[$row["inputname"]]=mob_date_post($inputvalue);
			}else{
				$workform[$row["inputname"]]=trim(getGP(''.$row["inputname"].'','P'));
			}
		}
	}
	update_db('wd_'.$tplid,$workform, array('workid' =>$workid,'tplid'=>$tplid,'typeid'=>$typeid));
	}
	
}
function mod_workform_view_update(){
	global $db;
	global $_USER;
	$workid=getGP('workid','P','int');
	$tplid=getGP('tplid','G','int');
	$typeid=getGP('typeid','G','int');
	$inputname6=getGP('inputname6','P','array');
	foreach ($inputname6 as $from) {
		if($from!=''){
			$froms=explode(',',$from);
			$workform = array();
			$workform[$froms[0]]=$froms[1];
			update_db('wd_'.$tplid,$workform, array('workid' =>$workid,'tplid'=>$tplid,'typeid'=>$typeid));
			$sqld="DELETE FROM ".DB_TABLEPRE."wd_".$tplid."_".$froms[1]." WHERE workid = '".$workid."' and tplid = '".$tplid."' and typeid = '".$typeid."' and fromid = '".$froms[1]."' ";
			$db->query($sqld);
			$t6=getGP('t6'.$froms[1].'','P','array');
			foreach ($t6 as $value) {
				$t6s.=$value.',';
			}
			$t6s=substr($t6s, 0, -1);
			$t6s=explode(',',$t6s);
			for($i=0;$i<sizeof($t6s);$i++){
				$workformview = array();
				$query = $db->query("SELECT inputname FROM ".DB_TABLEPRE."workclass_from_view where tplid='".$tplid."' and typeid='".$typeid."' and fromid='".$froms[1]."' ORDER BY viewid Asc");
				$a1='';
				while ($row = $db->fetch_array($query)) {
					$a=serialize(getGP(''.$row["inputname"].'','P','array'));
					$a1=unserialize($a);
					$workformview[$row["inputname"]]=$a1[$i];
				}
				$workformview['workid']=$workid;
				$workformview['tplid']=$tplid;
				$workformview['typeid']=$typeid;
				$workformview['fromid']=$froms[1];
				insert_db('wd_'.$tplid.'_'.$froms[1],$workformview);
			}
		}
	}	
}

function apps_pertype($type){
	switch ($type)
	{
		case 0:
		  echo "未审批";
		  break;
		case 1:
		  echo "通过";
		  break;
		case 2:
		  echo "拒绝";
		  break;
		case 3:
		  echo "退回";
		  break;
		case 4:
		  echo "等待审批";
		  break;
		case 5:
		  echo "结束";
		  break;
		default:
		  echo "错误类型";
	}
	return ;
}
function apps_pertype_log($type){
	switch ($type)
	{
		case 0:
		  echo "未审批";
		  break;
		case 1:
		  echo "通过";
		  break;
		case 2:
		  echo "拒绝";
		  break;
		case 3:
		  echo "退回";
		  break;
		default:
		  echo "错误类型";
	}
	return ;
}
//更新主表信息
function mod_apps_update(){
	global $db;
	global $_USER;
	$apps = array();
	$apps['number']=check_str(getGP('number','P'));
	$apps['title']=check_str(getGP('title','P'));
	update_db('apps',$apps, array('id' =>getGP('appid','P','int'),'uid'=>$_USER->id));
}
//更新表单信息
function mod_appsform_update(){
	global $db;
	global $_USER;
	$apps = array();
	$appid=getGP('appid','P','int');
	$tplid=getGP('tplid','G','int');
	$tpltype=getGP('tpltype','G','int');
	$flowid=getGP('flowid','P','int');
	$formkey=public_value('formkey','app_flow','fid='.$flowid);
	if(sizeof(explode('toa',$formkey))>1){
	$query = $db->query("SELECT * FROM ".DB_TABLEPRE."app_from where tplid='".$tplid."' and  inputtype!=7 ORDER BY fromid Asc");
	while ($row = $db->fetch_array($query)) {
		if(sizeof(explode('"'.$row['inputname'].'"',$formkey))>1){
			if($row['inputtype1']=='4'){
				$inputvalues='';
				$inputvalue=getGP(''.$row["inputname"].'','P','array');
				foreach ($inputvalue as $arrsave) {
					$inputvalues.=$arrsave.'|';
				}
				$apps[$row["inputname"]]=substr($inputvalues, 0, -1);
			}elseif($row['inputtype']=='3'){
				$inputvalue=getGP(''.$row["inputname"].'','P','array');
				$apps[$row["inputname"]]=mob_date_post($inputvalue);
			}else{
				$apps[$row["inputname"]]=trim(getGP(''.$row["inputname"].'','P'));
			}
		}
	}
	update_db('apps_'.$tplid,$apps, array('appid' =>$appid,'tplid'=>$tplid));
	}
	
}
//新成员审批条件
function m_work_newuser($_flowusertype,$_flowuser,$UserId){
	global $db;
	if($_flowusertype=='2'){
		$html='';
		$flowuser=explode(',',$_flowuser);
		for($i=0;$i<sizeof($flowuser);$i++){
			if(trim($flowuser[$i])!=''){
				$sqluser = "SELECT b.name FROM ".DB_TABLEPRE."user a,".DB_TABLEPRE."user_view b where a.id=b.uid and  a.positionid like '%,".trim($flowuser[$i]).",%'  ORDER BY b.uid desc";
				$rowuserresult = $db->fetch_all($sqluser);
				foreach ($rowuserresult as $rowuser1) {
					if($rowuser1['name']!=''){
						$html.=$rowuser1['name'].',';
					}
				}
			}
		}
		$flowuser1=substr($html, 0, -1);
	}elseif($_flowusertype=='3'){
		$html='';
		$flowuser=explode(',',$_flowuser);
		for($i=0;$i<sizeof($flowuser);$i++){
			if(trim($flowuser[$i])!=''){
				$sqluser = "SELECT b.name FROM ".DB_TABLEPRE."user a,".DB_TABLEPRE."user_view b where a.id=b.uid and  a.departmentid like '%,".trim($flowuser[$i]).",%'  ORDER BY b.uid desc";
				$rowuserresult = $db->fetch_all($sqluser);
				foreach ($rowuserresult as $rowuser1) {
					if($rowuser1['name']!=''){
						$html.=$rowuser1['name'].',';
					}
				}
			}
		}
		$flowuser1=substr($html, 0, -1);
	}elseif($_flowusertype=='4'){
		$html='';
		$r = $db->fetch_one_array("SELECT departmentid FROM ".DB_TABLEPRE."user WHERE id = '".$UserId."'  ");
		$flowuser=explode(',',$r['departmentid']);
		for($i=0;$i<sizeof($flowuser);$i++){
			if(trim($flowuser[$i])!=''){
				$sqluser = "SELECT b.name FROM ".DB_TABLEPRE."user a,".DB_TABLEPRE."user_view b where a.id=b.uid and  a.departmentid like '%,".trim($flowuser[$i]).",%'  ORDER BY b.uid desc";
				$rowuserresult = $db->fetch_all($sqluser);
				foreach ($rowuserresult as $rowuser1) {
					if($rowuser1['name']!=''){
						$html.=$rowuser1['name'].',';
					}
				}
			}
		}
		$flowuser1=substr($html, 0, -1);
	}else{
		$flowuser1=$_flowuser;
	}
	return $flowuser1;
}
?>