Skip to content

Commit c7aef3c

Browse files
committed
Add logic to extract column headers from file
Move import columns to their own partials
1 parent 694ab4a commit c7aef3c

File tree

7 files changed

+92
-55
lines changed

7 files changed

+92
-55
lines changed

modules/backend/behaviors/ImportExportController.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php namespace Backend\Behaviors;
22

33
use Backend\Classes\ControllerBehavior;
4-
use League\Csv\Writer;
4+
use League\Csv\Writer as CsvWrtier;
5+
use League\Csv\Reader as CsvReader;
56

67
/**
78
* Import/Export Controller Behavior
@@ -87,7 +88,8 @@ public function export()
8788
public function prepareVars()
8889
{
8990
$this->vars['importUploadFormWidget'] = $this->importUploadFormWidget;
90-
$this->vars['importColumns'] = $this->getImportDbColumns();
91+
$this->vars['importDbColumns'] = $this->getImportDbColumns();
92+
$this->vars['importFileColumns'] = $this->getImportFileColumns();
9193

9294
// Make these variables to widgets
9395
$this->controller->vars += $this->vars;
@@ -119,7 +121,20 @@ protected function getImportDbColumns()
119121

120122
protected function getImportFileColumns()
121123
{
124+
if (!$path = $this->getImportFilePath()) {
125+
return null;
126+
}
127+
128+
$reader = CsvReader::createFromPath($path);
129+
$firstRow = $reader->fetchOne(0);
122130

131+
if (!post('first_row_titles')) {
132+
array_walk($firstRow, function(&$value, $key) {
133+
$value = 'Column #'.($key + 1);
134+
});
135+
}
136+
137+
return $firstRow;
123138
}
124139

125140
protected function makeImportUploadFormWidget()
@@ -139,6 +154,21 @@ protected function makeImportUploadFormWidget()
139154
return $widget;
140155
}
141156

157+
protected function getImportFilePath()
158+
{
159+
$model = $this->importGetModel();
160+
$file = $model
161+
->import_file()
162+
->withDeferred($this->importUploadFormWidget->getSessionKey())
163+
->first();
164+
165+
if (!$file) {
166+
return null;
167+
}
168+
169+
return $file->getLocalPath();
170+
}
171+
142172
//
143173
// Exporting
144174
//

modules/backend/behaviors/importexportcontroller/assets/css/importexport.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
height: 400px;
1919
background: #f0f0f0;
2020
padding: 5px;
21+
overflow: auto;
2122
}
2223
.import-behavior .import-file-columns .upload-prompt {
2324
display: block;
@@ -61,6 +62,11 @@
6162
float: left;
6263
width: 45%;
6364
}
65+
.import-behavior .import-file-columns > ul div.import-column-name > span {
66+
white-space: nowrap;
67+
overflow: hidden;
68+
text-overflow: ellipsis;
69+
}
6470
.import-behavior .import-file-columns > ul .import-column-bindings > ul {
6571
float: right;
6672
width: 55%;

modules/backend/behaviors/importexportcontroller/assets/less/importexport.less

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
height: 400px;
3131
background: #f0f0f0;
3232
padding: 5px;
33+
overflow: auto;
3334
}
3435
.import-file-columns {
3536
.upload-prompt {
@@ -71,6 +72,12 @@
7172
div.import-column-name {
7273
float: left;
7374
width: 45%;
75+
76+
> span {
77+
white-space: nowrap;
78+
overflow: hidden;
79+
text-overflow: ellipsis;
80+
}
7481
}
7582

7683
.import-column-bindings > ul {

modules/backend/behaviors/importexportcontroller/partials/_import_columns.htm

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<div class="import-db-columns">
2+
<ul>
3+
<?php foreach ($importDbColumns as $column => $label): ?>
4+
<li data-column-name="<?= e($column) ?>">
5+
<span><?= e($label) ?></span>
6+
<input type="hidden" data-column-match Xname="column_match[1][]" Xvalue="<?= e($column) ?>" >
7+
</li>
8+
<?php endforeach ?>
9+
</ul>
10+
</div>
11+
12+
<script>
13+
$('.import-db-columns > ul, .import-column-bindings > ul').sortable({
14+
group: 'import-fields',
15+
usePlaceholderClone: true
16+
})
17+
</script>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<div class="import-file-columns">
2+
<?php if ($importFileColumns): ?>
3+
<ul>
4+
<?php foreach ($importFileColumns as $index => $column): ?>
5+
<li data-column-id="<?= $index ?>">
6+
<div class="import-column-name">
7+
<span><?= $column ?></span>
8+
</div>
9+
<div class="import-column-bindings">
10+
<ul data-empty-text="Drop column here..."></ul>
11+
</div>
12+
</li>
13+
<?php endforeach ?>
14+
</ul>
15+
<?php else: ?>
16+
<p class="upload-prompt">
17+
Please upload a valid CSV file.
18+
</p>
19+
<?php endif ?>
20+
</div>

modules/backend/behaviors/importexportcontroller/partials/fields_import.yaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,18 @@ fields:
2525
label: 2. Match fields to the CSV columns
2626
type: section
2727

28-
import_columns:
28+
import_file_columns:
29+
label: File columns
2930
type: partial
30-
path: ~/modules/backend/behaviors/importexportcontroller/partials/_import_columns.htm
31+
path: ~/modules/backend/behaviors/importexportcontroller/partials/_import_file_columns.htm
3132
dependsOn: import_file
33+
span: left
34+
35+
import_db_columns:
36+
label: Record columns
37+
type: partial
38+
path: ~/modules/backend/behaviors/importexportcontroller/partials/_import_db_columns.htm
39+
span: right
3240

3341
step3_section:
3442
label: 3. Set import options

0 commit comments

Comments
 (0)