<?php
require "./FCKeditor/fckeditor.php";
class articleAction extends CommonAction{
	private $oFCKeditor;
	private $article;
	function __construct(){
			parent::__construct();
			$this->article = new articleModel();
			$this->oFCKeditor = new FCKeditor("content") ;          // 创建FCKeditor实例，可创建多个实例
			$this->oFCKeditor->BasePath = './FCKeditor/';
		}

    function category_add() {
    	$this->article = new articleModel("_cate");
    	$select = $this->article->cate_select();
    	$this->assign('select',$select);
    	$this->assign('channelid',$_REQUEST['channelid']);
    	$this->display('admin/article/categroy.html');


    }

    function insert(){
    	$this->article = new articleModel("_cate");
		if($this->article->validate()){
			$pinfo = $this->article->find($_POST['parentid']);
			$_POST['htmldir']=$this->article->get_htmldir($pinfo['htmldir']);
			$this->upload = new upload(array('filepath'=>ATTCHMENT_REAL_PATH.'article'));
			if($this->upload->uploadFile('picdir')){
			global $thumbSize;
			$picname =$this->upload->getNewFileName();
			$this->image = new image(ATTCHMENT_REAL_PATH.'article');
			$imagename = $this->image->thumb($picname,$thumbSize['width'],$thumbSize['height'],'');

			$_POST['catepic']='article/'.$imagename;
				if($this->article->add($_POST)){
					$this->showMsg("成功添加分类","admin.php?m=article&a=category_mod&channelid=$_POST[channelid]");
				}else{
					$this->showMsg("添加分类失败","admin.php?m=article&a=category_mod&channelid=$_POST[channelid]");
				}

			}else{
				$this->showMsg($this->upload->getErrorMsg(),"admin.php?m=article&a=category_add&channelid=$_POST[channelid]");
			}


		}else{
			$this->showMsg($this->article->getMessList(),"admin.php?m=article&a=category_add&channelid=$_POST[channelid]");
		}
    }

    function category_mod() {
    	$this->article = new articleModel("_cate");
    	$list = $this->article->cate_list();
    	$this->assign('channelid',$_REQUEST['channelid']);

    	$this->assign('list',$list);
    	$this->display('admin/article/catemod.html');

    }





    function ordering(){
    	$this->article = new articleModel("_cate");
		if(empty($_POST['order'])){
			$this->showMsg("没有需要排序的频道","admin.php?m=article&a=category_mod&channelid=$_POST[channelid]");
		}
		if($this->article->valorder()){
			if($this->article->ordering()){
				$this->showMsg("排序修改成功","admin.php?m=article&a=category_mod&channelid=$_POST[channelid]");
			}else{
				$this->showMsg("排序修改失败","admin.php?m=article&a=category_mod&channelid=$_POST[channelid]");
			}

		}else{
			$this->showMsg($this->article->getMessList(),"admin.php?m=article&a=category_mod&channelid=$_POST[channelid]");
		}
	}



	function cate_del(){
		$this->article = new articleModel("_cate");
		if($this->article->checkcolumn()){
				if($this->article->del($_REQUEST["id"])){
					$this->showMsg("成功删除分类","admin.php?m=article&a=category_mod&channelid=$_REQUEST[channelid]");
				}else{
					$this->showMsg("删除分类失败","admin.php?m=article&a=category_mod&channelid=$_REQUEST[channelid]");;
				}
		}else{
			$this->showMsg("必须先删除下级分类","admin.php?m=article&a=category_mod&channelid=$_REQUEST[channelid]");
		}

	}

	function cate_edit(){
		$this->article = new articleModel("_cate");
		$_POST=$this->article->find($_REQUEST["id"]);
		$this->assign('catepic',$_POST['catepic']);
		$_POST['catepic']=ATTCHMENT_PATH.$_POST['catepic'];
		$_POST['htmldir']=$this->article->path_dir($_POST['htmldir']);
		$select = $this->article->cate_select($_REQUEST["id"],'0');
    	$this->assign('select',$select);
		$this->assign("post",$_POST);
		$this->assign('channelid',$_REQUEST['channelid']);
    	$this->display('admin/article/cateedit.html');

	}
	function cate_update(){
		$this->article = new articleModel("_cate");
		if($this->article->validate()){
			$pinfo = $this->article->find($_POST['parentid']);
			$_POST['htmldir']=$this->article->get_htmldir($pinfo['htmldir']);
			$this->upload = new upload(array('filepath'=>ATTCHMENT_REAL_PATH.'article'));
			if($this->upload->uploadFile('picdir')){
			global $thumbSize;
			$picname =$this->upload->getNewFileName();
			$this->image = new image(ATTCHMENT_REAL_PATH.'article');
			$imagename = $this->image->thumb($picname,$thumbSize['width'],$thumbSize['height'],'');
			@unlink(ATTCHMENT_REAL_PATH.$_POST['catepic']);
			$_POST['catepic']='article/'.$imagename;

			}else{
				unset ($_POST['catepic']);
			}

			if($_POST['parentid']==$_REQUEST['id']){
				$this->showMsg("不能以自己为父类","admin.php?m=article&a=category_mod&channelid=$_POST[channelid]");
			}
			$code = $this->article->check_parentid($_REQUEST['id']);
			if(is_array($code) && in_array($_REQUEST['parentid'],$code)){
				$this->showMsg("不能以自己的子类为父类","admin.php?m=article&a=category_mod&channelid=$_POST[channelid]");
			}
			if($this->article->mod($_POST)){
				$this->showMsg("成功修改分类","admin.php?m=article&a=category_mod&channelid=$_POST[channelid]");
			}else{
				$this->showMsg("修改分类失败","admin.php?m=article&a=category_mod&channelid=$_POST[channelid]");
			}

		}else{
			$this->showMsg($this->article->getMessList(),"admin.php?m=article&a=category_mod&channelid=$_POST[channelid]");
		}


	}



