三江源可视化管理中增加数据表和字段选择功能

This commit is contained in:
Li Jianxuan 2014-12-07 06:21:35 +00:00
parent 4a68d06ee1
commit 8a3d5fefb8
6 changed files with 212 additions and 46 deletions

View File

@ -3960,6 +3960,15 @@ class Admin_DataController extends Zend_Controller_Action
$uuid = $this->_getParam('uuid');
$submit = $this->_getParam('submit');
$visual = new Visual\Visual;
if(true == view::isXmlHttpRequest($this))
{
$tableName = $this->_getParam('tablename');
$visualTable = new Visual\DataTableControl;
$this->jsonexit($visualTable->readFields($tableName));
}
if(!empty($uuid))
{
@ -3968,8 +3977,12 @@ class Admin_DataController extends Zend_Controller_Action
if(empty($submit))
{
$visualTable = new Visual\DataTableControl;
$this->view->visualTable = $visualTable->readTables();
$data = $visual->getVisualVars($uuid);
if(!empty($data))
{
$this->view->info = $data;
@ -3982,7 +3995,9 @@ class Admin_DataController extends Zend_Controller_Action
'uuid' => $this->_getParam('uuid'),
'vars' => $this->_getParam('var'),
'data' => $this->_getParam('data'),
'status' => $this->_getParam('status')
'status' => $this->_getParam('status'),
'visual_datatable' => $this->_getParam('visual_datatable'),
'visual_timefield' => $this->_getParam('visual_timefield'),
);
$status = $visual->add($data);
@ -3992,6 +4007,18 @@ class Admin_DataController extends Zend_Controller_Action
view::Post($this,"添加成功!",-2);
return true;
}else{
$visualTable = new Visual\DataTableControl;
$this->view->visualTable = $visualTable->readTables();
$data = $visual->getVisualVars($uuid);
if(!empty($data))
{
$this->view->info = $data;
}
$this->view->error = $status;
return true;
}

View File

@ -21,6 +21,7 @@
<?= $this->error ?>
</div>
<?php }?>
<form class="form-horizontal" method="post" action="">
<div class="control-group">
<label class="control-label" for="inputUUID">UUID</label>
@ -53,12 +54,36 @@
</label>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputData">数据读取SQL或</label>
<label class="control-label">数据表</label>
<div class="controls">
<textarea name="data" rows="4" class="input-block-level" id="inputData"><?= !isset($this->info['data']) ? "":$this->info['data']?></textarea>
<select name="visual_datatable" id="data-table">
<?php foreach($this->visualTable as $k=>$v){ ?>
<?php if(isset($this->info['visual_datatable']) && $this->info['visual_datatable']==$v){ ?>
<option value="<?= $v ?>" selected="selected"><?= $v ?></option>
<?php }else{ ?>
<option value="<?= $v ?>"><?= $v ?></option>
<?php } ?>
<?php } ?>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label">时间字段</label>
<div class="controls" id="field-time">
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputData">数据读取SQL或表名</label>
<div class="controls">
<textarea name="data" rows="4" class="input-block-level" id="inputData"><?= !isset($this->info['data']) ? "":$this->info['data']?></textarea>
</div>
</div>
<?php if(empty($this->info['vars'])) { ?>
<div class="control-group">
<label class="control-label" for="inputVariable">可视化要素</label>
@ -71,14 +96,16 @@
<?php $encoder = new \Westdc\Visual\VariableEncoder; ?>
<?php $vars = $encoder->decode($this->info['vars']);?>
<?php foreach($vars as $k=>$v) { ?>
<div class="control-group">
<div class="control-group var-group">
<label class="control-label" for="inputVariable">可视化要素 <small><a href="javascript:void(0);" onclick="delVar(this)">删除</a></small></label>
<div class="controls">
<input type="text" id="inputVariable" placeholder="Variable" class="input-block-level" name="var[]" value="<?= $v ?>">
字段<input type="text" name="var[<?=$k?>][field]" value="<?= $v['field'] ?>" readonly />
显示名<input type="text" name="var[<?= $k ?>][name]" value="<?= $v['name'] ?>">
</div>
</div>
<?php } ?>
<?php } ?>
<div class="control-group" id="last-control-group">
<div class="controls">
<input type="hidden" name="submit" value="1" />
@ -90,16 +117,86 @@
</div>
</div>
<script type="text/javascript">
var fieldData,fieldHtml,varHtml,current_count;
function makeVarSelectHtml(field_data){
current_count = $('.var-group').length;
fieldHtml = []
for(i in field_data)
{
fieldHtml.push('<option value="'+ fieldData[i] +'">'+ fieldData[i] +'</option>');
}
varHtml = '<select name="var['+current_count+'][field]">' + fieldHtml.join('') + '</select>';
html = '<div class="control-group var-group">'
+ '<label class="control-label" for="inputVariable">可视化要素 <small><a href="javascript:void(0);" onclick="delVar(this)">删除</a></small></label>'
+ '<div class="controls">'
+ '字段' + varHtml
+ ' 显示名 <input type="text" placeholder="Variable" class="input-small" name="var['+current_count+'][name]">'
+ '</div>'
+'</div>';
return html;
}
$('#addVariable').click(function(e) {
html = '<div class="control-group">'
+ '<label class="control-label" for="inputVariable">可视化要素 <small><a href="javascript:void(0);" onclick="delVar(this)">删除</a></small></label>'
+ '<div class="controls">'
+ '<input type="text" id="inputVariable" placeholder="Variable" class="input-block-level" name="var[]">'
+ '</div>'
+'</div>';
$('#last-control-group').prepend(html);
if(null == fieldData)
{
alert('请先选择数据表和时间字段');
return false;
}
$('#last-control-group').prepend(makeVarSelectHtml(fieldData));
});
function delVar(dom){
$(dom).parent().parent().parent('.control-group').remove();
}
};
$(document).ready(function(){
$('#data-table').change(function(){
$('.var-group').remove();
$.ajax({
type:"POST",
url:'',
data:'tablename='+ $(this).val(),
success:function(data){
fieldData = data;
html = [];
for(i in data)
{
html.push('<option value="' + data[i] + '">' + data[i] + '</option>');
}
html = '<select name="visual_timefield">' + html.join('')+'</select>';
$('#field-time').html(html);
},
beforeSend:function(){
$('#field-time').html('加载中');
}
});
});
<?php if(!empty($this->info['ts_created'])) { ?>
$.ajax({
type:"POST",
url:'',
data:'tablename=<?= $this->info['visual_datatable'] ?>',
success:function(data){
fieldData = data;
html = [];
for(i in data)
{
if('<?= $this->info['visual_timefield'] ?>' == data[i])
html.push('<option value="' + data[i] + '" selected="selected">' + data[i] + '</option>');
else
html.push('<option value="' + data[i] + '">' + data[i] + '</option>');
}
html = '<select name="visual_timefield">' + html.join('')+'</select>';
$('#field-time').html(html);
},
beforeSend:function(){
$('#field-time').html('加载中');
}
});
<?php } ?>
});
</script>

