<?php
class cls_tab_hr_company extends cls_base_table
{
	private $tab_company;
	private $obj_db;
	private $perms=array();
	private $config=array();
	//构造函数
	function __construct()
	{
		global $obj_db,$obj_tab_sys_config;
		$arr_cfg_company=$obj_tab_sys_config->get_model("company");
		parent::__construct();
		$this->obj_db=$obj_db;
		$this->tab_company=DB_PRE."hr_company";
		$this->perms["industry"]=array("互联网/IT","建筑","房地产","教育","酒店","餐饮","贸易","培训","投资","制造","证券","运输/物流","设计","家居/设计/装潢","影视/传媒","其它");//所属行业
		$this->perms["nature"]=array("国有企业","集体企业","有限责任公司","股份有限公司","私营企业","中外合资企业","外商投资企业","个体户","其它");//所属行业
		$this->perms["currency"]=array("人民币"=>"人民币","美元"=>"美元");//货币类型
		$this->perms["scale"]=array("10人以下","10～50人","50～200人","200～500人","500～1000","1000以上");//公司规模
		$this->perms["reg_default_vip"]="";
		if(isset($arr_cfg_company['industry'])) $this->perms["industry"]=$arr_cfg_company['industry'];
		if(isset($arr_cfg_company['nature'])) $this->perms["nature"]=$arr_cfg_company['nature'];
		if(isset($arr_cfg_company['currency'])) $this->perms["currency"]=$arr_cfg_company['currency'];
		if(isset($arr_cfg_company['scale'])) $this->perms["scale"]=$arr_cfg_company['scale'];
		if(isset($arr_cfg_company["reg_default_vip"])) $this->perms["reg_default_vip"]=$arr_cfg_company["reg_default_vip"];

		// 是否生成静态文件
		$this->perms["ishtml"]=false;
		if(isset($arr_cfg_company["company_htmlpath"])&&$arr_cfg_company["company_htmlpath"]!=""){
			$str_dir=$arr_cfg_company["company_htmlpath"];
			$str_dirpath=get_real_path("",$str_dir);
			if(is_dir($str_dirpath)){
				$this->perms["ishtml"]=true;
			}
		}

	}
	/*添加
	arr_fields:关健字数组
	*/
	function on_save($arr_fields,$str_where="")
	{
		$arr_return=array("err"=>"","id"=>0);
		$arr_company=array();
		(!isset($arr_fields["company_id"]))? $arr_fields["company_id"]=0:$arr_fields["company_id"]=intval($arr_fields["company_id"]);
		//格式化数据
		if(!isset($arr_fields["company_name"])||$arr_fields["company_name"]==""){
			$arr_return["err"]="公司名称不能为空！";
			return $arr_return;
		}
		//保存操作
		$lng_id=$arr_fields["company_id"];
		unset($arr_fields["company_id"]);//释放id
		$arr_fields["company_updatetime"]=TIME;
		if($lng_id<1){
			$arr_fields["company_addtime"]=TIME;
			$arr_return=self::_insert($arr_fields);
		}else{
			$str_where="company_id='".$lng_id."'";
			$arr_return=self::_update($arr_fields,$str_where);
			$arr_return["id"]=$lng_id;
		}
		return $arr_return;
	}
	function _insert($arr_fields)
	{
		global $arr_cfg_company,$obj_ctl_domain;
		$arr_return=array("err"=>"","id"=>0);
		if(!isset($arr_fields["company_user_id"])||!is_numeric($arr_fields["company_user_id"])){
			$arr_return["err"]="只有本站用户才能注册企业信息！";
			return $arr_return;
		}
		$arr_fields["company_user_id"]=intval($arr_fields["company_user_id"]);

		if(!isset($arr_fields["company_state"])){
			$str_company_state=0;
			if(isset($arr_cfg_company["company_state"])) $str_company_state=intval($arr_cfg_company["company_state"]);
			$arr_fields["company_state"]=$str_company_state;
		}
		//是否从分站注册
		if(gettype($obj_ctl_domain)=="object" && $obj_ctl_domain->type=="fz"){
			$arr_fields["company_fz_id"]=$obj_ctl_domain->id;
		}
		$str_sql="select company_id from ".DB_PRE."hr_company where company_user_id=".$arr_fields["company_user_id"];
		$obj_result = $this->obj_db->query($str_sql);
		if($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$arr_return["err"]="此账号已经注册过企业了！";
			return $arr_return;
		}
		//注册时是否赠送VIP会员
		if(!isset($arr_fields["company_vip_id"])&&$this->perms["reg_default_vip"]!=""){
			$str_sql="select vip_id from ".DB_PRE."hr_company_vip where vip_name='".chk_escape($this->perms["reg_default_vip"])."'";
			$obj_result = $this->obj_db->query($str_sql);
			if($obj_rs = $this->obj_db->fetch_array($obj_result))
			{
				$arr_fields["company_vip_id"]=$obj_rs["vip_id"];
				$arr_fields["company_vip_time"]=TIME;
			}
		}
		$arr_return=parent::on_insert($this->tab_company,$arr_fields);

		return $arr_return;
	}
	function on_vip($arr_id,$msg_vipid){
		$arr_return=array("err"=>"");
		$str_id=get_arr_id($arr_id);
		if($str_id==""){
			$arr_return["err"]="没有指定要删除的记录！";
			return $arr_return;
		}
		if($msg_vipid>0){
			$arr_fields["company_vip_id"]=$msg_vipid;
			$arr_fields["company_vip_time"]=TIME;
			global $obj_db;
			$obj_rs=$obj_db->get_one("select vip_days from ".DB_PRE."hr_company_vip where vip_id=".$msg_vipid);
			$arr_fields["company_vip_endtime"]=TIME+$obj_rs["vip_days"]*86400;
		}else{
			$arr_fields["company_vip_id"]=0;
			$arr_fields["company_vip_time"]=0;
			$arr_fields["company_vip_endtime"]=0;
		}
		$arr_return=parent::on_update($this->tab_company,$arr_fields,"company_id in(".$str_id.")");
		return $arr_return;
	}
	function on_state($arr_id,$msg_stateid){
		$arr_return=array("err"=>"");
		$str_id=get_arr_id($arr_id);
		if($str_id==""){
			$arr_return["err"]="没有指定要删除的记录！";
			return $arr_return;
		}
		$arr_fields["company_state"]=$msg_stateid;
		$arr_return=parent::on_update($this->tab_company,$arr_fields,"company_id in(".$str_id.")");
		//当企业有效时，设置企业的招聘信息有效,否则招聘信息也无效
		$arr_userid=array();
		$str_sql="select company_user_id from ".DB_PRE."hr_company where company_id in(".$str_id.")";
		$obj_result = $this->obj_db->query($str_sql);
		while($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$arr_userid[]=$obj_rs["company_user_id"];
		}
		if(count($arr_userid)>0){
			$str_userid=implode(",",$arr_userid);
			if($msg_stateid>0){
				$str_sql="update ".DB_PRE."hr_job set job_state=1 where job_state=-3 and job_user_id in(".$str_userid.")";
			}else{
				$str_sql="update ".DB_PRE."hr_job set job_state=-3 where job_state=1 and job_user_id in(".$str_userid.")";
			}
			$this->obj_db->execute($str_sql);
		}
		return $arr_return;
	}
	function _update($arr_fields,$msg_where)
	{
		$arr_return=array("err"=>"","id"=>0);
		$arr_return=parent::on_update($this->tab_company,$arr_fields,$msg_where);
		return $arr_return;
	}
	function on_del($arr_id)
	{
		$arr_return=array("err"=>"");
		$str_id=get_arr_id($arr_id);
		if($str_id==""){
			$arr_return["err"]="没有指定要删除的记录！";
			return $arr_return;
		}
		$arr_return=parent::on_del($this->tab_company,"company_id in(".$str_id.")");

		return $arr_return;
	}
	function get_htmlinfo($msg_id)
	{
		global $arr_cfg_company;
		$this_html=array("path"=>"","url"=>"");
		if($msg_id<1) return $this_html;
		if(isset($arr_cfg_company["company_htmlpath"])&&$arr_cfg_company["company_htmlpath"]!=""){
			$str_dir=$arr_cfg_company["company_htmlpath"];
			$str_file=format_path($str_dir."/".$msg_id.".html");
			$str_dirpath=get_real_path("",$str_dir);
			$str_path=get_real_path("",$str_file);
			if(is_dir($str_dirpath)){
				$this_html["path"]=$str_path;
				$this_html["url"]=WEB_URL.$str_file;
			}
		}
		return $this_html;
	}
	function get_url($msg_id){
		global $arr_cfg_company;
		if($msg_id<1) return "";
		$str_return=WEB_URL."/index.php?app=companyview&id=".$msg_id;
		if(isset($arr_cfg_company["company_htmlpath"])&&$arr_cfg_company["company_htmlpath"]!=""){
			$str_dir=$arr_cfg_company["company_htmlpath"];
			$str_file=format_path($str_dir."/".$msg_id.".html");
			$str_return=WEB_URL.$str_file;
		}
		return $str_return;
	}
	function __get($name){
		if(isset($this->perms["$name"])){
			return $this->perms["$name"];
		}
	}
	function __set($name,$value){
		if(isset($this->perms["$name"])){
			$this->perms["$name"]=$value;
		}else{
		}
	}
	function get_temp_parm($msg_id,$msg_site_id=0){
		global $obj_db;
		$arr_return=array("temp"=>"","fields"=>array());
		if($msg_site_id==0){
			$str_where=" where a.site_temp_id=b.temp_id and site_source_id='".$msg_id."' and site_issel=1";
		}else{
			$str_where=" where site_id='".$msg_site_id."' and site_source_id='".$msg_id."'";
		}
		$obj_result=$obj_db->query("select temp_path,site_content from ".DB_PRE."temp_site a,".DB_PRE."temp b ".$str_where);
		if($obj_rs=$obj_db->fetch_array($obj_result)){
			$temp_dir=$obj_rs["temp_path"];
			$temp_dir=str_replace("\\","/",$temp_dir);
			if(substr($temp_dir,0,1)!="/") $temp_dir="/".$temp_dir;
			if(substr($temp_dir,-1,1)!="/") $temp_dir=$temp_dir."/";
			$arr_return["fields"]=unserialize($obj_rs["site_content"]);
			$arr_return["temp"]=$temp_dir;
		}
		return $arr_return;
	}
}
?>
