<?php
defined('IN_YUNYECMSAdmin') or exit('No permission.');
core::load_admin_class('admin');
class notice extends YUNYE_admin {
	private $lang;  //默认语言
	function __construct() {
		parent::__construct();
		$this->db = core::load_model('notice_model');
	    $this->lang=getlangid();
		 if(!getroot('member','notice')){
			messagebox(Lan('no_permission'),'back',"warn");			
		 }
	}
	 //加载首页
	 public function init() {
		  $parnav='<li><a href=\"'.url_admin('init',"notice").'\" target=\"maincontent\">系统</a></li><li><a href=\"'.url_admin('init','notice').'\" target=\"maincontent\">通知消息管理</a></li><li class=\"active\">通知消息列表</li>';
		   if(!getroot('users','logs')){
			  messagebox(Lan('no_permission'),'back',"warn");			
		   }
		 $pagesize=20;
		 $sqlquery="select * from `#yunyecms_notice`  ";
		 $where=" where  title<>'' and lang={$this->lang}  ";
		 $sqlcnt=" select count(*) from `#yunyecms_notice` ";
		 $order=" order by `addtime` desc ";
		  if(isset($_REQUEST)){
		   if(!empty($_REQUEST["searchkey"])){
		        $searchkey=usafestr(trim($_REQUEST["searchkey"]));
		        $where=$where." and ( `title`  like '%{$searchkey}%' )";
			  }
		 }
		 $pagearr=$this->db->pagelist($sqlcnt,$sqlquery,$where,$order,$pagesize);
		 if($pagearr["count"]!=0){
			 $list=$pagearr["query"];
			  foreach($list as $key=>$var){
				  $list[$key]['pic']=dopic($var['pic']);
			  }
			 $page=$pagearr["page"];
		 }
		 require tpl_adm('notice_list');
	  }
	
	
 public function add(){
	 	    $lang_list=getalllang();
	             foreach($lang_list as $kk=>$vv){
				    $lang_list[$kk]['lang_icon']=dopic($vv['lang_icon']);
				  }	
			$langid=intval($this->lang);
			$nums=count($lang_list);
			$tablename='notice';
		    if(!empty($_GET["id"])){
					$parnav='<li><a href=\"'.url_admin('init','notice').'\" target=\"maincontent\">通知消息管理</a></li><li class=\"active\">修改通知消息</li>';
					$id=trim($_GET["id"]);
					 if(!is_numeric($id)){
					   messagebox("通知消息参数错误",url_admin('init'));
					 }
					$rsinfo=$this->db->find("select * from `#yunyecms_{$tablename}` where `id`= {$id}");
				
					 if(empty($rsinfo)){
					   messagebox("该信息不存在，谢谢!",$_SERVER['HTTP_REFERER']);
					 }else{
						$syn=$rsinfo['syn'];
					 }
				     if($rsinfo['pic']) $rsinfo['pic']=dopic($rsinfo['pic']);
					 foreach($lang_list as $kk=>$vv){
						 $lid=$vv['id'];
						 $ldef=$vv['isdefault'];
						 if($syn){
							 $rs_shadow[$lid]=$this->db->find("select * from `#yunyecms_{$tablename}` where `syn`= {$syn} and lang={$lid}");
						 }else{
							 $rs_shadow[$lid]=$this->db->find("select * from `#yunyecms_{$tablename}` where `syn`= {$id} and lang={$lid}");
						 }
				          if($row_shadow[$lid]['pic']) $row_shadow[$lid]['pic']=dopic($row_shadow[$lid]['pic']);
					  }
					$yyact="edit";
			 }else{
				$yyact=yyact_get("add");
				$parnav='<li><a href=\"'.url_admin('init','member').'\" target=\"maincontent\">用户</a></li><li><a href=\"'.url_admin('init','notice').'\" target=\"maincontent\">通知消息管理</a></li><li class=\"active\">增加通知消息</li>';
				$sqlordermax="select max(ordernum) as maxordernum from `#yunyecms_notice`";
				$ordermaxquery=$this->db->find($sqlordermax);
				$ordermax=$ordermaxquery["maxordernum"]+1;
				}
	        if(isset($_POST["yyact"])){
				      $_POST=ustripslashes($_POST);
					  $data["isgood"]=empty($_POST["isgood"])?0:usafestr(trim($_POST["isgood"]));
				 	  $data["pic"]=usafepic(revpic(trim($_POST["pic"])));
				 	  $data["ordernum"]=usafestr(trim($_POST["ordernum"]));
				 	  $data["addtime"]=time();
				 	  $data["status"]=1;
		              foreach($lang_list as $kk=>$vv){
						  $lid=$vv['id'];
						  $ldef=$vv['isdefault'];
						  if($ldef==1){
				 	          $data["title"]=usafestr(trim($_POST["title_{$lid}"]));
							  $data['exlink']=uhtmlspecialchars(trim($_POST["exlink_{$lid}"]));							 if(uhtmlspecialchars(trim($_POST["picfile_{$lid}"])))$data["picfile"]=uhtmlspecialchars(trim($_POST["picfile{$lid}"]));
							  $data['summary']=uhtmlspecialchars(trim($_POST["summary_{$lid}"]));	
							  $data['seotitle']=uhtmlspecialchars(trim($_POST["seotitle_{$lid}"]));	
							  $data['seokeywords']=uhtmlspecialchars(trim($_POST["seokeywords_{$lid}"]));	
							  $data['seodesc']=uhtmlspecialchars(trim($_POST["seodesc_{$lid}"]));
							  $data['lang']=$lid;
	                           if(isset($_POST["content_{$lid}"])){
				 	           $data["content"]=revimgs(trim($_POST["content_{$lid}"]));
					           }							  
						     }else{
							  $data_shadow[$lid]['title']=uhtmlspecialchars(trim($_POST["title_{$lid}"]));	
							  $data_shadow[$lid]['exlink']=uhtmlspecialchars(trim($_POST["exlink_{$lid}"]));	
							  if(uhtmlspecialchars(trim($_POST["picfile_{$lid}"])))$data_shadow[$lid]["picfile"]=uhtmlspecialchars(trim($_POST["picfile{$lid}"]));
							  $data_shadow[$lid]['summary']=uhtmlspecialchars(trim($_POST["summary_{$lid}"]));	
							  $data_shadow[$lid]['seotitle']=uhtmlspecialchars(trim($_POST["seotitle_{$lid}"]));	
							  $data_shadow[$lid]['seokeywords']=uhtmlspecialchars(trim($_POST["seokeywords_{$lid}"]));	
							  $data_shadow[$lid]['seodesc']=uhtmlspecialchars(trim($_POST["seodesc_{$lid}"])); 
							  $data_shadow[$lid]['lang']=$lid;
							  $data_shadow[$lid]['isgood']=$data["isgood"];
							  $data_shadow[$lid]['pic']=$data["pic"];
							  $data_shadow[$lid]['ordernum']=$data["ordernum"];
							  $data_shadow[$lid]['status']=1;
							  if(isset($_POST["content_{$lid}"])){
				 	            $data_shadow[$lid]["content"]=revimgs(trim($_POST["content_{$lid}"]));
					          }
						   }
					   }				
			
					  if(empty($data["title"])){
							messagebox("通知消息标题不能为空，谢谢!",$_SERVER['HTTP_REFERER']);		
					   }
			 if($_POST["yyact"]=="add"){
				 	 $data["addtime"]=time();
				 	 $data["status"]=1;
				 	 $data["admin_userid"]=$this->admuser["userid"];
					 //计算记录编号  
				 	 $data["number"]=create_number($tablename,$this->lang);
					 $insertid=$this->db->insert($data,$tablename,true);
					 if($insertid){
						 	    //同步新增其他语言版记录
						        $syndata['syn']=$insertid;
						        $retsyn=$this->db->update($syndata,array('id'=>$insertid),$tablename);
						        if($retsyn){
									  foreach($lang_list as $kk=>$vv){
										  $lid=$vv['id'];
										  $ldef=$vv['isdefault'];
										  $data_shadow[$lid]['syn']=$insertid;
										  $data_shadow[$lid]["addtime"]=time();
				 	                      $data_shadow[$lid]["admin_userid"]=$this->admuser["userid"];
				 	                      $data_shadow[$lid]["number"]=$data["number"];;
										  if($ldef!=1){
										     $retsynadd[$lid]=$this->db->insert($data_shadow[$lid],$tablename);
										  }
									  }
									 //$this->db->Autocommit_commit(); 
								}else{
									 $this->db->Autocommit_rollback(); 
								}
								$doing="添加通知消息—".$data["title"];
								$yyact="Add_Notice";
								insert_admlogs($doing,$yyact);
								messagebox("添加通知消息成功！",url_admin('init'),"success");
					 }else{
								messagebox("添加通知消息失败！",url_admin('init'),"error");
					 }
			  }
		      if($_POST["yyact"]=="edit"){
					   $id=$_POST["id"];
					   if(!$this->check_exist($id)){
							messagebox("该消息不存在！",url_admin('init'),"warn");
					   }
					   $crow=$this->db->find("select * from `#yunyecms_{$tablename}`  where `id`= {$id}");
				 	   $data["addtime"]=strtotime(trim($_POST["addtime"]));
					   $retres=$this->db->update($data,"id={$id}","notice");
						if($retres){
							       //同步更新其他语言版
							     	    foreach($lang_list as $kk=>$vv){
										  $lid=$vv['id'];
										  $ldef=$vv['isdefault'];
										  $rs_check=$this->db->GetCount("select count(*) from `#yunyecms_{$tablename}` where `syn`= {$crow['syn']} and lang={$lid}");
										  if($rs_check&&!$ldef){
										     $data_shadow[$lid]["addtime"]=$data["addtime"];
											 $retsynadd[$lid]=$this->db->update($data_shadow[$lid],array("syn"=>$id,"lang"=>$lid));  
										  }else{
									         $data_shadow[$lid]['syn']=$crow['syn'];
										     $data_shadow[$lid]['number']=create_number($tablename,$this->lang);
				 	                         $data_shadow[$lid]["addtime"]=$crow['addtime'];
				 	                         $data_shadow[$lid]["updatetime"]=$crow['updatetime'];
				 	                         $data_shadow[$lid]["admin_userid"]=$this->admuser["userid"];
										     $retsynadd[$lid]=$this->db->insert($data_shadow[$lid],$tablename,true);								  
										  }
									  } 
							
									$doing="更新通知消息—".$data["title"];
									$yyact="Update_Notice";
									insert_admlogs($doing,$yyact);
									messagebox("通知消息更新成功！",url_admin('init'),"success");
						 }else{
									messagebox("通知消息更新失败！",url_admin('init'),"error");
						 }
			  }			  
		  }
		require tpl_adm('notice_add');
	 }	
	
