<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Link extends HC_Controller
{
	//注意无须构造函数,使用了构造函数得重构父类 parent::__construct();
	var $master_act = 'link';
	var $info_prefix = 'lin_';
	
	private $power_arr = array();
	function __construct()
	{
		parent::__construct();
		$this -> load_lang('adminsys/'.$this -> master_act);
		$this -> view -> assign('master_act', $this -> master_act);
		$this -> view -> assign('info_prefix',	$this -> info_prefix);
		
		//$this -> load('admin');
		//$this -> admin -> online_admin();
		$this -> load_model('admin');
		//var_dump($this->lang);
		$this -> power_arr = $this -> admin_mod -> get_power( $this -> master_act );
		
		$this -> load_db();
		$this -> load('helper');     //加载辅助类
		$this -> load_model('sort'); //加载分类模型
		
		
	}
	
 

	function info_list(  )
	{
		$def_list_arr = array('lin_id' => array('th'=>1,'td'=>'id'),'lin_name'=> array('th'=>0,'td'=>''),'lin_url'=> array('th'=>1,'td'=>''),'lin_logo'=> array('th'=>1,'td'=>'img'),'lin_list'=>array('th'=>1,'td'=>''),'lin_enabled'=>array('th'=>1,'td'=>'arr') );
		//列表字段显示
		$show_arr = $this -> admin_mod -> get_power_detail( $this -> power_arr['admp_action'] , $def_list_arr ,'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 ( lin_name LIKE  '%".$keyword."%' )";  
		}
		

		$table_arr['order_by']  = 'lin_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'] =  'lin_id';
		}

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

		
		$field_show = $this -> helper -> get_th_list( $show_arr , $list_val , $list_type ,  $this->lang['field_list'] );

		$table_arr['table']		= $this -> master_act;
 	
		$table_arr['prefix'] 	= $this -> db -> prefix;
		$table_arr['field_arr'] = array_keys((array)$show_arr);
		$table_arr['def_where'] = ' 1 ';
		$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['lin_enabled'] = $this->lang['enabled_arr'];

		$opt = array( 'edit'=> $this->lang['edit'],'delete'=> $this->lang['delete'] );

		$td_rows  = $this -> helper -> get_td_list( $info , $show_arr ,$key_arr , $this -> master_act, $opt );
		
		$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('list_val',		$list_val);
		$this -> view -> assign('list_type',	$list_type);
		
		$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('item_list.tpl');

	}
	

	function delete( )
	{
		//exit('test');
		$this -> admin_mod -> get_power_detail( $this -> power_arr['admp_action'] , '' ,'delete' );
		
		$info_id   = $this -> input ->  get["id"];
		$insert_ip = " DELETE FROM `".$this -> db -> prefix.$this -> master_act."` WHERE `lin_id` in ( $info_id ) ";

		$this -> db -> query_sql($insert_ip);
		$this -> view ->  assign('info_title',$this->lang['delsusse']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=".$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 lin_enabled = 1  WHERE `lin_id` in ( $info_id ) ";

		$this -> db -> query_sql($upshow);
		$this -> view ->  assign('info_title',$_LANG['showsusse']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}
	
	function hide( )
	{

		$info_id = $this -> input ->  get["id"];
		
		$up_hide = " UPDATE `".$this -> db -> prefix.$this -> master_act."` SET lin_enabled = 0  WHERE  `lin_id` in ( $info_id ) ";
		$this -> db -> query_sql($up_hide);
		
		$this -> view ->  assign('info_title',$_LANG['hidesusse']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}



	
	function info()
	{
		//i 代表输入项，t类型 l长度 
		$def_info_arr = array( 'lin_name'=> array('i'=>array('t'=>'text','l'=>255)),'lin_url'=> array('i'=>array('t'=>'text','l'=>255) ),'lin_logo'=> array('i'=>array('t'=>'images','l'=>255),'add'=>'img'),'lin_list'=>array('i'=>array('t'=>'text','l'=>255),'t'=>'int'),'lin_enabled'=>array('i'=>array('t'=>'radio'),'def'=>$this->lang['enabled_arr']) );
		
		$act_type 	= $this -> input -> get["act_type"];
		
		$show_arr = $this -> admin_mod -> get_power_detail( $this -> power_arr['admp_action'] , $def_info_arr ,'info_list'  );

		
		if($act_type=='edit'){
		
			
			$edit_arr = $this -> admin_mod -> get_power_detail( $this -> power_arr['admp_action'] , $show_arr ,'edit_info' );

			$id_val = $this -> input ->  get["id"];

			$sql_query = " SELECT *  FROM  ".$this -> db -> prefix.$this -> master_act."  WHERE lin_id = '".$id_val."' ORDER BY `lin_id` ";
			$info = $this -> db -> select_one($sql_query);

			$this -> view -> assign('edit_arr',$edit_arr);
			$this -> view -> assign('show_arr',$show_arr);
			
			$this -> view -> assign('txt_title',   $this->lang['edit_title']);
			$this -> view -> assign('edit_submit', $this->lang['edit']);
			$this -> view -> assign('info',$info);
			$this -> view -> assign('act_type',$act_type);
			$this -> view -> assign('sort_select_str',$sort_select_str);
			$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=edit_info&act_type=edit");
			
			
		}else{
			
			$show_arr = $this -> admin_mod -> get_power_detail( $this -> power_arr['admp_action'] , $show_arr ,'add_info' );
			
			//var_dump($show_arr);
			
			$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=edit_info&act_type=add");
			
			$this -> view -> assign('show_arr',$show_arr);
			$this -> view -> assign('act_type',$act_type);
			$this -> view -> assign('txt_title',$this->lang['add_title']);
			$this -> view -> assign('edit_submit', $this-> lang['add']);
			
		}

		$this -> view ->  display('link_edit.tpl');
	
	}
	
	
	function edit_info( )
	{	
		$show_arr = $this -> admin_mod -> get_power_detail( $this -> power_arr['admp_action'] , $def_info_arr ,'edit_info'  );
		$lin_id 			= $this -> input ->  funpost("lin_id");
		$lin_name 			= $this -> input ->  funpost("lin_name");
		$lin_url 			= $this -> input ->  funpost("lin_url");
		$lin_logo 			= $this -> input ->  funpost("lin_logo");
		$lin_list 			= $this -> input ->  funpost("lin_list");

		$lin_enabled 		= $this -> input ->  funpost("lin_enabled");
 

		
		$act_type = $this -> input ->  req["act_type"];


		
		if($act_type=='edit')
		{

		
			if($lin_name !='' and  $lin_url !='' and $lin_id !='')
			{
				
				$update = "UPDATE `".$this -> db -> prefix.$this -> master_act."` SET  lin_name = '".$lin_name."' , lin_url = '".$lin_url."' , lin_logo = '".$lin_logo."' , lin_enabled = '".$lin_enabled."', lin_list = '".$lin_list."' WHERE  lin_id = '".$lin_id."' ";
				
				
				$this -> db -> query_sql( $update );
				
				$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=".$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=".$this -> master_act."&act=info&id=".$lin_id);
				$this -> view ->  display('suggestion.tpl');	
			}
		

		}else{
			
				if($lin_name !='' and  $lin_url !='')
				{
					$sql_query = " SELECT count(1) as LNUM  FROM  ".$this -> db -> prefix.$this -> master_act."  WHERE lin_name = '".$lin_name."' ORDER BY `lin_id` ";
					$info = $this -> db -> select_one($sql_query);


					if( $info["LNUM"] < 1 )
					{
 
						
						$insert_sql =" INSERT INTO `".$this -> db -> prefix.$this -> master_act."` (
						`lin_name` ,`lin_url` , `lin_logo` , `lin_list`,`lin_enabled` ) 
						VALUES ( '$lin_name','$lin_url','$lin_logo','$lin_list','$lin_enabled')";
						
						$this -> db -> query_sql($insert_sql);
						$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=".$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=".$this -> master_act."&act=info&act_type=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=".$this -> master_act."&act=info&act_type=add");
					$this -> view ->  display('suggestion.tpl');
				}
		}
		
	}




		
}