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


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


    }



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

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

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


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




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

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

    }


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

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


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

	}



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

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

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

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

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


	}


//==================上面为分类==下面为下载=====================
	function download_add(){
		$this->download = new downloadModel("_cate");
    	$select = $this->download->cate_select();
    	$this->assign('select',$select);
    	$this->assign('channelid',$_REQUEST['channelid']);
		$this->assign("FCKeditor",$this->oFCKeditor->Create());
		$this->display('admin/download/download.html');
	}


	function download_insert(){

			if($this->download->chech_form()){
				if(empty($_POST['fileurl'])){
					$this->upload = new upload(array('filepath'=>ATTCHMENT_REAL_PATH.'download','allowtype'=>array('zip','rar','pdf'),'israndname'=>false));
					if($this->upload->uploadFile('fileurl')){
						$_POST['filesize']= Common::sizeCount($this->upload->getFileSize());
						$_POST['fileurl'] = 'download/'.$this->upload->getNewFileName();
					}else{
						$this->showMsg($this->upload->getErrorMsg(),'-1');
					}
				}
				$_POST['publish']=time();
				if($this->download->add($_POST)){
					$this->showMsg("成功添加下载","admin.php?m=download&a=download_add&channelid=$_REQUEST[channelid]");
				}else{
					$this->showMsg("添加下载失败","admin.php?m=download&a=download_add&channelid=$_REQUEST[channelid]");;
				}

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

	}




	function download_mod(){
		$where=$and="";
		if(isset($_REQUEST[select_id])){
			$this->dowcate = new downloadModel("_cate");
			$id = $this->dowcate->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->download->total($where));
		$this->page = new page($total,10,'');

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

	}


	function get_cateinfo($val2){
		$this->cateinfo = new downloadModel("_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 download_del(){
		if($_REQUEST["id"]=="data"){
		$_REQUEST["id"]=$_REQUEST["xm"];
		}
		if($this->download->del($_REQUEST["id"])){
			$this->showMsg("成功删除内容","admin.php?m=download&a=download_mod&channelid=$_REQUEST[channelid]");
		}else{
			$this->showMsg("删除内容失败","admin.php?m=download&a=download_mod&channelid=$_REQUEST[channelid]");
		}
	}


	function download_edit(){
		$cate = new downloadModel("_cate");
		$_POST=$this->download->find($_REQUEST["id"]);
    	$select = $cate->cate_select();
    	$this->assign('select',$select);
		$this->oFCKeditor->Value=$_POST["description"];
		$this->assign("post",$_POST);
		$this->assign("FCKeditor",$this->oFCKeditor->Create());
		$this->display('admin/download/downloadedit.html');


	}

	function download_update(){
			if($this->download->chech_form()){
				if(empty($_POST['fileurl'])){
					$this->upload = new upload(array('filepath'=>ATTCHMENT_REAL_PATH.'download','allowtype'=>array('zip','rar','pdf'),'israndname'=>false));
					if($this->upload->uploadFile('fileurl')){
						$_POST['filesize']= common::sizeCount($this->upload->getFileSize());
						$_POST['fileurl'] = 'download/'.$this->upload->getNewFileName();
						if($_POST['fileurl']!==$_POST['unlink']){
							@unlink(ATTCHMENT_REAL_PATH.$_POST['unlink']);
						}
					}else{
						$this->showMsg($this->upload->getErrorMsg(),'-1');
					}
				}
				if($this->download->mod($_POST)){
					$this->showMsg("下载修改成功","admin.php?m=download&a=download_edit&channelid=$_REQUEST[channelid]&cateid=$_REQUEST[cateid]&id=$_REQUEST[id]");
				}else{
					$this->showMsg("修改下载失败","admin.php?m=download&a=download_edit&channelid=$_REQUEST[channelid]&cateid=$_REQUEST[cateid]&id=$_REQUEST[id]");
				}

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

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

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

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








}
?>
