westdc-zf1/application/module/Fund/Fund.php

457 lines
10 KiB
PHP

<?php
namespace Fund;
use \Helpers\View as view;
use \Helpers\dbh;
use \Fund\Listener\FundListener;
use \Files\Files;
class Fund
{
private $db; //传入PDO对象.
private $config; //站点设置
protected $events = NULL;
public $table;
public $keyword;
public $order;
public $sort = "DESC";
public $field;
function __construct($db = NULL,$mail = NULL)
{
if(empty($db))
{
$this->db = \Zend_Registry::get('db');
}else{
$this->db = $db;
}
$this->config = \Zend_Registry::get('config');
$Listener = new FundListener();
@$this->events()->attachAggregate($Listener);
$this->table = new \Helpers\Table();
}
public function events(\Zend_EventManager_EventCollection $events = NULL)
{
if ($events !== NULL) {
$this->events = $events;
} elseif ($this->events === NULL) {
$this->events = new \Zend_EventManager_EventManager(__CLASS__);
}
return $this->events;
}
public function fund($id = 0)
{
$data = $this->getFundParam();
$params = compact('data');
$results = $this->events()->trigger('submit.checkParam', $this, $params);
$cache_data = $results->bottom();
if($cache_data !== true)
{
return $cache_data;
}
$results = $this->events()->trigger('submit.processData', $this, $params);
$data = $results->bottom();
unset($data['submit']);
$dbh = new dbh();
if(empty($id))
{
$id = $dbh->insert($this->table->fund,$data,true);
}else{
if(!$dbh->update($this->table->fund,$data," id=$id ",true))
{
return "修改失败!请重试";
}
}
if(!empty($id) && is_numeric($id))
{
return true;
}else{
return "修改失败";
}
}
//获得参数
public function getFundParam(\Zend_Controller_Request_Abstract $request = NULL)
{
$request = new \Zend_Controller_Request_Http();
$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')),
'userid' => (int)$request->getParam('userid'),
);
return $data;
}
//所有项目
public function fetchFunds()
{
$wheresql = array();
if(!empty($this->keyword))
{
$wheresql[] = " ({$this->table->fund}.title LIKE '%{$this->keyword}%' OR {$this->table->fund}.fund_id LIKE '%{$this->keyword}%') ";
}
if(!empty($this->field))
{
foreach($this->field as $k=>$v)
{
if(!empty($v))
{
if(!is_numeric($v)) $v="'{$v}'";
$wheresql[] = " ({$this->table->fund}.{$k}={$v} ) ";
}else{
if(is_numeric($v))
$wheresql[] = " ({$this->table->fund}.{$k} IS NULL OR {$this->table->fund}.{$k}=0 ) ";
else
$wheresql[] = " ({$this->table->fund}.{$k} IS NULL ) ";
}//if(empty($v)
}//foreach
}
if(count($wheresql)>0)
{
$wheresql = " WHERE ".join(" AND ",$wheresql);
}else{
$wheresql = "";
}
if(empty($this->order))
{
$order = "{$this->table->fund}.title";
}else{
$order = "{$this->table->fund}.{$this->order}";
}
$sql = "SELECT {$this->table->fund}.* FROM
{$this->table->fund}
$wheresql
ORDER BY $order {$this->sort}";
$rs = $this->db->query($sql);
return $rs->fetchAll();
}
//Get references by data UUID
public function fetchFundsByUUID($uuid)
{
$wheresql = array();
$wheresql[]=" mf.uuid='$uuid' ";
if(!empty($this->keyword))
{
$wheresql[] = " (f.title iLIKE '%{$this->keyword}%' OR f.fund_id iLIKE '%{$this->keyword}%') ";
}
if(count($wheresql)>0)
{
$wheresql = " WHERE ".join(" AND ",$wheresql);
}else{
$wheresql = "";
}
if(empty($this->order))
{
$order = "f.title";
}else{
$order = "f.{$this->order} {$this->sort}";
}
$sql="select distinct f.*,mf.place,mf.id as mfid from {$this->table->fund} f left join {$this->table->metadata_fund} mf on f.id=mf.fid
$wheresql
ORDER BY $order";
$rs=$this->db->query($sql);
return $rs->fetchAll();
}
//Get references with data UUID
//包含mdfund的对应信息
public function fetchFundsWithUUID($uuid)
{
$wheresql = array();
//$wheresql[]=" mf.uuid='$uuid' ";
if(!empty($this->keyword))
{
$wheresql[] = " (f.title LIKE '%{$this->keyword}%' OR f.fund_id iLIKE '%{$this->keyword}%') ";
}
if(count($wheresql)>0)
{
$wheresql = " WHERE ".join(" AND ",$wheresql);
}else{
$wheresql = "";
}
if(empty($this->order))
{
$order = "f.title";
}else{
$order = "f.{$this->order} {$this->sort}";
}
$sql="select distinct f.*,mf.place,mf.id as mfid from {$this->table->fund} f left join
(select * from {$this->table->metadata_fund} where uuid='$uuid') mf on f.id=mf.fid
$wheresql
ORDER BY $order";
$rs=$this->db->query($sql);
return $rs->fetchAll();
}
//数据相关的项目(已和数据进行关联)
public function fetchDataFunds()
{
$wheresql = array();
if(!empty($this->keyword))
{
$wheresql[] = " ({$this->table->fund}.title LIKE '%{$this->keyword}%' OR {$this->table->fund}.fund_id LIKE '%{$this->keyword}%') ";
}
if(!empty($this->field))
{
foreach($this->field as $k=>$v)
{
if(!empty($v))
{
if(!is_numeric($v)) $v="'{$v}'";
$wheresql[] = " ({$this->table->fund}.{$k}={$v} ) ";
}else{
if(is_numeric($v))
$wheresql[] = " ({$this->table->fund}.{$k} IS NULL OR {$this->table->fund}.{$k}=0 ) ";
else
$wheresql[] = " ({$this->table->fund}.{$k} IS NULL ) ";
}//if(empty($v)
}//foreach
}
if(count($wheresql)>0)
{
$wheresql = " WHERE ".join(" AND ",$wheresql);
}else{
$wheresql = "";
}
if(empty($this->order))
{
$order = "{$this->table->fund}.title";
}else{
$order = "{$this->table->fund}.{$this->order}";
}
$sql = "SELECT {$this->table->fund}.*,count({$this->table->metadata_fund}.uuid) as mdcount FROM
{$this->table->fund} left join {$this->table->metadata_fund}
on {$this->table->fund}.id={$this->table->metadata_fund}.fid
$wheresql
group by {$this->table->fund}.id
ORDER BY $order {$this->sort}";
$rs = $this->db->query($sql);
return $rs->fetchAll();
}
//数据不相关的项目(没有和数据进行关联)
public function fetchNoDataFunds()
{
$wheresql = array();
$wheresql[] = " id not in (select fid from {$this->table->metadata_fund}) ";
if(!empty($this->keyword))
{
$wheresql[] = " ({$this->table->fund}.title LIKE '%{$this->keyword}%' OR {$this->table->fund}.fund_id LIKE '%{$this->keyword}%') ";
}
if(!empty($this->field))
{
foreach($this->field as $k=>$v)
{
if(!empty($v))
{
if(!is_numeric($v)) $v="'{$v}'";
$wheresql[] = " ({$this->table->fund}.{$k}={$v} ) ";
}else{
if(is_numeric($v))
$wheresql[] = " ({$this->table->fund}.{$k} IS NULL OR {$this->table->fund}.{$k}=0 ) ";
else
$wheresql[] = " ({$this->table->fund}.{$k} IS NULL ) ";
}//if(empty($v)
}//foreach
}
if(count($wheresql)>0)
{
$wheresql = " WHERE ".join(" AND ",$wheresql);
}else{
$wheresql = "";
}
if(empty($this->order))
{
$order = "{$this->table->fund}.title";
}else{
$order = "{$this->table->fund}.{$this->order}";
}
$sql = "SELECT {$this->table->fund}.* FROM
{$this->table->fund}
$wheresql
ORDER BY $order {$this->sort}";
$rs = $this->db->query($sql);
return $rs->fetchAll();
}
//单个项目的信息
public function getOneFund($id)
{
if(empty($id) || !is_numeric($id))
{
return false;
}
$sql = "SELECT * FROM {$this->table->fund} WHERE id=$id LIMIT 1";
$rs = $this->db->query($sql);
$row = $rs->fetch();
return $row;
}
//建立项目与数据的关系
public function createRelationFromFundToData($fid,$uuid,$place,$id = NULL)
{
if(empty($fid) || !is_numeric($fid))
{
return "参数错误";
}
if(!view::isUuid($uuid))
{
return "参数错误";
}
$data = array(
'uuid'=>$uuid,
'fid'=>$fid,
'place'=>$place
);
$dbh = new dbh();
if(empty($id))
{
$id = $dbh->insert($this->table->metadata_fund,$data,true);
if(is_numeric($id))
{
return $id;
}else{
return "关系写入失败,请检查是否已经存在";
}
}else{
$status = $dbh->update($this->table->metadata_fund,$data," id=$id ");
if($status === true)
{
return $id;
}else{
return "修改失败";
}
}
}
//获得某个项目关联的数据
public function getDataByFund($id)
{
if(empty($id) || !is_numeric($id))
{
return "参数错误";
}
$sql = "SELECT mr.id,mr.fid,mr.place,md.title,md.uuid FROM {$this->table->metadata_fund} mr
LEFT JOIN {$this->table->metadata} md ON mr.uuid=md.uuid
WHERE mr.fid=$id
ORDER BY mr.place ASC";
$rs = $this->db->query($sql);
$rows = $rs->fetchAll();
return $rows;
}
//数据项目参数
public function getMdfundParam(\Zend_Controller_Request_Abstract $request = NULL)
{
$request = new \Zend_Controller_Request_Http();
$data = array(
'uuid' => trim($request->getParam('uuid')),
'fid' => (int)$request->getParam('fid'),
'place' => (int)$request->getParam('place'),
);
return $data;
}
//写入项目信息
public function makeMdfund($id = NULL)
{
$data = $this->getMdfundParam();
$results = $this->events()->trigger('mdfund.checkParam', $this, compact('data'));
$cache_data = $results->bottom();
if($cache_data !== true)
{
return $cache_data;
}
$results = $this->events()->trigger('mdfund.processData', $this, compact('data'));
$data = $results->bottom();
$id = $this->createRelationFromFundToData($data['fid'],$data['uuid'],$data['place'],$id);
if(is_numeric($id))
{
return true;
}else{
return $id;
}
}
//删除项目以及项目和数据的关联(彻底删除)
public function delete($id)
{
if(empty($id) || !is_numeric($id))
{
return false;
}
$sql = "DELETE FROM {$this->table->fund} WHERE id=$id";
@$this->db->exec($sql);
$sql = "DELETE FROM {$this->table->metadata_fund} WHERE fid=$id";
@$this->db->exec($sql);
return true;
}
//移除数据和项目的关联
public function remove($id)
{
if(empty($id) || !is_numeric($id))
{
return "参数错误";
}
$sql = "DELETE FROM {$this->table->metadata_fund} WHERE id=$id";
if($this->db->exec($sql))
{
return true;
}else{
return "删除失败";
}
}
}