<?php
if (!defined('GV32_COM')) exit('GV32.COM No direct script access allowed');
class Prosort
{
	function Prosort()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
	}
	
	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 prs_id,prs_name, prs_parentid, prs_enabled,prs_list FROM  ".SQL_PREFIX."prosort  WHERE 1 GROUP BY prs_id ORDER BY prs_list,`prs_id` LIMIT  $LIMITnum , $pageSize ";
		
		$infosm 		= $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		$info = array();
		foreach ($infosm as $rowkey => $rowval)
		{
				if($rowval["prs_parentid"] == 0)
				{
					$info[$rowval["prs_id"]]['prs_id'] = $rowval["prs_id"] ;
					$info[$rowval["prs_id"]]['prs_name'] = $rowval["prs_name"] ;
					$info[$rowval["prs_id"]]['prs_parentid'] = $rowval["prs_parentid"] ;
					$info[$rowval["prs_id"]]['prs_enabled'] = $rowval["prs_enabled"] ;
					$info[$rowval["prs_id"]]['prs_list'] = $rowval["prs_list"];
				}else{
					$info[$rowval["prs_parentid"]]['array'][] = $rowval;	
				}
		}
		
		$sqlQuery 	= " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."prosort  WHERE 1  ORDER BY `prs_id` ";
		$infocount  = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		$PageStr = $GLOBALS['Page']->buildPageStr( $url = '?load=prosort&act=infolist', $pageNum  , $pageSize  , $resultCount = $infocount["CNUM"] );
		
		$GLOBALS['Templ']->assign('PageStr',$PageStr);
		$GLOBALS['Templ']->assign('info',$info);
		$GLOBALS['Templ']->display('prosort_tpl.html');
	}
	
	function add(  )
	{
		$GLOBALS['Templ'] -> caching = true;
		$GLOBALS['Templ'] -> cache_lifetime = 86400 ;  //60 *  60 * 24

		
		$sqlQuery = " SELECT prs_id, prs_name  FROM  ".SQL_PREFIX."prosort  WHERE prs_parentid = 0  GROUP BY prs_id ORDER BY `prs_id` limit 1000 ";
		
		$asInfo = $GLOBALS['MySql'] -> selectInfo( $sqlQuery );
		
		$GLOBALS['Templ']->assign('asInfo',$asInfo);
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=prosort&act=addinfo");
		$GLOBALS['Templ']->display('prosortadd_tpl.html');
		
	}
	
	function addinfo()
	{
		$prs_nameval 		= $GLOBALS['Reque'] -> funpost("prs_name");
		$prs_parentidval 	= $GLOBALS['Reque'] -> funpost("prs_parentid");
		$prs_enabledval 	= $GLOBALS['Reque'] -> funpost("prs_enabled");
		$prs_listval 		= $GLOBALS['Reque'] -> funpost("prs_list");

		if($prs_nameval !='' and $prs_enabledval !='' and $prs_listval !='')
		{
			//同级不能同名，不同级可以同名
			$sqlQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."prosort  WHERE prs_name = '".$prs_nameval."' and prs_parentid ='".$prs_parentidval."' ORDER BY `prs_id` "; 
			$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
			if( $info["CNUM"] < 1 )
			{

				$insertip ="INSERT INTO `".SQL_PREFIX."prosort` (`prs_name` , `prs_parentid` , `prs_enabled` , `prs_list` ) VALUES (  '$prs_nameval', '$prs_parentidval', '$prs_enabledval', '$prs_listval')";
				
				$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=prosort&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=prosort&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=prosort&act=add");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
				
		}

	}
	
	function del( )
	{
		$idval = $GLOBALS['Reque'] -> getval["id"];
		
		if(!$idval){
			$indel = implode(',', $delArray);
		}else{
			$indel = $idval;
		}
		
		$deletesql ="DELETE FROM `".SQL_PREFIX."prosort` WHERE `prs_id` in ( $indel ) ";
		$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=prosort&act=infolist");
		$GLOBALS['Templ'] -> display('suggestion_tpl.html');
	}
	
	function info(  )
	{
		$idval = $GLOBALS['Reque'] -> getval["id"];
		
		if(!$idval){
			$infoid = $infoArray[0];
		}else{
			$infoid = $idval;
		}
		
		
		$sqlQuery = " SELECT *  FROM  ".SQL_PREFIX."prosort  WHERE prs_id = '".$infoid."' ORDER BY `prs_id` ";
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		//可选择的一级分类 
		$sqlQuery = " SELECT prs_id, prs_name  FROM ".SQL_PREFIX."prosort  WHERE prs_parentid = 0 and prs_id <> '".$infoid."'    GROUP BY prs_id ORDER BY `prs_id` limit 1000 ";
		$asInfo = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		$GLOBALS['Templ']->assign('asInfo',$asInfo);
		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=prosort&act=editinfo");
		$GLOBALS['Templ'] -> assign('info',$info);
		$GLOBALS['Templ'] -> display('prosortedit_tpl.html');
	
	}
	
	
	function editinfo( )
	{	
	
		$prs_idval 			= $GLOBALS['Reque'] -> funpost("prs_id");
		$prs_nameval 		= $GLOBALS['Reque'] -> funpost("prs_name");
		$prs_parentidval 	= $GLOBALS['Reque'] -> funpost("prs_parentid");
		$prs_enabledval 	= $GLOBALS['Reque'] -> funpost("prs_enabled");
		$prs_listval 		= $GLOBALS['Reque'] -> funpost("prs_list");
		
		if($prs_idval !='' and $prs_nameval !='' )
		{

			$updatesql = "UPDATE `".SQL_PREFIX."prosort` SET  prs_name = '".$prs_nameval."' , prs_parentid = '".$prs_parentidval."',prs_enabled = '".$prs_enabledval."' , prs_list = '".$prs_listval."'   WHERE  prs_id = '".$prs_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=prosort&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=prosort&act=info&id=".$prs_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
		}
		
	}
		
}

$Prosort = new Prosort();

?>