<?php
if (!defined('GV32_COM')) exit('GV32.COM No direct script access allowed');
class Picture
{
	function Picture()
	{
	}
	
	function infolist()
	{
		$pageSize = 20 ;
		
		$keywordval 	= $GLOBALS['Reque'] -> reqval["keyword"];
		$pis_idval 		= $GLOBALS['Reque'] -> funpost("pis_id");
		$pageNumval 	= $GLOBALS['Reque'] -> getval["pageNum"];
		
		if( $pageNumval > 1 )
		{
			$pageNum  = $pageNumval;
			$LIMITnum = ceil( ( $pageNumval -1 ) * $pageSize );
		}else{
			$pageNum  = 1;
			$LIMITnum = 0 ;
		}

		
		if($pis_idval !='' and $pis_idval !='A')
		{
			$pis_idval 	= $pis_idval;
		}else{
			$pis_idval = $GLOBALS['Reque'] -> getval["pis_id"];
		}
		
		if($pis_idval!='' and $pis_idval !='A' )
		{ 
			 
			 $endArray["pis_id"] = $pis_idval;
			 
			 $endurl 	  		.= '&prs='.$pis_idval;
			 
			 $sidsql  = " SELECT pis_id FROM ".SQL_PREFIX."picsort WHERE pis_parentid = ".$pis_idval." or  pis_id = ".$pis_idval;
			 
		     $sortInfo 	  = $GLOBALS['MySql'] -> selectInfo( $sidsql );
			 
			 //var_dump( $sortInfo  );
			 $sinid  = '';
			if($sortInfo!='')
			{
				foreach( $sortInfo as $skey => $sval )
				{
					if($skey==0)
					{
						$sinid = $sval["pis_id"];
					}else{
						$sinid .= ','.$sval["pis_id"];
					}
				}
				$whereadd .= " AND ( p.pis_id IN ( ".$sinid." ) )";
			}
		 }else{ 
		 	$endurl 	   .= '&prs=A'; 	
		 }
		
		
		if($GLOBALS['Reque'] -> reqval["keyword"] !='')
		{   
			$keywordval 	= $GLOBALS['Reque'] -> reqval["keyword"];
			$keywordval  	= urldecode($keywordval); 
		}
		if($keywordval!='')
		{ 	
			$endArray["keyword"] =$keywordval;
			$endurl 	   .= '&keyword='.urlencode($keywordval);
			$whereadd .= " AND ( p.pic_name LIKE  '%".$keywordval."%' )";  
		}

		$listval  = $GLOBALS['Reque'] -> getval["listval"];
		$listtype = $GLOBALS['Reque'] -> getval["listtype"];

		$orderbyval = "";
		if($listval!='' and $listtype =='DESC' )
		{
			$endArray["listtype"] = 'DESC';
			$endArray["listval"]  = $listval;
			$orderbyval = $listval ."  DESC ";
			$listhtml = '';	//反向排序参数
			
		}elseif($listval!='' )
		{
			$orderbyval = $listval;
			$listhtml = 'DESC';
			
			$endArray["listtype"] = '';
			$endArray["listval"]  = $listval;

		}else{
			$endArray["listtype"] = 'DESC';
			$endArray["listval"]  = 'pic_uptime';
			$orderbyval = " p.pic_uptime DESC , `pic_id` DESC ";	
		}
		
 
		$sqlQuery = " SELECT p.pic_id, p.pic_name,p.pis_id,p.pic_count, p.pic_enabled , p.pic_uptime   FROM  ".SQL_PREFIX."picture as p    WHERE  1 $whereadd  GROUP BY p.pic_id ORDER BY $orderbyval LIMIT  $LIMITnum , $pageSize ";
		
		$info 		= $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		
		$sqlQuery 	= " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."picture as p  WHERE 1 $whereadd  ORDER BY p.`pic_id`  ";
		
		$infocount 	= $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		//var_dump($info);
		
		$PageStr = $GLOBALS['Page']->buildPageStr( $url = '?load=picture&act=infolist', $pageNum  , $pageSize  , $resultCount = $infocount["CNUM"] , $endArray );
		
		$GLOBALS['Templ']->assign('listhtml',$listhtml);
		$GLOBALS['Templ']->assign('listval',$listval);
		$GLOBALS['Templ']->assign('listtype',$listtype);

		
		
		$GLOBALS['Templ']->assign('pis_idval',$pis_idval);
		$GLOBALS['Templ']->assign('keywordval',$keywordval);
		
		$this -> getSort();
		
		
		$GLOBALS['Templ']->assign('PageStr',$PageStr);
		$GLOBALS['Templ']->assign('info',$info);
		

		$GLOBALS['Templ']->display('picture_tpl.html');
		
	}
	
