<?php
class ZhaopinAction extends BaseAction{	
	// 招聘管理
    public function show(){
		$admin = array();
		//获取地址栏参数
		$admin['cid']= $_REQUEST['cid'];
		$admin['status'] = intval($_REQUEST['status']);
		$admin['stars'] = intval($_REQUEST['stars']);
		$admin['type'] = !empty($_GET['type'])?$_GET['type']:C('admin_order_type');
		$admin['order'] = !empty($_GET['order'])?$_GET['order']:'desc';
		$admin['orders'] = 'zhaopin_'.$admin["type"].' '.$admin['order'];
		$admin['wd'] = urldecode(trim($_REQUEST['wd']));
		$admin['tag'] = urldecode(trim($_REQUEST['tag']));
		$admin['tid'] = $_REQUEST['tid'];//专题ID
		$admin['p'] = '';
		//生成查询参数
		$limit = C('url_num_admin');
		$order = 'zhaopin_'.$admin["type"].' '.$admin['order'];
		if ($admin['cid']) {
			$where['zhaopin_cid']= getlistsqlin($admin['cid']);
		}	
		if ($admin['status'] == 2) {
			$where['zhaopin_status'] = array('neq',1);
		}elseif ($admin['status'] == 1) {
			$where['zhaopin_status'] = array('eq',1);
		}
		if($admin['stars']){
			$where['zhaopin_stars'] = $admin['stars'];
		}		
		if ($admin['wd']) {
			$where['zhaopin_name'] = array('like','%'.$admin['wd'].'%');
			$admin['wd'] = urlencode($admin['wd']);
		}
		//根据不同条件加载模型
		if($admin['tag']){
			$where['tag_sid'] = 2;
			$where['tag_name'] = $admin['tag'];
			$rs = D('TagzhaopinView');
			$admin['tag'] = urlencode($_REQUEST['tag']);
		}else{
			$rs = D("zhaopin");
		}
		//组合分页信息		
		$count = $rs->where($where)->count('zhaopin_id');
		$totalpages = ceil($count/$limit);
		$currentpage = !empty($_GET['p'])?intval($_GET['p']):1;
		$currentpage = get_maxpage($currentpage,$totalpages);
		$pageurl = U('Admin-zhaopin/Show',$admin,false,false).'{!page!}'.C('url_html_suffix');
		$admin['p'] = $currentpage;$_SESSION['zhaopin_jumpurl'] = U('Admin-zhaopin/Show',$admin).C('url_html_suffix');
		$pages = '共'.$count.'篇招聘信息&nbsp;当前:'.$currentpage.'/'.$totalpages.'页&nbsp;'.getpage($currentpage,$totalpages,8,$pageurl,'pagego(\''.$pageurl.'\','.$totalpages.')');
		$admin['pages'] = $pages;
		//查询数据
		$list = $rs->where($where)->order($order)->limit($limit)->page($currentpage)->select();
		foreach($list as $key=>$val){
		    $list[$key]['list_url'] = '?s=Admin-zhaopin-Show-cid-'.$list[$key]['zhaopin_cid'];
			$list[$key]['zhaopin_url'] = ff_data_url('zhaopin',$list[$key]['zhaopin_id'],$list[$key]['zhaopin_cid'],$list[$key]['zhaopin_name'],1,$list[$key]['zhaopin_jumpurl']);
			$list[$key]['zhaopin_starsarr'] = admin_star_arr($list[$key]['zhaopin_stars']);
		}				
		//dump($rs->getLastSql());
		//变量赋值并输出模板
		$this->assign($admin);
		$this->assign('list',$list);
		$this->assign('list_zhaopin',F('_GxlcmsQY/listzhaopin'));
		if($admin['tid']){
			$this->display('./Public/system/special_zhaopin.html');
		}else{
	    	$this->display('./Public/system/zhaopin_show.html');
		}
    }
	// 添加编辑
    public function add(){
		$rs = D("zhaopin");
		$zhaopin_id = intval($_GET['id']);
		if($zhaopin_id>0){
            $where['zhaopin_id'] = $zhaopin_id;
			$array = $rs->where($where)->relation('Tag')->find();
			$array['zhaopin_tplname'] = '编辑';
			foreach($array['Tag'] as $key=>$value){
				$tag[$key] = $value['tag_name'];
			}			
			$array['zhaopin_starsarr'] = admin_star_arr($array['zhaopin_stars']);
			if (C('admin_time_edit')){
				$array['checktime'] = 'checked';
			}
			$array['zhaopin_keywords'] = implode(',',$tag);
			$_SESSION['chanpin_jumpurl'] = $_SERVER['HTTP_REFERER'];
		}else{
		    $array['zhaopin_cid'] = cookie('zhaopin_cid');
		    $array['zhaopin_stars'] = 0;
		    $array['zhaopin_del'] = 0;
			$array['zhaopin_hits'] = 0;
			$array['zhaopin_inputer'] = $_SESSION['admin_name'];
			$array['zhaopin_addtime'] = time();
			$array['zhaopin_starsarr'] = admin_star_arr(1);
			$array['checktime'] = 'checked';
			$array['zhaopin_tplname'] = '添加';
		}
		$this->assign($array);
		$this->assign('list_zhaopin',F('_GxlcmsQY/listzhaopin'));
		$this->display('./Public/system/zhaopin_add.html');
    }
	//数据库写入前置操作
    public function _before_insert(){
		//自动获取关键词tag
		if(empty($_POST["zhaopin_keywords"]) && C('rand_tag')){
			$_POST["zhaopin_keywords"] = ff_tag_auto($_POST["zhaopin_name"],$_POST["zhaopin_content"]);
		}
	}	
	// 新增招聘保存到数据库
	public function insert(){
		$rs = D("zhaopin");
		if($rs->create()){
			$id = $rs->add();
			if( false !== $id){		
			    $this->assign("jumpUrl",'?s=Admin-zhaopin-Add');
			}else{
				$this->error('招聘信息添加失败！');
			}
		}else{
		    $this->error($rs->getError());
		}
	}
	// 新增招聘保存到数据库-后置操作
	public function _after_insert(){
		cookie('zhaopin_cid',intval($_POST["zhaopin_cid"]));
		$this->success('招聘信息添加成功,继续添加新招聘信息！');
	}
	// 更新数据
	public function update(){
	    $this->_before_insert();
		$tag = D('Tag');$rs = D("zhaopin");
		if($rs->create()){
			if(false !==  $rs->save()){
				//手动更新TAG
				if($_POST["zhaopin_keywords"]){
					$tag->tag_update($_POST["zhaopin_id"],$_POST["zhaopin_keywords"],2);
				}
				//后置操作条件
				$rs->$zhaopin_id = $_POST["zhaopin_id"];
			}else{
				$this->error("修改招聘信息失败！");
			}
		}else{
			$this->error($rs->getError());
		}
	}
	// 后置操作
	public function _after_update(){
		$rs = D("zhaopin");
		$zhaopin_id = $rs->$zhaopin_id;
		if($zhaopin_id){
			$this->_after_add_update($zhaopin_id);
			$this->assign("jumpUrl",$_SESSION['zhaopin_jumpurl']);
			$this->success('修改招聘信息成功！');
		}else{
			$this->error('修改招聘信息失败！');
		}		
	}
	//操作完毕后
	public function _after_add_update($zhaopin_id){
		//删除数据缓存
		if(C('data_cache_zhaopin')){
			S('data_cache_zhaopin_'.$zhaopin_id,NULL);
		}	
		//删除静态缓存
		if(C('html_cache_on')){
			$id = md5($zhaopin_id).C('html_file_suffix');
			@unlink('./Html/zhaopin_read/'.$zhaopin_id);
		}
		//生成网页
		if(C('url_html')){
			echo'<iframe scrolling="no" src="?s=Admin-Create-zhaopinid-id-'.$zhaopin_id.'" frameborder="0" style="display:none"></iframe>';
		}				
	}		
	// Ajax设置星级
    public function ajaxstars(){
		$where['zhaopin_id'] = $_GET['id'];
		$data['zhaopin_stars'] = intval($_GET['stars']);
		$rs = D("zhaopin");
		$rs->where($where)->save($data);		
		exit('ok');
    }
	// 设置状态
    public function status(){
		$where['zhaopin_id'] = $_GET['id'];
		$rs = D("zhaopin");
		if($_GET['value']){
			$rs->where($where)->setField('zhaopin_status',1);
		}else{
			$rs->where($where)->setField('zhaopin_status',0);
		}
		redirect($_SESSION['zhaopin_jumpurl']);
    }
	// 删除招聘信息
    public function del(){
		$this->delfile($_GET['id']);
		redirect($_SESSION['zhaopin_jumpurl']);
    }
	// 删除招聘信息all
    public function delall(){
		if(empty($_POST['ids'])){
			$this->error('请选择需要删除的招聘信息！');
		}	
		$array = $_POST['ids'];
		foreach($array as $val){
			$this->delfile($val);
		}
		redirect($_SESSION['zhaopin_jumpurl']);
    }
	// 删除静态文件与图片
    public function delfile($id){
		//删除专题收录
		$rs = D("Topic");
		$where['topic_sid'] = 1;
		$where['topic_did'] = $id;
		$rs->where($where)->delete();
		unset($where);
		//删除招聘评论
		unset($where);
		$rs = D("Cm");
		$where['cm_cid'] = $id;
		$where['cm_sid'] = 2;
		$rs->where($where)->delete();			
		//删除招聘TAG
		$rs = D("Tag");
		$where['tag_id'] = $id;
		$where['tag_sid'] = 2;
		$rs->where($where)->delete();
		unset($where);
		//删除静态文件与图片
		$rs = D("zhaopin");
		$where['zhaopin_id'] = $id;
		$array = $rs->field('zhaopin_id,zhaopin_cid,zhaopin_pic,zhaopin_name')->where($where)->find();
		@unlink(ff_img_url($arr['zhaopin_pic']));
		if(C('url_html')>0){
			@unlink(ff_data_url('zhaopin',$array['zhaopin_id'],$array['zhaopin_cid'],$array['zhaopin_name'],1));
		}
		unset($where);				
		//删除招聘ID
		$where['zhaopin_id'] = $id;
		$rs = D("zhaopin");
		$rs->where($where)->delete();
		unset($where);
    }
	// 批量转移招聘信息
    public function pestcid(){
		if(empty($_POST['ids'])){
			$this->error('请选择需要转移的招聘！');
		}	
		$cid = intval($_POST['pestcid']);
		if (getlistson($cid)) {
			$rs = D("zhaopin");
			$data['zhaopin_cid'] = $cid;
			$where['zhaopin_id'] = array('in',$_POST['ids']);
			$rs->where($where)->save($data);
			redirect($_SESSION['zhaopin_jumpurl']);
		}else{
			$this->error('请选择当前大类下面的子分类！');		
		}
    }	
	// 批量生成数据
    public function create(){
		echo'<iframe scrolling="no" src="?s=Admin-Create-zhaopinid-id-'.implode(',',$_POST['ids']).'" frameborder="0" style="display:none"></iframe>';
		$this->assign("jumpUrl",$_SESSION['zhaopin_jumpurl']);
		$this->success('批量生成招聘成功！');
    }				
}
?>