<?php
/***
 *----------------------------------------------------------------------------*
 * 版权所有 2010-2016 HuCart.COM，并保留所有权利。
 * 此程序是HuCart的一部分。  
 * 根据MIT许可证授权(在软件和软件的所有副本中都必须包含版权声明和许可声明。)
 * 官方网址:http://www.hucart.com
 *
 * 对于完整的版权和许可证信息，请参阅MIT-LICENSE.TXT
 * 程序的再发布必须保留以上版权声明。
 *
 *@author pgmk<pgmk@hucart.com>
 *@copyright pgmk<pgmk@hucart.com>
 *@link http://www.hucart.com
 *@license The MIT License.
 *----------------------------------------------------------------------------*
**/
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class video  extends HC_Controller
{
	function __construct()
	{
		parent::__construct();
		$this -> load_db();
		$this -> load_lang('www/picture');
		$this -> load('helper');     	 	//加载辅助类
		$this -> load_model('ads'); 		//加载模型
		$this -> load_model('web_config'); 	//加载模型
		$web_config = $this -> web_config_mod -> get_sys_config();
		$this -> set_view_dir( $web_config['temp'] -> dir );
		$this -> view -> assign('web_config', $web_config);


		$this -> view -> assign('menu_info',$this -> web_config_mod -> get_web_menu());
		$this -> view -> assign('ads_info', $this -> ads_mod -> get_ads( '1,2,3,4,5,6,7,8,9,10,11'  ));
		$this -> view -> assign('user_link',$this -> session -> get_user_link( $this->lang['user_link'] ));
		
 
		$this -> load_model('personal'); //加载模型
		$vid_sort = $this -> personal_mod  -> get_sort('vid_sort');

		$this -> view -> assign('vid_sort',$vid_sort);
		
		$contact_arr =  $this -> helper -> build_contact( $web_config );
		$this -> view -> assign('contact_arr',$contact_arr);
	}
	
	
	function info_list()
	{
	
		$id 		= $this -> input -> get["id"];
		$page_num 	= $this -> input -> get["page"];
		$keyword 	= $this -> input -> req["keyword"];
 
		$page_size = 16 ;

		if( $page_num > 1 )
		{
			$page_num  = $page_num;
			$limit_num = ceil( ( $page_num -1 ) * $page_size );
		}else{
			$page_num  = 1;
			$limit_num = 0 ;
		}

		$addsql = '';

		$webtitle = '视频中心';
		//处理你现在的位置
		$now_address = '<li><a href="'.BASE_WEBURL.'/">'.$this->lang['home'].'</a></li><li><a href="'.BASE_WEBURL.'/index.php?/video.html">视频中心</a></li>';

		if( $sid > 0 )
		{
			$addsql = " and sort_id = ".$sid;
			////$endArray["sid"] = $sid;
		}
		if( $id != '' )
		{
			$seletsort = " SELECT sort_id,sort_name  FROM ".$this -> db -> prefix."vid_sort WHERE  sort_id = ".$id." or  sort_parent_id = ".$id ;
			$sortInfo  = $this -> db ->  select_info($seletsort);
			$sinid 	   = '';
			$addpage   = '&sid='.$sid;
			if($sortInfo!='')
			{
				foreach( $sortInfo as $skey => $sval )
				{
					if( $sval["sort_id"]== $id )
					{
						$now_address =  $now_address. '<li><a href="'.BASE_WEBURL.'/video-list-1-'.$sid.'.html">'.$sval["sort_name"].'</a></li>';
					}
					if($skey==0)
					{
						$sinid = $sval["sort_id"];
					}else{
						$sinid .= ','.$sval["sort_id"];
					}
				}
			}
			$addsql = " and sort_id in ( ".$sinid." ) ";
		}
		
		
		if($this -> input -> req["keyword"] !='')
		{   
			$keywordval 	= $this -> input -> req["keyword"];
			$keywordval  	= urldecode($keywordval); 
		}
		if($keywordval!='')
		{ 	
			//$endArray["keyword"] =$keywordval;
			$endurl 	   .= '&keyword='.urlencode($keywordval);
			$addsql .= " AND ( vid_title LIKE  '%".$keywordval."%' )";  
		}


		$sql_query 	= " SELECT vid_id, vid_title ,vid_img, vid_up_time   FROM  ".$this -> db -> prefix."video   WHERE vid_delete = 0 AND vid_enabled= 1 $addsql  GROUP BY  vid_id ORDER BY  `vid_id` LIMIT  $limit_num , $page_size ";
		$info 		= $this -> db ->  select_info( $sql_query );
		
		
		foreach((array)$info as $pkey => $pval )
		{
			 $info[$pkey]["vid_img"] = $this -> helper -> build_img( $pval["vid_img"] ) ; 
			 $info[$pkey]["get_url"] = $this -> helper  -> build_url( $valid = $pval["vid_id"],'vid');
		}


		
		$sql_query 	= " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."video  WHERE vid_delete = 0 AND vid_enabled = 1 $addsql ORDER BY `vid_id`  ";
		$infocount 	= $this -> db ->  select_one( $sql_query );

		$url = BASE_WEBURL.'/index.php?/video-info_list-'.$sid.'.html';
		
		$this -> load('page',$this->lang['page']);
		
		$page_str 	= $this -> page -> buildpage_str( $url , $page_num  , $page_size  , $result_count = $infocount["CNUM"] ,$endArray );
		
		//左则自定义内容
		$sql_query 	= " SELECT per_id,per_column,per_main  FROM  ".$this -> db -> prefix."personal  WHERE per_type = 'vidlist' ORDER BY `per_id` ";	
		$infoper 	= $this -> db ->  select_one($sql_query);
		$this -> personal_mod -> get_column( $infoper["per_column"] );

		$this -> view -> assign('keyword',$keywordval);
		
		$this -> view -> assign('webtitle',$webtitle);
		$this -> view -> assign('now_address',$now_address);
		$this -> view -> assign('page_str',$page_str);
		$this -> view -> assign('info',$info);
		if($sid !='' or $page_num!='' or  $newkeyword !='')
		{
			$cache_id = $sid.'p'.$page_num.'k'.$keyid;
		}
		$this -> view -> display('vid_list.tpl',$cache_id);
	}
	
	function show()
	{
		$id 		= $this -> input -> get["id"];
		$page_num 	= $this -> input -> get["page"];

		$now_address = '<li><a href="'.BASE_WEBURL.'/">'.$this->lang['home'].'</a></li><li><a href="'.BASE_WEBURL.'/index.php?/video.html">视频中心</a></li>';
		
		if($id > 0 )
		{
			$sql_query 	= " SELECT p.* ,s.sort_name  FROM  ".$this -> db -> prefix."video as p , ".$this -> db -> prefix."vid_sort  as s   WHERE p.vid_delete = 0 AND p.sort_id = s.sort_id and p.vid_enabled = 1  and p.vid_id = ".$id." ORDER BY p.`vid_id`  ";
			
			$infodetail 	= $this -> db ->  select_one($sql_query);
			//var_dump($infodetail);
			$now_address = $now_address .'<li><a href="'.BASE_WEBURL.'/index.php?/video-info_list-'.$infodetail['sort_id'].'.html">'.$infodetail['sort_name'].'</a></li><li>'.$infodetail['vid_title'].'</li>';
			//var_dump( $infoItem );

			$webtitle = $infodetail["vid_title"];
			
			$sqlup = " UPDATE ".$this -> db -> prefix."video set vid_count = vid_count +1 WHERE vid_id = ".$id ;
			
			$this -> db -> query_sql($sqlup);
			
			$this -> load_model('comemt'); 	//加载模型	
			//获得评论
			$commentInfo = $this -> comemt_mod  -> get_comemt( $id , $Num = 10 , $type = 'video');
			$this -> view -> assign('commentInfo',$commentInfo);
			
			
			//查询上一组及下一组
			//查询上一组
			$prosql = " SELECT vid_id, vid_title  FROM ".$this -> db -> prefix."video WHERE  vid_id < ".$infodetail["vid_id"]." GROUP BY vid_id   ORDER BY vid_id  DESC ";
			$Prodetail 	= $this -> db ->  select_one($prosql);
			$Prodetail["get_url"]= $this -> helper  -> build_url( $valid = $Prodetail["vid_id"],'vid');
			
			//查询下一组
			$nextsql = " SELECT vid_id, vid_title  FROM ".$this -> db -> prefix."video WHERE  vid_id > ".$infodetail["vid_id"]." GROUP BY vid_id  ORDER BY vid_id  ";
			$Nextdetail 	= $this -> db ->  select_one($nextsql);
			$Nextdetail["get_url"]= $this -> helper  -> build_url( $valid = $Nextdetail["vid_id"],'vid');
			
			
			//左则自定义内容
			$sql_query 	= " SELECT per_id,per_column,per_main  FROM  ".$this -> db -> prefix."personal  WHERE per_type = 'vid' ORDER BY `per_id` ";	
			$info_per 	= $this -> db ->  select_one($sql_query);
			//var_dump($info_per);
			//exit();
			$this -> personal_mod -> get_column( $info_per["per_column"] );
			
			
			$this -> view -> assign('Prodetail',$Prodetail);
			$this -> view -> assign('Nextdetail',$Nextdetail);

			$this -> view -> assign('now_address',$now_address);
			$this -> view -> assign('webtitle',$webtitle);
			$this -> view -> assign('infodetail',$infodetail);
			$cache_id = $id.'d'.$page_num;

			
			if($infodetail["vid_palyer"]==2)
			{
				//echo $infodetail["urlhash"] =  hash(md5,$infodetail["vid_url"]);
				
				$this -> view -> display('vidbaidu.tpl',$cache_id);
			}else if($infodetail["vid_palyer"]==4)
			{
				//echo $infodetail["urlhash"] =  hash(md5,$infodetail["vid_url"]);
				
				$this -> view -> display('vidstorm.tpl',$cache_id);
			}else if($infodetail["vid_palyer"]==3)
			{
				//echo $infodetail["urlhash"] =  hash(md5,$infodetail["vid_url"]);
				
				$this -> view -> display('vidmedia.tpl',$cache_id);
			}else if($infodetail["vid_palyer"]==5)
			{
				//echo $infodetail["urlhash"] =  hash(md5,$infodetail["vid_url"]);
				
				$this -> view -> display('vidqvod.tpl',$cache_id);
			}else{
				$this -> view -> display('vid.tpl',$cache_id);
			}
			
			
		}else{
			$this -> error('404');

		}
	
	}


	function act_default()
	{
		$webtitle = '视频中心';
		
		//处理你现在的位置
		$now_address = '<li><a href="'.BASE_WEBURL.'/">'.$this->lang['home'].'</a></li><li><a href="'.BASE_WEBURL.'/index.php?/video.html">视频中心</a></li>';
		//查询一级菜单分类数量
 				$seletsort = " SELECT sort_id,sort_name  FROM ".$this -> db -> prefix."vid_sort WHERE   sort_parent_id = 0 " ;
				$sortInfo  = $this -> db ->  select_info($seletsort);

				foreach( (array)$sortInfo as $skey => $sval )
				{
 					 
					$info[$skey]["title"] 	= $sortval = $sval["sort_name"];
					$info[$skey]["url"] 	= BASE_WEBURL.'/index.php?/video-info_list-'.$sval["sort_id"].'.html';

					$sql_query = " SELECT vid_id, vid_title , vid_img, vid_up_time   FROM  ".$this -> db -> prefix."video   WHERE vid_delete = 0 AND  vid_enabled = 1 and  sort_id in ( ".$sval["sort_id"]." )  GROUP BY  vid_id ORDER BY vid_up_time DESC, `vid_id` LIMIT  0 , 8 ";
					$info[$skey]["Arr"] = $this -> db -> select_info($sql_query);
				}
			   
 
		//var_dump($info);
		$this -> view -> assign('webtitle',$webtitle);
		$this -> view -> assign('now_address',$now_address);
		$this -> view -> assign('info',$info); 
		$this -> view -> display('vid_index.tpl',$cache_id);
		
	}

	function play()
	{
		$id = intval(trim($_REQUEST["id"]));
		
		if($id > 0 )
		{
			$sql_query 	= " SELECT vid_title,vid_url   FROM  ".$this -> db -> prefix."video   WHERE vid_enabled = 1  and vid_id = ".$id." ORDER BY `vid_id`  ";
			$info 	= $this -> db ->  select_one($sql_query);
		}
		
		$this -> view -> assign('info',$info);
		$this -> view -> display('vidplay_tpl.html');
	
	}
	

}


