add checkTableField function in csv-dataimport,perfection the processing interface

This commit is contained in:
Li Jianxuan 2015-01-29 15:22:49 +00:00
parent 80ffd16e2e
commit 119978c5a0
6 changed files with 98 additions and 29 deletions

View File

@ -4267,18 +4267,30 @@ class Admin_DataController extends Zend_Controller_Action
//检查能否写入数据表
if($ac == "checktables"){
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$table = $this->_getParam('table');
if(empty($table)){
$schema = $this->_getParam('schema');
$file = $this->_getParam('file');
if(empty($table) || empty($schema) || empty($file)){
$this->_helper->json(['error'=>'參數錯誤']);
return true;
}
$tableControl = new Visual\DataTableControl;
$fields = $tableControl->readFields($table);
$fields = $tableControl->readFields($schema . "." .$table);
foreach($fields as $k=>$v){
$fileHandle = new Dataimport\File;
$realFile = $fileHandle->getRealName($file);
}
$processing = Dataimport\ProcessFactory::Bootstrap($fileHandle->getFileTextExt($file));
/** @var \Westdc\Dataimport\Processing\Csv $processing */
$processing->init($realFile);
$status = $processing->checkTableField($fields);
//$this->_helper->json($status);
return true;
}

View File

@ -36,16 +36,21 @@ $this->headTitle()->setSeparator(' - ');
<h6 id="error-loading"></h6>
<form action="/admin/data/dataimport/ac/table" id="schema-form">
<form action="/admin/data/dataimport/ac/checktables" id="schema-form">
<input type="hidden" name="file" value="<?= $this->file ?>">
<input type="hidden" name="schema" value="">
<input type="hidden" name="table" value="">
<button type="submit" id="ctl-next" class="btn btn-success disabled" disabled>下一步</button>
<input type="hidden" name="table" value="" id="input-table">
<button type="button" id="ctl-next" class="btn btn-success disabled" disabled>下一步</button>
</form>
</div>
</div>
<script>
$(document).ready(function(){
$('#ctl-next').click(function(){
ctl.checkTable();
});
$('.ctl-schema-btn').click(function(){
$('#schema-form input[name=schema]').val($(this).val());
@ -56,10 +61,14 @@ $this->headTitle()->setSeparator(' - ');
'success':function(data){
if (data!=null)
{
if($.isEmptyObject(data))
{
ctl.disableNext();
alert("此数据库中尚无数据表");
return true;
}
$('#table-list').html('<h5>选择数据表</h5>');
if(!$.isEmptyObject(data))
{
$.each(data,function(index,value){
$('#table-list').append('<label class="radio">'
+ '<input type="radio" value="'+value.tablename+'" name="table" class="ctl-table-name">'
@ -67,10 +76,13 @@ $this->headTitle()->setSeparator(' - ');
+"</label>"
);
});
}
$('.ctl-table-name').click(function(){
$('#schema-form input[name=table]').val($(this).val());
if($('#schema-form input[name=schema]').val() != '' && $('#schema-form input[name=table]').val() != '')
{
ctl.enableNext();
}
});
$('#clt-next').removeClass('disabled');
@ -102,11 +114,15 @@ $this->headTitle()->setSeparator(' - ');
$('#ctl-next').removeClass("disabled");
$('#ctl-next').removeAttr("disabled");
},
checkTable : function(table){
disableNext : function(){
$('#ctl-next').addClass("disabled");
$('#ctl-next').attr("disabled","disabled");
},
checkTable : function(){
$.ajax({
'type':"POST",
'url':'/admin/data/dataimport/ac/checktables',
'data':'schema=' + $(this).val(),
'data':$('#schema-form').serialize(),
'success':function(data){
if (data!=null)
{
@ -119,10 +135,8 @@ $this->headTitle()->setSeparator(' - ');
},
'timeout': 30000,
'beforeSend' : function(){
$('#error-loading').html("正在加载数据表......");
$('#error-loading').html("正在校验数据是否可以导入......");
$('#error-loading').show();
$('#table-list').html('');
$('.ctl-table-name').unbind();
},
'complete' : function(){
$('#error-loading').hide();

View File

@ -17,7 +17,7 @@ $theme->AppendPlus($this,"jquery-fileupload");
<label>上传文件,请选择Excel文件进行上传</label>
<span class="btn btn-success fileinput-button file-upload-ctls">上传文件<input id="fileupload" type="file" name="FileData"></span>
<a href="javascript:void(0);" class="btn btn-info" id="ctl-show-files">查看已上的文件</a>
<a href="javascript:void(0);" class="btn btn-info" id="ctl-show-files">查看已上的文件</a>
<div class="progress" id="pdf-upload-progress" style="display: none;">
<div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%">
</div>
@ -116,8 +116,8 @@ $theme->AppendPlus($this,"jquery-fileupload");
makeFileHtml : function(data){
return '<div class="alert alert-success">'
+ '<span class="ctl-files">'+data.file+'</span>'
+ ' <a href="/admin/data/dataimport/ac/prepare/?file='+ encodeURI(data.file) +'">導入數據</a>'
+ '<a href="javascript:void(0)" onclick="upload.delete(this,\''+data.file+'\')" class="pull-right"></a>'
+ ' <a href="/admin/data/dataimport/ac/prepare/?file='+ encodeURI(data.file) +'">导入数据</a>'
+ '<a href="javascript:void(0)" onclick="upload.delete(this,\''+data.file+'\')" class="pull-right"></a>'
+ "</div>";
},
makeFileList : function(data){
@ -127,8 +127,8 @@ $theme->AppendPlus($this,"jquery-fileupload");
$.each(data,function(k,v){
li.push('<div class="alert alert-success ctl-files">'
+ '<span class="ctl-files">'+v+'</span>'
+ ' <a href="/admin/data/dataimport/ac/prepare/?file='+ encodeURI(v) +'">導入數據</a>'
+ '<a href="javascript:void(0);" onclick="upload.delete(this,\''+v+'\')" class="pull-right"></a>'
+ ' <a href="/admin/data/dataimport/ac/prepare/?file='+ encodeURI(v) +'">导入数据</a>'
+ '<a href="javascript:void(0);" onclick="upload.delete(this,\''+v+'\')" class="pull-right"></a>'
+ "</div>");
});

View File

@ -16,8 +16,9 @@ class Csv implements ProcessingInterface{
private $csv_object;
private $csv_line_data;
const ERROR_INDEX_NOT_MATCHED = "值的個數於其它行不匹配";
const ERROR_INDEX_TYPE_DIFFERENT = "值的類型於上一行的類型不同";
const ERROR_INDEX_NOT_MATCHED = "值的个数于其它行不匹配";
const ERROR_INDEX_TYPE_DIFFERENT = "值的类型于上一行不同";
const ERROR_EMPTY_FILE = "文件是空的";
const ERROR_LINE_TITLE = "行:";
/**
@ -109,6 +110,9 @@ class Csv implements ProcessingInterface{
$error_stack = [];
if(!is_array($lines) || count($lines) < 1)
return [self::ERROR_EMPTY_FILE];
foreach($lines as $k=>$v){
if($k < 1 || empty($v))
continue;
@ -135,5 +139,31 @@ class Csv implements ProcessingInterface{
}//checkRegularity()
/**
* 检查字段是否对应
* @param $fields
* @return array|bool
*/
public function checkTableField(array $fields)
{
$error_stack = [];
$frist_line = $this->getLines()[0];
var_dump($fields);
foreach($fields as $index => $field)
{
if($field['data_type']){
}
}
if(0)
return true;
else
return $error_stack;
}
}

View File

@ -16,4 +16,17 @@ interface ProcessingInterface {
*/
public function init($file);
/**
* 检查数据文件是否规整
* @return mixed
*/
public function checkRegularity();
/**
* 检查字段是否对应
* @param array $fields
* @return mixed
*/
public function checkTableField(array $fields);
}

View File

@ -44,12 +44,12 @@ class DataTableControl extends Database{
return $rs->fetchAll(\PDO::FETCH_ASSOC);
}
public function readFields($tablename)
public function readFields($tablename,$identity = "")
{
$st=explode('.',$tablename);
$sql = "select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where table_schema='".$st[0]."' and TABLE_NAME='".$st[1]."'";
$sql = "select * from INFORMATION_SCHEMA.COLUMNS where table_schema='".$st[0]."' and TABLE_NAME='".$st[1]."'";
$rs = $this->db->query($sql);
return $rs->fetchAll(\PDO::FETCH_COLUMN);
return $rs->fetchAll(\PDO::FETCH_ASSOC);
}
}