<?php
/*
	[Office 515158] (C) 2009-2012 天生创想 Inc.
	$Id: fcnction_common.php 1209087 2013-01-30 08:58:28Z baiwei.jiang $
*/
!defined('IN_TOA') && exit('Access Denied!');
function get_login($user){
	if ( !$user ) {
		show_msg('你还没有登录，请先登录', 'login.php', 1000);
	}
}
//读取姓名
function get_realname($id){
	if($id!=''){
		global $db;
		$html='';
		$strid=explode(',',$id);
		for($i=0;$i<sizeof($strid);$i++){
			if(trim($strid[$i])!=''){
				$sql = "SELECT name FROM ".DB_TABLEPRE."user_view where uid='".$strid[$i]."'   ORDER BY uid desc limit 0,1";
				$row = $db->fetch_one_array($sql);
				if(trim($row['name'])!=''){
					//return $row['name'];
					$html.=trim($row['name']).',';
				}
			}
		}
	}
	return trim(substr($html, 0, -1));
}
//读取用户名及USER表其它名称
function get_realusername($fid=0,$uname=''){
    global $db;
	$sql = "SELECT ".$uname."  FROM ".DB_TABLEPRE."user where id='".$fid."'  ORDER BY id desc limit 0,1";
	$row = $db->fetch_one_array($sql);
	if($row[$uname]!=''){
		return $row[$uname];
	}
}
//读取id
function get_userid($id=0){
    global $db;
	$sql = "SELECT uid FROM ".DB_TABLEPRE."user_view where name='".$id."' ORDER BY uid desc limit 0,1";
	$row = $db->fetch_one_array($sql);
	if($row['uid']!=''){
		return $row['uid'];
	}
}
//根据姓名获取批量ID
function get_realid($name=''){
	if($name!=''){
		global $db;
		$staff=explode(',',$name);
		for($i=0;$i<sizeof($staff);$i++){
			$sql = "SELECT uid FROM ".DB_TABLEPRE."user_view where name='".trim($staff[$i])."' limit 0,1";
			$row = $db->fetch_one_array($sql);
			if($row['uid']!=''){
				$html.=$row['uid'].',';
			}
		}
	}
	return substr($html, 0, -1);
}
//读取手机
function get_realphone($id){
    global $db;
	$staff=explode(',',$id);
	for($i=0;$i<sizeof($staff);$i++){
		if(trim($staff[$i])!=''){
			$sql = "SELECT phone FROM ".DB_TABLEPRE."user_view where name='".$staff[$i]."' ORDER BY uid desc limit 0,1";
			$row = $db->fetch_one_array($sql);
			if($row['phone']!=''){
				//return $row['phone'];
				$html.=$row['phone'].',';
			}
		}
	}
	return substr($html, 0, -1);
}
$headurl="http://".$_SERVER ['HTTP_HOST'].$_SERVER['PHP_SELF'];
$pahttp="http://";
//部门
function get_realdepalist($fatherid=0,$selid=0,$layer=0 ){
    $str=""; 
    global $db;
	global $_USER;
	$query = $db->query("SELECT * FROM ".DB_TABLEPRE."department where father='$fatherid' and jituanid='".$_USER->jituanid."' ORDER BY id Asc");
	if(count($query)>0){
	   for($i=0;$i<$layer;$i++){
		   $str.="├";
	   }
		while ($row = $db->fetch_array($query)) {
			$selstr = $row['id'] == $selid ? 'selected="selected"' : '';
			$htmlstr= '<option value="'.$row['id'].'"  '.$selstr.'>'.$str.$row['name'].'</option>';
			echo $htmlstr;
			get_realdepalist($row['id'],$selid,$layer+1);
		}
	}
    return ;

}
//岗位
function get_postlist($fatherid=0,$selid=0,$layer=0 ){
    $str=""; 
    global $db;
	global $_USER;
	$query = $db->query("SELECT * FROM ".DB_TABLEPRE."position where father='$fatherid' and jituanid='".$_USER->jituanid."' ORDER BY id Asc  ");
	if(count($query)>0){
	   for($i=0;$i<$layer;$i++){
		   $str.="├";
	   }
		while ($row = $db->fetch_array($query)) {
			$selstr = $row['id'] == $selid ? 'selected="selected"' : '';
			$htmlstr= '<option value="'.$row['id'].'"  '.$selstr.'>'.$str.$row['name'].'</option>';
			echo $htmlstr;
			get_postlist($row['id'],$selid,$layer+1);
		}
	}
   return ;
}
//用户组
function get_grouplist($selid = 0) {
	global $db;
	global $_USER;
	$html = '';
	$sql = "SELECT id,groupname FROM ".DB_TABLEPRE."usergroup where jituanid='".$_USER->jituanid."'";
	$query = $db->query($sql);
	while ($row = $db->fetch_array($query)) {
		$selstr = $row['id'] == $selid ? 'selected="selected"' : '';
		$html .= '<option value="'.$row['id'].'" '.$selstr.' >'.$row['groupname'].'</option>';
	}
	return $html;
}
//输出用户组名称
function get_groupname($id = 0) {
	global $db;
	global $_USER;
	if((int)$id){
		$sql = "SELECT groupname FROM ".DB_TABLEPRE."usergroup where id=".$id." and jituanid='".$_USER->jituanid."' ";
		$result = $db->fetch_one_array($sql);
		if($result['groupname']!=''){
			return $result['groupname'];
		}
	}
}
//成员
function get_reallist($selid = 0) {
	global $db;
	global $_USER;
	$html = '';
	$sql = "SELECT uid,name FROM ".DB_TABLEPRE."user_view where jituanid='".$_USER->jituanid."' order by uid asc";
	$query = $db->query($sql);
	while ($row = $db->fetch_array($query)) {
		$selstr = $row['uid'] == $selid ? 'selected="selected"' : '';
		$html .= '<option value="'.$row['uid'].'" '.$selstr.' >'.$row['name'].'</option>';
	}
	return $html;
}
//读部门
function get_realdepaname($name=0,$jt=''){
	if($name!=''){
		global $db;
		global $_USER;
		if($jt==''){
			$wheres=" and jituanid='".$_USER->jituanid."'";
		}
		$staff=explode(',',$name);
		for($i=0;$i<sizeof($staff);$i++){
			$sql = "SELECT name FROM ".DB_TABLEPRE."department where id='".trim($staff[$i])."' ".$wheres." limit 0,1";
			$row = $db->fetch_one_array($sql);
			if($row['name']!=''){
				$html.=$row['name'].',';
			}
		}
	}
	return substr($html, 0, -1);
}
//岗位
function get_postname($name=0,$jt=''){
	if($name!=''){
		global $db;
		global $_USER;
		if($jt==''){
			$wheres=" and jituanid='".$_USER->jituanid."'";
		}
		$staff=explode(',',$name);
		for($i=0;$i<sizeof($staff);$i++){
			$sql = "SELECT name FROM ".DB_TABLEPRE."position where id='".trim($staff[$i])."' ".$wheres." limit 0,1";
			$row = $db->fetch_one_array($sql);
			if($row['name']!=''){
				$html.=$row['name'].',';
			}
		}
	}
	return substr($html, 0, -1);
}
function get_postid($name=0,$jt=''){
	if($name!=''){
		global $db;
		global $_USER;
		$staff=explode(',',$name);
		if($jt==''){
			$wheres=" and jituanid='".$_USER->jituanid."'";
		}
		for($i=0;$i<sizeof($staff);$i++){
			$sql = "SELECT id FROM ".DB_TABLEPRE."position where name='".trim($staff[$i])."' ".$wheres."  limit 0,1";
			$row = $db->fetch_one_array($sql);
			if($row['id']!=''){
				$html.=$row['id'].',';
			}
		}
	}
	//return substr($html, 0, -1);
	return ','.$html;
}
//办公类型(下拉)
function get_typelist($selid = 0,$typeid=0) {
	global $db;
	global $_USER;
	$html = '';
	$sql = "SELECT oid,oname FROM ".DB_TABLEPRE.DB_JT."office_type where otype=".$typeid." ";
	$query = $db->query($sql);
	while ($row = $db->fetch_array($query)) {
		$selstr = $row['oid'] == $selid ? 'selected="selected"' : '';
		$html .= '<option value="'.$row['oid'].'" '.$selstr.' >'.$row['oname'].'</option>';
	}
	return $html;
}
//办公类型(输出)
function get_typename($selid = 0) {
    if($selid==''){
		$oid=0;
	}else{
		$oid=$selid;
	}
	global $db;
	global $_USER;
	$html = '';
	$sql = "SELECT oid,oname FROM ".DB_TABLEPRE.DB_JT."office_type where oid=".$oid."";
	$query = $db->query($sql);
	while ($row = $db->fetch_array($query)) {
		$html .= ''.$row['oname'].'';
	}
	return $html;
}
//2013版去掉
function get_log($id){
	if($id=='1'){
		$t_id='|515158.com|';
	}else{
		$t_id=str_replace("|515158.com|","<br>",$id);
	}
	return $t_id;
}
//权限
function get_key($inputname){

      if(!is_superadmin() && !check_purview($inputname))
			{
				show_msg('对不起，你没有权限执行本操作！', 'home.php');
			}
		return ;
}
//选项（单/多）；表单名称；默认值；选项名称,宽,高
function get_pubuser($input=0,$inputname=0,$value=0,$name=0,$width=50,$height=4,$user='',$type='',$jt=''){
   
   if($input=='1'){
   echo "<input type='text' name='".$inputname."' id='".$inputname."' style='width:".$width."px;height:".$height."px;background-color:#F5F5F5;color:#006600;' value='".$value."' readonly />";
   echo "<input type='hidden' name='".$inputname."id' id='".$inputname."id' value='".get_userid($value)."' />";
   echo "<input type='hidden' name='".$inputname."phone' id='".$inputname."phone' value='".get_realphone($value)."' />";
   ?>
   <a href="javascript:;" onClick="window.open ('admin.php?ac=<?php echo $jt;?>user_radio&fileurl=public&inputname=<?php echo $inputname?>&user=<?php echo $user?>', 'newwindow', 'height=500, width=600, top=50, left=100, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no')"><?php echo $name?></a>
   <?php
   
   }elseif($input=='2'){
   echo "<textarea name='".$inputname."' cols='".$width."' rows='".$height."' readonly style='background-color:#F5F5F5;color:#006600;' id='".$inputname."'>".$value."</textarea>";
   echo "<input type='hidden' name='".$inputname."id' id='".$inputname."id' value='".get_realid($value)."' />";
   echo "<input type='hidden' name='".$inputname."phone' id='".$inputname."phone' /><br>";
   ?>
   <script type="text/javascript"> 
   function show_<?php echo trim($inputname)?>(){
	   <?php if($value!=''){?>
	   window.open ('admin.php?ac=<?php echo $jt;?>user_checkbox&fileurl=public&inputname=<?php echo $inputname?>&user=<?php echo $value?>&type=<?php echo $type;?>', 'newwindow', 'height=500, width=600, top=50, left=100, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no');
	   <?php }else{?>
	   var uservalue = document.getElementById('<?php echo trim($inputname)?>').value;
	   window.open ('admin.php?ac=<?php echo $jt;?>user_checkbox&fileurl=public&inputname=<?php echo $inputname?>&user='+uservalue, 'newwindow', 'height=500, width=600, top=50, left=100, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no');
	   <?php }?>
	}
</script>
   <a href="javascript:;" onClick="show_<?php echo trim($inputname)?>();"><?php echo $name?></a>
   <?php
   }elseif($input=='3'){
   echo "<input type='text' name='".$inputname."' id='".$inputname."' style='width:".$width."px;height:".$height."px;background-color:#F5F5F5;color:#006600;' value='".$value."' readonly />";
   echo "<input type='hidden' name='".$inputname."id' />";
   echo "<input type='hidden' name='".$inputname."phone' />";
   ?>
   <a href="javascript:;" onClick="window.open ('admin.php?ac=<?php echo $jt;?>user_checkbox&fileurl=public&inputname=<?php echo $inputname?>', 'newwindow', 'height=500, width=600, top=50, left=100, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no')"><?php echo $name?></a>
   <?php
   }
   return ;
}
//列表选择按钮权限
//名称；值；条件1；条件2；
function get_boxlistkey($name=0,$value=0,$user1=0,$user2=0){
	echo '<input type="checkbox" name="';
	echo $name.'" value="'.$value.'" class="checkbox"';
		if(!is_superadmin() && $user1!=$user2)
		{
			echo 'disabled="disabled"';
		}
	echo ' />';
}
//
function get_helps(){
	echo '<font color="#FF0000" title="打*号表示为必填">(*)</font>';
}
//URL权限
//标题；URL；条件1；条件2；
function get_urlkey($name=0,$url,$user1=0,$user2=0){
	if(!is_superadmin() && $user1){
		echo '<font color=#cccccc>'.$name.'</font>';
	}else{
		echo '<a href="'.$url.'">'.$name.'</a>';
	}
}
//提示框
function get_smsbox($name=0,$value=0){
	  $_CONFIGs=new config();
	  echo '<b>提醒'.$name.'：</b>';
	  if($_CONFIGs->config_data('configinfo')=='1'){
		  echo '<input type="checkbox" name="sms_info_box_'.$value.'" value="1" checked="checked" />短消息提示';
	  }
	  if($_CONFIGs->config_data('configsms')=='1'){
		  if(check_purview("office_sms")!='0'){
			  echo '<input type="checkbox" name="sms_phone_box_'.$value.'" value="1" />手机短信提示
			   ';
		   }
	   }
}
function get_newsmsbox($name=0,$value=0,$content=''){
	$html='';
	$_CONFIGs=new config();
	$html.='<div class="control-group ">';
	$html.='<div class="controls">';
	$html.='向'.$name.'发送提醒：';
	if($_CONFIGs->config_data('configinfo')=='1'){
		$html.='<div class="checkbox inline">';
		$html.='<label><input type="checkbox" name="sms_box_'.$value.'" id="sms_box_'.$value.'" value="1" checked="checked">短消息</label>';
		$html.='</div>';
	}
	if($_CONFIGs->config_data('configsms')=='1'){
		if(check_purview("office_sms")!='0'){
			$html.='<div class="checkbox inline">';
			$html.='<label><input type="checkbox" name="sms_phone_'.$value.'" id="sms_phone_'.$value.'" value="1" checked="checked">手机短信</label>';
			$html.='</div>';
		}
	}
	if($_CONFIGs->config_data('configmail')=='1'){
		$html.='<div class="checkbox inline">';
		$html.='<label><input type="checkbox" name="sms_mail_'.$value.'" id="sms_mail_'.$value.'" value="1" checked="checked">邮件</label>';
		$html.='</div>';
	}
	$html.='<input class="span5" name="box_content_'.$value.'" id="box_content_'.$value.'" type="text" value="'.$content.'">';
	$html.='</div></div>';
	return $html;
}

