2013-04-22 02:35:32 +00:00
|
|
|
<?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;
|
|
|
|
}
|
|
|
|
|
2013-05-22 07:06:30 +00:00
|
|
|
function fetch($uuid = "",$include = true,$uid=0,$keyword=""){
|
2013-04-22 02:35:32 +00:00
|
|
|
if(empty($uuid))
|
|
|
|
{
|
2013-05-22 07:06:30 +00:00
|
|
|
$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)
|
2013-04-22 02:35:32 +00:00
|
|
|
{
|
2013-05-22 07:06:30 +00:00
|
|
|
$wheresql = " WHERE ".join(" AND ",$wheresql);
|
2013-04-22 02:35:32 +00:00
|
|
|
}else{
|
2013-05-22 07:06:30 +00:00
|
|
|
$wheresql = "";
|
2013-04-22 02:35:32 +00:00
|
|
|
}
|
2013-05-22 07:06:30 +00:00
|
|
|
|
|
|
|
$sql = "SELECT * FROM ".$this->tbl_fund." $wheresql ORDER BY id desc";
|
|
|
|
|
2013-04-22 02:35:32 +00:00
|
|
|
$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' ";
|
|
|
|
}
|
2013-05-22 07:06:30 +00:00
|
|
|
|
|
|
|
if(!empty($keyword))
|
|
|
|
{
|
|
|
|
if(preg_match("/\'/",$keyword))
|
|
|
|
{
|
|
|
|
$keyword = preg_replace("/\'/","''",$keyword);
|
|
|
|
}
|
|
|
|
$wheresql[] = " f.title LIKE '%$keyword%'";
|
|
|
|
}
|
2013-04-22 02:35:32 +00:00
|
|
|
|
|
|
|
if(count($wheresql)>0)
|
|
|
|
{
|
|
|
|
$wheresql = " WHERE ".join(" AND ",$wheresql);
|
|
|
|
}else{
|
|
|
|
$wheresql = "";
|
|
|
|
}
|
|
|
|
|
2013-05-22 07:06:30 +00:00
|
|
|
if($include === true)
|
2013-04-22 02:35:32 +00:00
|
|
|
{
|
|
|
|
$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";
|
2013-04-22 02:35:32 +00:00
|
|
|
}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){
|
2013-05-22 07:06:30 +00:00
|
|
|
$wheresql = array();
|
|
|
|
|
|
|
|
if(!empty($uid) && is_int($uid))
|
|
|
|
{
|
2013-05-28 05:36:26 +00:00
|
|
|
$wheresql[] = " a.userid='$uid' ";
|
2013-05-22 07:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 ";
|
|
|
|
}
|
|
|
|
|
2013-05-22 07:06:30 +00:00
|
|
|
$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
|
2013-05-31 02:48:56 +00:00
|
|
|
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
|
2013-05-22 07:06:30 +00:00
|
|
|
LEFT JOIN ".$this->tbl_mdfund." mf ON mf.fid=f.id
|
2013-05-31 02:48:56 +00:00
|
|
|
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 07:06:30 +00:00
|
|
|
$wheresql
|
2013-05-22 09:13:50 +00:00
|
|
|
GROUP BY md.title,md.uuid,mf.id
|
|
|
|
";
|
|
|
|
}
|
2013-05-22 07:06:30 +00:00
|
|
|
|
|
|
|
$rs = $this->db->query($sql);
|
|
|
|
$rows = $rs->fetchAll(PDO::FETCH_BOTH);
|
|
|
|
return $rows;
|
|
|
|
}
|
|
|
|
|
2013-05-31 02:48:56 +00:00
|
|
|
function add($data,$uuid = NULL)
|
2013-04-22 02:35:32 +00:00
|
|
|
{
|
|
|
|
include_once("helper/dbh.php");
|
|
|
|
$dbh = new dbh($this->db);
|
2013-05-31 02:48:56 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2013-04-22 02:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2013-05-31 02:48:56 +00:00
|
|
|
'fid'=>(int)$id,
|
|
|
|
'place'=>(int)$order
|
2013-04-22 02:35:32 +00:00
|
|
|
);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-16 08:47:29 +00:00
|
|
|
function getData($id,$keyword="")
|
2013-04-22 02:35:32 +00:00
|
|
|
{
|
2013-05-16 08:47:29 +00:00
|
|
|
$search = "";
|
|
|
|
if(!empty($keyword))
|
|
|
|
{
|
2013-05-22 07:06:30 +00:00
|
|
|
if(preg_match("/\'/",$keyword))
|
|
|
|
{
|
|
|
|
$keyword = preg_replace("/\'/","''",$keyword);
|
|
|
|
}
|
2013-05-16 08:47:29 +00:00
|
|
|
$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;
|
2013-04-22 02:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|