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

		$sqlQuery = " SELECT imagesort_id, imagesort_name  FROM  imagesort  WHERE imagesort_parentid = 0  GROUP BY imagesort_id ORDER BY `imagesort_id` limit 1000 ";
		$asInfo = $GLOBALS['MySql'] -> selectInfo( $sqlQuery );
		
		$GLOBALS['Templ']->assign('asInfo',$asInfo);
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php/imagesort/addinfo");
		$GLOBALS['Templ']->display('imagesortadd_tpl.html');
		
	}
	
	function addinfo()
	{
		$imagesort_nameval 		= $GLOBALS['Reque'] -> funpost("imagesort_name");
		$imagesort_parentidval 	= $GLOBALS['Reque'] -> funpost("imagesort_parentid");
		$imagesort_enabledval 	= $GLOBALS['Reque'] -> funpost("imagesort_enabled");
		$imagesort_listval 		= $GLOBALS['Reque'] -> funpost("imagesort_list");

		if($imagesort_nameval !='' and $imagesort_enabledval !='' and $imagesort_listval !='')
		{
			$sqlQuery = " SELECT count(1) as CNUM  FROM  imagesort  WHERE imagesort_name = '".$imagesort_nameval."' ORDER BY `imagesort_id` ";
			
			$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
			if( $info["CNUM"] < 1 )
			{

				$insertip ="INSERT INTO `imagesort` (`imagesort_name` , `imagesort_parentid` , `imagesort_enabled` , `imagesort_list` ) VALUES (  '$imagesort_nameval', '$imagesort_parentidval', '$imagesort_enabledval', '$imagesort_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/imagesort/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/imagesort/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/imagesort/add/");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
				
		}

	}
	
	function del( )
	{
		$indel = implode(',', $delArray);
		$deletesql ="DELETE FROM `imagesort` WHERE `imagesort`.`imagesort_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/imagesort/infolist/");
		$GLOBALS['Templ'] -> display('suggestion_tpl.html');
	}
	
	function info(  )
	{
		$infoid = $infoArray[0];
		
		$sqlQuery = " SELECT *  FROM  imagesort  WHERE imagesort_id = '".$infoid."' ORDER BY `imagesort_id` ";
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		$sqlQuery = " SELECT imagesort_id, imagesort_name  FROM  imagesort  WHERE imagesort_parentid = 0 and imagesort_id <> '".$infoid."'    GROUP BY imagesort_id ORDER BY `imagesort_id` limit 1000 ";
		
		$asInfo = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		$GLOBALS['Templ']->assign('asInfo',$asInfo);
		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php/imagesort/editinfo");
		$GLOBALS['Templ'] -> assign('info',$info);
		$GLOBALS['Templ'] -> display('imagesortedit_tpl.html');
	}
	
	
	function editinfo( )
	{	
	
		$imagesort_idval 			= $GLOBALS['Reque'] -> funpost("imagesort_id");
		$imagesort_nameval 		= $GLOBALS['Reque'] -> funpost("imagesort_name");
		$imagesort_parentidval 	= $GLOBALS['Reque'] -> funpost("imagesort_parentid");
		$imagesort_enabledval 	= $GLOBALS['Reque'] -> funpost("imagesort_enabled");
		$imagesort_listval 		= $GLOBALS['Reque'] -> funpost("imagesort_list");
		
		if($imagesort_idval !='' and $imagesort_nameval !='' )
		{

			$updatesql = "UPDATE `imagesort` SET  imagesort_name = '".$imagesort_nameval."' , imagesort_parentid = '".$imagesort_parentidval."',imagesort_enabled = '".$imagesort_enabledval."' , imagesort_list = '".$imagesort_listval."'   WHERE  imagesort_id = '".$imagesort_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/imagesort/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/imagesort/info/".$imagesort_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
		}
		
		
	}
		
}

$Imagesort = new Imagesort();

?>