<?php
/* 
*本类用于处理数据包括数据库的查询，新增更改和更新；
*/
class DatabaseAction extends Action {

	public $table;
	public function _initialize($table,$a){
	   $this->table = $table;
	}
	public function getlist($Where='',$Order='',$config='',$num=10,$table='',$theme=''){
		if($table){
	    	$Table = M($table);
		}else{
			$Table = M($this->table);
		}
		import('ORG.Util.Page');// 导入分页类
		$count      = $Table->where($Where)->count();// 查询满足要求的总记录数
		$Page       = new Page($count,$num);// 实例化分页类 传入总记录数和每页显示的记录数
		$Page->setConfig('header','$config');
		if($theme){
			$Page->setConfig('theme',$theme);
		}
		$show       = $Page->show();// 分页显示输出
		$list = $Table->where($Where)->order($Order)->limit($Page->firstRow.','.$Page->listRows)->select();
		$result['list'] = $list;
		$result['page'] = $show;
		return $result;
	}
	public function getDlist($Where='',$Order='',$config='',$table='',$num=10,$theme=''){
		if($table){
	    	$Table = D($table);
		}else{
			$Table = D($this->table);
		}
		import('ORG.Util.Page');// 导入分页类
		$count      = $Table->where($Where)->count();// 查询满足要求的总记录数
		$Page       = new Page($count,$num);// 实例化分页类 传入总记录数和每页显示的记录数
		$Page->setConfig('header','$config');
		if($theme){
			$Page->setConfig('theme',$theme);
		}
		$show       = $Page->show();// 分页显示输出
		$list = $Table->relation(true)->where($Where)->order($Order)->limit($Page->firstRow.','.$Page->listRows)->select();
		$result['list'] = $list;
		$result['page'] = $show;
		return $result;
	}
	public function addone($table='',$data,$SavePath='',$MaxSize=3145728){
		if($data){
			if($table){
				$Table = M($table);
			}else{
				$Table = M($this->table);
			}
			import('ORG.Net.UploadFile');
			$upload = new UploadFile();// 实例化上传类
			$upload->maxSize  = $MaxSize ;// 设置附件上传大小
		 	$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->savePath =  'Public/Uploads/'.$SavePath;// 设置附件上传目录
			if(!$upload->upload()) {// 上传错误提示错误信息
			}else{// 上传成功 获取上传文件信息
				$info =  $upload->getUploadFileInfo();
			}
			if(!empty($info)){
			$Table->photo = $info[0]['savename']; // 保存上传的照片根据需要自行组装
			$_POST['img'] = $info[0]['savename'];
			}
			$_POST['infor'] =stripslashes($_POST['infor']);
			if($Table->add($_POST,$options=array(),$replace=true)){
			  $this->success('编辑成功');
			}else{
			  $this->error('编辑失败');
			} 	
			exit;
		}
	}
	public function delone($table='',$id='',$idname='',$MaxSize=3145728){
	    if($id){
			if($table){
				$Table = M($table);
			}else{
				$Table = M($this->table);
			}
	 		$img = $Table->where($idname."=".$id)->getField('img');
	        unlink("Public/Uploads/Table/".$img);
			if($Table->where($idname."=".$id)->delete()){
			  $this->success('删除成功');
			}else{
			  $this->error('删除失败');
			} 
		}
	}
	public function getone($id='',$idname='',$data='',$table=''){
		if($table){
			$Table = M($table);
		}else{
			$Table = M($this->table);
		}
		$data = $Table->where($idname."=".$id)->save($data);
        echo json_encode($data);  //jsonp请求
		return $data;
    }
}