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

326 lines
7.0 KiB
PHP
Raw Normal View History

<?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,$keyword=""){
if(empty($uuid))
{
$wheresql = array();
if(!empty($keyword))
{
if(preg_match("/\'/",$keyword))
{
$keyword = preg_replace("/\'/","''",$keyword);
}
$wheresql[] = " title LIKE '%$keyword%'";
}
if(!empty($uid))
{
$wheresql[] = " userid=$uid ";
}
if(count($wheresql)>0)
{
$wheresql = " WHERE ".join(" AND ",$wheresql);
}else{
$wheresql = "";
}
$sql = "SELECT * FROM ".$this->tbl_fund." $wheresql 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(!empty($keyword))
{
if(preg_match("/\'/",$keyword))
{
$keyword = preg_replace("/\'/","''",$keyword);
}
$wheresql[] = " f.title LIKE '%$keyword%'";
}
if(count($wheresql)>0)
{
$wheresql = " WHERE ".join(" AND ",$wheresql);
}else{
$wheresql = "";
}
if($include === true)
{
$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
2013-04-22 17:24:30 +00:00
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;
}
}
2013-05-28 05:36:26 +00:00
function fetchFromData($count = true,$uid,$keyword){
$wheresql = array();
if(!empty($uid) && is_int($uid))
{
2013-05-28 05:36:26 +00:00
$wheresql[] = " a.userid='$uid' ";
}
if(!empty($keyword))
{
if(preg_match("/\'/",$keyword))
{
$keyword = preg_replace("/\'/","''",$keyword);
}
$wheresql[] = " f.title LIKE '%$keyword%'";
}
2013-05-22 09:13:50 +00:00
if($count !== true)
{
$wheresql[] = " f.id=$count ";
}
$wheresql[] = " md.title IS NOT NULL ";
if(count($wheresql)>0)
{
$wheresql = " WHERE ".join(" AND ",$wheresql);
}else{
$wheresql = "";
}
2013-05-22 09:13:50 +00:00
if($count === true)
{
$sql = "SELECT f.*,count(md.uuid) as mds FROM ".$this->tbl_fund." f
LEFT JOIN ".$this->tbl_mdfund." mf ON mf.fid=f.id
LEFT JOIN metadata md ON md.uuid=mf.uuid
2013-05-28 05:36:26 +00:00
left join mdauthor a on md.uuid=a.uuid
2013-05-22 09:13:50 +00:00
$wheresql
GROUP BY f.id";
}else{
$sql = "SELECT mf.id as mfid,md.title as mdtitle,md.uuid FROM ".$this->tbl_fund." f
LEFT JOIN ".$this->tbl_mdfund." mf ON mf.fid=f.id
LEFT JOIN metadata md ON md.uuid=mf.uuid
2013-05-28 05:36:26 +00:00
left join mdauthor a on md.uuid=a.uuid
$wheresql
2013-05-22 09:13:50 +00:00
GROUP BY md.title,md.uuid,mf.id
";
}
$rs = $this->db->query($sql);
$rows = $rs->fetchAll(PDO::FETCH_BOTH);
return $rows;
}
function add($data,$uuid = NULL)
{
include_once("helper/dbh.php");
$dbh = new dbh($this->db);
if(empty($uuid))
{
return $dbh->insert($this->tbl_fund,$data);
}else{
$fid = $dbh->insert($this->tbl_fund,$data,true);
return $this->addToMdfund($uuid,$fid,1);
}
}
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'=>(int)$id,
'place'=>(int)$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))
{
if(preg_match("/\'/",$keyword))
{
$keyword = preg_replace("/\'/","''",$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;
}
}