<?php
if (!defined('GV32_COM')) exit('GV32.COM No direct script access allowed');
class Image
{
	function Image()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
	}
	
	function infolist(  )
	{	
		$pageSize = 20 ;		$pageNumval 	= $GLOBALS['Reque'] -> reqval["pageNum"];
		
		if( $pageNumval > 1 )
		{
			$pageNum  = $pageNumval;
			$LIMITnum = ceil( ( $pageNumval -1 ) * $pageSize );
		}else{
			$pageNum  = 1;
			$LIMITnum = 0 ;
		}
		
		$endurl = '';
		$whereadd = "";
			
		$imagesort_idval 	= $GLOBALS['Reque'] -> funpost("imagesort_id");
		$keywordval 		= $GLOBALS['Reque'] -> funpost("keyword");
		
		if($strArray[1] !='' and $strArray[1] !='A')
		{
			$imagesort_idval 	=  $strArray[1];
		}
		
		if($imagesort_idval!='' and $imagesort_idval !='A' )
		{ 
			 $endurl 	   .= '/'.$imagesort_idval;
			 
			$sidsql  = " SELECT imagesort_id FROM imagesort WHERE imagesort_parentid = ".$imagesort_idval." or  imagesort_id = ".$imagesort_idval;
			 
		   $sortInfo 	  = $GLOBALS['MySql'] -> selectInfo( $sidsql );
			//var_dump( $sortInfo  );
			$sinid  = '';
			if($sortInfo!='')
			{
				foreach( $sortInfo as $skey => $sval )
				{
					if($skey==0)
					{
						$sinid = $sval["imagesort_id"];
					}else{
						$sinid .= ','.$sval["imagesort_id"];
					}
				}
				$whereadd .= " AND ( i.imagesort_id IN ( ".$sinid." )  OR imagesort2_id IN ( ".$sinid." ) OR imagesort3_id IN  ( ".$sinid." )  )";
			}
		 }else{ 
		 	$endurl 	   .= '/A'; 	
		 }
		
		
		if($strArray[2] !='')
		{   
			$keywordval  	= urldecode($strArray[2]); 
		}
		if($keywordval!='')
		{ 	
			$endurl 	   .= '/'.urlencode($keywordval);
			$whereadd .= " AND ( i.image_name LIKE  '%".$keywordval."%' )";  
		}
		
		$sqlQuery = " SELECT i.image_id, i.image_name,s.imagesort_name,i.image_num ,i.image_count , i.image_uptime   FROM  image as i , imagesort  as s   WHERE s.imagesort_id= i.imagesort_id $whereadd GROUP BY i.image_id ORDER BY i.`image_id` LIMIT  $LIMITnum , $pageSize ";
		
		$info 	 = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		
		$sqlQuerycount 	= " SELECT count(1) as CNUM  FROM  image  as i WHERE 1 $whereadd  ORDER BY i.`image_id`  ";
		
		$infocount 	= $GLOBALS['MySql'] -> selectOne($sqlQuerycount);
		
		$PageStr 	= $GLOBALS['Page']	-> buildPageStr( $url = '?load=image&act=infolist', $pageNum  , $pageSize  , $resultCount = $infocount["CNUM"] , $endurl );
		
		
		$GLOBALS['Templ']->assign('imagesort_idval',$imagesort_idval);
		$GLOBALS['Templ']->assign('keywordval',$keywordval);
		
		$wsInfo = $this -> getSort();
		$GLOBALS['Templ']->assign('wsInfo',$wsInfo);

		$GLOBALS['Templ']->assign('PageStr',$PageStr);
		$GLOBALS['Templ']->assign('info',$info);
		$GLOBALS['Templ']->display('image_tpl.html');
		
	}

	
	function getSort()
	{
		$sqlQuery = " SELECT imagesort_id, imagesort_name,imagesort_parentid  FROM  imagesort  WHERE 1 and imagesort_enabled = 1 GROUP BY imagesort_id ORDER BY `imagesort_list` limit 100 ";
		
		$infosm = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		
		$wsInfo = array();
		foreach ($infosm as $rowkey => $rowval)
		{
				if($rowval["imagesort_parentid"] == 0)
				{
					$wsInfo[$rowval["imagesort_id"]]['imagesort_id'] = $rowval["imagesort_id"] ;
					$wsInfo[$rowval["imagesort_id"]]['imagesort_name'] = $rowval["imagesort_name"] ;
					$wsInfo[$rowval["imagesort_id"]]['imagesort_parentid'] = $rowval["imagesort_parentid"] ;
				}else{
					$wsInfo[$rowval["imagesort_parentid"]]['array'][] = $rowval;	
				}
		}
		return $wsInfo;
		//$GLOBALS['Templ']->assign('asInfo',$asInfo);
	}



	function add(  )
	{
		$GLOBALS['Templ'] -> caching = true;
		$GLOBALS['Templ'] -> cache_lifetime = 86400 ;  //60 *  60 * 24
		
		$isInfo = $this -> getSort();
		$GLOBALS['Templ']->assign('isInfo',$isInfo);
		
		
		$GLOBALS['Helpe']-> load_ckeditor( $textName = 'image_content', $textValue = '', $toolbar = 'Full', $uiColor = '#D3D3D3', $width = '800',$height = '120','ckeditorLoad',true);

		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php/image/addinfo");
		$GLOBALS['Templ']->display('imageadd_tpl.html');
	}
	
	function additem(  )
	{
		$infoid = $infoArray[0];
		
		$sqlQuery = " SELECT *  FROM  image  WHERE image_id = '".$infoid."' ORDER BY `image_id` ";
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		$GLOBALS['Helpe']-> load_ckeditor( $textName = 'imageitem_content', $textValue = '', $toolbar = 'Basic', $uiColor = '#D3D3D3', $width = '800',$height = '120','ckeditorLoad',true);

		$GLOBALS['Templ'] -> assign('info',$info);
		
		$GLOBALS['Templ'] -> assign('infoid',$infoid);
		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php/image/additeminfo");
		$GLOBALS['Templ']->display('imageitemadd_tpl.html'); //imageitemadd_tpl.html
		
	}
	
	function itemlist(  )
	{
		
		$infoid = $infoArray[0];
		
		if($infoid!='')
		{
			$sqlQueryitem = " SELECT  imageitem_id, image_id,imageitem_name,imageitem_content,imageitem_list,imageitem_enabled  FROM  imageitem  WHERE 1  and image_id = $infoid GROUP BY imageitem_id ORDER BY `imageitem_list` limit 200 ";
			$infoitem = $GLOBALS['MySql'] -> selectInfo($sqlQueryitem);
		}
		
		$GLOBALS['Templ'] -> assign('infoid',$infoid);
		
		$GLOBALS['Templ'] -> assign('infoitem',$infoitem);

		$GLOBALS['Templ'] -> display('imageitemlist_tpl.html');
		
	}
	
	function addinfo()
	{
		
		$image_nameval 		= $GLOBALS['Reque'] -> funpost("image_name");
		$imagesort_idval 	= $GLOBALS['Reque'] -> funpost("imagesort_id");
		$image_numval 		= $GLOBALS['Reque'] -> funpost("image_num");
		$image_contentval 	= $GLOBALS['Reque'] -> funpost("image_content");

		if( $image_nameval !='' and $imagesort_idval !='' and $image_numval !='' and $image_contentval != '' )
		{
			$sqlQuery = " SELECT count(1) as CNUM  FROM  image  WHERE image_name = '".$image_nameval."' ORDER BY `image_id` ";
			$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
			if( $info["CNUM"] < 1 )
			{
				$image_uptimeval = $image_addtimeval 	= time();
				$admin_idval = $_SESSION["emplyeeUser"]["use_id"];
				
				$insertsql =" INSERT INTO `image` (`image_name` , `imagesort_id` ,  `image_num` ,`image_content`, `image_addtime`,`image_uptime`,`admin_id` ) VALUES (  '$image_nameval','$imagesort_idval', '$image_numval','$image_contentval','$image_addtimeval','$image_uptimeval','$admin_idval')";
				
				$GLOBALS['MySql'] -> querySql($insertsql);
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_addsusse']);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php/image/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/image/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/image/add/");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');		}
			//var_dump($_REQUEST);
	}
	
	function additeminfo(  )
	{
		$infoid = $infoArray[0];
		
		$imageitem_nameval 		= $GLOBALS['Reque'] -> funpost("imageitem_name");
		$image_idval 			= $infoid;
		$imageitem_imgval 		= $GLOBALS['Reque'] -> funpost("imageitem_img");
		$imageitem_contentval 	= $GLOBALS['Reque'] -> funpost("imageitem_content");
		
		$imageitem_listval 	= $GLOBALS['Reque'] -> funpost("imageitem_list");
		$imageitem_enabledval 	= $GLOBALS['Reque'] -> funpost("imageitem_enabled");

		if( $imageitem_nameval !='' and $image_idval !='' and $imageitem_imgval !='' and $imageitem_contentval != '' )
		{
			$sqlQuery = " SELECT count(1) as CNUM  FROM  imageitem  WHERE imageitem_name = '".$imageitem_nameval."' ORDER BY `imageitem_id` ";
			$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
			if( $info["CNUM"] < 1 )
			{				
				echo $insertsql =" INSERT INTO `imageitem` (`imageitem_name` , `image_id` ,  `imageitem_img`, `imageitem_content`,`imageitem_list`,`imageitem_enabled` ) VALUES (  '$imageitem_nameval','$image_idval', '$imageitem_imgval','$imageitem_contentval','$imageitem_listval','$imageitem_enabledval')";
				
				$GLOBALS['MySql'] -> querySql($insertsql);
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_addsusse']);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php/image/additem/".$infoid);
				$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/image/additem/".$infoid);
				$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/image/additem/".$infoid);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');		}
	
	}
	
	
	
	function del( )
	{
		$indel = implode(',', $delArray);
		$deletesql ="DELETE FROM `image` WHERE `image`.`image_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/image/infolist/");
		$GLOBALS['Templ'] -> display('suggestion_tpl.html');
	}
	
	function info(  )
	{
		$infoid = $infoArray[0];
		
		$sqlQuery = " SELECT *  FROM  image  WHERE image_id = '".$infoid."' ORDER BY `image_id` ";
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php/image/editinfo");
		$GLOBALS['Templ'] -> assign('info',$info);
		
		$GLOBALS['Helpe']-> load_ckeditor( $textName = 'image_content', $textValue = $info["image_content"], $toolbar = 'Full', $uiColor = '#D3D3D3', $width = '800',$height = '120','ckeditorLoad',true);
		$isInfo = $this -> getSort();
		$GLOBALS['Templ'] -> assign('isInfo',$isInfo);
		
		$GLOBALS['Templ'] -> display('imageedit_tpl.html');
	
	}
	
	
	function editinfo( )
	{	
	
		$image_idval 		= $GLOBALS['Reque'] -> funpost("image_id");
		
		$image_nameval 		= $GLOBALS['Reque'] -> funpost("image_name");
		$imagesort_idval 	= $GLOBALS['Reque'] -> funpost("imagesort_id");
		$image_numval 		= $GLOBALS['Reque'] -> funpost("image_num");
		$image_contentval 	= $GLOBALS['Reque'] -> funpost("image_content");
		

		
		if($image_idval !='' and  $image_nameval !='' and $imagesort_idval !='' and $image_numval !='' and $image_contentval != '' )
		{

			$updatesql = "UPDATE `image` SET  image_name = '".$image_nameval."' , imagesort_id = '".$imagesort_idval."' , image_num = '".$image_numval."',  image_content = '".$image_contentval."' WHERE  image_id = '".$image_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/image/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/image/info/".$image_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
		}
		
	}
		
}

//nmon_x86_rhel4

$Image = new Image();

?>