View File

@ -0,0 +1,35 @@
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2014/12/4
* Time: 11:31
*/
namespace Westdc\Visual;
class DataTableControl extends Database{
function __construct()
{
parent::__construct();
}
public function readTables()
{
$sql = "SELECT tablename FROM pg_tables WHERE tablename LIKE 'data_%' ORDER BY tablename ";
$rs = $this->db->query($sql);
$row = $rs->fetchAll(\PDO::FETCH_COLUMN);
return $row;
}
public function readFields($tableName)
{
$sql = "select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='$tableName'";
$rs = $this->db->query($sql);
return $rs->fetchAll(\PDO::FETCH_COLUMN);
}
}

View File

@ -0,0 +1,34 @@
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2014/12/4
* Time: 11:28
*/
namespace Westdc\Visual;
class Database {
public $db;
function __construct()
{
$this->initDatabase();
}
public function initDatabase()
{
$config = \Zend_Registry::get('config');
$dsn = "pgsql:host={$config->visual_db->hostname};"
."port={$config->visual_db->port};"
."dbname={$config->visual_db->database};"
."user={$config->visual_db->username};"
."password={$config->visual_db->password}";
$this->db = new \PDO($dsn);
}
}

View File

@ -1,11 +1,9 @@
<?php
namespace Westdc\Visual;
class Record
class Record extends Database
{
public $db;
public $subdataset;
public $timeFiled = "utctime";
@ -14,7 +12,7 @@ class Record
function __construct($uuid = NULL,$identifier = "")
{
$this->initDatabase();
parent::__construct();
if(!empty($identifier))
{
@ -32,19 +30,6 @@ class Record
return $this->getData();
}
public function initDatabase()
{
$config = \Zend_Registry::get('config');
$dsn = "pgsql:host={$config->visual_db->hostname};"
."port={$config->visual_db->port};"
."dbname={$config->visual_db->database};"
."user={$config->visual_db->username};"
."password={$config->visual_db->password}";
$this->db = new \PDO($dsn);
}
public function initVisual($uuid)
{
$visual = new Visual;
@ -84,7 +69,7 @@ class Record
{
$sql = "SELECT
{$this->timeFiled} as utctime , \"$fieldValue\" as value
FROM $table
FROM $table as tbl
ORDER BY
extract(YEAR from \"{$this->timeFiled}\") ASC,
extract(MONTH from \"{$this->timeFiled}\") ASC,

View File

@ -15,25 +15,13 @@ class VariableEncoder
public function encode($vars)
{
$arr = [];
foreach($vars as $k=>$v)
{
$names = explode("|",$v);
$arr[] = [ 'dataset' => $names[0], 'subdataset' => $names[1],'seriename' => $names[2]];
}
return json_encode($arr);
return json_encode($vars);
}
public function decode($json)
{
$arr = json_decode($json,true);
//var_dump($arr);
$data = [];
foreach($arr as $k=>$v)
{
$data[$k] = join("|",$v);
}
return $data;
return $arr;
}
public function normaldecode($json)