<?php
 /*
 * 74cms 个人，企业 委托猎头
 * ============================================================================
 * 版权所有: 骑士网络，并保留所有权利。
 * 网站地址: http://www.74cms.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
*/
define('IN_QISHI', true);
require_once(dirname(__FILE__).'/../include/common.inc.php');
$act = isset($_REQUEST['act']) ? trim($_REQUEST['act']) : 'app';
require_once(QISHI_ROOT_PATH.'include/mysql.class.php');
require_once(QISHI_ROOT_PATH.'include/fun_user.php');
$db = new mysql($dbhost,$dbuser,$dbpass,$dbname);
if ($_SESSION['uid']=='' || $_SESSION['username']=='')
{
	$captcha=get_cache('captcha');
	$smarty->assign('verify_userlogin',$captcha['verify_userlogin']);
	$smarty->display('plus/ajax_login.htm');
	exit();
}
if ($_SESSION['utype']=='3')
{
	exit('<div class="floatContent">
		<div class="float_common_box">
			<div class="float_tips">
				<div>必须是企业或者个人才可以委托猎头！</div>
			</div>
		</div>
	</div>');
}
if ($user['status']=="2") 
{
	exit('<div class="floatContent">
		<div class="float_common_box">
			<div class="float_tips">
				<div>您的账号处于暂停状态，请先联系网站管理员设为正常后进行操作！</div>
			</div> 
		</div>
	</div>');
}

require_once(QISHI_ROOT_PATH.'include/fun_user.php');
//个人委托职位  //批量和非批量得到不同的职位id（批量得到的id是个数组jobsyid，非批量得到的是一个id值jobsid）
if($_SESSION['utype'] == '2')
{	 
	require_once(QISHI_ROOT_PATH.'include/fun_personal.php');
	$user=get_user_info($_SESSION['uid']);
	if($act == 'app'){
		$hunterid =$_REQUEST['id'];
		$hunteryid =$_REQUEST['y_id'];
		if(empty($hunterid) && empty($hunteryid)){
			exit('<div class="floatContent">
				<div class="float_common_box">
					<div class="float_tips">
						<div>没有选择猎头！</div>
					</div> 
				</div>
			</div>');
		} 
		$hunter_id = empty($hunterid)?$hunteryid:$hunterid;
		$uid = intval($_SESSION['uid']);
		$pid = intval(get_pid($uid));
		$resume_info = get_resume_info($uid);
		if($resume_info['dislpay']=='2'||$resume_info['dislpay']=='4'){
			exit('<div class="floatContent">
				<div class="float_common_box">
					<div class="float_tips">
						<div>请设置你的简历为猎头不可见！</div>
					</div>
				</div>
			</div>');
		}
		 if (strpos($hunter_id,"-"))
		{
			$hunter_id=str_replace("-",",",$hunter_id);
			if (!preg_match("/^(\d{1,10},)*(\d{1,10})$/",$hunter_id)) return false;
		}
		else
		{
		$hunter_id=intval($hunter_id);
		} 
		$hunterarr_id='';
		if(is_array($hunter_id)){
			foreach ($hunter_id as  $value) {
				if(empty($hunterarr_id)){
					$hunterarr_id = $value;
				}else{
					$hunterarr_id = $hunterarr_id.','.$value;
				}
			}
		}else{
			$hunterarr_id = $hunter_id;
		}
		$sql = "select * from ".table('hunter_profile')." WHERE id IN ({$hunterarr_id}) ";
		$hunterarr = $db->getall($sql);
		if (empty($hunterarr))
		{
			exit('<div class="floatContent">
				<div class="float_common_box">
					<div class="float_tips">
						<div>无此猎头信息！'.$hunterarr_id.'</div>
					</div> 
				</div>
			</div>');
		}
		$resume_basic=$db->getone("select * from ".table('resume')." where id='{$pid}' and uid='{$uid}'  LIMIT 1 ");
		if (empty($resume_basic))
		{
			exit('<div class="floatContent">
				<div class="float_common_box">
					<div class="float_tips">
						<div>无简历信息！</div>
					</div> 
				</div>
			</div>');
		}
		$i=0;
		foreach($hunterarr as $hunter)
		{
			//检查一下此猎头是否委托过
			if (check_hunter_entrust($hunter['id'],$uid))
			{
			 $i++;
			 continue ;
			} 
			$addarr['hunter_id']=$hunter['id'];
			$addarr['hunter_uid']=$hunter['uid'];
			$addarr['hunter_name']=$hunter['fullname'];
			
			$addarr['resume_id']=$resume_basic['id'];
			$addarr['resume_name']=$resume_basic['fullname'];
			$addarr['resume_uid']=$resume_basic['uid'];
			
			$addarr['entrust_type']=2;
			$addarr['is_apply']=0;
			$addarr['state']=0;
			$addarr['addtime']=time();
			$entrust_id = inserttable(table('entrust_hunter'),$addarr,1);
			if ($entrust_id)
			{
				//个人显示
				add_pms($_SESSION['uid'],$entrust_id,"您委托猎头【".$hunter['fullname']."】为您的简历推荐职位"); 
				add_pms($hunter['uid'],$entrust_id,$resume_basic['fullname'].'委托您为其推荐职位');
				write_memberslog($_SESSION['uid'],2,1301,$_SESSION['username'],"/",$_SESSION['parentuid'],"投递了简历，职位:{$jobs['jobs_name']}",0);
			} 
		}
		if($i==0){
			echo('
			<div class="floatContent"  id="add_ok" >
				<div class="float_common_box" >
					<div class="float_auth_success">投递成功！<a href="'.get_member_url(2,true).'personal_hunter_service.php?act=hunter_service" >查看委托的猎头</a></div>
				</div>
			</div>');
		}else{
		   exit('<div class="floatContent">
				<div class="float_common_box">
					<div class="float_tips">
						<div>你已经委托过该猎头！</div>
					</div> 
				</div>
			</div>');
		}
	}
	//取消委托 （删除委托）
	elseif($act == 'app_del'){
		$hunterid =$_REQUEST['id'];
		$hunteryid =$_REQUEST['y_id'];
		if(empty($hunterid) && empty($hunteryid)){
			exit('<div class="floatContent">
				<div class="float_common_box">
					<div class="float_tips">
						<div>没有选择委托的猎头！</div>
					</div> 
				</div>
			</div>');
		} 
		$hunter_id = empty($hunterid)?$hunteryid:$hunterid;
		if (strpos($hunter_id,"-"))
			{
				$hunter_id=str_replace("-",",",$hunter_id);
				if (!preg_match("/^(\d{1,10},)*(\d{1,10})$/",$hunter_id)) return false;
			}
			else
			{
			$hunter_id=intval($hunter_id);
			} 
		if(is_array($hunter_id)){
			foreach ($hunter_id as  $value) {
				if(empty($hunterarr_id)){
					$hunterarr_id = $value;
				}else{
					$hunterarr_id = $hunterarr_id.','.$value;
				}
			}
		}else{
			$hunterarr_id = $hunter_id;
		}
		$sql = "DELETE FROM  ".table('entrust_hunter')."  WHERE hunter_id IN ({$hunterarr_id}) and resume_uid = ".$_SESSION['uid'];
		if($db->query($sql)){ 
			showmsg('取消成功！',2);
		}else{ 
			showmsg('取消委托猎头失败！',0);
		}
	}
} 
//企业委托猎头
elseif($_SESSION['utype']=='1')
{ 
	require_once(QISHI_ROOT_PATH.'include/fun_company.php');
	$uid = intval($_SESSION['uid']); 
	if($act=='app')
	{ 
		$hunterid =$_REQUEST['id'];
		$hunteryid =$_REQUEST['y_id'];
		
		if(empty($hunterid) && empty($hunteryid)){
			exit('<div class="floatContent">
				<div class="float_common_box">
					<div class="float_tips">
						<div>没有选择猎头！</div>
					</div> 
				</div>
			</div>');
		}  
		$hunter_id = empty($hunterid)?$hunteryid:$hunterid; 
		$setmeal = get_user_setmeal($uid); 
		if($setmeal['is_entrust_hunter'] != '1'){	//1为允许 0为不允许
			exit('<div class="floatContent">
				<div class="float_common_box">
					<div class="float_tips">
						<div>你目前的套餐不允许委托猎头！</div>
					</div> 
				</div>
			</div>');
		}   
		if (strpos($hunter_id,"-"))
		{
			$hunter_id=str_replace("-",",",$hunter_id);
			if (!preg_match("/^(\d{1,10},)*(\d{1,10})$/",$hunter_id)) return false;
		}
		else
		{
		$hunter_id=intval($hunter_id);
		}  
		$hunterarr_id='';
		if(is_array($hunter_id)){
			foreach ($hunter_id as  $value) {
				if(empty($hunterarr_id)){
					$hunterarr_id = $value;
				}else{
					$hunterarr_id = $hunterarr_id.','.$value;
				}
			}
		}else{
			$hunterarr_id = $hunter_id;
		}  
		$sql = "select * from ".table('hunter_profile')." WHERE id IN ({$hunterarr_id}) ";
		$hunterarr = $db->getall($sql);
		if (empty($hunterarr))
		{
			exit('<div class="floatContent">
				<div class="float_common_box">
					<div class="float_tips">
						<div>无此猎头信息！'.$hunterarr_id.'</div>
					</div> 
				</div>
			</div>');
		}
		$jobs=get_auditjobs($uid);
		if (empty($jobs))
		{
			exit('<div class="floatContent">
				<div class="float_common_box">
					<div class="float_tips">
						<div>无职位信息！</div>
					</div> 
				</div>
			</div>');
		} 
		?> 
<div class="floatContent" id="app">
	<div class="float_common_box">
		<table class="float_auth_table">
			<tbody>
				<tr height="40">
					<td align="right">您要委托的职位：</td>
					<td>
						<select name="jobs" id="app_selected">
							<option value="0" >请选择职位</option> 
						<?php  foreach($jobs as $j){    ?>	 
							<option value="<?php echo $j['id']?>" ><?php echo $j['jobs_name']?></option> 
						<?php }?>	
						</select>
						<input id="entrust_input" type="hidden" value="0"/>
						<input id="entrust_uid" type="hidden" value="<?php echo $hunterarr_id; ?>"/>
					</td>
				</tr> 
			</tbody>
		</table>
		<div class="float_btn_guoup">
			<input type="submit" value="提交" name="Submit2" class="float_but55lan" id="ajax_app">&nbsp;&nbsp;&nbsp;
			<input type="button" value="取消" class="float_but55hui DialogClose">
		</div>
	</div>
</div>	
<div class="floatContent" style="display:none" id="waiting" >
	<div class="float_common_box" >
		<img src="<?php echo  $_CFG['site_template']?>images/30.gif" style="margin-left:110px;" border="0"/>
	</div>
</div>
<div class="floatContent" style="display:none" id="app_ok" >
	<div class="float_common_box" >
		<div class="float_auth_success">委托成功！<a href="<?php echo get_member_url(1,true)?>company_hunter_service.php?act=my_entrust_hunter" >查看发出的委托</a></div>
	</div>
</div>	 
<script type="text/javascript">   
//验证
 $("select").live('change',function(){
		val = $(this).val();
		$("#entrust_input").val(val); 
	});
	$("#ajax_app").die().live('click',function() {  
		 jobsid = $("#entrust_input").val();
		 if (jobsid == '0') 
		{
			alert("请选择委托的职位！");
		}
		else
		{ 
			$val = $("#entrust_uid").val();
			var tsTimeStamp= new Date().getTime(); 
			$.post("<?php echo $_CFG['site_dir'] ?>user/user_entrust_hunter.php", { "jobsid": jobsid,"hunterid":$val ,"time":tsTimeStamp,"act":"app_save"},
			function (data,textStatus)
			 {
				if (data=="ok")
				{ 
					$("#app").hide();
					$("#waiting").hide();
					$("#app_ok").show();
					$("#app_ok .closed").click(function(){
					});
				}
				else if(data=="repeat")
				{ 
					$("#app").hide();
					$("#waiting").hide();
					$("#app_ok").show();
					$(".float_auth_success").html("您已经委托过该猎头了，不能重复申请委托!");
				}
				else
				{ 
					$("#app").hide();
					$("#waiting").hide();
					$("#app_ok").show();
					$(".float_auth_success").html("委托失败!"+data);
				} 
			 })
		}
	}); 	
</script> 	
	<?php   
	}
	elseif($act == 'app_save')
	{ 
		 
		$jobsid = $_REQUEST['jobsid']; 
		$hunterid =$_REQUEST['hunterid'];
		$hunteryid =$_REQUEST['y_id'];
		if(empty($hunterid) && empty($hunteryid)){ 
			exit("没有选择猎头！");
		}   
		$sql = "select * from ".table('hunter_profile')." WHERE id IN ({$hunterid}) ";
		$hunterarr = $db->getall($sql);
 
		$jobs_info = get_jobs_one($jobsid);	//获取一个职位的信息
		$i=0;
		foreach($hunterarr as $hunter)
		{
			//检查一下此猎头是否委托过
			if (check_hunter_entrust($hunter['id'],$jobsid))
			{
			 $i++;
			 continue ;
			} 
			// $addarr['hunter_id']=$hunter['id'];
			// $addarr['hunter_uid']=$hunter['uid'];
			// $addarr['hunter_name']=$hunter['fullname'];
			
			$addarr['jobs_id']=$jobs_info['id'];			
			$addarr['jobs_type']=$jobs_info['jobs_type'];			
			$addarr['jobs_name']=$jobs_info['jobs_name'];			
			$addarr['company_id']=$jobs_info['company_id'];			
			$addarr['company_uid']=$jobs_info['uid'];	
			$addarr['company_name']=$jobs_info['companyname'];		
			$addarr['company_puid']=$jobs_info['parentuid'];		
			
			$addarr['entrust_type']=1;
			$addarr['is_apply']=0;
			$addarr['state']=0;
			$addarr['addtime']=time();
			$entrust_id = inserttable(table('entrust_hunter'),$addarr,true);
			//企业显示
			add_pms($_SESSION['uid'],$entrust_id,"您委托猎头【".$hunter['fullname']."】为您发布的".$jobs_info['jobs_name'].'推荐人才'); 
			add_pms($hunter['uid'],$entrust_id,$jobs_info['companyname'].'委托您为其发布的职位'.$jobs_info['jobs_name']."推荐人才");
			
			write_memberslog($_SESSION['uid'],2,1301,$_SESSION['username'],"/",$_SESSION['parentuid'],"投递了简历，职位:{$jobs['jobs_name']}",0);
			 
		}
		if($i==0){ 
			exit('ok');
		}else{ 
			exit('repeat');
		}
	
	}
} 
else
{
	exit('<div class="floatContent">
		<div class="float_common_box">
			<div class="float_tips">
				<div>必须是企业或者个人才可以委托职位！</div>
			</div> 
		</div>
	</div>');
}  
?>