//部门选择
//选项（单/多）；表单名称；默认值；选项名称,宽,高
function get_depabox($input=0,$inputname=0,$value=0,$name=0,$width=50,$height=4,$jt='',$value2=''){
   if($value2){
	   $valueid=$value2;
   }else{
	   $valueid=get_realdepaid($value);
   }
   if($input=='1'){
   echo "<input type='text' name='".$inputname."' id='".$inputname."' style='width:".$width."px;height:".$height."px;background-color:#F5F5F5;color:#006600;' value='".$value."' readonly />";
   echo "<input type='hidden' name='".$inputname."id' id='".$inputname."id' value='".$valueid."' />";
   //echo "<input type='hidden' name='".$inputname."phone' />";
   ?>
   <a href="#" onClick="window.open ('admin.php?ac=<?php echo $jt;?>dep_radio&fileurl=public&inputname=<?php echo $inputname?>', 'newwindow', 'height=500, width=600, top=50, left=100, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no')"><?php echo $name?></a>
   <?php
   
   }else{
   echo "<textarea name='".$inputname."' id='".$inputname."' cols='".$width."' rows='".$height."' readonly style='background-color:#F5F5F5;color:#006600;'>".$value."</textarea>";
   echo "<input type='hidden' name='".$inputname."id' value='".$valueid."'/>";
  // echo "<input type='hidden' name='".$inputname."phone' /><br>";
   ?><br>
   <a href="#" onClick="window.open ('admin.php?ac=<?php echo $jt;?>dep_checkbox&fileurl=public&inputname=<?php echo $inputname?>', 'newwindow', 'height=500, width=600, top=50, left=100, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no')"><?php echo $name?></a>
   <?php
   }
   return ;
}