	//==================上面为分类==下面为文章=====================

	function article_add(){
		$this->article = new articleModel("_cate");
    	$select = $this->article->cate_select();
    	$this->assign('select',$select);
    	$this->assign('channelid',$_REQUEST['channelid']);
		$this->assign("FCKeditor",$this->oFCKeditor->Create());
		$this->display('admin/article/article.html');
	}


	function article_insert(){
			if($this->article->chech_form()){
				$_POST['publish']=time();
				if($this->article->add($_POST)){
					$this->showMsg("成功添加新闻","admin.php?m=article&a=article_add&channelid=$_REQUEST[channelid]");
				}else{
					$this->showMsg("添加新闻失败","admin.php?m=article&a=article_add&channelid=$_REQUEST[channelid]");;
				}

			}else{
				$this->showMsg($this->article->getMessList(),"admin.php?m=article&a=article_add&channelid=$_REQUEST[channelid]");
			}

	}

	function article_mod(){
		$where=$and="";
		if(isset($_REQUEST[select_id])){
			$this->newcate = new articleModel("_cate");
			$id = $this->newcate->check_parentid($_REQUEST[select_id]);

				if(!empty($id)){
					$id  = implode(",",$id).",".$_REQUEST[select_id];
					$con = "cateid in (".$id.")";
					$where ="where $con and channelid=$_REQUEST[channelid]";
					$and = "and $con";
				}else{
					$con = "cateid = ".$_REQUEST[select_id]."";
					$where ="where $con and channelid=$_REQUEST[channelid]";
					$and = "and $con";
				}
		}else{
			$where ="where channelid=$_REQUEST[channelid]";
		}

		$total = count( $this->article->total($where));
		$this->page = new page($total,ARTICLE_PAGE_SIZE,'');

		$val2 = $this->article->findAll("",$this->page->getOffset(),ARTICLE_PAGE_SIZE,"where channelid=$_REQUEST[channelid] $and ","id");
		$fpage =$this->page->fpage();
		$val=$this->get_cateinfo($val2);
		$this->assign("fpage",$fpage);
		$this->assign("article",$val['list']);
		$this->assign("select",$val['select']);
		$this->assign("channelid",$_REQUEST['channelid']);
		$this->display('admin/article/articlemod.html');

	}


	function get_cateinfo($val2){
		$this->cateinfo = new articleModel("_cate");
		$select = $this->cateinfo->cate_select();
		$val = $this->cateinfo->findAll();
		foreach ($val as $key => $v){
			$value[$v['id']]=$v['catename'];
		}
		foreach ($val2 as $key =>$v){
			$val2[$key]['catename']=$value[$v['cateid']];

		}
		$value = array('select'=>$select,'list'=>$val2);
		return $value;


	}

	function article_del(){
		if($_REQUEST["id"]=="data"){
		$_REQUEST["id"]=$_REQUEST["xm"];
		}
		if($this->article->del($_REQUEST["id"])){
			$this->showMsg("成功删除内容","admin.php?m=article&a=article_mod&channelid=$_REQUEST[channelid]");
		}else{
			$this->showMsg("删除内容失败","admin.php?m=article&a=article_mod&channelid=$_REQUEST[channelid]");
		}
	}

	function article_edit(){
		$cate = new articleModel("_cate");
		$_POST=$this->article->find($_REQUEST["id"]);
    	$select = $cate->cate_select();
    	$this->assign('select',$select);
		$this->oFCKeditor->Value=$_POST["content"];
		$this->assign("post",$_POST);
		$this->assign("FCKeditor",$this->oFCKeditor->Create());
		$this->display('admin/article/articleedit.html');


	}


	function article_update(){
			if($this->article->chech_form()){
				if($this->article->mod($_POST)){
					$this->showMsg("新闻修改成功","admin.php?m=article&a=article_edit&channelid=$_REQUEST[channelid]&cateid=$_REQUEST[cateid]");
				}else{
					$this->showMsg("修改新闻失败","admin.php?m=article&a=article_edit&channelid=$_REQUEST[channelid]&cateid=$_REQUEST[cateid]");
				}

			}else{
				$this->showMsg($this->article->getMessList(),"-1");
			}
	}

	function change_cate(){
		if($_REQUEST['id']=='data' && $_REQUEST['change_cate']!=0){

			if($this->article->change_cate()){
				$this->showMsg("成功移动新闻","admin.php?m=article&a=article_mod&channelid=$_REQUEST[channelid]");
			}else{
				$this->showMsg("移动新闻失败","admin.php?m=article&a=article_mod&channelid=$_REQUEST[channelid]");
			}

		}else{
				$this->showMsg("请选择目标分类","admin.php?m=article&a=article_mod&channelid=$_REQUEST[channelid]");
		}
	}





}
?>
