Skip to content

Commit 068a977

Browse files
committed
Simplify approach by using Form Widget
1 parent 846feb6 commit 068a977

File tree

6 files changed

+79
-44
lines changed

6 files changed

+79
-44
lines changed

modules/backend/behaviors/ImportExportController.php

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class ImportExportController extends ControllerBehavior
2828
*/
2929
public $importModel;
3030

31+
/**
32+
* @var array Import column configuration.
33+
*/
34+
public $importColumns;
35+
3136
/**
3237
* @var Backend\Classes\WidgetBase Reference to the widget used for uploading import file.
3338
*/
@@ -55,6 +60,9 @@ public function __construct($controller)
5560
$modelClass = $this->getConfig('import[modelClass]');
5661
$this->importModel = new $modelClass;
5762

63+
$columnConfig = $this->getConfig('import[list]');
64+
$this->importColumns = $this->makeListColumns($columnConfig);
65+
5866
$this->importUploadFormWidget = $this->makeImportUploadFormWidget();
5967
$this->importUploadFormWidget->bindToController();
6068
}
@@ -66,6 +74,7 @@ public function __construct($controller)
6674
public function prepareVars()
6775
{
6876
$this->vars['importUploadFormWidget'] = $this->importUploadFormWidget;
77+
$this->vars['importColumns'] = $this->importColumns;
6978
}
7079

7180
public function import()
@@ -75,17 +84,7 @@ public function import()
7584

7685
public function importRender()
7786
{
78-
return $this->importExportMakePartial('container');
79-
}
80-
81-
public function importRenderUpload()
82-
{
83-
return $this->importExportMakePartial('import_upload');
84-
}
85-
86-
public function importRenderColumns()
87-
{
88-
return $this->importExportMakePartial('import_columns');
87+
return $this->importExportMakePartial('import');
8988
}
9089

9190
/**
@@ -104,30 +103,37 @@ public function importExportMakePartial($partial, $params = [])
104103
return $contents;
105104
}
106105

106+
protected function makeListColumns($config)
107+
{
108+
$config = $this->makeConfig($config);
109+
110+
if (!isset($config->columns) || !is_array($config->columns)) {
111+
return null;
112+
}
113+
114+
$result = [];
115+
foreach ($config->columns as $attribute => $column) {
116+
$result[$attribute] = array_get($column, 'label', $attribute);
117+
}
118+
119+
return $result;
120+
}
121+
107122
protected function makeImportUploadFormWidget()
108123
{
109-
$fields = [
110-
'import_file' => [
111-
'label' => 'Import file',
112-
'type' => 'fileupload',
113-
'mode' => 'file'
114-
],
115-
'first_row_titles' => [
116-
'label' => 'First row contains column titles',
117-
'comment' => 'Leave this checked if the first row in the CSV is used as the column titles.',
118-
'type' => 'checkbox',
119-
'default' => true
120-
]
121-
];
122124

123125
// first_row_titles FALSE is generic columns (1,2,3,4,5..)
124126

125-
$widgetConfig = $this->makeConfig();
127+
$widgetConfig = $this->makeConfig('~/modules/backend/behaviors/importexportcontroller/partials/fields_import.yaml');
126128
$widgetConfig->model = $this->importModel;
127129
$widgetConfig->alias = 'importUploadForm';
128-
$widgetConfig->fields = $fields;
129130

130131
$widget = $this->makeWidget('Backend\Widgets\Form', $widgetConfig);
132+
133+
$widget->bindEvent('form.beforeRefresh', function($holder) {
134+
$holder->data = [];
135+
});
136+
131137
return $widget;
132138
}
133139

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

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div class="import-behavior">
2+
3+
<?= $importUploadFormWidget->render() ?>
4+
5+
</div>

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
<div class="row">
42
<div class="col-md-6">
53
File Columns
@@ -10,15 +8,15 @@
108
</p>
119

1210
<ul style="display: none">
13-
<li>
11+
<li data-import-code="1|name">
1412
<div class="import-column-name">
1513
<span>Name</span>
1614
</div>
1715
<div class="import-column-bindings">
1816
<ul data-empty-text="Drop column here..."></ul>
1917
</div>
2018
</li>
21-
<li>
19+
<li data-import-code="2|url_name">
2220
<div class="import-column-name">
2321
<span>URL Name</span>
2422
</div>
@@ -34,9 +32,12 @@
3432

3533
<div class="import-record-columns">
3634
<ul>
37-
<li><span>SKU</span></li>
38-
<li><span>Title</span></li>
39-
<li><span>Description</span></li>
35+
<?php foreach ($importColumns as $column => $label): ?>
36+
<li data-column-name="<?= e($column) ?>">
37+
<span><?= e($label) ?></span>
38+
<input type="hidden" data-column-match Xname="column_match[1|xxx][]" Xvalue="<?= e($column) ?>" >
39+
</li>
40+
<?php endforeach ?>
4041
</ul>
4142
</div>
4243
</div>

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

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# ===================================
2+
# Field Definitions
3+
# ===================================
4+
5+
fields:
6+
step1_section:
7+
label: 1. Upload a CSV file
8+
type: section
9+
10+
import_file:
11+
label: Import file
12+
type: fileupload
13+
mode: file
14+
span: left
15+
16+
first_row_titles:
17+
label: First row contains column titles
18+
comment: Leave this checked if the first row in the CSV is used as the column titles.
19+
type: checkbox
20+
default: true
21+
span: left
22+
23+
step2_section:
24+
label: 2. Match fields to the CSV columns
25+
type: section
26+
27+
import_columns:
28+
type: partial
29+
path: ~/modules/backend/behaviors/importexportcontroller/partials/_import_columns.htm
30+
dependsOn: import_file
31+
32+
step3_section:
33+
label: 3. Set import options
34+
type: section

0 commit comments

Comments
 (0)