<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST, ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace news\admin\controller;
use \temmoku\db;
use \temmoku\controller;
use \temmoku\lib\custom_model;
use \temmoku\lib\pages;
use news\public_class\article;
use \news\init;
class articles extends controller
{
    public function add()
    {	
    	if('post'===$_POST['post']['step']){
			hook_listen('news_post_begin');
    		(new article)->check();
	    	$aid = (new article)->title($_POST['post']);
	    	//专题入库
	    	(new article)->post_article_special('post',$aid);
	    	//添加内容
	    	$arr_content=explode('##page##',$_POST['post']['content']);
			foreach($arr_content AS $value){
				$array['aid']=$aid;
				$array['content']=$value;
				(new article)->content($array);
				unset($array);
			}
			if($_POST['post']['mid']){
				$Model_Setting=C(init::$dir.'|WN_MODELDB.'.$_POST['post']['mid'])['field'];
				$module_post_data=(new Custom_Model)->module_post_data($Model_Setting,$_POST['_post']);
	    		(new article)->module($aid,$_POST['post']['mid'],$module_post_data);
			}
			hook_listen('news_post_end',['aid'=>$aid]);
    		$this->Success('发布成功', FromUrl);
    	}
    	if($_GET['cid']){
    		$cidDB=db::select('*')->from(init::$table."category")->where("cid='{$_GET[cid]}'")->row();
	    	$this->assign('mid', $cidDB['cmid']);
			if($cidDB['cmid']){
				$_ModelDB=C(init::$dir.'|WN_MODELDB.'.$cidDB['cmid'])['field'];
				if($_ModelDB){
					$Custom_Model_Template=	(new Custom_Model)->Custom_Model_Get($cidDB['cmid'],$_ModelDB);
					$this->assign('Custom_Model_Template', $Custom_Model_Template);
				}
			}
    	}

    	$special_list=(new Article)->get_all_special_list();
    	$this->assign('special_list', $special_list);
    	$moduleDB=db::select('*')->from(init::$table."module")->where(" close = 0")->query();
    	$allcidDB = (new article)-> allcidDB('0',init::$table.'category');
        $this->assign('allcidDB', $allcidDB);
        $this->assign('moduleDB', $moduleDB);
        $this->assign('cid', $_GET['cid']);
        $this->display(tpl('articles/add'));
    }
    
    public function index(){
    	!isset($_GET['page']) && $_GET['page']=1;
		!isset($_GET['cid']) && $_GET['cid']=0;
		$rows='20';
		$url="";
		if($_GET['cid']){
			$WHERE="cid = '$_GET[cid]'";
			$url.='cid_'.$_GET['cid'].'/';
		}else{
			$WHERE="1";
		}
		if($_GET['state']=='0' || $_GET['state']=='1'){
			$WHERE.=" AND state = '$_GET[state]'";
			$url.='state_'.$_GET['state'].'/';
		}
		if($_GET['is_recommend']=='1'){
			$WHERE.=" AND is_recommend = '$_GET[is_recommend]'";
			$url.='is_recommend_'.$_GET['is_recommend'].'/';
		}
		$data=db::select('*')->from(init::$table."article")->where($WHERE)->orderByDESC(['aid'])->setPaging($rows)->page($_GET['page'])->query();

		$Get_Page=(new Pages)->Pages(init::$table."article",$WHERE,admin_module_url."articles/index/{$url}",$rows,$_GET['page']);
    	$this->assign('data', $data);
    	$this->assign('Get_Page', $Get_Page);
    	$this->display(tpl('articles/index'));
    }
    
