<?php
class ChanpinAction extends BaseAction{
	// 产品列表
    public function show(){
		$admin = array();
		//获取地址栏参数
		$admin['cid']= $_REQUEST['cid'];
		$admin['continu'] = $_REQUEST['continu'];
		$admin['status'] = intval($_REQUEST['status']);
		$admin['isfilm'] = intval($_REQUEST['isfilm']);
		$admin['player'] = trim($_REQUEST['player']);
		$admin['stars'] = intval($_REQUEST['stars']);
		$admin['url'] = intval($_REQUEST['url']);
		$admin['type'] = !empty($_GET['type'])?$_GET['type']:C('admin_order_type');
		$admin['order'] = !empty($_GET['order'])?$_GET['order']:'desc';
		$admin['orders'] = 'chanpin_'.$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 = 'chanpin_'.$admin["type"].' '.$admin['order'];
		if ($admin['cid']) {
			$where['chanpin_cid']= getlistsqlin($admin['cid']);
		}
		if($admin["continu"] == 1){
			$where['chanpin_continu'] = array('neq','0');
		}
		if ($admin['status'] == 2) {
			$where['chanpin_status'] = array('eq',0);
		}elseif ($admin['status'] == 1) {
			$where['chanpin_status'] = array('eq',1);
		}elseif ($admin['status'] == 3) {
			$where['chanpin_status'] = array('eq',-1);
		}
		if($admin["isfilm"]){
			$where['chanpin_isfilm'] = array('eq',$admin["isfilm"]);
		}				
		if($admin['player']){
			$where['chanpin_play'] = array('like','%'.trim($admin['player']).'%');
		}
		if($admin['stars']){
			$where['chanpin_stars'] = $admin['stars'];
		}	
		if($admin["url"]){
			$where['chanpin_url'] = array('eq','');
		}			
		if ($admin['wd']) {
			$search['chanpin_name'] = array('like','%'.$admin['wd'].'%');
			$search['chanpin_title'] = array('like','%'.$admin['wd'].'%');
			$search['chanpin_actor'] = array('like','%'.$admin['wd'].'%');
			$search['chanpin_director'] = array('like','%'.$admin['wd'].'%');
			$search['_logic'] = 'or';
			$where['_complex'] = $search;
			$admin['wd'] = urlencode($admin['wd']);
		}
		//根据不同条件加载模型
		if($admin['tag']){
			$where['tag_sid'] = 1;
			$where['tag_name'] = $admin['tag'];
			$rs = D('TagView');
			$admin['tag'] = urlencode($_REQUEST['tag']);
		}else{
			$rs = D("Chanpin");
		}
		//组合分页信息
		$count = $rs->where($where)->count('chanpin_id');
		$totalpages = ceil($count/$limit);
		$currentpage = !empty($_GET['p'])?intval($_GET['p']):1;
		$currentpage = get_maxpage($currentpage,$totalpages);//$admin['page'] = $currentpage;
		$pageurl = U('Admin-Chanpin/Show',$admin,false,false).'{!page!}'.C('url_html_suffix');
		$admin['p'] = $currentpage;$_SESSION['chanpin_jumpurl'] = U('Admin-Chanpin/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-Chanpin-Show-cid-'.$list[$key]['chanpin_cid'];
			$list[$key]['chanpin_url'] = ff_data_url('chanpin',$list[$key]['chanpin_id'],$list[$key]['chanpin_cid'],$list[$key]['chanpin_name'],1,$list[$key]['chanpin_jumpurl']);
			$list[$key]['chanpin_starsarr'] = admin_star_arr($list[$key]['chanpin_stars']);
		}
		//dump($rs->getLastSql());
		//变量赋值并输出模板
		$this->ppchanpin_play();
		$this->assign($admin);
		$this->assign('list',$list);
		$this->assign('list_chanpin',F('_GxlcmsQY/listchanpin'));
		if($admin['tid']){
			$this->display('./Public/system/special_chanpin.html');
		}else{
	    	$this->display('./Public/system/chanpin_show.html');
		}
    }
	// 添加编辑产品
    public function add(){
		$rs = D("Chanpin");
		$chanpin_id = intval($_GET['id']);
		if($chanpin_id){
			$where = array();
            $where['chanpin_id'] = $chanpin_id;
			$array = $rs->where($where)->relation('Tag')->find();
			foreach($array['Tag'] as $key=>$value){
				$tag[$key] = $value['tag_name'];
			}
			foreach(explode('$$$',$array['chanpin_play']) as $key=>$val){
			    $play[array_search($val,C('play_player'))] = $val;
			}
			$array['chanpin_play_list'] = C('play_player');
			$array['chanpin_server_list'] = C('play_server');
			$array['chanpin_play'] = explode('$$$',$array['chanpin_play']);
			$array['chanpin_server'] = explode('$$$',$array['chanpin_server']);	
			$array['chanpin_url'] = explode('$$$',$array['chanpin_url']);
			$array['chanpin_starsarr'] = admin_star_arr($array['chanpin_stars']);
			$array['chanpin_keywords'] = implode(',',$tag);
			if (C('admin_time_edit')){
				$array['checktime'] = 'checked';
			}
			$array['chanpin_tplname'] = '编辑';
			$_SESSION['chanpin_jumpurl'] = $_SERVER['HTTP_REFERER'];
		}else{
		    $array['chanpin_cid'] = cookie('chanpin_cid');
		    $array['chanpin_stars'] = 1;
		    $array['chanpin_status'] = 1;
			$array['chanpin_hits'] = 0;
			$array['chanpin_addtime'] = time();
			$array['chanpin_continu'] = 0;
			$array['chanpin_inputer'] = $_SESSION['admin_name'];
			$array['chanpin_play_list'] = C('play_player');
			$array['chanpin_server_list'] = C('play_server');
			$array['chanpin_url']=array(0=>'');
			$array['chanpin_starsarr'] = admin_star_arr(1);
			$array['checktime'] = 'checked';
			$array['chanpin_tplname'] = '添加';
		}
		$array['chanpin_language_list']=explode(',',C('play_language'));
		$array['chanpin_area_list']=explode(',',C('play_area'));
		$array['chanpin_year_list']=explode(',',C('play_year'));
		$this->ppchanpin_play();
		$this->assign($array);
		$this->assign("jumpUrl",$_SESSION['chanpin_jumpurl']);
		$this->assign('listchanpin',F('_GxlcmsQY/listchanpin'));
		$this->display('./Public/system/chanpin_add.html');
    }
	// 数据库写入前置操作
    public function _before_insert(){
		//自动获取关键词tag
		if(empty($_POST["chanpin_keywords"]) && C('rand_tag')){
			$_POST["chanpin_keywords"] = ff_tag_auto($_POST["chanpin_name"],$_POST["chanpin_content"]);
		}
		//播放器组与地址组
		$play = $_POST["chanpin_play"];
		$server = $_POST["chanpin_server"];
		foreach($_POST["chanpin_url"] as $key=>$val){
			$val = trim($val);
			if($val){
			    $chanpin_play[] = $play[$key];
				$chanpin_server[] = $server[$key];
				$chanpin_url[] = $val;
			};
		}
		$_POST["chanpin_play"] = strval(implode('$$$',$chanpin_play));
		$_POST["chanpin_server"] = strval(implode('$$$',$chanpin_server));
		$_POST["chanpin_url"] = strval(implode('$$$',$chanpin_url));				
	}
	// 新增数据
	public function insert(){
		$tag = D('Tag');$rs = D("Chanpin");
		if($rs->create()){
			//关联操作>>写入tag
			if($_POST["chanpin_keywords"]){
				$rs->Tag = $tag->tag_array($_POST["chanpin_keywords"],1);
				$id = $rs->relation('Tag')->add();
			}else{
				$id = $rs->add();
			}
			$rs->$chanpin_id = $id;
		}else{
		    $this->error($rs->getError());
		}
	}
	// 数据库写入-后置操作
	public function _after_insert(){
		$rs = D("Chanpin");
		$chanpin_id = $rs->$chanpin_id;
		if($chanpin_id){
			cookie('chanpin_cid',$chanpin_id);
			$this->_after_add_update($chanpin_id);
			$this->assign("jumpUrl",'?s=Admin-Chanpin-Add');
			$this->success('产品添加成功，继续添加新产品！');
		}else{
			$this->error('产品添加失败。');
		}
	}
	// 更新数据
	public function update(){
	    $this->_before_insert();
		$tag = D('Tag');$rs = D("Chanpin");
		if($rs->create()){	
			if(false !==  $rs->save()){
				//手动更新TAG
				if($_POST["chanpin_keywords"]){
					$tag->tag_update($_POST["chanpin_id"],$_POST["chanpin_keywords"],1);
				}
				//后置操作条件
				$rs->$chanpin_id = $_POST["chanpin_id"];
			}else{
				$this->error("修改产品信息失败！");
			}
		}else{
			$this->error($rs->getError());
		}
	}
	// 后置操作
	public function _after_update(){
		$rs = D("Chanpin");
		$chanpin_id = $rs->$chanpin_id;
		if($chanpin_id){
			$this->_after_add_update($chanpin_id);
			$this->assign("jumpUrl",$_SESSION['chanpin_jumpurl']);
			$this->success('产品更新成功！');
		}else{
			$this->error('产品更新失败。');
		}		
	}
	//操作完毕后
	public function _after_add_update($chanpin_id){
		//删除数据缓存
		if(C('data_cache_chanpin')){
			S('data_cache_chanpin_'.$chanpin_id,NULL);
		}
		//删除静态缓存
		if(C('html_cache_on')){
			$id = md5($chanpin_id).C('html_file_suffix');
			@unlink('./Html/Chanpin_read/'.$chanpin_id);
			@unlink('./Html/Chanpin_play/'.$chanpin_id);
		}
		//生成网页
		if(C('url_html')){
			echo'<iframe scrolling="no" src="?s=Admin-Create-chanpinid-id-'.$chanpin_id.'" frameborder="0" style="display:none"></iframe>';
		}				
	}	
	// 删除产品
    public function del(){
		$this->delfile($_GET['id']);
		redirect($_SESSION['chanpin_jumpurl']);
    }
	// 删除产品all
    public function delall(){
		if(empty($_POST['ids'])){
			$this->error('请选择需要删除的产品！');
		}	
		$array = $_POST['ids'];
		foreach($array as $val){
			$this->delfile($val);
		}
		redirect($_SESSION['chanpin_jumpurl']);
    }
	// 删除静态文件与图片
    public function delfile($id){
		$where = array();
		//删除产品观看记录
		//$rs = D("View");
		//$where['did'] = $id;
		//$rs->where($where)->delete();
		//删除专题收录
		$rs = D("Topic");
		$where['topic_sid'] = 1;
		$where['topic_did'] = $id;
		$rs->where($where)->delete();
		unset($where);					
		//删除产品评论
		$rs = D("Cm");
		$where['cm_cid'] = $id;
		$where['cm_sid'] = 1;
		$rs->where($where)->delete();
		unset($where);	
		//删除产品TAG
		$rs = D("Tag");
		$where['tag_id'] = $id;
		$where['tag_sid'] = 1;
		$rs->where($where)->delete();
		unset($where);
		//删除静态文件与图片
		$rs = D("Chanpin");
		$where['chanpin_id'] = $id;
		$array = $rs->field('chanpin_id,chanpin_cid,chanpin_pic,chanpin_name')->where($where)->find();
		@unlink(ff_img_url($arr['chanpin_pic']));
		if(C('url_html') > 0){	
			@unlink(ff_data_url_dir('chanpin',$array['chanpin_id'],$array['chanpin_cid'],$array['chanpin_name'],1).C('html_file_suffix'));
			@unlink(ff_play_url_dir($array['chanpin_id'],0,1,$array['chanpin_cid'],$array['chanpin_name']).C('html_file_suffix'));
			@unlink(ff_play_url_dir($array['chanpin_id'],0,1,$array['chanpin_cid'],$array['chanpin_name']).'js');
		}
		unset($where);
		//删除产品ID
		$where['chanpin_id'] = $id;
		$rs->where($where)->delete();
		unset($where);
    }
	// 批量生成数据
    public function create(){
		echo'<iframe scrolling="no" src="?s=Admin-Create-chanpinid-id-'.implode(',',$_POST['ids']).'" frameborder="0" style="display:none"></iframe>';
		$this->assign("jumpUrl",$_SESSION['chanpin_jumpurl']);
		$this->success('批量生成数据成功！');
    }	
	// 批量转移产品
    public function pestcid(){
		if(empty($_POST['ids'])){
			$this->error('请选择需要转移的产品！');
		}	
		$cid = intval($_POST['pestcid']);
		if (getlistson($cid)) {
			$rs = D("Chanpin");
			$data['chanpin_cid'] = $cid;
			$where['chanpin_id'] = array('in',$_POST['ids']);
			$rs->where($where)->save($data);
			redirect($_SESSION['chanpin_jumpurl']);
		}else{
			$this->error('请选择当前大类下面的子分类！');		
		}
    }	
	// 设置状态
    public function status(){
		$where['chanpin_id'] = $_GET['id'];
		$rs = D("Chanpin");
		if($_GET['value']){
			$rs->where($where)->setField('chanpin_status',1);
		}else{
			$rs->where($where)->setField('chanpin_status',0);
		}
		redirect($_SESSION['chanpin_jumpurl']);
    }	
	// Ajax设置星级
    public function ajaxstars(){
		$where['chanpin_id'] = $_GET['id'];
		$data['chanpin_stars'] = intval($_GET['stars']);
		$rs = D("Chanpin");
		$rs->where($where)->save($data);		
		echo('ok');
    }	
	// Ajax设置连载
    public function ajaxcontinu(){
		$where['chanpin_id'] = $_GET['id'];
		$data['chanpin_continu'] = trim($_GET['continu']);
		$rs = D("Chanpin");
		$rs->where($where)->save($data);		
		echo('ok');
    }			
}
?>