<?php
if (!defined('GV32_COM')) exit('GV32.COM No direct script access allowed');
class Picsort
{
	function Picsort()
	{
		//$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 pis_id,pis_name, pis_parentid, pis_enabled,pis_list FROM  ".SQL_PREFIX."picsort  WHERE 1 GROUP BY pis_id ORDER BY pis_list,`pis_id` LIMIT  $LIMITnum , $pageSize ";
		
		$infosm 		= $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		$info = array();
		foreach ($infosm as $rowkey => $rowval)
		{
				if($rowval["pis_parentid"] == 0)
				{
					$info[$rowval["pis_id"]]['pis_id'] = $rowval["pis_id"] ;
					$info[$rowval["pis_id"]]['pis_name'] = $rowval["pis_name"] ;
					$info[$rowval["pis_id"]]['pis_parentid'] = $rowval["pis_parentid"] ;
					$info[$rowval["pis_id"]]['pis_enabled'] = $rowval["pis_enabled"] ;
					$info[$rowval["pis_id"]]['pis_list'] = $rowval["pis_list"];
				}else{
					$info[$rowval["pis_parentid"]]['array'][] = $rowval;	
				}
		}
		
		$sqlQuery 	= " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."picsort  WHERE 1  ORDER BY `pis_id` ";
		$infocount  = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		$PageStr = $GLOBALS['Page']->buildPageStr( $url = '?load=picsort&act=infolist', $pageNum  , $pageSize  , $resultCount = $infocount["CNUM"] );
		
		$GLOBALS['Templ']->assign('PageStr',$PageStr);
		$GLOBALS['Templ']->assign('info',$info);
		$GLOBALS['Templ']->display('picsort_tpl.html');
	}
	
	function add(  )
	{
		//$GLOBALS['Templ'] -> caching = true;
		//$GLOBALS['Templ'] -> cache_lifetime = 86400 ;  //60 *  60 * 24
		
		$sqlQuery = " SELECT pis_id, pis_name  FROM  ".SQL_PREFIX."picsort  WHERE pis_parentid = 0  GROUP BY pis_id ORDER BY `pis_id` limit 1000 ";
		
		$asInfo = $GLOBALS['MySql'] -> selectInfo( $sqlQuery );
		
		$GLOBALS['Templ']->assign('asInfo',$asInfo);
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=picsort&act=addinfo");
		$GLOBALS['Templ']->display('picsortadd_tpl.html');
		
	}
	
	function addinfo()
	{
		$pis_nameval 		= $GLOBALS['Reque'] -> funpost("pis_name");
		$pis_parentidval 	= $GLOBALS['Reque'] -> funpost("pis_parentid");
		$pis_enabledval 	= $GLOBALS['Reque'] -> funpost("pis_enabled");
		$pis_listval 		= $GLOBALS['Reque'] -> funpost("pis_list");

		if($pis_nameval !='' and $pis_enabledval !='' and $pis_listval !='')
		{
			//同级不能同名，不同级可以同名
			$sqlQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."picsort  WHERE pis_name = '".$pis_nameval."' and pis_parentid ='".$pis_parentidval."' ORDER BY `pis_id` "; 
			$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
			if( $info["CNUM"] < 1 )
			{

				$insertip ="INSERT INTO `".SQL_PREFIX."picsort` (`pis_name` , `pis_parentid` , `pis_enabled` , `pis_list` ) VALUES (  '$pis_nameval', '$pis_parentidval', '$pis_enabledval', '$pis_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=picsort&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=picsort&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=picsort&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."picsort` WHERE `pis_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=picsort&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."picsort  WHERE pis_id = '".$infoid."' ORDER BY `pis_id` ";
		
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		//可选择的一级分类 
		$sqlQuery = " SELECT pis_id, pis_name  FROM ".SQL_PREFIX."picsort  WHERE pis_parentid = 0 and pis_id <> '".$infoid."'    GROUP BY pis_id ORDER BY `pis_id` limit 1000 ";
		$asInfo = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		$GLOBALS['Templ']->assign('asInfo',$asInfo);
		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=picsort&act=editinfo");
		$GLOBALS['Templ'] -> assign('info',$info);
		$GLOBALS['Templ'] -> display('picsortedit_tpl.html');
	
	}
	
	
	function editinfo( )
	{	
	
		$pis_idval 			= $GLOBALS['Reque'] -> funpost("pis_id");
		$pis_nameval 		= $GLOBALS['Reque'] -> funpost("pis_name");
		$pis_parentidval 	= $GLOBALS['Reque'] -> funpost("pis_parentid");
		$pis_enabledval 	= $GLOBALS['Reque'] -> funpost("pis_enabled");
		$pis_listval 		= $GLOBALS['Reque'] -> funpost("pis_list");
		
		if($pis_idval !='' and $pis_nameval !='' )
		{

			$updatesql = "UPDATE `".SQL_PREFIX."picsort` SET  pis_name = '".$pis_nameval."' , pis_parentid = '".$pis_parentidval."',pis_enabled = '".$pis_enabledval."' , pis_list = '".$pis_listval."'   WHERE  pis_id = '".$pis_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=picsort&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=picsort&act=info&id=".$pis_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
		}
		
		
	}
		
}

$Picsort = new Picsort();

?>