westdc-zf1/application/models/data/Fund.php

232 lines
5.0 KiB
PHP

<?php
class Fund extends Zend_Controller_Plugin_Abstract
{
private $db; //传入PDO对象.
private $auth = NULL; //Zend_Auth 对象
public $tbl_fund = "fund"; //项目
public $tbl_mdfund = "mdfund"; //对应表
private $fundFields;
function __construct($db,Zend_Auth $auth=NULL)
{
$this->db = $db;
$this->auth = $auth;
}
function fetch($uuid = "",$include = true,$uid=0){
if(empty($uuid))
{
if(empty($uid))
{
$sql = "SELECT * FROM ".$this->tbl_fund." ORDER BY id desc";
}else{
$sql = "SELECT * FROM ".$this->tbl_fund." WHERE userid=$uid ORDER BY id desc";
}
$rs = $this->db->query($sql);
$rows = $rs->fetchAll();
return $rows;
}else{
$wheresql = array();
if($include)
{
$wheresql[] = " mf.uuid='$uuid' ";
}else{
$fids = $this->getMdFunds($uuid);
if(count($fids)>0)
{
$wheresql[] = ' f.id NOT IN ('.join(",",$fids).')';
}
}
if(!empty($uid) && is_int($uid))
{
$wheresql[] = " f.userid='$uid' ";
}
if(count($wheresql)>0)
{
$wheresql = " WHERE ".join(" AND ",$wheresql);
}else{
$wheresql = "";
}
if($include)
{
$sql = "SELECT f.*,mf.place,mf.id as mfid FROM ".$this->tbl_fund." f
LEFT JOIN ".$this->tbl_mdfund." mf ON mf.fid=f.id
$wheresql
ORDER BY mf.place";
}else{
$sql = "SELECT f.* FROM ".$this->tbl_fund." f $wheresql ORDER BY f.id DESC";
}
$rs = $this->db->query($sql);
$rows = $rs->fetchAll(PDO::FETCH_BOTH);
return $rows;
}
}
function add($data)
{
include_once("helper/dbh.php");
$dbh = new dbh($this->db);
return $dbh->insert($this->tbl_fund,$data);
}
function update($data,$id,$uid=0)
{
if(!is_numeric($id))
{
return "参数错误";
}
if(!empty($uid))
{
if(!is_numeric($uid))
{
return "参数错误";
}
$sql = "SELECT * FROM ".$this->tbl_fund." WHERE userid=$uid AND id=$id";
$rs = $this->db->query($sql);
$row = $rs->fetch();
if(empty($row['id']))
{
return "您无权修改这条记录";
}
$condition = " id=$id AND userid=$uid ";
}else{
$condition = " id=$id ";
}
include_once("helper/dbh.php");
$dbh = new dbh($this->db);
$state = $dbh->update($this->tbl_fund,$data,$condition,true);
if( $state == true)
{
return true;
}else{
return $state;
}
}
function delete($id,$uid=0){
if(!is_numeric($id))
{
return "参数错误";
}
if(!empty($uid))
{
if(!is_numeric($uid))
{
return "参数错误";
}
$sql = "SELECT * FROM ".$this->tbl_fund." WHERE userid=$uid AND id=$id";
$rs = $this->db->query($sql);
$row = $rs->fetch();
if(empty($row['id']))
{
return "您无权修改这条记录";
}
$condition = " id=$id AND userid=$uid ";
}else{
$condition = " id=$id ";
}
$sql = "DELETE FROM ".$this->tbl_fund." WHERE $condition";
return $this->db->exec($sql);
}
function mfdelete($id){
$sql = "DELETE FROM ".$this->tbl_mdfund." WHERE id=$id";
return $this->db->exec($sql);
}
function view($id){
$sql = "SELECT * FROM ".$this->tbl_fund." WHERE id=$id";
$rs = $this->db->query($sql);
$row = $rs->fetch();
return $row;
}
function getMdFunds($uuid){
$sql = "SELECT * FROM ".$this->tbl_mdfund." WHERE uuid='$uuid'";
$rs = $this->db->query($sql);
$rows = $rs->fetchAll();
$fids = array();
foreach($rows as $k=>$v)
{
$fids[] = $v['fid'];
}
return $fids;
}
function addToMdfund($uuid,$id,$order){
if(empty($order))
{
$order = 0;
}
$data = array(
'uuid'=>$uuid,
'fid'=>$id,
'place'=>$order
);
include_once("helper/dbh.php");
$dbh = new dbh($this->db);
return $dbh->insert($this->tbl_mdfund,$data);
}
function changeorder($id,$order){
if(!is_numeric($id) || !is_numeric($order))
{
return false;
}
$data = array(
'place'=>$order
);
include_once("helper/dbh.php");
$dbh = new dbh($this->db);
$state = $dbh->update($this->tbl_mdfund,$data,"id=$id",true);
if( $state == true)
{
return true;
}else{
return $state;
}
}
function getData($id,$keyword="")
{
$search = "";
if(!empty($keyword))
{
$search = " AND md.title LIKE '%$keyword%' ";
}
$sql = "SELECT md.title,mf.id FROM ".$this->tbl_mdfund." mf
LEFT JOIN ".$this->tbl_fund." f ON mf.fid=f.id
LEFT JOIN metadata md ON md.uuid=mf.uuid
WHERE f.id=$id $search";
$rs = $this->db->query($sql);
$rows = $rs->fetchAll();
return $rows;
}
function _getParams(Zend_Controller_Request_Abstract $request)
{
$data = array(
'title' => trim($request->getParam('title')),
'fund_id' => trim($request->getParam('fund_id')),
'fund_type' => trim($request->getParam('fund_type')),
'title_en' => trim($request->getParam('title_en')),
'fund_type_en' => trim($request->getParam('fund_type_en')),
);
return $data;
}
}