<?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 user\controller;
use \temmoku\db;
use temmoku\user;
use \temmoku\lib\custom_model;
use \temmoku\lib\pages;
use \admin\public_class\article;
class articles extends user
{
	function __construct()
    {

    	parent :: __construct();
    	$dir=APP_PATH.'home/conf/';
    	if(is_dir($dir)){
    		Load_conf($dir);
    	}
    	
    }
    public function add()
    {	
	    	if($_GET['cid'] || $_POST['post']['cid']){
				$cid=$_POST['post']['cid'] ? $_POST['post']['cid'] : $_GET['cid'];
	    		$cidDB = db::select('*')->from(jab."category")->where("cid= '$cid' ")->row();
	    		//获取栏目权限设置
	    		$setting=unserialize($cidDB['setting'])['power'][C('mydb.groupid')];
	    		if($setting['add'] !='1' && C('mydb.groupid') !='3'){
	    			$this->err('没有权限发布');
	    		}
	    	}
    		if('post'===$_POST['post']['step']){
				hook_listen('cms_post_begin');
    			(new article)->check();
	    		$aid = (new article)->title($_POST['post']);
	    		//添加内容
	    	
	    		$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=unserialize(C('home|home_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('cms_post_end');
    			$this->success('发布成功', '/user/articles/index/');
    		}
    		if($cidDB){
				$this->assign('setting', $setting);
	    		$this->assign('mid', $cidDB['cmid']);
				if($cidDB['cmid']){
					$_ModelDB=unserialize(C('home|home_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);
					}
				}
    		}
    		$allcidDB = (new article)-> allcidDB('0',jab.'category');
        	$this->assign('allcidDB', $allcidDB);
        	$this->assign('cid', $_GET['cid']);
			if(DEVICE != 'pc'){
				$this->assign('webname','发布文章');
			}
        	$this->display(tpl('articles/add'));
    }
    
    public function index(){
    	!isset($_GET['page']) && $_GET['page']=1;
		!isset($_GET['cid']) && $_GET['cid']=0;
		$rows='20';
		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'].'/';
		}
		$WHERE .=' AND uid='.C('mydb.uid');
		$data=db::select('*')->from(jab."article")->where($WHERE)->orderByDESC(['aid'])->setPaging($rows)->page($_GET['page'])->query();
		$Get_Page=(new pages)->Pages(jab."article",$WHERE,'',$rows,$_GET['page']);
		$Get_Page = preg_replace_callback('/page_([0-9]+)/',function ($matches) {return "/user/articles/index/page/".$matches[1];}, $Get_Page );
    	$this->assign('data', $data);
    	$this->assign('Get_Page', $Get_Page);
		if(DEVICE != 'pc'){
			$this->assign('webname','我发布的文章');
		}
    	$this->display(tpl('articles/index'));
    }
    
    public function edit(){
    	
	    $edit=db::select('*')->from(jab."article")->where("aid= '$_GET[aid]' ")->row();
	    if(!$edit){
	    	$this->err('不存在的数据');
	    }elseif($edit['uid'] != C('mydb.uid') && C('mydb.groupid') !='3'){
	    	$this->err('没有权限修改');
	    }
	    $cidDB=C('home|CATEGORY_SETTING.'.$edit['cid']);
	    //获取栏目权限设置
	    $setting=unserialize($cidDB['setting'])['power'][C('mydb.groupid')];
	    	
	    if($edit['state'] && !$setting['is_edit'] && C('mydb.groupid') !='3'){
	    	$this->err('没有权限修改');
	    }
		if('post'===$_POST['post']['step']){
			hook_listen('cms_post_begin',$edit);
	    	(new article)->check($edit);
			//修改模型内容
	    	if($_POST['post']['mid']){
	    		$Model_Setting=unserialize(C('home|home_MODELDB.'.$_POST['post']['mid'])['field']);
	    		$edit_Custom_Model=db::select('*')->from(jab."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('cms_post_end',$edit);
	    	$this->Success('修改成功', '/user/articles/index/');
		}
		
		if($edit['mid']){
			$edit_Custom_Model=db::select('*')->from(jab."content_".$edit['mid'])->where("aid= '$edit[aid]' ")->row();
			$_ModelDB=unserialize(C('home|home_MODELDB.'.$edit['mid'])['field']);
			$Custom_Model_Template=(new Custom_Model)->Custom_Model_Get($edit['mid'],$_ModelDB,$edit_Custom_Model);
		}
		
		$cont_query=db::select('*')->from(jab."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',jab.'category');
		
		$this->assign('setting', $setting);
		$this->assign('allcidDB', $allcidDB);
		
		$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']);
		$this->assign('cid', $edit['cid']);
		$this->assign('cont_query', $cont_queryDB);
		if(DEVICE != 'pc'){
				$this->assign('webname','修改文章');
			}
	    $this->display(tpl('articles/add'));
    }
    
    public function del(){
			hook_listen('cms_articles_del',['aid'=>$_GET[aid]]);
	    	$del=db::select('*')->from(jab."article")->where("aid= '$_GET[aid]' ")->row();
	    	if(!$del){
	    		$this->err('不存在的数据');
	    	}elseif($del['uid'] != C('mydb.uid') && C('mydb.groupid') !='3'){
	    		$this->err('没有权限删除');
	    	}
	    	//获取栏目权限设置
	    	$cidDB=C('home|CATEGORY_SETTING.'.$del['cid']);
	    	$setting=unserialize($cidDB['setting'])['power'][C('mydb.groupid')];
	    	if(!$del['state'] && !$setting['is_state_no_del'] && C('mydb.groupid') !='3'){
	    		$this->err('没有权限修改');
	    	}elseif($del['state'] && !$setting['is_state_yes_del'] && C('mydb.groupid') !='3'){
	    		$this->err('没有权限修改');
	    	}
	    	(new article)->del($_GET['aid']);
		$this->success('删除成功');
    }

    public function ajax_Module(){
	    	$_ModelDB=unserialize(C('home|home_MODELDB.'.$_GET['mid'])['setting']);
	    	if(!$_ModelDB){
	    		echo '暂未创建字段'; 
	    		exit;
	    	}
	    	$return=(new Custom_Model)->Custom_Model_Get($_GET['mid'],$_ModelDB);
	    	echo $return;
    }
    
}
?>