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

		$sqlQuery = " SELECT worksort_id, worksort_name  FROM  ".SQL_PREFIX."worksort  WHERE worksort_parentid = 0  GROUP BY worksort_id ORDER BY `worksort_id` limit 1000 ";
		$asInfo = $GLOBALS['MySql'] -> selectInfo( $sqlQuery );
		
		$GLOBALS['Templ']->assign('asInfo',$asInfo);
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php/worksort/addinfo");
		$GLOBALS['Templ']->display('worksortadd_tpl.html');
		
	}
	
	function addinfo()
	{
		$worksort_nameval 		= $GLOBALS['Reque'] -> funpost("worksort_name");
		$worksort_parentidval 	= $GLOBALS['Reque'] -> funpost("worksort_parentid");
		$worksort_enabledval 	= $GLOBALS['Reque'] -> funpost("worksort_enabled");
		$worksort_listval 		= $GLOBALS['Reque'] -> funpost("worksort_list");

		if($worksort_nameval !='' and $worksort_enabledval !='' and $worksort_listval !='')
		{
			$sqlQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."worksort  WHERE worksort_name = '".$worksort_nameval."' ORDER BY `worksort_id` ";
			
			$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
			if( $info["CNUM"] < 1 )
			{

				$insertip ="INSERT INTO `".SQL_PREFIX."worksort` (`worksort_name` , `worksort_parentid` , `worksort_enabled` , `worksort_list` ) VALUES (  '$worksort_nameval', '$worksort_parentidval', '$worksort_enabledval', '$worksort_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/worksort/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/worksort/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/worksort/add/");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
				
		}

	}
	
	function del( )
	{
		$indel = implode(',', $delArray);
		$deletesql ="DELETE FROM `".SQL_PREFIX."worksort` WHERE `worksort`.`worksort_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/worksort/infolist/");
		$GLOBALS['Templ'] -> display('suggestion_tpl.html');
	}
	
	function info(  )
	{
		$infoid = $infoArray[0];
		
		$sqlQuery = " SELECT *  FROM  ".SQL_PREFIX."worksort  WHERE worksort_id = '".$infoid."' ORDER BY `worksort_id` ";
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		$sqlQuery = " SELECT worksort_id, worksort_name  FROM  ".SQL_PREFIX."worksort  WHERE worksort_parentid = 0 and worksort_id <> '".$infoid."'    GROUP BY worksort_id ORDER BY `worksort_id` limit 1000 ";
		
		$asInfo = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		$GLOBALS['Templ']->assign('asInfo',$asInfo);
		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php/worksort/editinfo");
		$GLOBALS['Templ'] -> assign('info',$info);
		$GLOBALS['Templ'] -> display('worksortedit_tpl.html');
	
	}
	
	
	function editinfo( )
	{	
	
		$worksort_idval 			= $GLOBALS['Reque'] -> funpost("worksort_id");
		$worksort_nameval 		= $GLOBALS['Reque'] -> funpost("worksort_name");
		$worksort_parentidval 	= $GLOBALS['Reque'] -> funpost("worksort_parentid");
		$worksort_enabledval 	= $GLOBALS['Reque'] -> funpost("worksort_enabled");
		$worksort_listval 		= $GLOBALS['Reque'] -> funpost("worksort_list");
		
		if($worksort_idval !='' and $worksort_nameval !='' )
		{

			$updatesql = "UPDATE `".SQL_PREFIX."worksort` SET  worksort_name = '".$worksort_nameval."' , worksort_parentid = '".$worksort_parentidval."',worksort_enabled = '".$worksort_enabledval."' , worksort_list = '".$worksort_listval."'   WHERE  worksort_id = '".$worksort_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/worksort/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/worksort/info/".$worksort_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
		}
		
	}
		
}

$Worksort = new Worksort();

?>