//部门选择
//选项（单/多）；表单名称；默认值；选项名称,宽,高
function get_positionbox($input=0,$inputname=0,$value=0,$name=0,$width=50,$height=4,$jt='',$value2=''){
   if($value2){
	   $valueid=$value2;
   }else{
	   $valueid=get_postid($value);
   }
   if($input=='1'){
   echo "<input type='text' name='".$inputname."' id='".$inputname."' style='width:".$width."px;height:".$height."px;background-color:#F5F5F5;color:#006600;' value='".$value."' readonly />";
   echo "<input type='hidden' name='".$inputname."id' value='".$valueid."' />";
   //echo "<input type='hidden' name='".$inputname."phone' />";
   ?>
   <a href="#" onClick="window.open ('admin.php?ac=<?php echo $jt;?>pos_radio&fileurl=public&inputname=<?php echo $inputname?>', 'newwindow', 'height=500, width=600, top=50, left=100, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no')"><?php echo $name?></a>
   <?php
   
   }else{
   echo "<textarea name='".$inputname."' id='".$inputname."' cols='".$width."' rows='".$height."' readonly style='background-color:#F5F5F5;color:#006600;'>".$value."</textarea>";
   echo "<input type='hidden' name='".$inputname."id' value='".$valueid."' />";
  // echo "<input type='hidden' name='".$inputname."phone' /><br>";
   ?><br>
   <a href="#" onClick="window.open ('admin.php?ac=<?php echo $jt;?>pos_checkbox&fileurl=public&inputname=<?php echo $inputname?>', 'newwindow', 'height=500, width=600, top=50, left=100, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no')"><?php echo $name?></a>
   <?php
   }
   return ;
}
//公共汇总
function public_number($table,$where=''){
	global $db;
	$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE.$table.$where." ");
	return $num;
}
//公共读取数据
function public_value($name='',$table='config',$where=''){
	$wheres=explode('=',$where);
	if($name!='' && $wheres[1]!=''){
		global $db;
		$sql = "SELECT ".$name." FROM ".DB_TABLEPRE.$table." where ".$where." limit 0,1";
		$row = $db->fetch_one_array($sql);
		if($row[$name]!=''){
			return $row[$name];
		}
	}
}
//上传
function public_upload($name='',$value='',$w,$h,$title){
	if($w=='' || $w=='0') $w='180';
	if($h=='' || $h=='0') $h='22';
	if($title=='') $title='附件上传';
	$_CONFIGS=new config();
	if($_CONFIGS->config_data('upload')==1){
		$html= '<input type="text" name="'.$name.'" class="BigInput" style="width:'.$w.'px;height:'.$h.'px;line-height:'.$h.'px;font-size:14px;" value="'.$value.'" />';
		$html.= '<a href="#" onClick="';
		$html.= "window.open ('admin.php?ac=uploadflash&fileurl=public&name=".$name."', ";
		$html.= "'newwindow111', 'height=200, width=480, top=0, left=0, toolbar=no, ";
		$html.= "menubar=no, scrollbars=yes, resizable=no,location=no, status=no'";
		$html.= ')">+'.$title.'</a>';
	}else{
		$html= '<input type="text" name="'.$name.'" class="BigInput" style="width:'.$w.'px;height:'.$h.'px;line-height:'.$h.'px;font-size:14px;" value="'.$value.'" />';
		$html.= '<a href="#" onClick="';
		$html.= "window.open ('admin.php?ac=uploadadd&fileurl=public&name=".$name."', ";

		$html.= "'newwindow111', 'height=200, width=480, top=0, left=0, toolbar=no, ";
		$html.= "menubar=no, scrollbars=yes, resizable=no,location=no, status=no'";
		$html.= ')">+'.$title.'</a>';
	}
	echo $html;
}
//当前用户ID获取部门名称
function get_depauseridname($userid=0,$type=0){
	global $db;
	if(trim($userid)){
		$sql = "SELECT departmentid FROM ".DB_TABLEPRE."user where id=".$userid."";
		if($result = $db->fetch_one_array($sql)){
			if($type==1){
				return $result['departmentid'];
			}else{
				return get_realdepaname($result['departmentid']);
			}
		}
	}
}
//获取ID，根据名称部门
function get_realdepaid($name=''){
	if($name!=''){
		global $db;
		global $_USER;
		$staff=explode(',',$name);
		for($i=0;$i<sizeof($staff);$i++){
			$sql = "SELECT id FROM ".DB_TABLEPRE."department where name='".trim($staff[$i])."' and jituanid='".$_USER->jituanid."' limit 0,1";
			$row = $db->fetch_one_array($sql);
			if($row['id']!=''){
				$html.=$row['id'].',';
			}
		}
	}
	//return substr($html, 0, -1);
	return ','.$html;
}
//成员检测
function get_usershow($vuidtype='',$url=''){
	global $db;
	global $_USER;
	$sql = "SELECT id,keytypeuser FROM ".DB_TABLEPRE."user where id='".$_USER->id."' and keytype='1' ";
	if($result = $db->fetch_one_array($sql)){
		//return '<img class="v-al-middle" src="template/default/images/2EDkuGH6eXMM.gif" /><a id="J-show-amount" href="'.$url.'" >查看下属成员信息</a>';
		$html.='<style type="text/css"> #attachshow{display:none;}</style><a href="#"';
		$html.='onClick="showMenus(';
		$html.="'attachshow'";
		$html.=')" style="line-height:30px;"><span>查看下属成员信息';
		$html.='<img src="template/default/images/2EC5tZlqdV.gif"';
		$html.=' align="absmiddle"></span></a>';
		$html.='<div class="attach_div" id="attachshow" align="left">';
		$html.='<a href="'.$url.'&vuidtype="><img src="template/default/images/newfolder.gif" ';
		$html.='align="absmiddle" width=16 height=16>只看自己</a>';
		$html.='<a href="'.$url.'&vuidtype=-1"><img src="template/default/images/movetofolder.gif" ';
		$html.='align="absmiddle" width=16 height=16>所有成员</a>';
		$query = $db->query("SELECT a.id,b.name FROM ".DB_TABLEPRE."user a,".DB_TABLEPRE."user_view b where a.id=b.uid and a.id in (".get_realid($result['keytypeuser']).") ORDER BY a.numbers Asc");
		while ($row = $db->fetch_array($query)) {
			$html.='<a href="'.$url.'&vuidtype='.$row['id'].'"><img src="template/default/images/2EDkuGH6eXMM.gif" ';
			$html.='align="absmiddle" width=16 height=16>'.$row['name'].'</a>';
		}
		$html.='</div>';
	}
	return $html;
}
//查看下级成员,老版本方法，2014版本后失效
function get_subordinate($uid=0,$username=''){
	global $db;
	$whsql='ssss';
	$sql = "SELECT keytypeuser FROM ".DB_TABLEPRE."user where id='".$uid."' and keytype='1'";
	if($result = $db->fetch_one_array($sql)){
		$keytypeuser=explode(',',$result['keytypeuser']);
		for($i=0;$i<sizeof($keytypeuser);$i++){
			$whsql .=" or ".$username."=".get_userid($keytypeuser[$i]);
		}
		$sqlstrname=str_replace('ssss or','',$whsql);
		$html=" AND (".str_replace('ssss','',$sqlstrname).")";
	}
	return $html;
}
function get_suborname($uid=0,$username=''){
	global $db;
	$whsql='';
	$sql = "SELECT keytypeuser FROM ".DB_TABLEPRE."user where id='".$uid."' and keytype='1'";
	if($result = $db->fetch_one_array($sql)){
		$keytypeuser=explode(',',$result['keytypeuser']);
		for($i=0;$i<sizeof($keytypeuser);$i++){
			$whsql .=$username."='".trim($keytypeuser[$i])."'"." or ";
		}
		$sqlstrname=substr($whsql, 0, -3);
		$html=" AND (".trim($sqlstrname).")";
	}
	return $html;
}
//新方法，目前适用于CRM
function get_keyuser($ui,$un){
	global $db;
	global $_USER;
	$sql = "SELECT keytypeuser,keytype FROM ".DB_TABLEPRE."user where id='".$_USER->id."' and keytype='1'";
	if($row = $db->fetch_one_array($sql)){
		echo '<input type="hidden" name="un" id="un"';
		//echo ' readonly ';
		echo ' value="'.$un.'" />';
		echo "<input type='hidden' name='ui' id='ui' value='".$ui."' />";
		echo '<a href="#" onClick="';
		echo "window.open ('admin.php?ac=keyuser&fileurl=public&keytype=".$row['keytype']."&un=".$un."', 't".$_USER->id."', 'height=600, width=500, top=50, left=100, toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=no')";
		echo '">+查看下属人员</a>';
	}
}
//excel下载
function get_exceldown($keys,$type=0){
	if(!is_superadmin() && !check_purview($keys)){
	}else{
		if($type==1){
			return '<button type="button" id="query_export" class="btn btn-info" onClick="javascript:document:excel.submit();">导出查询列表</button>';	
		}else{
			return '<a href="javascript:document:excel.submit();" seed="CR-download-top" id="J-download"><img class="v-al-middle" src="template/default/images/2EC5tZlqdV.gif" />下载查询结果</a>';	
		}
	}
}
//载取内容
function get_subcontent($str, $start, $end){
        if ( $start == '' || $end == '' ){
               return;
        }
        $str = explode($start, $str);
        $str = explode($end, $str[1]);
        return $str[0]; 
}

