<?php
if (!defined('GV32_COM')) exit('GV32.COM No direct script access allowed');
class Dept
{
	function Dept()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
		//$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php/login/actlogin");
	}
	
	function infolist(  )
	{
		$pageSize = 200 ;		$pageNumval 	= $GLOBALS['Reque'] -> reqval["pageNum"];
		
		if( $pageNumval > 1 )
		{
			$pageNum  = $pageNumval;
			$LIMITnum = ceil( ( $pageNumval -1 ) * $pageSize );
		}else{
			$pageNum  = 1;
			$LIMITnum = 0 ;
		}
		
		$sqlQuery = " SELECT dep_id,dep_name, dep_parentid, dep_enabled, dep_locked, dep_list  FROM  ".SQL_PREFIX."dept  WHERE 1 GROUP BY dep_id ORDER BY dep_list,`dep_id` LIMIT  $LIMITnum , $pageSize ";
		
		$infosm 		= $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		$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;	
				}
		   }
		
		
		
		$sqlQuery 	= " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."dept  WHERE 1  ORDER BY `dep_id` ";
		$infocount  = $GLOBALS['MySql'] -> selectOne($sqlQuery);
	
		$PageStr = $GLOBALS['Page']->buildPageStr( $url = '?load=dept&act=infolist', $pageNum  , $pageSize  , $resultCount = $infocount["CNUM"] );
		
		$GLOBALS['Templ']->assign('PageStr',$PageStr);
		$GLOBALS['Templ']->assign('info',$info);
		$GLOBALS['Templ']->display('dept_tpl.html');
		
	}
	
	function add(  )
	{
		//$GLOBALS['Templ'] -> caching = true;
		//$GLOBALS['Templ'] -> cache_lifetime = 86400 ;  //60 *  60 * 24

		
		$sqlQuery = " SELECT dep_id, dep_name  FROM  ".SQL_PREFIX."dept  WHERE dep_parentid = 0  GROUP BY dep_id ORDER BY `dep_id` limit 1000 ";
		
		$deInfo = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
	
		$GLOBALS['Templ']->assign('deInfo',$deInfo);
		
		$GLOBALS['Helpe'] -> load_ckeditor( $textName = 'dep_remark','','Basic' );	//加载文本编辑器
		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=dept&act=addinfo");
		$GLOBALS['Templ']->display('deptadd_tpl.html');
	}
	
	function addinfo()
	{
		$dep_nameval 		= $GLOBALS['Reque'] -> funpost("dep_name");
		$dep_parentidval 	= $GLOBALS['Reque'] -> funpost("dep_parentid");
		$dep_enabledval 	= $GLOBALS['Reque'] -> funpost("dep_enabled");
		$dep_lockedval 		= $GLOBALS['Reque'] -> funpost("dep_locked");
		$dep_listval 		= $GLOBALS['Reque'] -> funpost("dep_list");
		
		$dep_exttelval 		= $GLOBALS['Reque'] -> funpost("dep_exttel");
		$dep_remarkval 		= $GLOBALS['Reque'] -> funpost("dep_remark");
		
		
		if($dep_nameval !='' and $dep_enabledval !='' and $dep_listval !='')
		{
			$sqlQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."dept  WHERE dep_name = '".$dep_nameval."' ORDER BY `dep_id` ";
			
			$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
			if( $info["CNUM"] < 1 )
			{

				$insertip ="INSERT INTO `".SQL_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')";
				
				$GLOBALS['MySql'] -> querySql($insertip);
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_addsusse']);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=dept&act=infolist");
				$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
			}else{
				
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_isrealyerror']);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=dept&act=add");
				$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
			}
			
		}else{
			
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_losssusse'] );
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=dept&act=add");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');		
		}

	}
	
	function del( )
	{
		$infoid = $GLOBALS['Reque'] -> getval["id"];
		
		//存在分类信息不能删除
		
		///SELECT count( 1 ) AS CNUM FROM artlice WHERE dep_id  IN ( 4 ) ORDER BY dep_id
		
		$sqlQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."artlice  WHERE dep_id in ( $infoid )  ORDER BY dep_id  ";
		
		$infoOne = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		if( $infoOne["CNUM"] < 1 )
		{
		
			$sqllock = " SELECT dep_id ,dep_locked  FROM  ".SQL_PREFIX."dept  WHERE dep_id in ( $infoid )  ORDER BY dep_id  ";
		
			$infoLock = $GLOBALS['MySql'] -> selectOne($sqllock);
		  
		  
		   if( $infoLock["dep_locked"] == 0 )
		   {
			
				$deletesql ="DELETE FROM `".SQL_PREFIX."dept` WHERE `dep_id` in ( $infoid ) ";
				$GLOBALS['MySql'] -> querySql($deletesql);
				
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_delsusse']);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=dept&act=infolist");
				$GLOBALS['Templ'] -> display('suggestion_tpl.html');
				
			}else{
			
				//被锁定不能删除
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_lockerror'] );
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=dept&act=infolist");
				$GLOBALS['Templ'] -> display('suggestion_tpl.html');

			}
			
		}else{
		
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_delerror']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=dept&act=infolist");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
		}
		
	}
	
	function info(  )
	{
		$infoid = $GLOBALS['Reque'] -> getval["id"];
		
		$sqlQuery = " SELECT *  FROM  ".SQL_PREFIX."dept  WHERE dep_id = '".$infoid."' ORDER BY `dep_id` ";
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		$sqlQuery = " SELECT dep_id, dep_name  FROM  ".SQL_PREFIX."dept  WHERE dep_parentid = 0 and dep_id <> '".$infoid."'    GROUP BY dep_id ORDER BY `dep_id` limit 1000 ";
		
		$asInfo = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		$GLOBALS['Templ']->assign('asInfo',$asInfo);
		
		$GLOBALS['Helpe'] -> load_ckeditor( $textName = 'dep_remark',$info["dep_remark"],'Basic' );	//加载文本编辑器
		
		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=dept&act=editinfo");
		$GLOBALS['Templ'] -> assign('info',$info);
		$GLOBALS['Templ'] -> display('deptedit_tpl.html');
	
	}
	
	
	function editinfo( )
	{	
	
		$dep_idval 			= $GLOBALS['Reque'] -> funpost("dep_id");
		$dep_nameval 		= $GLOBALS['Reque'] -> funpost("dep_name");
		$dep_parentidval 	= $GLOBALS['Reque'] -> funpost("dep_parentid");
		$dep_enabledval 	= $GLOBALS['Reque'] -> funpost("dep_enabled");
		$dep_lockedval 		= $GLOBALS['Reque'] -> funpost("dep_locked");
		$dep_listval 		= $GLOBALS['Reque'] -> funpost("dep_list");
		
		$dep_exttelval 		= $GLOBALS['Reque'] -> funpost("dep_exttel");
		$dep_remarkval 		= $GLOBALS['Reque'] -> funpost("dep_remark");
		
		if($dep_idval !='' and $dep_nameval !='' )
		{

			$updatesql = "UPDATE `".SQL_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."' ";
			
			$GLOBALS['MySql'] -> querySql( $updatesql );
			
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_editsusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=dept&act=infolist");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
				
		}else{
		
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_losssusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=dept&act=info&id=".$dep_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
		}
		
	}
		
}

//nmon_x86_rhel4

$Dept = new Dept();

?>