<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Dept
{
	function Dept()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
		//$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?/login/actlogin");
	}
	
	function info_list(  )
	{
		$page_size = 200 ;		$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 dep_id,dep_name, dep_parentid, dep_enabled, dep_locked, dep_list  FROM  ".$this -> db -> prefix."dept  WHERE 1 GROUP BY dep_id ORDER BY dep_list,`dep_id` LIMIT  $limit_num , $page_size ";
		
		$infosm 		= $this -> db -> select_info($sql_query);
		$info = array();
		   foreach ($infosm as $rowkey => $rowval)
		   {
		   		if($rowval["dep_parentid"] == 0)
				{
				
					$info[$rowval["dep_id"]]['dep_id'] 		 = $rowval["dep_id"] ;
					$info[$rowval["dep_id"]]['dep_name'] 	 = $rowval["dep_name"] ;
					$info[$rowval["dep_id"]]['dep_parentid'] = $rowval["dep_parentid"] ;
					$info[$rowval["dep_id"]]['dep_enabled']  = $rowval["dep_enabled"] ;
					$info[$rowval["dep_id"]]['dep_locked']   = $rowval["dep_locked"] ;
					$info[$rowval["dep_id"]]['dep_list'] 	 = $rowval["dep_list"] ;
				}else{
					$info[$rowval["dep_parentid"]]['array'][] = $rowval;	
				}
		   }
		
		
		
		$sql_query 	= " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."dept  WHERE 1  ORDER BY `dep_id` ";
		$infocount  = $this -> db -> select_one($sql_query);
	
		$page_str = $this -> page ->get_admin_page( $url = '?load=dept&act=info_list', $page_num  , $page_size  , $result_count = $infocount["CNUM"] );
		
		$this -> view -> assign('page_str',$page_str);
		$this -> view -> assign('info',$info);
		$this -> view -> display('dept.tpl');
		
	}
	
	function add(  )
	{
		//$this -> view ->  caching = true;
		//$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24

		
		$sql_query = " SELECT dep_id, dep_name  FROM  ".$this -> db -> prefix."dept  WHERE dep_parentid = 0  GROUP BY dep_id ORDER BY `dep_id` limit 1000 ";
		
		$deInfo = $this -> db -> select_info($sql_query);
	
		$this -> view -> assign('deInfo',$deInfo);
		
		$this -> helper -> load_ckeditor( 'dep_remark','','Basic' );	//加载文本编辑器
		
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=dept&act=add_info");
		$this -> view -> display('deptadd.tpl');
	}
	
	function add_info()
	{
		$dep_nameval 		= $this -> input ->  funpost("dep_name");
		$dep_parentidval 	= $this -> input ->  funpost("dep_parentid");
		$dep_enabledval 	= $this -> input ->  funpost("dep_enabled");
		$dep_lockedval 		= $this -> input ->  funpost("dep_locked");
		$dep_listval 		= $this -> input ->  funpost("dep_list");
		
		$dep_exttelval 		= $this -> input ->  funpost("dep_exttel");
		$dep_remarkval 		= $this -> input ->  funpost("dep_remark");
		
		
		if($dep_nameval !='' and $dep_enabledval !='' and $dep_listval !='')
		{
			$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."dept  WHERE dep_name = '".$dep_nameval."' ORDER BY `dep_id` ";
			
			$info = $this -> db -> select_one($sql_query);
			
			if( $info["CNUM"] < 1 )
			{

				$insertip ="INSERT INTO `".$this -> db -> prefix."dept` (`dep_name` , `dep_parentid` , `dep_enabled` ,  `dep_locked` ,`dep_list`,`dep_exttel`,`dep_remark` ) VALUES (  '$dep_nameval', '$dep_parentidval', '$dep_enabledval','$dep_lockedval', '$dep_listval','$dep_exttelval','$dep_remarkval')";
				
				$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=dept&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=dept&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=dept&act=add");
			$this -> view ->  display('suggestion.tpl');		
		}

	}
	
	function del( )
	{
		$infoid = $this -> input ->  get["id"];
		
		//存在分类信息不能删除
		
		///SELECT count( 1 ) AS CNUM FROM article WHERE dep_id  IN ( 4 ) ORDER BY dep_id
		
		$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."article  WHERE dep_id in ( $infoid )  ORDER BY dep_id  ";
		
		$infoOne = $this -> db -> select_one($sql_query);
		
		if( $infoOne["CNUM"] < 1 )
		{
		
			$sqllock = " SELECT dep_id ,dep_locked  FROM  ".$this -> db -> prefix."dept  WHERE dep_id in ( $infoid )  ORDER BY dep_id  ";
		
			$infoLock = $this -> db -> select_one($sqllock);
		  
		  
		   if( $infoLock["dep_locked"] == 0 )
		   {
			
				$deletesql ="DELETE FROM `".$this -> db -> prefix."dept` WHERE `dep_id` in ( $infoid ) ";
				$this -> db -> query_sql($deletesql);
				
				$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=dept&act=info_list");
				$this -> view ->  display('suggestion.tpl');
				
			}else{
			
				//被锁定不能删除
				$this -> view ->  assign('info_title',$_LANG['bd_lockerror'] );
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=dept&act=info_list");
				$this -> view ->  display('suggestion.tpl');

			}
			
		}else{
		
			$this -> view ->  assign('info_title',$_LANG['bd_delerror']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=dept&act=info_list");
			$this -> view ->  display('suggestion.tpl');
		}
		
	}
	
	function info(  )
	{
		$infoid = $this -> input ->  get["id"];
		
		$sql_query = " SELECT *  FROM  ".$this -> db -> prefix."dept  WHERE dep_id = '".$infoid."' ORDER BY `dep_id` ";
		$info = $this -> db -> select_one($sql_query);
		
		$sql_query = " SELECT dep_id, dep_name  FROM  ".$this -> db -> prefix."dept  WHERE dep_parentid = 0 and dep_id <> '".$infoid."'    GROUP BY dep_id ORDER BY `dep_id` limit 1000 ";
		
		$asInfo = $this -> db -> select_info($sql_query);
		$this -> view -> assign('asInfo',$asInfo);
		
		$this -> helper -> load_ckeditor( 'dep_remark',$info["dep_remark"],'Basic' );	//加载文本编辑器
		
		
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=dept&act=edit_info");
		$this -> view ->  assign('info',$info);
		$this -> view ->  display('deptedit.tpl');
	
	}
	
	
	function edit_info( )
	{	
	
		$dep_idval 			= $this -> input ->  funpost("dep_id");
		$dep_nameval 		= $this -> input ->  funpost("dep_name");
		$dep_parentidval 	= $this -> input ->  funpost("dep_parentid");
		$dep_enabledval 	= $this -> input ->  funpost("dep_enabled");
		$dep_lockedval 		= $this -> input ->  funpost("dep_locked");
		$dep_listval 		= $this -> input ->  funpost("dep_list");
		
		$dep_exttelval 		= $this -> input ->  funpost("dep_exttel");
		$dep_remarkval 		= $this -> input ->  funpost("dep_remark");
		
		if($dep_idval !='' and $dep_nameval !='' )
		{

			$updatesql = "UPDATE `".$this -> db -> prefix."dept` SET  dep_name = '".$dep_nameval."' , dep_parentid = '".$dep_parentidval."',dep_enabled = '".$dep_enabledval."' ,dep_locked = '".$dep_lockedval."', dep_list = '".$dep_listval."' ,dep_exttel = '".$dep_exttelval."' ,dep_remark = '".$dep_remarkval."' WHERE  dep_id = '".$dep_idval."' ";
			
			$this -> db -> query_sql( $updatesql );
			
			$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=dept&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=dept&act=info&id=".$dep_idval);
			$this -> view ->  display('suggestion.tpl');
		}
		
	}
		
}

//

$Dept = new Dept();

?>