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


		$sqlQuery = " SELECT ars_id, ars_name  FROM  ".SQL_PREFIX."artsort  WHERE ars_parentid = 0  GROUP BY ars_id ORDER BY `ars_id` limit 1000 ";
		
		$asInfo = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		$GLOBALS['Templ']->assign('asInfo',$asInfo);
		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=artsort&act=addinfo");
		$GLOBALS['Templ']->display('artsortadd_tpl.html');
	}
	
	function addinfo()
	{
		$ars_nameval 		= $GLOBALS['Reque'] -> funpost("ars_name");
		$ars_parentidval 	= $GLOBALS['Reque'] -> funpost("ars_parentid");
		$ars_enabledval 	= $GLOBALS['Reque'] -> funpost("ars_enabled");
		$ars_lockedval 		= $GLOBALS['Reque'] -> funpost("ars_locked");
		$ars_listval 		= $GLOBALS['Reque'] -> funpost("ars_list");
		
		//$ars_addtimeval 	= time();
		
		if($ars_nameval !='' and $ars_enabledval !='' and $ars_listval !='')
		{
			$sqlQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."artsort  WHERE ars_name = '".$ars_nameval."' ORDER BY `ars_id` ";
			$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
			if( $info["CNUM"] < 1 )
			{

				$insertip ="INSERT INTO `".SQL_PREFIX."artsort` (`ars_name` , `ars_parentid` , `ars_enabled` ,  `ars_locked` ,`ars_list` ) VALUES (  '$ars_nameval', '$ars_parentidval', '$ars_enabledval','$ars_lockedval', '$ars_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=artsort&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=artsort&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=artsort&act=add/");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');		
		}

	}
	
	function del( )
	{
		$infoid = $GLOBALS['Reque'] -> getval["id"];
		
		//存在分类信息不能删除
		
		///SELECT count( 1 ) AS CNUM FROM artlice WHERE ars_id  IN ( 4 ) ORDER BY ars_id
		
		$sqlQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."artlice  WHERE ars_id in ( $infoid )  ORDER BY ars_id  ";
		
		$infoOne = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		if( $infoOne["CNUM"] < 1 )
		{
		
			$sqllock = " SELECT ars_id ,ars_locked  FROM  ".SQL_PREFIX."artsort  WHERE ars_id in ( $infoid )  ORDER BY ars_id  ";
		
			$infoLock = $GLOBALS['MySql'] -> selectOne($sqllock);
		  
		  
		   if( $infoLock["ars_locked"] == 0 )
		   {
			
				$deletesql ="DELETE FROM `".SQL_PREFIX."artsort` WHERE `ars_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=artsort&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=artsort&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=artsort&act=infolist");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
		}
		
	}
	
	function info(  )
	{
		$infoid = $GLOBALS['Reque'] -> getval["id"];
		
		$sqlQuery = " SELECT *  FROM  ".SQL_PREFIX."artsort  WHERE ars_id = '".$infoid."' ORDER BY `ars_id` ";
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		$sqlQuery = " SELECT ars_id, ars_name  FROM  ".SQL_PREFIX."artsort  WHERE ars_parentid = 0 and ars_id <> '".$infoid."'    GROUP BY ars_id ORDER BY `ars_id` limit 1000 ";
		
		$asInfo = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		$GLOBALS['Templ']->assign('asInfo',$asInfo);
		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=artsort&act=editinfo");
		$GLOBALS['Templ'] -> assign('info',$info);
		$GLOBALS['Templ'] -> display('artsortedit_tpl.html');
	
	}
	
	
	function editinfo( )
	{	
	
		$ars_idval 			= $GLOBALS['Reque'] -> funpost("ars_id");
		$ars_nameval 		= $GLOBALS['Reque'] -> funpost("ars_name");
		$ars_parentidval 	= $GLOBALS['Reque'] -> funpost("ars_parentid");
		$ars_enabledval 	= $GLOBALS['Reque'] -> funpost("ars_enabled");
		$ars_lockedval 		= $GLOBALS['Reque'] -> funpost("ars_locked");
		$ars_listval 		= $GLOBALS['Reque'] -> funpost("ars_list");
		
		if($ars_idval !='' and $ars_nameval !='' )
		{

			$updatesql = "UPDATE `".SQL_PREFIX."artsort` SET  ars_name = '".$ars_nameval."' , ars_parentid = '".$ars_parentidval."',ars_enabled = '".$ars_enabledval."' ,ars_locked = '".$ars_lockedval."', ars_list = '".$ars_listval."'   WHERE  ars_id = '".$ars_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=artsort&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=artsort&act=info&id=".$ars_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
		}
		
	}
		
}

//nmon_x86_rhel4

$Artsort = new Artsort();

?>