<?php
/*
　* @$Id
　* @
　* @Ver: 2.0
　* @Create: 2009-11-18 8:10
　* @Modify: 
　* @Author: CPWEB http://www.cpweb.net.cn
　* @Copyright (C) 2008 www.cpweb.net.cn. All Rights Reserved.
　* @License CPWEB is free software and use is subject to license terms
　*/
class CurdAction extends Action{
	public $model=MODULE_NAME;
	public $cpweb_model='';
	public $cpweb_find=array();
	public $cpweb_page='';
	public $cpweb_list='';
	public $cpweb_read='';
	public $cpweb_count='';
	function _initialize()
	{
		if($this->model=='Curd'){
			$this->error('此操作无效。');
		}
	}
	public function cpweb_read(){
		$id=intval($_REQUEST["id"]);
		if(!$id) $this->error(L('CPWEB_SELECT_NOT_EXIST'));
		if($this->cpweb_find['view']){
			$map[$this->model.'.id']=$id;
		}else{
			$map['id']=$id;
		}
		if($this->cpweb_model){
			if($this->cpweb_find['view']){
				$Read=D($this->model.$this->cpweb_find['view']);
			}else{
				$Read=M($this->model);
			}
		}else{
			if($this->cpweb_find['view']){
				$Read=D($this->model.$this->cpweb_find['view']);
			}else{
				$Read=M($this->model);
			}
		}
		if($this->cpweb_find['setInc']){
			if($this->cpweb_find['view']){
				$Read->setInc($this->model.'.'.$this->cpweb_find['setInc'],$this->model.'.id='.$id);
			}else{
				$Read->setInc($this->cpweb_find['setInc'],'id='.$id);
			}
		}
		$this->cpweb_read=$Read->where($map)->find();
		if (!$this->cpweb_read) $this->error(L('CPWEB_SELECT_NOT_EXIST'));
	}
	public function cpweb_list(){
		if($this->cpweb_model){
			if($this->cpweb_model=='model'){
				if($this->cpweb_find['view']){
					if($this->cpweb_find['channel']) $map['channel.id']=$this->cpweb_find['channel'];
					if($this->cpweb_find['category']) $map['Category.id']=$this->cpweb_find['category'];
				}else{
					if($this->cpweb_find['category']){
						$map['category_id']=$this->cpweb_find['category'];
					}
				}
			}
			if($this->cpweb_find['view']){
				$List=D($this->model.$this->cpweb_find['view']);
			}else{
				$List=M($this->model);
			}
		}else{
			if($this->cpweb_find['view']){
				$List=D($this->model.$this->cpweb_find['view']);
			}else{
				$List=M($this->model);
			}
		}
		if($_REQUEST['key']){
			if($_REQUEST['key']['keyword']) $map[$_REQUEST['key']['act']]=array('like',$_REQUEST['key']['keyword']."%");
			if($_REQUEST['key']['category_id']) $map[$this->model.'.category_id']=$_REQUEST['key']['category_id'];
		}
		$this->cpweb_count=$List->count();
		if($this->cpweb_count){
			if($this->cpweb_find['page']){
				import("@.ORG.Page");
				$listRows=15;
				$p=new Page($this->cpweb_count,$listRows);
				$this->cpweb_list=$List->limit($p->firstRow.','.$p->listRows)->where($map)->order('id desc')->findAll();
				$this->cpweb_page=$p->show();
			}else{
				$this->cpweb_list=$List->where($map)->order('id desc')->limit($this->cpweb_find['limit'])->findAll();
				$this->cpweb_page='共'.$this->cpweb_count.'条记录';
			}
		}
	}
	public function cpweb_add(){
		$Read=D($this->model);
		if($Read->create($_POST['data'])){
			if($_FILES['fileToUpload']['name']){
				$cpweb_upload=$this->cpweb_upload_new();
				$Read->attachment=$cpweb_upload['cpweb_url'];
				if($this->cpweb_find['thumb']) $Read->attachthumb=$cpweb_upload['cpweb_thumb_url'];
			}
			if($Read->add()){
				if($this->cpweb_find['cpweb_url']){
					$this->assign('jumpUrl',$this->cpweb_find['cpweb_url']);
				}else{
					$this->assign('jumpUrl',__URL__);
				}
				$this->success(L('CPWEB_SUCCESS_ADD'));
			}else{
				$this->error(L('CPWEB_ERROR_ADD'));
			}
		}else{
			$this->error($Read->getError());
		}
	}
	public function cpweb_edit(){
		$id=intval($_REQUEST['data']['id']);
		$Read=D($this->model);
		$map['id']=$id;
		$read=$Read->where($map)->find();
		if(!$id||!$read) $this->error(L('CPWEB_SELECT_NOT_EXIST'));
		if($Read->create($_POST['data'])){
			if($_FILES['fileToUpload']['name']){
				$cpweb_upload=$this->cpweb_upload_new();
				if($read['attachment']) $this->cpweb_new_delete_attach($read);
				if($read['attachthumb']) $this->cpweb_new_delete_attachthumb($read);
				$Read->attachment=$cpweb_upload['cpweb_url'];
				if($this->cpweb_find['thumb']) $Read->attachthumb=$cpweb_upload['cpweb_thumb_url'];
			}
			if($Read->save()){
				if($this->cpweb_find['cpweb_url']){
					$this->assign('jumpUrl',$this->cpweb_find['cpweb_url']);
				}else{
					$this->assign('jumpUrl',__URL__);
				}
				$this->success(L('CPWEB_SUCCESS_EDIT'));
			}else{
				$this->error(L('CPWEB_ERROR_EDIT'));
			}
		}else{
			$this->error($Read->getError());
		}
	}
	public function cpweb_delete(){
		$Read=D($this->model);
		$id=intval($_REQUEST['id']);
		$read=$Read->find($id);
		if($this->cpweb_find['xt']==true){
			if($read['attachment']) $this->cpweb_new_delete_attach($read);
			if($read['attachthumb']) $this->cpweb_new_delete_attachthumb($read);
			if($Read->delete()){
				$this->success(L('CPWEB_SUCCESS_DELETE'));
			}else{
				$this->error(L('CPWEB_ERROR_DELETE'));
			}
		}else{
			$this->error(L('CPWEB_ERROR_DELETE_CPWEB'));
		}
	}
	public function cpweb_upload_new(){
		$path=date('Ym');
		$path=C('ATTACHDIR').'/new/'.$path.'/';
		if (!is_dir($path))	@mk_dir($path);   
		import("@.ORG.UploadFile");
        $upload = new UploadFile();
		$attachsize=C('ATTACHSIZE');
		$attach=C('ATTACH');
		$thumbmaxwidth=($this->cpweb_set('thumbmaxwidth')=="")?C('THUMBMAXWIDTH'):$module;
		$upload->maxSize=$attachsize;
		$upload->allowExts=explode(',',strtolower(C('attachext')));
		$upload->savePath=$path;
		$upload->saveRule='uniqid';
		isset($this->cpweb_find['thumb'])?$upload->thumb=$this->cpweb_find['thumb']:$upload->thumb=$attach;
		isset($width)?$upload->thumbMaxWidth=$width:$upload->thumbMaxWidth=C('thumbmaxwidth');
		isset($height)?$upload->thumbMaxHeight=$height:$upload->thumbMaxHeight=C('thumbmaxheight');
		$upload->thumbPrefix ='';
		$upload->thumbSuffix =C('thumbsuffix');
        if(!$upload->upload()){
			$return_upload=array(0,$upload->getErrorMsg(),0);
			return $return_upload;
        }else{
			$cpweb_up=$upload->getUploadFileInfo();
			$cpweb_newup['cpweb_url']=$upload->savePath.$cpweb_up[0]['savename'];
			$cpweb_newup['cpweb_newup']=1;
			if($this->cpweb_find['thumb']){
				$cpweb_thumb=explode('.',$cpweb_up[0]['savename']);
				$cpweb_newup['cpweb_thumb_url']=$upload->savePath.$cpweb_thumb[0].C('thumbsuffix').'.'.$cpweb_thumb[1];
			}
			return $cpweb_newup;
    	}
	}
	public function cpweb_upload(){
		$path=C('ATTACHDIR').'/';
		if (!is_dir($path))	@mk_dir($path);
		import("@.ORG.UploadFile");
        $upload = new UploadFile();
		$attachsize=C('ATTACHSIZE');
		$thumbmaxwidth=C('thumbmaxwidth');
		$upload->maxSize=$attachsize;
		$upload->allowExts=explode(',',strtolower(C('attachext')));
		$upload->savePath=$path;
		$upload->uploadReplace=true;
		$upload->saveRule='logo';
        if(!$upload->upload()){
			$return_upload=array(0,$upload->getErrorMsg(),0);
			return $return_upload;
        }else{
			$cpweb_up=$upload->getUploadFileInfo();
			$cpweb_newup['cpweb_url']=$upload->savePath.$cpweb_up[0]['savename'];
			$cpweb_newup['cpweb_newup']=1;
			if($this->cpweb_find['thumb']){
				$cpweb_thumb=explode('.',$cpweb_up[0]['savename']);
				$cpweb_newup['cpweb_thumb_url']=$upload->savePath.$cpweb_thumb[0].C('thumbsuffix').'.'.$cpweb_thumb[1];
			}
			return $cpweb_newup;
    	}
	}
	public function cpweb_set($title){
		$maps["title"]=$title;
		$Settings=M('Settings')->where($maps)->find();
		return $Settings['set_values'];
	}
	/*
	@删除原文件
	*/
	public function cpweb_new_delete_attach($name){
		$delete=attach($name,$this->model,C('ATTACHDIR'),false);
		$this->cpweb_new_delete_file($delete);
	}
	/*
	@删除缩略图
	*/
	public function cpweb_new_delete_attachthumb($name){
		$delete=attachthumb($name,$this->model,C('ATTACHDIR'),false);
		$this->cpweb_new_delete_file($delete);
	}
	/*
	@删除文件
	*/
	public function cpweb_new_delete_file($name){
		$delete=@unlink(CPWEB_ROOT.$name);
	}
}
?>