<?php
if (!defined('GV32_COM')) exit('GV32.COM No direct script access allowed');
class Vidsort
{
	function Vidsort()
	{
		//$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 vis_id,vis_name, vis_parentid, vis_enabled,vis_list FROM  ".SQL_PREFIX."vidsort  WHERE 1 GROUP BY vis_id ORDER BY vis_list,`vis_id` LIMIT  $LIMITnum , $pageSize ";
		
		$infosm 		= $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		$info = array();
		foreach ($infosm as $rowkey => $rowval)
		{
				if($rowval["vis_parentid"] == 0)
				{
					$info[$rowval["vis_id"]]['vis_id'] = $rowval["vis_id"] ;
					$info[$rowval["vis_id"]]['vis_name'] = $rowval["vis_name"] ;
					$info[$rowval["vis_id"]]['vis_parentid'] = $rowval["vis_parentid"] ;
					$info[$rowval["vis_id"]]['vis_enabled'] = $rowval["vis_enabled"] ;
					$info[$rowval["vis_id"]]['vis_list'] = $rowval["vis_list"];
				}else{
					$info[$rowval["vis_parentid"]]['array'][] = $rowval;	
				}
		}
		
		$sqlQuery 	= " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."vidsort  WHERE 1  ORDER BY `vis_id` ";
		$infocount  = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		$PageStr = $GLOBALS['Page']->buildPageStr( $url = '?load=vidsort&act=infolist', $pageNum  , $pageSize  , $resultCount = $infocount["CNUM"] );
		
		$GLOBALS['Templ']->assign('PageStr',$PageStr);
		$GLOBALS['Templ']->assign('info',$info);
		$GLOBALS['Templ']->display('vidsort_tpl.html');
	}
	
	function add(  )
	{
		//$GLOBALS['Templ'] -> caching = true;
		//$GLOBALS['Templ'] -> cache_lifetime = 86400 ;  //60 *  60 * 24
		
		$sqlQuery = " SELECT vis_id, vis_name  FROM  ".SQL_PREFIX."vidsort  WHERE vis_parentid = 0  GROUP BY vis_id ORDER BY `vis_id` limit 1000 ";
		
		$asInfo = $GLOBALS['MySql'] -> selectInfo( $sqlQuery );
		
		$GLOBALS['Templ']->assign('asInfo',$asInfo);
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=vidsort&act=addinfo");
		$GLOBALS['Templ']->display('vidsortadd_tpl.html');
		
	}
	
	function addinfo()
	{
		$vis_nameval 		= $GLOBALS['Reque'] -> funpost("vis_name");
		$vis_parentidval 	= $GLOBALS['Reque'] -> funpost("vis_parentid");
		$vis_enabledval 	= $GLOBALS['Reque'] -> funpost("vis_enabled");
		$vis_listval 		= $GLOBALS['Reque'] -> funpost("vis_list");

		if($vis_nameval !='' and $vis_enabledval !='' and $vis_listval !='')
		{
			//同级不能同名，不同级可以同名
			$sqlQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."vidsort  WHERE vis_name = '".$vis_nameval."' and vis_parentid ='".$vis_parentidval."' ORDER BY `vis_id` "; 
			$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
			if( $info["CNUM"] < 1 )
			{

				$insertip ="INSERT INTO `".SQL_PREFIX."vidsort` (`vis_name` , `vis_parentid` , `vis_enabled` , `vis_list` ) VALUES (  '$vis_nameval', '$vis_parentidval', '$vis_enabledval', '$vis_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=vidsort&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=vidsort&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=vidsort&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."vidsort` WHERE `vis_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=vidsort&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."vidsort  WHERE vis_id = '".$infoid."' ORDER BY `vis_id` ";
		
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		//可选择的一级分类 
		$sqlQuery = " SELECT vis_id, vis_name  FROM ".SQL_PREFIX."vidsort  WHERE vis_parentid = 0 and vis_id <> '".$infoid."'    GROUP BY vis_id ORDER BY `vis_id` limit 1000 ";
		$asInfo = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		$GLOBALS['Templ']->assign('asInfo',$asInfo);
		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=vidsort&act=editinfo");
		$GLOBALS['Templ'] -> assign('info',$info);
		$GLOBALS['Templ'] -> display('vidsortedit_tpl.html');
	
	}
	
	
	function editinfo( )
	{	
	
		$vis_idval 			= $GLOBALS['Reque'] -> funpost("vis_id");
		$vis_nameval 		= $GLOBALS['Reque'] -> funpost("vis_name");
		$vis_parentidval 	= $GLOBALS['Reque'] -> funpost("vis_parentid");
		$vis_enabledval 	= $GLOBALS['Reque'] -> funpost("vis_enabled");
		$vis_listval 		= $GLOBALS['Reque'] -> funpost("vis_list");
		
		if($vis_idval !='' and $vis_nameval !='' )
		{

			$updatesql = "UPDATE `".SQL_PREFIX."vidsort` SET  vis_name = '".$vis_nameval."' , vis_parentid = '".$vis_parentidval."',vis_enabled = '".$vis_enabledval."' , vis_list = '".$vis_listval."'   WHERE  vis_id = '".$vis_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=vidsort&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=vidsort&act=info&id=".$vis_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
		}
		
		
	}
		
}

$Vidsort = new Vidsort();

?>