<?php

Class DownAction extends CommonAction {

   public function insert() {
   	    $file=$_FILES['attachment']['name'];
		//初始值设为空
		$savefile='';	
		$Down = D("Down");
		$Path=date('Ym');
		
		if ($vo = $Down->create()) {
			if ($file){
				$uploadfile=$this->_upload('Down',$Path);
				$savefile=$uploadfile[0]['savename'];
				
			}
			$Down->attachpath=$Path;
			$Down->savepath = './Public/Uploads/';
			$Down->attachment=$savefile;
			
			$list = $Down->add();
			if ($list) {
				//数据保存触发器
				
				$this->success("操作成功");
			}else {
				$this->error("操作失败");
			}
		}else {
			$this->error($Down->getError());
		}
	}


        public function _after_show() {
            $Down = D("Down");
            $down = $this->get('vo');
            // 阅读计数
            $id	= $down["id"];
            if(!isset($_SESSION['down_read_count_'.$id])) {
		$Down->setInc('readCount',"id=".$id);
		$_SESSION['down_read_count_'.$id] = true;
            }
        }

        public function _before_add() {
                $verify = build_verify(8);
                $_SESSION['down_verify'] = $verify;
                $this->assign('verify',$verify);
        }
        public function add() {
            $this->display();
        }
	public function index() {
            $Down = M("Down");
            $count = $Down->count();
            $listRows = 12;
            //$fields	= 'id,title,categoryId,category,cTime,readCount,commentCount';
         
            import("@.ORG.Page");
            $p = new Page($count, $listRows);
            $p->setConfig("header", "篇文档");
            
            $list =  $Down->order("id desc")->limit($p->firstRow.','.$p->listRows)->select();
            $page = $p->show();
            $this->assign("list", $list);
            $this->assign("count", $count);
            $this->assign("page", $page);

            //统计数据
            $Down = M("Down");
            $stat = array();
            $stat["beginTime"] = $Down->min('uploadTime');
            $stat["downCount"] = $Down->count();
            $stat["readCount"] = $Down->sum("downloadTime");
          
            $this->assign($stat);

            $this->display();
	}

        public function edit() {
            $Down = M("Down");
            $id = $_REQUEST['id'];
            $down = $Down->where('id='.$id)->find();
            //dump($down);
            if($down) {
                $this->assign('vo',$down);
                $this->display();
            }else{
                $this->redirect('Down/index');
            }
	}
        public function update() {
            $model = D("Down");
            $vo = $model->create('','edit');
            if(!$vo) {
                $this->error($model->getError());
            }
            $id	= is_array($vo)?$vo[$model->getPk()]:$vo->{$model->getPk()};
            $result  = $model->save($vo);
            $file=$_FILES['attachment']['name'];
		    $attachpath=trim($_POST['attachpath']);
		    $oattach=trim($_POST['attachment']);
		
		    if($result) {
                //如果有文件上传
                if ($file){
				if ($attachpath=="") $attachpath=date('Ym');
				$uploadfile=$this->_upload('down',$attachpath);
				$savefile=$uploadfile[0]['savename'];
				@unlink(C(ATTACHDIR).$attachpath.'/'.$oattach);
			}
			$model->savepath = './Public/Uploads/';
			$model->attachment=$savefile;
			$list = $model->save();
			if($list) {
			    //成功提示
                $this->success("操作成功！");
            }else {
                //错误提示
                $this->error("操作失败");
            }
		    }
        }
        	  // 删除数据 
    public function delete() { 
        if(!empty($_POST['id'])) { 
            $Down    =    M("Down"); 
            $result    =    $Down->delete($_POST['id']); 
            if(false !== $result) { 
                $this->ajaxReturn($_POST['id'],'删除成功！',1); 
            }else{ 
                $this->error('删除出错！'); 
            } 
        }else{ 
            $this->error('删除项不存在！'); 
        } 
    }

   /**
     +----------------------------------------------------------
     * 文件上传功能，支持多文件上传、保存数据库、自动缩略图
     +----------------------------------------------------------
        * @return void
     +----------------------------------------------------------
     * @throws ThinkExecption
     +----------------------------------------------------------
     */
        // 文件上传
    protected function _upload() {
        import("@.ORG.UploadFile");
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize = 3292200;
        //设置上传文件类型
        $upload->allowExts  = array('rar','zip','doc','swf','txt','ppt','jpg','gif');
        //设置附件上传目录
        $upload->savePath = './Public/Uploads/';
        if(!empty($_POST['_uploadImgThumb'])) {
            //设置需要生成缩略图，仅对图像文件有效
            $upload->thumb =  $_POST['_uploadImgThumb'];
            $upload->imageClassPath = '@.ORG.Image';
        }
        if(!empty($_POST['_uploadThumbSuffix'])) {
            //设置需要生成缩略图的文件后缀
            $upload->thumbSuffix =  $_POST['_uploadThumbSuffix'];
        }
        if(!empty($_POST['_uploadThumbMaxWidth'])) {
            //设置缩略图最大宽度
            $upload->thumbMaxWidth =  $_POST['_uploadThumbMaxWidth'];
        }
        if(!empty($_POST['_uploadThumbMaxHeight'])) {
            //设置缩略图最大高度
            $upload->thumbMaxHeight =  $_POST['_uploadThumbMaxHeight'];
        }
        //设置上传文件规则
        if(isset($_POST['_uploadSaveRule'])) {
            //设置附件命名规则
            $upload->saveRule =  $_POST['_uploadSaveRule'];
        }else{
              $upload->saveRule = 'uniqid';
        }
        //删除原图
        $upload->thumbRemoveOrigin = true;
        if (!$upload->upload()) {
            //捕获上传异常
            $this->error($upload->getErrorMsg());
        } else {

          return $upload->getUploadFileInfo();
        }
        
    }
 
}
?>