  public function finaldelete() {
		  if(!getroot('info','del')){
			    messagebox(Lan('no_permission'),'back');			
		   }
            $id = $_REQUEST["id"];
		    if(!is_array($id)){
			  $id=compact('id');
			}
			$idarray=$id;
			 foreach($idarray as $key=>$var){
				 		if(!is_numeric($var)){
					        messagebox("错误的参数！",'back',"warn");			
					    }
                        $var = usafestr($var);
				        $idarray[$key]=$var;
		                $curmodelnr=$this->db->find("select id,pic,content,syn from `#yunyecms_notice` where `id`= {$var}");
					    if($curmodelnr){
							 $syn=$curmodelnr["syn"]; 
							 foreach($curmodelnr as $kk=>$vv){
							  $thumb=$vv["pic"];
							  $ct_content=uhtmlspecialchars_decode($vv["content"]);
							  $ct_content_img=getcontentimg($ct_content);
								 if(!empty($thumb)){
									 file_delete($thumb);
								 }
								 foreach($ct_content_img as $key3=>$var3){
									 file_delete($var3);
								 }
							 }
				             $retres =$this->db->delete("syn = $syn",'notice');
						 }	
					}
            if (isset($idarray)){
                if ($retres !== false) {
						$yyact="Notice_Del";
						$logcontent['tablename']="notice";
						$logcontent['action']=$yyact;
						$logcontent['ids']=serialize($id);
						$logcontent=serialize($logcontent);
						$doing="删除通知消息：{$idarray} ";
						insert_admlogs($doing,$yyact,$logcontent);
				    messagebox(Lan('admin_delete_success'),$_SERVER['HTTP_REFERER'],"success");
                } else {
				    messagebox(Lan('admin_delete_error'),$_SERVER['HTTP_REFERER'],"warn");
                }
            } else {
				    messagebox(Lan('admin_delete_error'),$_SERVER['HTTP_REFERER'],"warn");
            }
    }	
	
	
	
	
    public function check(){
        parent::infocheck("notice",$this->db);
    }	
	
    public function nocheck() {
        parent::infonocheck("notice",$this->db);
    }		
	 
	private function check_exist($id) {
		 $id = trim($id);
		 if(empty($id)){
		     return false;
			 }else{
			    if(!is_numeric($id)){
					  return false;
				 }
			  if ($this->db->find("select count(*) as cnt from `#yunyecms_notice` where `id`= {$id}")){
				  return true;
			  }				 
		  }
	  }	

	 
}
?>