	function getSort()
	{
		$sqlQuery = " SELECT pis_id, pis_name,pis_parentid  FROM  ".SQL_PREFIX."picsort  WHERE 1 and pis_enabled = 1 GROUP BY pis_id ORDER BY `pis_list` limit 1000 ";
		
		$infosm = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		
		$psInfo = array();
		foreach ($infosm as $rowkey => $rowval)
		{
				if($rowval["pis_parentid"] == 0)
				{
					$psInfo[$rowval["pis_id"]]['pis_id'] = $rowval["pis_id"] ;
					$psInfo[$rowval["pis_id"]]['pis_name'] = $rowval["pis_name"] ;
					$psInfo[$rowval["pis_id"]]['pis_parentid'] = $rowval["pis_parentid"] ;
				}else{
					$psInfo[$rowval["pis_parentid"]]['array'][] = $rowval;	
				}
				$psInfoval[$rowval["pis_id"]] = $rowval["pis_name"] ;
		}
		
		$GLOBALS['Templ']->assign('psInfoval',$psInfoval);		//列排表显示使用
		
		$GLOBALS['Templ']->assign('psInfo',$psInfo);		
		//return $psInfo;
	}
	
	
	function add()
	{

		$this -> getSort();
				
		
		$GLOBALS['Helpe'] -> load_ckeditor( $textName = 'pic_content' ,'','Basic' );	//加载文本编辑器

		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=picture&act=addinfo");
		$GLOBALS['Templ']->display('pictureadd_tpl.html');
		
	}
	
	function addinfo()
	{
		
		$pic_nameval 	= $GLOBALS['Reque'] -> funpost("pic_name");
		$pis_idval 		= $GLOBALS['Reque'] -> funpost("pis_id");
		$pic_pinyinval	= $GLOBALS['Reque'] -> funpost("pic_pinyin");
		$pic_urlval 	= $GLOBALS['Reque'] -> funpost("pic_url");
		$pic_contentval = $_POST["pic_content"];
		$pic_enabledval = $GLOBALS['Reque'] -> funpost("pic_enabled");

		
		if( $pic_nameval !='' and $pis_idval !='' and  $pic_urlval != '' )
		{
			$sqlQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."picture  WHERE pic_name = '".$pic_nameval."' ORDER BY `pic_id` ";
			
			$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
			if( $info["CNUM"] < 1 )
			{
				$pic_uptimeval = $pic_addtimeval 	= time();

				///$use_idval			= $_SESSION["emplyeeUser"]["use_id"];
				
				//图片应该一主图，任意添加多少

				$insertpro =" INSERT INTO `".SQL_PREFIX."picture` (`pic_name` , `pic_pinyin` , `pis_id` , `pic_url` , `pic_content` , `pic_enabled` ,pic_uptime, pic_addtime) VALUES (  '$pic_nameval', '$pic_pinyinval',  '$pis_idval', '$pic_urlval', '$pic_contentval', '$pic_enabledval','$pic_uptimeval', '$pic_addtimeval')";

				$proinsertId = $GLOBALS['MySql'] -> insertSql($insertpro);

				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_addsusse']);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=picture&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=picture&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=picture&act=add");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');		}
			//var_dump($_REQUEST);
	}
	