    public function edit(){
    	$edit=db::select('*')->from(init::$table."article")->where("aid= '$_GET[aid]' ")->row();
		if('post'===$_POST['post']['step']){
			hook_listen('news_post_begin',$edit);
	    	(new article)->check($edit);
			//专题入库
	    	(new article)->post_article_special('edit',$_GET['aid']);
			//修改模型内容
	    	if($_POST['post']['mid']){
	    		$Model_Setting=C(init::$dir.'|WN_MODELDB.'.$_POST['post']['mid'])['field'];
	    		$edit_Custom_Model=db::select('*')->from(init::$table."content_".$edit['mid'])->where("aid= '$edit[aid]' ")->row();
				$module_post_data=(new Custom_Model)->module_post_data($Model_Setting,$_POST['_post'],$edit_Custom_Model);
	    		(new article)->module($_POST['post']['aid'],$_POST['post']['mid'],$module_post_data,'edit');
			}
			//修改主表内容
	    	(new article)->title($_POST['post'],'edit');
	    	//修改内容
	    	(new article)->content($_POST['post'],'edit');
			hook_listen('news_post_end',['aid'=>$edit['aid']]);
	    	$this->success('修改成功');
		}
		
		if($edit['mid']){
			$edit_Custom_Model=db::select('*')->from(init::$table."content_".$edit['mid'])->where("aid= '$edit[aid]' ")->row();
			$_ModelDB=C(init::$dir.'|WN_MODELDB.'.$edit['mid'])['field'];
			$Custom_Model_Template=(new Custom_Model)->Custom_Model_Get($edit['mid'],$_ModelDB,$edit_Custom_Model);
		}
		$cont_query=db::select('*')->from(init::$table."content")->where("aid= '$edit[aid]' ")->query();
		foreach($cont_query AS $V){
			$V['content']=str_replace  ( 'WWW_TEMMOKU_CN'  , WEBURL.'/'.C('upfiles_Catalog') ,  $V['content']);
			$cont_queryDB[]=$V;
		}
		
		$allcidDB = (new article)->allcidDB('0',init::$table.'category');
		$this->assign('allcidDB', $allcidDB);
		//专题
		$special_list=(new Article)->get_all_special_list();
    	$this->assign('special_list', $special_list);
		
		//获取已选择的专题
		$_selected_special_list=db::select('lid')->from(init::$table."special_article")->where(" aid = $_GET[aid]")->query();
		foreach($_selected_special_list AS $row){
			$selected_special_list[]=$row['lid'];
		}
		$this->assign('selected_special_list', $selected_special_list);
		
		$moduleDB=db::select('*')->from(init::$table."module")->where(" close = 0")->query();
		$edit['posttime']=date("Y-m-d H:i:s",$edit['posttime']);
		$this->assign('edit', $edit);
		$this->assign('cont', $cont);
		
		$this->assign('Custom_Model_Template', $Custom_Model_Template);
		$this->assign('mid', $edit['mid']);
		if($_GET['cid']){
			$edit['cid']=$_GET['cid'];
		}
		$this->assign('cid', $edit['cid']);
		$this->assign('cont_query', $cont_queryDB);
		$this->assign('moduleDB', $moduleDB);
	    $this->display(tpl('articles/add'));
    }
    
    public function del(){
		hook_listen('news_article_del_begin',['aid'=>$_GET['aid']]);
    	(new Article)->del($_GET['aid']);
		hook_listen('news_article_del_end',['aid'=>$_GET['aid']]);
		$this->Success('删除成功');
    }

    public function ajax_Module(){
		
    	$_ModelDB=C(init::$dir.'|WN_MODELDB.'.$_GET['mid'])['field'];
    	if(!$_ModelDB){
    		echo '暂未创建字段'; 
    		exit;
    	}
    	$return=(new Custom_Model)->Custom_Model_Get($_GET['mid'],$_ModelDB);
    	echo $return;
    }
    //批量|单个审核或者推荐
    public function batch_edit(){
    	if($_POST['aid']){
    		if($_POST['state']=='1'||$_POST['state']=='0'){
    			$this->state($_POST['aid'],$_POST['state']);
    		}elseif($_POST['is_recommend']=='1' || $_POST['is_recommend'] =='0'){
    			$this->is_recommend($_POST['aid'],$_POST['is_recommend']);
    		}
    		echo json_encode(['Result'=>'1','content'=>'操作成功']);
    	}elseif($_POST['iddb'] && $_POST['step'] !='del'){
    		foreach($_POST['iddb'] AS $aid){
    			$aid=intval($aid);
    			if($_POST['state']=='1'||$_POST['state']=='0'){
	    			$this->state($aid,$_POST['state']);
	    		}elseif($_POST['is_recommend']=='1' || $_POST['is_recommend'] =='0'){
	    			$this->is_recommend($aid,$_POST['is_recommend']);
	    		}
    		}
    		$this->Success('操作成功');
    	}elseif($_POST['step']=='del'){
    		foreach($_POST['iddb'] AS $aid){
    			(new Article)->del($aid);
    		}
    		$this->Success('操作成功');
    	}
    }
    private function state($aid,$state){
		hook_listen('news_article_state',['aid'=>$aid,'state'=>$state]);
    	db::update(init::$table."article")->cols(['state'=>$state,'state_time'=>NOWTIME])->where("aid='$aid'")->query();
    }
    private function is_recommend($aid,$is_recommend){
		hook_listen('news_article_recommend',['aid'=>$aid,'state'=>$is_recommend]);
    	if($is_recommend){
    		$reorder=",`reorder`=`reorder`+3600*7*24";
    	}else{
    		$reorder=",`reorder`=`reorder`-3600*7*24";
    	}
    	db::query("UPDATE ".init::$table."article SET `is_recommend` = '$is_recommend' $reorder WHERE aid='$aid'");
    }
}
?>
