<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Addressbook
{
	function Addressbook()
	{

	}
	
	function info_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 ;
		}
		
		$use_idval   		= $_SESSION["emplyeeUser"]["use_id"];
			
		$sql_query = " SELECT adb_id, adb_domain, adb_desc,  adb_list   FROM  ".$this -> db -> prefix."addressbook  WHERE 1 and use_id = $use_idval GROUP BY adb_id ORDER BY `adb_id` LIMIT  $limit_num , $page_size ";
		
		$info = $this -> db -> select_info($sql_query);
		
		$sql_query = " SELECT count(1) as LNUM  FROM  ".$this -> db -> prefix."addressbook  WHERE 1 and use_id = $use_idval ORDER BY `adb_id`  ";
		
		$infocount = $this -> db -> select_one($sql_query);
		//var_dump();
		$page_str = $this -> page ->get_admin_page( $url = '?load=addressbook&act=info_list', $page_num  , $page_size  , $result_count = $infocount["LNUM"]);
		
		$this -> view -> assign('page_str',$page_str);
		
		$this -> view -> assign('info',$info);
		
		$this -> view -> display('adb.tpl');
		
	}
	
	
	
	function add(  )
	{
		
		//$this -> view ->  caching = true;
		//$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24
		
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=addressbook&act=add_info");
		$this -> view -> display('addressbookadd.tpl');
	}
	
	function add_info()
	{
		$adb_domainval 	= $this -> input ->  funpost("adb_domain");
		$adb_descval 	= $this -> input ->  funpost("adb_desc");
		$adb_listval 	= $this -> input ->  funpost("adb_list");
 		$use_idval   	= $_SESSION["emplyeeUser"]["use_id"];
		
		if($adb_domainval !='' and $adb_descval !='')
		{
			$sql_query = " SELECT count(1) as LNUM  FROM  ".$this -> db -> prefix."addressbook  WHERE adb_domain = '".$adb_domainval."' ORDER BY `adb_id` ";
			
			$info = $this -> db -> select_one($sql_query);
			
			if( $info["LNUM"] < 1 )
			{
			
				$insertip =" INSERT INTO `".$this -> db -> prefix."addressbook` (`adb_domain` , `adb_desc` , `use_id` , `adb_list`  ) VALUES (  '$adb_domainval', '$adb_descval', '$use_idval', '$adb_listval' )";

				$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=addressbook&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=addressbook&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=addressbook&act=add");
			$this -> view ->  display('suggestion.tpl');		}
			//var_dump($_REQUEST);
	}
	
	function del()
	{
		$infoid = $this -> input ->  get["id"];
		$insertip = " DELETE FROM `".$this -> db -> prefix."addressbook` WHERE `".$this -> db -> prefix."addressbook`.`adb_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=addressbook&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}
	
	
	function show( )
	{
		$infoid = $this -> input ->  get["id"];
		$upshow = " UPDATE `".$this -> db -> prefix."addressbook` SET adb_enabled = 1  WHERE `".$this -> db -> prefix."addressbook`.`adb_id` in ( $infoid ) ";

		$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=addressbook&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}
	
	function hide( )
	{
		$infoid = $this -> input ->  get["id"];
		$uphide = " UPDATE `".$this -> db -> prefix."addressbook` SET adb_enabled = 0  WHERE  `adb_id` in ( $infoid ) ";

		$this -> db -> query_sql($uphide);
		$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=addressbook&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}
	
	
	function info( )
	{
		$infoid = $this -> input ->  get["id"];
		$sql_query = " SELECT *  FROM  ".$this -> db -> prefix."addressbook  WHERE adb_id = '".$infoid."' ORDER BY `adb_id` ";
		$info = $this -> db -> select_one($sql_query);
		//var_dump($info);
		$this -> view ->  assign('info',$info);
		$this -> view ->  display('addressbookedit.tpl');
	
	}
	
	
	function edit_info( )
	{	
	
		$adb_idval 		= $this -> input ->  funpost("adb_id");
		$adb_domainval 	= $this -> input ->  funpost("adb_domain");
		$adb_descval 	= $this -> input ->  funpost("adb_desc");
		$adb_listval 	= $this -> input ->  funpost("adb_list");

		
		if($adb_domainval !='' and $adb_descval !='' and $adb_idval !='')
		{
			$updateaddressbook = "UPDATE `".$this -> db -> prefix."addressbook` SET  adb_domain = '".$adb_domainval."' , adb_desc = '".$adb_descval."' ,   adb_list = '".$adb_listval."' WHERE  adb_id = '".$adb_idval."' ";	
			$this -> db -> query_sql( $updateaddressbook );
			
			$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=addressbook&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=addressbook&act=info&id=".$adb_idval);
			$this -> view ->  display('suggestion.tpl');	
		}
		
	}
		
}

//


$Addressbook = new Addressbook();

?>