//附件操作
function get_publicfileadd($number,$fileid,$filetype,$officetype,$filename,$fileaddr){
		global $_USER;
  		$fileoffice = array(
			'number' => $number,
			'fileid' => $fileid,
			'filetype' => $filetype,
			'officetype' => $officetype,
			'filename' => $filename,
			'uid' => $_USER->id,
			'date' => get_date('Y-m-d H:i:s',PHP_TIME),
			'fileaddr' => $fileaddr
		);
		insert_db(DB_JT.'fileoffice',$fileoffice);
		return ;
}
//LOG日志
function get_logadd($id,$content,$title,$type,$userid){
	if($id!=''){
  		$oalog = array(
		'uid' => $userid,
		'content' => $content,
		'title' => $title,
		'startdate' => get_date('Y-m-d H:i:s',PHP_TIME),
		'contentid' => $id,
		'type' => $type
		);
		insert_db(DB_JT.'oalog',$oalog);
	}
	return ;
}

//解析CSV
function input_csv($handle) {
	$out = array ();
	$n = 0;
	while ($data = fgetcsv_reg($handle, 10000)) {
		$num = count($data);
		for ($i = 0; $i < $num; $i++) {
			$out[$n][$i] = $data[$i];
		}
		$n++;
	} 
	return $out; 
} 

