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

101 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2014-12-22 06:35:50 +00:00
<?php
namespace Westdc\Visual;
class Record extends Database
{
2015-01-22 04:11:52 +00:00
public $subdataset,$valueFilter,$vars,$uuid,$xaxis,$field;
2014-12-22 06:35:50 +00:00
private $sql;
2015-01-22 04:11:52 +00:00
function __construct($uuid,$identifier )
2014-12-22 06:35:50 +00:00
{
parent::__construct();
2015-01-22 04:11:52 +00:00
$this->uuid=$uuid;
$this->subdataset = $identifier;
$this->init();
2014-12-22 06:35:50 +00:00
}
public function __invoke()
{
return $this->getData();
}
2015-01-22 04:11:52 +00:00
public function init()
2014-12-22 06:35:50 +00:00
{
$visual = new Visual;
2015-01-22 04:11:52 +00:00
$var_data = $visual->getVisualVars($this->uuid);
$this->vars = $var_data['variable'];
$this->xaxis=$var_data['xaxis'];
$json=json_decode($this->vars,true);
//$subvar=array_filter($json,function($sub) { return $sub['name']==$this->subdataset; });
$subvar=$json[$this->subdataset];
$this->field=$subvar['name'];
if (isset($subvar['sql']) && !empty($subvar['sql']))
$this->sql=$this->makeSql($subvar['sql']);
elseif(isset($var_data['data']) && !empty($var_data['data']))
{
$condition=null;
if (isset($subvar['condition']) && !empty($subvar['condition']))
$condition=$subvar['condition'];
$this->sql = $this->makeSql($var_data['data'],$condition);
}
2014-12-22 06:35:50 +00:00
//echo $this->sql;exit();
}
public function getSql()
{
return $this->sql;
}
public function getData()
{
$rs = $this->db->query($this->sql);
2015-01-22 04:11:52 +00:00
2014-12-22 06:35:50 +00:00
$data = [];
while($row = $rs->fetch(\PDO::FETCH_ASSOC))
{
2015-01-22 04:11:52 +00:00
if ($this->xaxis!='category')
$row[$this->xaxis] = $this->utcMsTime(strtotime($row[$this->xaxis]));
2014-12-22 06:35:50 +00:00
//var_dump($row);
2015-01-22 04:11:52 +00:00
$t=[];
foreach($row as $k=>$v)
$t[]=$v;
$data[] = $t;
2014-12-22 06:35:50 +00:00
}
return $data;
}
2015-01-22 04:11:52 +00:00
public function makeSql($table,$condition=NULL)
2014-12-22 06:35:50 +00:00
{
$wheresql = "";
2015-01-22 04:11:52 +00:00
if ($condition)
{
$filter=explode(';',$condition);
if(!empty($filter))
{
if(is_string($filter))
$wheresql = " WHERE $filter ";
elseif(is_array($filter))
{
$wheresql = ' WHERE '.join(" AND ",$filter);
}
}
}
2014-12-22 06:35:50 +00:00
2015-01-22 04:11:52 +00:00
$sql = "SELECT {$this->xaxis},{$this->field}
FROM ($table) as tbl
2014-12-22 06:35:50 +00:00
$wheresql
2015-01-22 04:11:52 +00:00
ORDER BY {$this->xaxis}";
2014-12-22 06:35:50 +00:00
return $sql;
}
public function utcMsTime($time=''){
if(empty($time))
return (time()+8*3600)*1000;
else
return ($time)*1000;
}
2014-05-12 06:53:21 +00:00
}