<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Info extends HC_Controller
{
	//注意无须构造函数,使用了构造函数得重构父类 parent::__construct();
	var $info_arr = array('download','article','picture','video');
	var $info_val = '';
	var $info_prefix = 'dow_';
	function __construct()
	{
		parent::__construct();
		$this -> master_act = $this -> input ->  req["master_act"];
		if( $this -> master_act =='' or !in_array( $this -> master_act,$this->info_arr) ){
			exit('error!');
		}
		
		$this -> load_lang('adminsys/'.$this -> master_act);
		$this -> load('admin');
		$this -> admin -> online_admin();
		$this -> load_db();
		$this -> load('helper');     //加载辅助类
		$this -> load_model('sort'); //加载分类模型
	}

	
	function info_list(  )
	{
		$keyword 	= $this -> input -> req["keyword"];
		$list_val   = $this -> input -> req["list_val"];
		$list_type  = $this -> input -> req["list_type"];

		$table_arr['where_add'] = '';

		if($keyword!='')
		{ 	
			$table_arr['where_add'] .= " AND ( dow_name LIKE  '%".$keyword."%' )";  
		}
		

		$table_arr['order_by']  = 'dow_id';
		if($list_val!='' and $list_type =='DESC' )
		{
			$table_arr['order_by'] = $list_val ."  DESC ";
		}elseif($list_val!='' and $list_type =='ASC')
		{
			$table_arr['order_by'] = $list_val." ASC";
		}else{
			$table_arr['order_by'] =  'dow_id';
		}

		$this -> load('page',$this->lang['page']);

		//列表字段显示
		$show_arr = array('dow_id' => array('th'=>1,'td'=>'id'),'dow_name'=> array('th'=>0,'td'=>''),'sort_name'=> array('th'=>1,'td'=>''),'dow_file'=> array('th'=>0,'td'=>''),'dow_size'=>array('th'=>1,'td'=>''),'dow_count'=>array('th'=>1,'td'=>''),'dow_view'=>array('th'=>1,'td'=>''),'dow_list'=>array('th'=>1,'td'=>''),'dow_enabled'=>array('th'=>1,'td'=>'arr'));
		
		$field_show = $this -> helper -> get_th_list( $show_arr , $list_val , $list_type , $lang_arr = $this->lang['field_list'] );

		$table_arr['table']				= $this -> master_act;
		$table_arr['left']['table'] 	= 'dow_sort';
		$table_arr['left']['on']		= $this -> db -> prefix.'download.sort_id = '.$this -> db -> prefix.'dow_sort.sort_id ';
		
		$table_arr['prefix'] 	= $this -> db -> prefix;
		$table_arr['field_arr'] = array_keys($show_arr);
		$table_arr['def_where'] = 'dow_delete =0';
		$table_arr['group_by']  = '';
		$table_arr['limit_val'] = $this -> page ->get_limit_val();
		
		$re_sql   = $this -> helper -> get_list_sql( $table_arr );

		//var_dump($re_sql);
		
		$info 	  = $this -> db -> select_info( $re_sql['sql_list'] );
		
		$key_arr['dow_enabled'] = $this->lang['enabled_arr'];

		$lang_arr['edit'] 		= $this->lang['edit'];
		$lang_arr['is_delete']  = $this->lang['is_delete'];
		$lang_arr['delete'] 	= $this->lang['delete'];

		$td_rows  = $this -> helper -> get_td_list( $info , $show_arr ,$key_arr ,$lang_arr );
		
		$count 	  = $this -> db -> select_one( $re_sql['sql_count'] );

		$page_str = $this -> page ->get_admin_page( $count["CNUM"] );
		
		$this -> view -> assign('keyword',		$keyword);
		$this -> view -> assign('info_prefix',	$this -> info_prefix);
		$this -> view -> assign('list_val',		$list_val);
		$this -> view -> assign('list_type',	$list_type);
		$this -> view -> assign('master_act',	$this -> master_act);
		$this -> view -> assign('colspan_val',	count($show_arr)+1);
		$this -> view -> assign('field_show',	$field_show);
		$this -> view -> assign('page_str',		$page_str);
		$this -> view -> assign('td_rows',		$td_rows);
		$this -> view -> display('table_list.tpl');
		
	}
	
	
	
	function trash_list(  )
	{
		$page_size = 20 ;		
		$page_num_val 	= $this -> input ->  req["page_num"];
		
		if( $page_num_val > 1 )
		{
			$page_num  = $page_num_val;
			$limit_num = ceil( ( $page_num_val -1 ) * $page_size );
		}else{
			$page_num  = 1;
			$limit_num = 0 ;
		}
			
		$sql_query = " SELECT dow_id, dow_name, dow_file, dow_size, dow_list ,dow_count, dow_enabled  FROM  ".$this -> db -> prefix."download  WHERE  dow_delete =1 GROUP BY dow_id ORDER BY `dow_id` LIMIT  $limit_num , $page_size ";
		
		$info = $this -> db -> select_info($sql_query);
		
		$sql_query = " SELECT count(1) as LNUM  FROM  ".$this -> db -> prefix."download  WHERE  dow_delete =1  ORDER BY `dow_id`  ";
		
		$infocount = $this -> db -> select_one($sql_query);
		//var_dump();
		$page_str = $this -> page ->get_admin_page( $infocount["LNUM"]);
		
		
		
		$this -> view -> assign('page_str',$page_str);
		$this -> view -> assign('info',$info);
		$this -> view -> display('info_trash.tpl');
		
	}


	function recycle()
	{
		
		$info_id = $infoid = $this -> input ->  get["id"];
		
		$uprec = " UPDATE `".$this -> db -> prefix."download` SET dow_delete = 1  WHERE  `dow_id` in ( $info_id ) ";
		$this -> db -> query_sql($uprec);
		
		$this -> view ->  assign('info_title',$this->lang['del_success']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=info&master_act=".$this -> master_act."&act=info_list");
		$this -> view ->  display('suggestion.tpl');

	}
	
	//还原
	function restore( )
	{
		$infoid = $this -> input ->  delUrl($_REQUEST["id"]);
		
		$uprec = " UPDATE `".$this -> db -> prefix."download` SET dow_delete = 0  WHERE  `dow_id` in ( $infoid ) ";
		@$this -> db -> query_sql($uprec);
		
		$this -> view ->  assign('info_title',$_LANG['bd_restoresusse']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=info&master_act=".$this -> master_act."&act=recycler");
		$this -> view ->  display('suggestion.tpl');
	}
	
	function add(  )
	{
		//$this -> view ->  caching = true;
		//$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24
		//Download::getSort();
		
		$sort_info  		= $this -> sort_mod -> get_sort('dow_sort','sort_');
		//生成分类选择控件
		$sort_select_str = $this -> helper -> build_out_str( $sort_info , $object_name = 'sort_id', $object_id = 'sort_id' , $object_val_str = 'sort_id', $object_txt_str = 'sort_name', $sort_id  ,$lang_txt = $this-> lang['txt_select'] , $key_id = 'sort_id', $parent_id = 'sort_parent_id', $grade_val = 6 );
		
		$this -> view -> assign( 'sort_select_str', $sort_select_str);
		
 		$ckeditorLoad 	= $this -> helper -> load_ckeditor( 'dow_desc' );	//加载文本编辑器
		$this -> view -> assign( 'ckeditorLoad', $ckeditorLoad);

		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=info&master_act=".$this -> master_act."&act=add_info");
		$this -> view -> display('table_edit.tpl');
	}
	
	
	
	function add_info()
	{
		$dow_nameval 	= $this -> input ->  funpost("dow_name");
		
		$dow_fileval 	= $_REQUEST["pro_img"]["1"];
	    $dow_imgeval 	= $_REQUEST["pro_img"]["2"];
		
		$dow_sizeval 	= $this -> input ->  funpost("dow_size");
 		$dos_idval 		= $this -> input ->  funpost("dos_id");
		
		$dow_descval 	= html_entity_decode($this -> input ->  delart($_POST["dow_desc"]));

		
		$dow_listval 	= $this -> input ->  funpost("dow_list");
		$dow_enabledval = $this -> input ->  funpost("dow_enabled");
		
		 
		if($dow_nameval and $dow_fileval)
		{
			$sql_query = " SELECT count(1) as LNUM  FROM  ".$this -> db -> prefix."download  WHERE dow_name = '".$dow_nameval."' ORDER BY `dow_id` ";
			$info = $this -> db -> select_one($sql_query);
			
			if( $info["LNUM"] < 1 )
			{
				$dow_addtimeval = time();
				$insertip =" INSERT INTO `".$this -> db -> prefix."download` (`dow_name` , `dos_id` , `dow_file` ,  `dow_img` ,`dow_size` , `dow_desc` ,`dow_list` , `dow_enabled`,`dow_addtime` ) VALUES (  '$dow_nameval','$dos_idval', '$dow_fileval', '$dow_imgeval','$dow_sizeval','$dow_descval',  '$dow_listval', '$dow_enabledval','$dow_addtimeval')";
				
				$this -> db -> query_sql($insertip);
				$this -> view ->  assign('info_title',$this->lang['add_success']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=info&master_act=".$this -> master_act."&act=info_list");
				$this -> view ->  display('suggestion.tpl');	
			}else{
				
				$this -> view ->  assign('info_title',$this->lang['err_info_exist']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=info&master_act=".$this -> master_act."&act=add");
				$this -> view ->  display('suggestion.tpl');	
			}
			
		}else{
			
			$this -> view ->  assign('info_title',$this->lang['err_info_missing']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=info&master_act=".$this -> master_act."&act=add");
			$this -> view ->  display('suggestion.tpl');		}
		    //var_dump($_REQUEST);
	}
	
	function del( )
	{
		$infoid = $this -> input ->  get["id"];
		$insertip = " DELETE FROM `".$this -> db -> prefix."download` WHERE `dow_id` in ( $infoid ) ";
		

		$this -> db -> query_sql($insertip);
		$this -> view ->  assign('info_title',$this->lang['del_success']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=info&master_act=".$this -> master_act."&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}
	
	
	function show( )
	{
		$info_id = $this -> input ->  get["id"];
		$upshow = " UPDATE `".$this -> db -> prefix.$this -> master_act."` SET dow_enabled = 1  WHERE `dow_id` in ( $info_id ) ";

		$this -> db -> query_sql($upshow);
		$this -> view ->  assign('info_title',$_LANG['bd_showsusse']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=info&master_act=".$this -> master_act."&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}
	
	function hide( )
	{
		//$indel = implode(',', $delArray);
		$info_id = $this -> input ->  get["id"];
		
		$up_hide = " UPDATE `".$this -> db -> prefix.$this -> master_act."` SET dow_enabled = 0  WHERE  `dow_id` in ( $info_id ) ";
		$this -> db -> query_sql($up_hide);
		
		$this -> view ->  assign('info_title',$_LANG['bd_hidesusse']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=info&master_act=".$this -> master_act."&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}
	
	
	function info(  )
	{
		$act_type 	= $this -> input -> get["act_type"];
		
		if($act_type=='edit'){
			
			$id_val = $this -> input ->  get["id"];

			$sql_query = " SELECT *  FROM  ".$this -> db -> prefix.$this -> master_act."  WHERE sort_id = '".$id_val."' ORDER BY `sort_id` ";
			$info = $this -> db -> select_one($sql_query);
			
			//可选择的一级分类 
			$asInfo 	= $this -> sort_mod -> get_sort($this -> master_act,'sort_');
			$sminfo 	= $this -> helper -> grade_info_sort( $asInfo , $key_id = 'sort_id', $parent_id = 'sort_parent_id', $grade_val = 4 );
			//var_dump($sminfo);
			
			$this -> view ->  assign('txt_title',$this->lang['add_sort_title']);
			$this -> view -> assign('edit_submit', $this-> lang['edit']);
			$this -> view -> assign('info',$info);
			$this -> view -> assign('act_type',$act_type);
			$this -> view -> assign('sminfo',$sminfo);
			$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=info&master_act=".$this -> master_act."&act=edit_info&act_type=edit");
			
			
		}else{
		    //添加信息
			
			$list_info 	= $this -> helper -> get_add_list( $this->lang['field_add_list'] );
			
			$this -> view -> assign('list_info',$list_info);

			$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=info&master_act=".$this -> master_act."&act=edit_info&act_type=add");
			
			$this -> view -> assign('act_type',$act_type);
			$this -> view ->  assign('txt_title',$this->lang['add_sort_title']);
			$this -> view -> assign('edit_submit', $this-> lang['add']);
			
		}



		$this -> view ->  display('info_edit.tpl');
	
	}
	
	
	function edit_info( )
	{	
	
		$dow_idval 		= $this -> input ->  funpost("dow_id");
		$dow_nameval 	= $this -> input ->  funpost("dow_name");
		$dos_idval 		= $this -> input ->  funpost("dos_id");
		$dow_fileval 	= $_REQUEST["pro_img"]["1"];
	    $dow_imgeval 	=$_REQUEST["pro_img"]["2"];
		$dow_sizeval 	= $this -> input ->  funpost("dow_size");
		$dow_descval 	= html_entity_decode($this -> input ->  delart($_POST["dow_desc"]));
 		
		$dow_listval 	= $this -> input ->  funpost("dow_list");
		$dow_enabledval = $this -> input ->  funpost("dow_enabled");
		
		
		if($dow_nameval !='' and $dow_fileval !='' and $dow_idval !='')
		{
			$updatedownload = "UPDATE `".$this -> db -> prefix.$this -> master_act."` SET  dow_name = '".$dow_nameval."' ,  dos_id = '".$dos_idval."' ,dow_file = '".$dow_fileval."' , dow_img = '".$dow_imgval."' , dow_size = '".$dow_sizeval."', dow_desc = '".$dow_descval."', dow_list = '".$dow_listval."',dow_enabled = '".$dow_enabledval."'  WHERE  dow_id = '".$dow_idval."' ";
			
			$this -> db -> query_sql( $updatedownload );
			
			$this -> view ->  assign('info_title',$this->lang['edit_success']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=info&master_act=".$this -> master_act."&act=info_list");
			$this -> view ->  display('suggestion.tpl');
				
		}else{
		
			$this -> view ->  assign('info_title',$this->lang['err_info_missing']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=info&master_act=".$this -> master_act."&act=info&id=".$dow_idval);
			$this -> view ->  display('suggestion.tpl');	
		}
		
	}
	


		
}