function fgetcsv_reg($handle, $length = null, $d = ',', $e = '"') {
	$d = preg_quote($d);
	$e = preg_quote($e);
	$_line = "";
	$eof=false;
	while ($eof != true) {
		$_line .= (empty ($length) ? fgets($handle) : fgets($handle, $length));
		$itemcnt = preg_match_all('/' . $e . '/', $_line, $dummy);
		if ($itemcnt % 2 == 0)
		$eof = true;
	}
	$_csv_line = preg_replace('/(?: |[ ])?$/', $d, trim($_line));
	$_csv_pattern = '/(' . $e . '[^' . $e . ']*(?:' . $e . $e . '[^' . $e . ']*)*' . $e . '|[^' . $d . ']*)' . $d . '/';
	preg_match_all($_csv_pattern, $_csv_line, $_csv_matches);
	$_csv_data = $_csv_matches[1];
		for ($_csv_i = 0; $_csv_i < count($_csv_data); $_csv_i++) {
		$_csv_data[$_csv_i] = preg_replace('/^' . $e . '(.*)' . $e . '$/s', '$1', $_csv_data[$_csv_i]);
		$_csv_data[$_csv_i] = str_replace($e . $e, $e, $_csv_data[$_csv_i]);
	}
	return empty ($_line) ? false : $_csv_data;
}
function App($data=0){
	global $_CACHE;
	get_cache('app_config');
	foreach ( $_CACHE['app_config'] as $row) {
		if($row['name']==$data){
			$html=$row[value];
		}
	}
	return $html;
		
}

