Create ImportModel base class, implement import_file uploader
This commit is contained in:
parent
e25a4de8fa
commit
5ca55e149a
|
|
@ -13,6 +13,26 @@ use League\Csv\Writer;
|
|||
class ImportExportController extends ControllerBehavior
|
||||
{
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected $requiredProperties = ['importExportConfig'];
|
||||
|
||||
/**
|
||||
* @var array Configuration values that must exist when applying the primary config file.
|
||||
*/
|
||||
protected $requiredConfig = [];
|
||||
|
||||
/**
|
||||
* @var Model Import model
|
||||
*/
|
||||
public $importModel;
|
||||
|
||||
/**
|
||||
* @var Backend\Classes\WidgetBase Reference to the widget used for uploading import file.
|
||||
*/
|
||||
protected $importUploadFormWidget;
|
||||
|
||||
/**
|
||||
* Behavior constructor
|
||||
* @param Backend\Classes\Controller $controller
|
||||
|
|
@ -23,11 +43,34 @@ class ImportExportController extends ControllerBehavior
|
|||
|
||||
$this->addJs('js/october.importexport.js', 'core');
|
||||
$this->addCss('css/importexport.css', 'core');
|
||||
|
||||
/*
|
||||
* Build configuration
|
||||
*/
|
||||
$this->config = $this->makeConfig($controller->importExportConfig, $this->requiredConfig);
|
||||
|
||||
/*
|
||||
* Import
|
||||
*/
|
||||
$modelClass = $this->getConfig('import[modelClass]');
|
||||
$this->importModel = new $modelClass;
|
||||
|
||||
$this->importUploadFormWidget = $this->makeImportUploadFormWidget();
|
||||
$this->importUploadFormWidget->bindToController();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the view data.
|
||||
* @return void
|
||||
*/
|
||||
public function prepareVars()
|
||||
{
|
||||
$this->vars['importUploadFormWidget'] = $this->importUploadFormWidget;
|
||||
}
|
||||
|
||||
public function import()
|
||||
{
|
||||
|
||||
$this->prepareVars();
|
||||
}
|
||||
|
||||
public function importRender()
|
||||
|
|
@ -40,9 +83,9 @@ class ImportExportController extends ControllerBehavior
|
|||
return $this->importExportMakePartial('import_upload');
|
||||
}
|
||||
|
||||
public function importRenderFields()
|
||||
public function importRenderColumns()
|
||||
{
|
||||
return $this->importExportMakePartial('import_fields');
|
||||
return $this->importExportMakePartial('import_columns');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -61,4 +104,31 @@ class ImportExportController extends ControllerBehavior
|
|||
return $contents;
|
||||
}
|
||||
|
||||
protected function makeImportUploadFormWidget()
|
||||
{
|
||||
$fields = [
|
||||
'import_file' => [
|
||||
'label' => 'Import file',
|
||||
'type' => 'fileupload',
|
||||
'mode' => 'file'
|
||||
],
|
||||
'first_row_titles' => [
|
||||
'label' => 'First row contains column titles',
|
||||
'comment' => 'Leave this checked if the first row in the CSV is used as the column titles.',
|
||||
'type' => 'checkbox',
|
||||
'default' => true
|
||||
]
|
||||
];
|
||||
|
||||
// first_row_titles FALSE is generic columns (1,2,3,4,5..)
|
||||
|
||||
$widgetConfig = $this->makeConfig();
|
||||
$widgetConfig->model = $this->importModel;
|
||||
$widgetConfig->alias = 'importUploadForm';
|
||||
$widgetConfig->fields = $fields;
|
||||
|
||||
$widget = $this->makeWidget('Backend\Widgets\Form', $widgetConfig);
|
||||
return $widget;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -5,5 +5,5 @@
|
|||
|
||||
<h4>2. Match fields to the CSV columns</h4>
|
||||
|
||||
<?= $this->importRenderFields() ?>
|
||||
<?= $this->importRenderColumns() ?>
|
||||
</div>
|
||||
|
|
@ -1,10 +1,2 @@
|
|||
|
||||
[ Uploader ]
|
||||
|
||||
[ Chk default-checked ]
|
||||
|
||||
First row contains column titles.
|
||||
Leave this checked if the first row in the CSV is used as the column titles.
|
||||
|
||||
(Otherwise Column 1,2,3,4,5,6,7,etc is used)
|
||||
|
||||
<?= $importUploadFormWidget->render() ?>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
<?php namespace Backend\Models;
|
||||
|
||||
use Model;
|
||||
|
||||
/**
|
||||
* Model used for importing data
|
||||
*
|
||||
* @package october\backend
|
||||
* @author Alexey Bobkov, Samuel Georges
|
||||
*/
|
||||
abstract class ImportModel extends Model
|
||||
{
|
||||
|
||||
/**
|
||||
* Relations
|
||||
*/
|
||||
public $attachOne = [
|
||||
'import_file' => ['System\Models\File']
|
||||
];
|
||||
|
||||
/**
|
||||
* Called when data is being imported.
|
||||
*/
|
||||
abstract public function importData();
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue