<?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 Download  extends HC_Controller
{
	
	function __construct()
	{
		parent::__construct();
		$this -> load_db();
		$this -> load_lang('www/article');
		$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'); //加载模型
		$dow_sort = $this -> personal_mod  -> get_sort('dow_sort');
		$this -> view -> assign('dow_sort',$dow_sort);
		
		$contact_arr =  $this -> helper -> build_contact( $web_config );
		$this -> view -> assign('contact_arr',$contact_arr);
		
		//var_dump($web_info);
		//var_dump($GLOBALS);
	}

/*
	function Dow()
	{
		//缓存
		if($GLOBALS["WebConfig"]["webtmp"]==1)
		{
			$this -> view -> caching = true;
			if($GLOBALS["WebConfig"]["webtmptime"] > 0 )
			{
				$this -> view -> cache_lifetime = $GLOBALS["WebConfig"]["webtmptime"] ;
			}else{
				$this -> view -> cache_lifetime = 3600 ;  //60 *  60 * 24
			}
			$smdirid 	= intval(trim($_REQUEST["id"])).intval(trim($_REQUEST["page_num"]));
			$this -> view -> cache_dir = ROOT_PATH.$GLOBALS['cache_path'].'/'.$this -> helper -> buildsmartydir( $type = 'd', $smdirid );
		}
 

		$getmenu = $this -> helper -> get_menu();
		$this -> view -> assign('getmenu',$getmenu);
		
		$getlink = $this -> helper -> get_link();
		$this -> view -> assign('getlink',$getlink);

		$getdow_sort = $this -> helper -> get_dow_sort();
		$this -> view -> assign('getdow_sort',$getdow_sort);
		
		$getads = $this -> helper -> get_ads( $instr = '1,2,3,4,5,6,7,8,9,10,11' );
		$this -> view -> assign('getads',$getads);
		
		$getuserlink = $GLOBALS['WebSe']->getuserlink();
		$this -> view ->assign('getuserlink',$getuserlink);	
		
		$this -> view -> assign('sorttype','download');
 
		
	}
*/


	function info_list()
	{
		$id 		= $this -> input -> get["id"];
		$page_num 	= $this -> input -> get["page"];
		
		$web_title = $this->lang['web_title'];
		//处理你现在的位置
		$now_address = $this->lang['u_address'].'<a href="'.BASE_WEBURL.'/">'.$this->lang['home'].'</a> > <a href="'.BASE_WEBURL.'/index.php?/download.html">'.$this->lang['web_title'].'</a>';

		//$id 		= intval($GLOBALS['Webur'] -> getval["idval"]);
		$page_num 	= $GLOBALS['Webur'] -> getval["page"];
		
 
		$page_size = 1 ;
		
		if($this -> input -> req["keyword"] !='')
		{   
			$keywordval 	= $this -> input -> req["keyword"];
			$keywordval  	= urldecode($keywordval); 
		}
		if($keywordval!='')
		{ 	
			//$endArray["keyword"] =$keywordval;
			$endurl 	   .= '&keyword='.urlencode($keywordval);
			$addsql .= " AND ( pic_title LIKE  '%".$keywordval."%' )";  
		}
		
		
		if( $page_num > 1 )
		{
			$page_num  = $page_num;
			$limit_num = ceil( ( $page_num -1 ) * $page_size );
		}else{
			$page_num  = 1;
			$limit_num = 0 ;
		}
		if( $sid > 0 )
		{
			$addsql = " and sort_id = ".$sid;
			//$endArray["sid"] = $sid;
		}elseif(intval($GLOBALS['Webur'] -> getval["sid"])!=''){
			$addsql = " and sort_id = ".intval($GLOBALS['Webur'] -> getval["sid"]);
			//$endArray["sid"] = intval($GLOBALS['Webur'] -> getval["sid"]);
		}
		if( $sid != '' )
		{
			$seletsort = " SELECT sort_id,sort_name  FROM ".$this -> db -> prefix."dow_sort WHERE sort_id = ".$sid." or  sort_parent_id = ".$sid ;
			$sortInfo  = $this -> db ->  select_info($seletsort);
			$sinid 	   = '';
			$addpage   = '&sid='.$sid;
			if($sortInfo!='')
			{
				foreach( $sortInfo as $skey => $sval )
				{
					if( $sval["sort_id"]== $sid )
					{
						$web_title = $sortval = $sval["sort_name"];

						$now_address = $now_address. ' > <a href="'.BASE_WEBURL.'/index.php?/download-info_list-'.$sid.'.html">'.$sval["sort_name"].'</a>';

					}
					if($skey==0)
					{
						$sinid = $sval["sort_id"];
					}else{
						$sinid .= ','.$sval["sort_id"];
					}
				}
			}
			$addsql = " and sort_id in ( ".$sinid." ) ";
		}
		
		//sort_id
		
		$sql_query = " SELECT dow_id, dow_title , dow_up_time ,dow_count  FROM  ".$this -> db -> prefix."download   WHERE  dow_delete = 0 AND dow_enabled = 1  $addsql GROUP BY  dow_id ORDER BY dow_up_time DESC,  `dow_id` LIMIT  $limit_num , $page_size ";
		$info 		= $this -> db ->  select_info($sql_query);
		foreach((array)$info as $dkey => $dval )
		{
			 
			$info[$dkey]["get_url"] = $this -> helper  -> build_url( $valid = $dval["dow_id"],'dow');	
		}
		
 
		$sql_query 	= " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."download  WHERE   dow_delete = 0 AND  dow_enabled = 1 $addsql  ORDER BY `dow_id`  ";
		$infocount 	= $this -> db ->  select_one($sql_query);

		$url = BASE_WEBURL.'/index.php?/download-info_list.html';
		
		$this -> load('page',$this->lang['page']);
		$page_str = $this -> page -> buildpage_str( $url , $page_num  , $page_size  , $infocount["CNUM"] , $endArray);
		
		//左则自定义内容
		$sql_query 	= " SELECT per_id,per_column,per_main  FROM  ".$this -> db -> prefix."personal  WHERE per_type = 'dowlist' ORDER BY `per_id` ";	
		$infoper 	= $this -> db ->  select_one($sql_query);
		$this -> personal_mod -> get_column( $infoper["per_column"] );
		
		$this -> view -> assign('web_title',$web_title);
		$this -> view -> assign('now_address',$now_address);
		
		$this -> view -> assign('keyword',$keywordval);
		
		$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('download_list.tpl',$cache_id);
		
	}
	
	
	function show()
	{

		$web_title = $this->lang['web_title'];
		//处理你现在的位置
		$now_address = $this->lang['u_address'].'<a href="'.BASE_WEBURL.'/">'.$this->lang['home'].'</a> > <a href="'.BASE_WEBURL.'/index.php?/download.html">'.$this->lang['web_title'].'</a>';
		
		$id 		= intval($GLOBALS['Webur'] -> getval["idval"]);
		$page_num 	= $GLOBALS['Webur'] -> getval["page"];
		

		if($id > 0 )
		{
			$sql_query 	= " SELECT *  FROM  ".$this -> db -> prefix."download as a  WHERE dow_delete = 0 AND  dow_enabled = 1  and dow_id = ".$id." ORDER BY `dow_id`  ";
			$infodetail 	= $this -> db ->  select_one($sql_query);
			
			//var_dump($infodetail);
			$this -> view -> assign('infodetail',$infodetail);
						
			$sqlup = " UPDATE ".$this -> db -> prefix."download set dow_count = dow_count +1 WHERE dow_id = ".$id ; 
			$this -> db -> query_sql($sqlup);
			
			//上下篇
			//查询上一组
			$prosql = " SELECT dow_id, dow_title  FROM ".$this -> db -> prefix."download WHERE  dow_id < ".$id." GROUP BY dow_id   ORDER BY dow_id  DESC ";
			$Prodetail 	= $this -> db ->  select_one($prosql);
			$Prodetail["get_url"]= $this -> helper  -> build_url( $valid = $Prodetail["dow_id"],'dow');
				
			//查询下一组
			$nextsql = " SELECT dow_id, dow_title  FROM ".$this -> db -> prefix."download WHERE  dow_id > ".$id." GROUP BY dow_id  ORDER BY dow_id  ";
			$Nextdetail = $this -> db ->  select_one($nextsql);
			$Nextdetail["get_url"]= $this -> helper  -> build_url( $valid = $Nextdetail["dow_id"],'dow');
			
			//左则自定义内容
			$sql_query 	= " SELECT per_id,per_column,per_main  FROM  ".$this -> db -> prefix."personal  WHERE per_type = 'dow' ORDER BY `per_id` ";	
			$infoper 	= $this -> db ->  select_one($sql_query);
			$this -> personal_mod -> get_column( $infoper["per_column"] );
			
			$this -> view -> assign('Prodetail',$Prodetail);
			$this -> view -> assign('Nextdetail',$Nextdetail);
			
			$this -> view -> assign('web_title',$web_title);
			$this -> view -> assign('now_address',$now_address);
			$cache_id = $id.'d'.$page_num;
			$this -> view -> display('download_tpl.html',$cache_id);
			
		}else{
			$this -> error('404');
		}
		
	}
	
	function act_default()
	{
		$web_title = $this->lang['web_title'];
		//处理你现在的位置
		$now_address = $this->lang['u_address'].'<a href="'.BASE_WEBURL.'/">'.$this->lang['home'].'</a> > <a href="'.BASE_WEBURL.'/index.php?/download.html">'.$this->lang['web_title'].'</a>';
		
		//查询一级菜单分类数量
		$sql_query = " SELECT dow_id, dow_title , dow_up_time   FROM  ".$this -> db -> prefix."download   WHERE dow_delete = 0 AND  dow_enabled = 1   GROUP BY  dow_id ORDER BY dow_up_time DESC, `dow_id` LIMIT  0 , 8 ";
		$info = $this -> db -> select_info($sql_query);
 		
		//var_dump($info);
		$this -> view -> assign('web_title',$web_title);
		$this -> view -> assign('now_address',$now_address);
		$this -> view -> assign('info',$info); 
		$this -> view -> display('download_list.tpl',$cache_id);
	}

 
}