//流程审批替换图片
function participationwork($name,$user,$type){
	global $_CONFIG;
	global $db;
	if ($_CONFIG->config_data('flowuser')=='1'){
		$row = $db->fetch_one_array("SELECT participationwork FROM ".DB_TABLEPRE."user_view WHERE uid='".$user."'");
		if($type=='1'){
			return '<img src="'.App('Doname').$row['participationwork'].'" style="width:100px;height:33px;" />';
		}else{
			return '<img src="'.$row['participationwork'].'" style="width:100px;height:33px;" />';
		}
	}else{
		return $name;
	}
}
function php_ueditor_js(){
	$html='';
	$html.='<script type="text/javascript" src="ueditor/ueditor.config.js"></script>';
    $html.='<script type="text/javascript" src="ueditor/ueditor.all.js"></script>';
	return $html;
}
function php_ueditor($value='',$w=600,$h=300,$name='content'){
	$html='';
    $html.='<script type="text/javascript">';
    $html.='    var ue'.$name.' = UE.getEditor("'.$name.'");';
    $html.='</script>';
	$html.='<textarea name="'.$name.'" id="'.$name.'" class="input" style="width:'.$w.'px;height:'.$h.'px;">'.$value.'</textarea>';
	return $html;
}
function SmsTpl($name,$title,$value){
	global $_CACHE;
	get_cache('smstpl');
	foreach ( $_CACHE['smstpl'] as $row) {
		$html=str_replace('{$name}',$name,$row[$value]);
	}
	return str_replace('{$title}',$title,$html);
	//return;	
}
?>