Allows custom options to be specified via form fields

This commit is contained in:
Samuel Georges 2015-07-22 18:30:50 +10:00
parent 8a4ac533e5
commit 5be21f6231
2 changed files with 42 additions and 2 deletions

View File

@ -1,6 +1,7 @@
<?php namespace Backend\Behaviors; <?php namespace Backend\Behaviors;
use Str; use Str;
use Lang;
use Backend\Classes\ControllerBehavior; use Backend\Classes\ControllerBehavior;
use League\Csv\Writer as CsvWriter; use League\Csv\Writer as CsvWriter;
use League\Csv\Reader as CsvReader; use League\Csv\Reader as CsvReader;
@ -42,6 +43,11 @@ class ImportExportController extends ControllerBehavior
*/ */
protected $importUploadFormWidget; protected $importUploadFormWidget;
/**
* @var Backend\Classes\WidgetBase Reference to the widget used for specifing import options.
*/
protected $importOptionsFormWidget;
/** /**
* Behavior constructor * Behavior constructor
* @param Backend\Classes\Controller $controller * @param Backend\Classes\Controller $controller
@ -59,11 +65,16 @@ class ImportExportController extends ControllerBehavior
$this->config = $this->makeConfig($controller->importExportConfig, $this->requiredConfig); $this->config = $this->makeConfig($controller->importExportConfig, $this->requiredConfig);
/* /*
* Import form widget * Import form widgets
*/ */
if ($this->importUploadFormWidget = $this->makeImportUploadFormWidget()) { if ($this->importUploadFormWidget = $this->makeImportUploadFormWidget()) {
$this->importUploadFormWidget->bindToController(); $this->importUploadFormWidget->bindToController();
} }
if ($this->importOptionsFormWidget = $this->makeImportOptionsFormWidget()) {
$this->importOptionsFormWidget->bindToController();
}
} }
// //
@ -72,6 +83,9 @@ class ImportExportController extends ControllerBehavior
public function import() public function import()
{ {
$this->controller->pageTitle = $this->controller->pageTitle
?: Lang::get($this->getConfig('import[title]', 'Import records'));
$this->prepareVars(); $this->prepareVars();
} }
@ -91,6 +105,10 @@ class ImportExportController extends ControllerBehavior
$matches = post('column_match', []); $matches = post('column_match', []);
$sessionKey = $this->importUploadFormWidget->getSessionKey(); $sessionKey = $this->importUploadFormWidget->getSessionKey();
if ($optionData = post('ImportOptions')) {
$model->fill($optionData);
}
$model->importDataFromColumnMatch($matches, $sessionKey, [ $model->importDataFromColumnMatch($matches, $sessionKey, [
'firstRowTitles' => post('first_row_titles', false) 'firstRowTitles' => post('first_row_titles', false)
]); ]);
@ -134,7 +152,7 @@ class ImportExportController extends ControllerBehavior
$reader->setOffset(1); $reader->setOffset(1);
} }
$data = $reader->setLimit(20)->fetchColumn((int) $columnId); $data = $reader->setLimit(50)->fetchColumn((int) $columnId);
/* /*
* Clean up data * Clean up data
@ -163,6 +181,7 @@ class ImportExportController extends ControllerBehavior
public function prepareVars() public function prepareVars()
{ {
$this->vars['importUploadFormWidget'] = $this->importUploadFormWidget; $this->vars['importUploadFormWidget'] = $this->importUploadFormWidget;
$this->vars['importOptionsFormWidget'] = $this->importOptionsFormWidget;
$this->vars['importDbColumns'] = $this->getImportDbColumns(); $this->vars['importDbColumns'] = $this->getImportDbColumns();
$this->vars['importFileColumns'] = $this->getImportFileColumns(); $this->vars['importFileColumns'] = $this->getImportFileColumns();
@ -227,6 +246,25 @@ class ImportExportController extends ControllerBehavior
return $widget; return $widget;
} }
protected function makeImportOptionsFormWidget()
{
if ($fieldConfig = $this->getConfig('import[form]')) {
$widgetConfig = $this->makeConfig($fieldConfig);
$widgetConfig->model = $this->importGetModel();
$widgetConfig->alias = 'importOptionsForm';
$widgetConfig->arrayName = 'ImportOptions';
$widget = $this->makeWidget('Backend\Widgets\Form', $widgetConfig);
return $widget;
}
elseif ($this->importUploadFormWidget) {
$stepSection = $this->importUploadFormWidget->getField('step3_section');
$stepSection->hidden = true;
}
return null;
}
protected function getImportFilePath() protected function getImportFilePath()
{ {
return $this return $this

View File

@ -2,4 +2,6 @@
<?= $importUploadFormWidget->render() ?> <?= $importUploadFormWidget->render() ?>
<?= $importOptionsFormWidget->render() ?>
</div> </div>