<?php
class cls_tab_hr_job_category extends cls_base_table
{
	private $tab_jobcategory;
	private $obj_db;
	private $config=array();
	//构造函数
	function __construct()
	{
		global $obj_db;
		parent::__construct();
		$this->obj_db=$obj_db;
		$this->tab_jobcategory=DB_PRE."hr_job_category";
	}
	/*添加
	arr_fields:关健字数组
	*/
	function on_save($arr_fields,$str_where="")
	{
		$arr_return=array("err"=>"","id"=>0);
		$arr_category=array();
		(!isset($arr_fields["category_id"]))? $arr_fields["category_id"]=0:$arr_fields["category_id"]=intval($arr_fields["category_id"]);
		//格式化数据
		if(!isset($arr_fields["category_name"])||$arr_fields["category_name"]==""){
			$arr_return["err"]="类型名称题不能为空！";
			return $arr_return;
		}
		//保存操作
		$lng_id=$arr_fields["category_id"];
		unset($arr_fields["category_id"]);//释放id
		$arr_fields["category_updatetime"]=TIME;
		if($lng_id<1){
			$arr_fields["category_addtime"]=TIME;
			$arr_return=self::_insert($arr_fields);
		}else{
			$str_where="category_id='".$lng_id."'";
			$arr_return=self::_update($arr_fields,$str_where);
			$arr_return["id"]=$lng_id;
		}
		return $arr_return;
	}
	function _insert($arr_fields)
	{
		$arr_return=array("err"=>"","id"=>0);
		//判断是否重名
		$obj_result = $this->obj_db->query("SELECT * FROM ".$this->tab_jobcategory." where category_name='".$arr_fields["category_name"]."' and category_pid='".$arr_fields["category_pid"]."'");
		if($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$arr_return["err"]="该类型已经存在！";
			return $arr_return;
		}
		$arr_return=parent::on_insert($this->tab_jobcategory,$arr_fields);
		return $arr_return;
	}
	function _update($arr_fields,$msg_where)
	{
		$arr_return=array("err"=>"","id"=>0);
		$arr_return=parent::on_update($this->tab_jobcategory,$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_id_next=array();
		$str_sql="select category_id from ".$this->tab_jobcategory." where category_pid in (".$str_id.")";
		$obj_result = $this->obj_db->query($str_sql);
		while($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$arr_id_next[]=$obj_rs["category_id"];
		}
		if(count($arr_id_next)>0)
		{
			$arr_return=self::on_del($arr_id_next);
		}
		$arr_return=parent::on_del($this->tab_jobcategory,"category_id in(".$str_id.")");
		return $arr_return;
	}
	function get_select_category($msg_name,$msg_category="")
	{
		
		$str_sql="select * from ".$this->tab_jobcategory." where category_pid='0'";
		$obj_result = $this->obj_db->query($str_sql);
		$str_category="";
		while($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$str_category.="<option value='".$obj_rs["category_id"]."'>".$obj_rs["category_name"]."</option>";
		}
		if($msg_category==""){
			$str_button="";
			$str_input="style='display:none'";
			$str_select="";
		}else{
			$str_select="style='display:none'";
			$str_input="";
			$str_button="<span style='cursor:hand' onclick=\"this.style.display='none';document.getElementById('txt_".$msg_name."').style.display='none';document.getElementById('sel_".$msg_name."').style.display='';\">编辑</span>";
		}
		$str_category="<div id='id_jobcategory_box'><input type='hidden' name='".$msg_name."' id='".$msg_name."' value='".$msg_category."'><span  id='txt_".$msg_name."' ".$str_input.">".$msg_category."</span><select onchange=\"get_jobcategory_select_1(this.value,1);\" ".$str_select." id='sel_".$msg_name."'><option value=''>--请选择--</option>".$str_category;
		$str_category.="</select> ".$str_button."</div>";
		return $str_category;
	}
	function get_next_category($msg_id)
	{		
		$str_sql="select * from ".$this->tab_jobcategory." where category_pid='".$msg_id."'";
		$obj_result = $this->obj_db->query($str_sql);
		$str_category="";
		$arr_category=array();
		$lng_lay=-1;
		while($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			if($lng_lay<1){
				$str_pids=$obj_rs["category_pids"];
				$arr_pids=explode(",",$str_pids);
				$lng_lay=count($arr_pids);
			}
			$arr_category[]=$lng_lay.",".$obj_rs["category_id"]."=".$obj_rs["category_name"];
		}
		$str_category=implode("|",$arr_category);
		return $str_category;
	}
}
?>
