westdc-zf1/application/module/Westdc/Visual/Visual.php

124 lines
2.8 KiB
PHP

<?php
namespace Westdc\Visual;
use Westdc\Visual\Listener\VisualListener as Listener;
use Helpers\dbh;
class Visual
{
private $db;
private $config;
protected $events;
private $metadataTable;
function __construct()
{
$this->db = \Zend_Registry::get('db');
$this->config = \Zend_Registry::get('config');
$this->mainTable = "datavisual";
$Listener = new Listener();
@$this->getEventManager()->attachAggregate($Listener);
if(isset($this->config->sub->metadata) && !empty($this->config->sub->metadata))
{
$this->metadataTable = $this->config->sub->metadata;
}else{
$this->metadataTable = 'metadata';
}
}
public function getEventManager(\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 add($data)
{
$params = compact('data');
$results = $this->getEventManager()->trigger('submit.checkParam', $this, $params);
$cache_data = $results->last();
if($cache_data !== true)
{
return $cache_data;
}
$results = $this->getEventManager()->trigger('submit.processData', $this, $params);
$data = $results->last();
$dbh = new dbh();
$record = $this->getVisualVars($data['uuid']);
if(empty($record))
{
$id = $dbh->insert($this->mainTable,$data);
if($id)
{
$this->getEventManager()->trigger('submit.recordPosted', $this, $params);
return true;
}else{
if($id === false)
{
return '服务器开小差了,请稍后再试';
}else{
return '服务器处理中遇到错误,请联系管理员';
}
}
}//add
else{
if(!$dbh->update($this->mainTable,$data," uuid='{$data['uuid']}' "))
{
$this->getEventManager()->trigger('submit.recordChanged', $this, $params);
return "修改失败!请重试";
}else{
return true;
}
}//edit
}// add()
//删除
public function del($uuid)
{
if(!is_numeric($id) || empty($id) || $id< 0)
{
return false;
}
$sql = "DELETE FROM {$this->mainTable} WHERE uuid='$uuid'";
return $this->db->exec($sql);
}
//获得可视化变量
public function getVisualVars($uuid)
{
$sql = "SELECT * FROM {$this->mainTable} WHERE uuid='$uuid' LIMIT 1";
$rs = $this->db->query($sql);
return $rs->fetch(\PDO::FETCH_ASSOC);
}
//获得可以可视化的元数据
public function getVisualMetadata()
{
$sql = "SELECT * FROM {$this->metadataTable} md
LEFT JOIN {$this->mainTable} v ON v.uuid = md.uuid
WHERE v.uuid IS NOT NULL
order by v.ts_changed desc";
$rs = $this->db->query($sql);
return $rs->fetchAll(\PDO::FETCH_ASSOC);
}
}