	function additem()
	{
		
		$pic_idval		= $GLOBALS['Reque'] -> funpost("pic_id");
		$pii_urlval 	= $GLOBALS['Reque'] -> funpost("pii_url");
		$pii_contentval = $_POST["pii_content"];
		$pii_enabledval = $GLOBALS['Reque'] -> funpost("pii_enabled");

		if( $pic_idval !='' and $pii_urlval !='')
		{
		
				$pii_uptimeval = $pii_addtimeval 	= time();

				$insertpro =" INSERT INTO `".SQL_PREFIX."picitem` (`pic_id`,`pii_url` , `pii_content` , `pii_enabled` , pii_addtime ,pii_uptime) VALUES (  '$pic_idval',  '$pii_urlval', '$pii_contentval',  '$pii_enabledval', '$pii_addtimeval','$pii_uptimeval')";

				$proinsertId = $GLOBALS['MySql'] -> insertSql($insertpro);

				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_addsusse']);
				
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=picture&act=piclist&id=".$pic_idval);
				$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=picture&act=piclist&id=".$pic_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');		
		}
			//var_dump($_REQUEST);
	}
	
	
	function del()
	{
		
		$infoid = $GLOBALS['Reque'] -> getval["id"];
		
		$deletesql ="DELETE FROM `".SQL_PREFIX."picture` WHERE `".SQL_PREFIX."picture`.`pic_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=picture&act=infolist");
		$GLOBALS['Templ'] -> display('suggestion_tpl.html');
		
		
	}
	
	function delitem()
	{
		
		$infoid = $GLOBALS['Reque'] -> getval["id"];
		
		$pic_idval = $GLOBALS['Reque'] -> getval["pic_id"];
		
		$deletesql ="DELETE FROM `".SQL_PREFIX."picitem` WHERE `".SQL_PREFIX."picitem`.`pii_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=picture&act=piclist&id=".$pic_idval);
		$GLOBALS['Templ'] -> display('suggestion_tpl.html');
		
		
	}
	
	
	function info()
	{
		//$infoid = $infoArray[0];
		
		$idval = $GLOBALS['Reque'] -> getval["id"];

		$sqlQuery = " SELECT *  FROM  ".SQL_PREFIX."picture  WHERE pic_id = '".$idval."' ORDER BY `pic_id` ";
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=picture&act=editinfo");
		$GLOBALS['Templ'] -> assign('info',$info);
		
		$this -> getSort();
		
		$GLOBALS['Helpe'] -> load_ckeditor( $textName = 'pic_content' ,$textValue = $info["pic_content"] ,'Basic' );	//加载文本编辑器
		
		
		$GLOBALS['Templ'] -> display('pictureedit_tpl.html');
	
	}
	
	function infoitem()
	{
		//$infoid = $infoArray[0];
		
		$idval = $GLOBALS['Reque'] -> getval["id"];

		$sqlQuery = " SELECT *  FROM  ".SQL_PREFIX."picitem  WHERE pii_id = '".$idval."' ORDER BY `pii_id` ";
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=picture&act=editinfo");
		$GLOBALS['Templ'] -> assign('info',$info);
		
		$this -> getSort();
		
		$GLOBALS['Helpe'] -> load_ckeditor( $textName = 'pii_content' ,$textValue = $info["pii_content"] ,'Basic' );	//加载文本编辑器
		
		$GLOBALS['Templ'] -> display('pictureitemedit_tpl.html');
	
	}
	
	
	function piclist()
	{	
		
		$idval = $GLOBALS['Reque'] -> getval["id"];
		
		$sqlQuery = " SELECT *  FROM  ".SQL_PREFIX."picture  WHERE pic_id = '".$idval."' ORDER BY `pic_id` ";
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);

		$sqlQueryitem = " SELECT *  FROM  ".SQL_PREFIX."picitem  WHERE pic_id = '".$idval."' ORDER BY `pic_id` ";
		$infoItem = $GLOBALS['MySql'] -> selectInfo( $sqlQueryitem );
		
		
		$GLOBALS['Helpe'] -> load_ckeditor( $textName = 'pii_content' ,'' ,'Basic' );	//加载文本编辑器
		
		$GLOBALS['Templ'] -> assign('info',$info);
		$GLOBALS['Templ'] -> assign('infoItem',$infoItem);
		$GLOBALS['Templ'] -> display('picturelist_tpl.html');
	}
	
	
	function editinfo()
	{	
	
		$pic_idval 		= $GLOBALS['Reque'] -> funpost("pic_id");	
		$pic_nameval 	= $GLOBALS['Reque'] -> funpost("pic_name");
		$pis_idval 		= $GLOBALS['Reque'] -> funpost("pis_id");
		$pic_pinyinval	= $GLOBALS['Reque'] -> funpost("pic_pinyin");
		$pic_urlval 	= $GLOBALS['Reque'] -> funpost("pic_url");
		$pic_contentval = $_POST["pic_content"];
		$pic_enabledval = $GLOBALS['Reque'] -> funpost("pic_enabled");
		
		
		if($pic_idval !='' and  $pic_nameval !='' and $pis_idval !=''  and $pic_contentval != '' )
		{
			$pic_uptimeval =  time();
			
			$updatesql = " UPDATE `".SQL_PREFIX."picture` SET  pic_name = '".$pic_nameval."' , pic_pinyin = '".$pic_pinyinval."' , pis_id = '".$pis_idval."', pic_url = '".$pic_urlval."', pic_content = '".$pic_contentval."', pic_enabled = '".$pic_enabledval."' WHERE  pic_id = '".$pic_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=picture&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=picture&act=info&id=".$pic_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
		}
		
	}
	
	function edititem()
	{	
	
		$pic_idval 		= $GLOBALS['Reque'] -> funpost("pic_id");	
		$pii_idval 		= $GLOBALS['Reque'] -> funpost("pii_id");	
		
		$pii_urlval 	= $GLOBALS['Reque'] -> funpost("pii_url");
		$pii_contentval = $_POST["pii_content"];
		$pii_enabledval = $GLOBALS['Reque'] -> funpost("pii_enabled");
		
		
		if($pii_idval !='' and  $pii_urlval !='' and $pii_contentval !='' )
		{
			$pii_uptimeval =  time();
			
			$updatesql = " UPDATE `".SQL_PREFIX."picitem` SET  pii_url = '".$pii_urlval."' , pii_content = '".$pii_contentval."' , pii_enabled = '".$pii_enabledval."' WHERE  pii_id = '".$pii_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=picture&act=infoitem&id=".$pii_idval);
			$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=picture&act=infoitem&id=".$pii_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
		}
		
	}
	
	function show()
	{

		$infoid = $GLOBALS['Reque'] -> getval["id"];
		
		$upshow = " UPDATE `".SQL_PREFIX."picture` SET pic_enabled = 1  WHERE  `pic_id` in ( $infoid ) ";

		$GLOBALS['MySql'] -> querySql($upshow);
		$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_showsusse']);
		$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
		$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=picture&act=infolist");
		$GLOBALS['Templ'] -> display('suggestion_tpl.html');
	}
	
	function hide()
	{
		
		$infoid = $GLOBALS['Reque'] -> getval["id"];

		$uphide = " UPDATE `".SQL_PREFIX."picture` SET pic_enabled = 0  WHERE `pic_id` in ( $infoid ) ";

		$GLOBALS['MySql'] -> querySql($uphide);
		$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_hidesusse']);
		$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
		$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=picture&act=infolist");
		$GLOBALS['Templ'] -> display('suggestion_tpl.html');
	}
	

}


$Picture = new Picture();

?>