<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Invite extends HC_Controller
{
	//注意无须构造函数,使用了构造函数得重构父类 parent::__construct();
	var $master_act = 'invite';
	var $info_prefix = 'inv_';
	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_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 ( inv_position LIKE  '%".$keyword."%' )";  
		}
		

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

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

		//列表字段显示
		$show_arr = array('inv_id' => array('th'=>1,'td'=>'id'),'inv_position'=> array('th'=>0,'td'=>''),'inv_num'=> array('th'=>0,'td'=>''),'inv_year'=> array('th'=>1,'td'=>'arr'),'inv_educational'=> array('th'=>1,'td'=>''),'inv_subject'=>array('th'=>1,'td'=>''),'inv_age'=>array('th'=>1,'td'=>'arr'),'inv_add_time'=>array('th'=>1,'td'=>'time'),'inv_educational'=>array('th'=>1,'td'=>''));
		
		$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($show_arr);
		$table_arr['def_where'] = 'inv_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['inv_educational'] = $this->lang['enabled_arr'];
		$key_arr['inv_year'] 		= $this->lang['inv_year_arr'];
		$key_arr['inv_age'] 		= $this->lang['inv_age_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('table_list.tpl');


	}
	
	
	
	function trash_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 ( inv_position LIKE  '%".$keyword."%' )";  
		}
		

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

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

		//列表字段显示
		//列表字段显示
		$show_arr = array('inv_id' => array('th'=>1,'td'=>'id'),'inv_position'=> array('th'=>0,'td'=>''),'inv_pay'=> array('th'=>0,'td'=>'img'),'sort_name'=> array('th'=>1,'td'=>''),'inv_view'=> array('th'=>1,'td'=>''),'inv_count'=>array('th'=>1,'td'=>''),'inv_educational'=>array('th'=>1,'td'=>'arr'),'inv_up_time'=>array('th'=>1,'td'=>'time'));
		
		
		$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($show_arr);
		$table_arr['def_where'] = 'inv_delete =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['inv_educational'] = $this->lang['enabled_arr'];

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

		$td_rows  = $this -> helper -> get_td_list( $info , $show_arr ,$key_arr ,$lang_arr ,$this -> master_act , 'trash');
		
		$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('table_trash.tpl');
		
	}


	function recycle()
	{
		
		$info_id = $infoid = $this -> input ->  get["id"];
		
		$uprec = " UPDATE `".$this -> db -> prefix.$this -> master_act."` SET inv_delete = 1  WHERE  `inv_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=".$this -> master_act."&act=info_list");
		$this -> view ->  display('suggestion.tpl');

	}
	
	//还原
	function restore( )
	{
		$infoid = $this -> input ->  delUrl($_REQUEST["id"]);
		
		$uprec = " UPDATE `".$this -> db -> prefix.$this -> master_act."` SET inv_delete = 0  WHERE  `inv_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=".$this -> master_act."&act=trash_list");
		$this -> view ->  display('suggestion.tpl');
	}
	

	function delete( )
	{
		$info_id   = $this -> input ->  get["id"];
		$insert_ip = " DELETE FROM `".$this -> db -> prefix.$this -> master_act."` WHERE `inv_id` in ( $info_id ) ";

		$this -> db -> query_sql($insert_ip);
		$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=".$this -> master_act."&act=trash_list");
		$this -> view ->  display('suggestion.tpl');
	}
	
	
	function show( )
	{
		$info_id = $this -> input ->  get["id"];
		$upshow = " UPDATE `".$this -> db -> prefix.$this -> master_act."` SET inv_educational = 1  WHERE `inv_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=".$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 inv_educational = 0  WHERE  `inv_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=".$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 inv_id = '".$id_val."' ORDER BY `inv_id` ";
			$info = $this -> db -> select_one($sql_query);


			$desc_ckeditorLoad 	= $this -> helper -> load_ckeditor( 'inv_desc' ,$info['inv_desc'],'Basic');//加载文本编辑器
			$this -> view -> assign( 'desc_ckeditorLoad', $desc_ckeditorLoad);

			$requests_ckeditorLoad 	= $this -> helper -> load_ckeditor( 'inv_requests' ,$info['inv_requests'],'Basic');//加载文本编辑器
			$this -> view -> assign( 'requests_ckeditorLoad', $requests_ckeditorLoad);

			
			$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{

			$desc_ckeditorLoad 	= $this -> helper -> load_ckeditor( 'inv_desc' ,'','Basic');//加载文本编辑器
			$this -> view -> assign( 'desc_ckeditorLoad', $desc_ckeditorLoad);

			$requests_ckeditorLoad 	= $this -> helper -> load_ckeditor( 'inv_requests' ,'','Basic');//加载文本编辑器
			$this -> view -> assign( 'requests_ckeditorLoad', $requests_ckeditorLoad);


			$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=".$this -> master_act."&act=edit_info&act_type=add");
			
			$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('invite_edit.tpl');
	
	}
	
	
	function edit_info( )
	{	
	
		$inv_id 			= $this -> input ->  funpost("inv_id");
		$inv_position 		= $this -> input ->  funpost("inv_position");
		$inv_num 			= $this -> input ->  funpost("inv_num");
		$inv_type 			= $this -> input ->  funpost("inv_type");
	    $inv_pay 			= $this -> input ->  funpost("inv_pay");
		$inv_year 			= $this -> input ->  funpost("inv_year");
		$inv_age 			= $this -> input ->  funpost("inv_age");
		$inv_educational 	= $this -> input ->  funpost("inv_educational");
		
		$inv_sex 			= $this -> input ->  funpost("inv_sex");
		$inv_marriage		= $this -> input ->  funpost("inv_marriage");
		$inv_active_time 	= $this -> input ->  funpost("inv_active_time");
		$inv_active_time 	= strtotime($inv_active_time);
		$inv_subject 		= $this -> input ->  funpost("inv_subject");
		$inv_province 		= $this -> input ->  funpost("province");
		$inv_city 			= $this -> input ->  funpost("city");
		$inv_district 		= $this -> input ->  funpost("district");
		$inv_pcd_all 		= $this -> input ->  funpost("inv_pcd_all");
		

		$inv_desc 			= html_entity_decode($_POST["inv_desc"]);
		$inv_requests 		= html_entity_decode($_POST["inv_requests"]);
		

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


		
		if($act_type=='edit')
		{

		
			if($inv_position !='' and $inv_type !='' and $inv_year !=''  and $inv_id !='')
			{
				$update = "UPDATE `".$this -> db -> prefix.$this -> master_act."` 
				SET  inv_position = '".$inv_position."' ,  inv_num = '".$inv_num."' ,inv_type = '".$inv_type."' , inv_pay = '".$inv_pay."' , 
				inv_year = '".$inv_year."', inv_age = '".$inv_age."', inv_age = '".$inv_age."',inv_educational = '".$inv_educational."',
				inv_sex = '".$inv_sex."',inv_marriage = '".$inv_marriage."',inv_active_time = '".$inv_active_time."',
				inv_subject = '".$inv_subject."',inv_province = '".$inv_province."',inv_city = '".$inv_city."',
				inv_district = '".$inv_district."',inv_pcd_all = '".$inv_pcd_all."',inv_desc = '".$inv_desc."',
				inv_requests = '".$inv_requests."'	WHERE  inv_id = '".$inv_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=".$inv_id);
				$this -> view ->  display('suggestion.tpl');	
			}
		

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


					if( $info["LNUM"] < 1 )
					{
						$inv_add_time = time();
						$insert_sql =" INSERT INTO `".$this -> db -> prefix.$this -> master_act."` (
						`inv_position` , `inv_num` , `inv_type` ,  `inv_pay` ,`inv_year` , `inv_age` ,
						`inv_educational` , `inv_sex`,`inv_marriage`,`inv_active_time`,`inv_subject`,`inv_province`,
						`inv_city`,`inv_district`,`inv_pcd_all`,`inv_desc`,`inv_requests`,`inv_add_time` ) 
						VALUES ( '$inv_position','$inv_num', '$inv_type', '$inv_pay','$inv_year','$inv_age',
						'$inv_educational', '$inv_sex','$inv_marriage','$inv_active_time','$inv_subject','$inv_province'
						,'$inv_city','$inv_district','$inv_pcd_all','$inv_desc','$inv_requests','$inv_add_time')";
						
						$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');
				}
		}
		
	}




		
}