Add logic to extract column headers from file
Move import columns to their own partials
This commit is contained in:
parent
694ab4a08b
commit
c7aef3c58c
|
|
@ -1,7 +1,8 @@
|
||||||
<?php namespace Backend\Behaviors;
|
<?php namespace Backend\Behaviors;
|
||||||
|
|
||||||
use Backend\Classes\ControllerBehavior;
|
use Backend\Classes\ControllerBehavior;
|
||||||
use League\Csv\Writer;
|
use League\Csv\Writer as CsvWrtier;
|
||||||
|
use League\Csv\Reader as CsvReader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Import/Export Controller Behavior
|
* Import/Export Controller Behavior
|
||||||
|
|
@ -87,7 +88,8 @@ class ImportExportController extends ControllerBehavior
|
||||||
public function prepareVars()
|
public function prepareVars()
|
||||||
{
|
{
|
||||||
$this->vars['importUploadFormWidget'] = $this->importUploadFormWidget;
|
$this->vars['importUploadFormWidget'] = $this->importUploadFormWidget;
|
||||||
$this->vars['importColumns'] = $this->getImportDbColumns();
|
$this->vars['importDbColumns'] = $this->getImportDbColumns();
|
||||||
|
$this->vars['importFileColumns'] = $this->getImportFileColumns();
|
||||||
|
|
||||||
// Make these variables to widgets
|
// Make these variables to widgets
|
||||||
$this->controller->vars += $this->vars;
|
$this->controller->vars += $this->vars;
|
||||||
|
|
@ -119,7 +121,20 @@ class ImportExportController extends ControllerBehavior
|
||||||
|
|
||||||
protected function getImportFileColumns()
|
protected function getImportFileColumns()
|
||||||
{
|
{
|
||||||
|
if (!$path = $this->getImportFilePath()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$reader = CsvReader::createFromPath($path);
|
||||||
|
$firstRow = $reader->fetchOne(0);
|
||||||
|
|
||||||
|
if (!post('first_row_titles')) {
|
||||||
|
array_walk($firstRow, function(&$value, $key) {
|
||||||
|
$value = 'Column #'.($key + 1);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return $firstRow;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function makeImportUploadFormWidget()
|
protected function makeImportUploadFormWidget()
|
||||||
|
|
@ -139,6 +154,21 @@ class ImportExportController extends ControllerBehavior
|
||||||
return $widget;
|
return $widget;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getImportFilePath()
|
||||||
|
{
|
||||||
|
$model = $this->importGetModel();
|
||||||
|
$file = $model
|
||||||
|
->import_file()
|
||||||
|
->withDeferred($this->importUploadFormWidget->getSessionKey())
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if (!$file) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $file->getLocalPath();
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Exporting
|
// Exporting
|
||||||
//
|
//
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@
|
||||||
height: 400px;
|
height: 400px;
|
||||||
background: #f0f0f0;
|
background: #f0f0f0;
|
||||||
padding: 5px;
|
padding: 5px;
|
||||||
|
overflow: auto;
|
||||||
}
|
}
|
||||||
.import-behavior .import-file-columns .upload-prompt {
|
.import-behavior .import-file-columns .upload-prompt {
|
||||||
display: block;
|
display: block;
|
||||||
|
|
@ -61,6 +62,11 @@
|
||||||
float: left;
|
float: left;
|
||||||
width: 45%;
|
width: 45%;
|
||||||
}
|
}
|
||||||
|
.import-behavior .import-file-columns > ul div.import-column-name > span {
|
||||||
|
white-space: nowrap;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
}
|
||||||
.import-behavior .import-file-columns > ul .import-column-bindings > ul {
|
.import-behavior .import-file-columns > ul .import-column-bindings > ul {
|
||||||
float: right;
|
float: right;
|
||||||
width: 55%;
|
width: 55%;
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@
|
||||||
height: 400px;
|
height: 400px;
|
||||||
background: #f0f0f0;
|
background: #f0f0f0;
|
||||||
padding: 5px;
|
padding: 5px;
|
||||||
|
overflow: auto;
|
||||||
}
|
}
|
||||||
.import-file-columns {
|
.import-file-columns {
|
||||||
.upload-prompt {
|
.upload-prompt {
|
||||||
|
|
@ -71,6 +72,12 @@
|
||||||
div.import-column-name {
|
div.import-column-name {
|
||||||
float: left;
|
float: left;
|
||||||
width: 45%;
|
width: 45%;
|
||||||
|
|
||||||
|
> span {
|
||||||
|
white-space: nowrap;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.import-column-bindings > ul {
|
.import-column-bindings > ul {
|
||||||
|
|
|
||||||
|
|
@ -1,51 +0,0 @@
|
||||||
<div class="row">
|
|
||||||
<div class="col-md-6">
|
|
||||||
File Columns
|
|
||||||
|
|
||||||
<div class="import-file-columns">
|
|
||||||
<p class="upload-prompt">
|
|
||||||
Please upload a valid CSV file.
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<ul style="display: none">
|
|
||||||
<li data-import-code="1|name">
|
|
||||||
<div class="import-column-name">
|
|
||||||
<span>Name</span>
|
|
||||||
</div>
|
|
||||||
<div class="import-column-bindings">
|
|
||||||
<ul data-empty-text="Drop column here..."></ul>
|
|
||||||
</div>
|
|
||||||
</li>
|
|
||||||
<li data-import-code="2|url_name">
|
|
||||||
<div class="import-column-name">
|
|
||||||
<span>URL Name</span>
|
|
||||||
</div>
|
|
||||||
<div class="import-column-bindings">
|
|
||||||
<ul data-empty-text="Drop column here..."></ul>
|
|
||||||
</div>
|
|
||||||
</li>
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="col-md-6">
|
|
||||||
Record Columns
|
|
||||||
|
|
||||||
<div class="import-db-columns">
|
|
||||||
<ul>
|
|
||||||
<?php foreach ($importColumns as $column => $label): ?>
|
|
||||||
<li data-column-name="<?= e($column) ?>">
|
|
||||||
<span><?= e($label) ?></span>
|
|
||||||
<input type="hidden" data-column-match Xname="column_match[1|xxx][]" Xvalue="<?= e($column) ?>" >
|
|
||||||
</li>
|
|
||||||
<?php endforeach ?>
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
$('.import-db-columns > ul, .import-column-bindings > ul').sortable({
|
|
||||||
group: 'import-fields',
|
|
||||||
usePlaceholderClone: true
|
|
||||||
})
|
|
||||||
</script>
|
|
||||||
|
|
@ -0,0 +1,17 @@
|
||||||
|
<div class="import-db-columns">
|
||||||
|
<ul>
|
||||||
|
<?php foreach ($importDbColumns as $column => $label): ?>
|
||||||
|
<li data-column-name="<?= e($column) ?>">
|
||||||
|
<span><?= e($label) ?></span>
|
||||||
|
<input type="hidden" data-column-match Xname="column_match[1][]" Xvalue="<?= e($column) ?>" >
|
||||||
|
</li>
|
||||||
|
<?php endforeach ?>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
$('.import-db-columns > ul, .import-column-bindings > ul').sortable({
|
||||||
|
group: 'import-fields',
|
||||||
|
usePlaceholderClone: true
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
@ -0,0 +1,20 @@
|
||||||
|
<div class="import-file-columns">
|
||||||
|
<?php if ($importFileColumns): ?>
|
||||||
|
<ul>
|
||||||
|
<?php foreach ($importFileColumns as $index => $column): ?>
|
||||||
|
<li data-column-id="<?= $index ?>">
|
||||||
|
<div class="import-column-name">
|
||||||
|
<span><?= $column ?></span>
|
||||||
|
</div>
|
||||||
|
<div class="import-column-bindings">
|
||||||
|
<ul data-empty-text="Drop column here..."></ul>
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
<?php endforeach ?>
|
||||||
|
</ul>
|
||||||
|
<?php else: ?>
|
||||||
|
<p class="upload-prompt">
|
||||||
|
Please upload a valid CSV file.
|
||||||
|
</p>
|
||||||
|
<?php endif ?>
|
||||||
|
</div>
|
||||||
|
|
@ -25,10 +25,18 @@ fields:
|
||||||
label: 2. Match fields to the CSV columns
|
label: 2. Match fields to the CSV columns
|
||||||
type: section
|
type: section
|
||||||
|
|
||||||
import_columns:
|
import_file_columns:
|
||||||
|
label: File columns
|
||||||
type: partial
|
type: partial
|
||||||
path: ~/modules/backend/behaviors/importexportcontroller/partials/_import_columns.htm
|
path: ~/modules/backend/behaviors/importexportcontroller/partials/_import_file_columns.htm
|
||||||
dependsOn: import_file
|
dependsOn: import_file
|
||||||
|
span: left
|
||||||
|
|
||||||
|
import_db_columns:
|
||||||
|
label: Record columns
|
||||||
|
type: partial
|
||||||
|
path: ~/modules/backend/behaviors/importexportcontroller/partials/_import_db_columns.htm
|
||||||
|
span: right
|
||||||
|
|
||||||
step3_section:
|
step3_section:
|
||||||
label: 3. Set import options
|
label: 3. Set import options
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue