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

101 lines
2.1 KiB
PHP
Raw Normal View History

<?php
namespace Westdc\Visual;
class Record extends Database
{
2015-01-22 03:14:36 +00:00
public $subdataset,$valueFilter,$vars,$uuid,$xaxis,$field;
private $sql;
2015-01-22 03:14:36 +00:00
function __construct($uuid,$identifier )
{
parent::__construct();
2015-01-22 03:14:36 +00:00
$this->uuid=$uuid;
$this->subdataset = $identifier;
$this->init();
}
public function __invoke()
{
return $this->getData();
}
2015-01-22 03:14:36 +00:00
public function init()
{
$visual = new Visual;
2015-01-22 03:14:36 +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);
}
//echo $this->sql;exit();
}
public function getSql()
{
return $this->sql;
}
public function getData()
{
$rs = $this->db->query($this->sql);
2015-01-22 03:14:36 +00:00
$data = [];
while($row = $rs->fetch(\PDO::FETCH_ASSOC))
{
2015-01-22 03:14:36 +00:00
if ($this->xaxis!='category')
$row[$this->xaxis] = $this->utcMsTime(strtotime($row[$this->xaxis]));
//var_dump($row);
2015-01-22 03:14:36 +00:00
$t=[];
foreach($row as $k=>$v)
$t[]=$v;
$data[] = $t;
}
return $data;
}
2015-01-22 03:14:36 +00:00
public function makeSql($table,$condition=NULL)
{
$wheresql = "";
2015-01-22 03:14:36 +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);
}
}
}
2015-01-22 03:14:36 +00:00
$sql = "SELECT {$this->xaxis},{$this->field}
FROM ($table) as tbl
$wheresql
2015-01-22 03:14:36 +00:00
ORDER BY {$this->xaxis}";
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
}