From 5ca55e149ad58c0fad7f6e3ef2596a796bae47ab Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Thu, 16 Jul 2015 22:15:30 +1000 Subject: [PATCH] Create ImportModel base class, implement import_file uploader --- .../behaviors/ImportExportController.php | 76 ++++++++++++++++++- .../partials/_container.htm | 2 +- ..._import_fields.htm => _import_columns.htm} | 0 .../partials/_import_upload.htm | 10 +-- modules/backend/models/ImportModel.php | 26 +++++++ 5 files changed, 101 insertions(+), 13 deletions(-) rename modules/backend/behaviors/importexportcontroller/partials/{_import_fields.htm => _import_columns.htm} (100%) create mode 100644 modules/backend/models/ImportModel.php diff --git a/modules/backend/behaviors/ImportExportController.php b/modules/backend/behaviors/ImportExportController.php index ee6998e91..733e77e9e 100644 --- a/modules/backend/behaviors/ImportExportController.php +++ b/modules/backend/behaviors/ImportExportController.php @@ -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; + } + } \ No newline at end of file diff --git a/modules/backend/behaviors/importexportcontroller/partials/_container.htm b/modules/backend/behaviors/importexportcontroller/partials/_container.htm index f33012294..09ef8c97e 100644 --- a/modules/backend/behaviors/importexportcontroller/partials/_container.htm +++ b/modules/backend/behaviors/importexportcontroller/partials/_container.htm @@ -5,5 +5,5 @@

2. Match fields to the CSV columns

- importRenderFields() ?> + importRenderColumns() ?> \ No newline at end of file diff --git a/modules/backend/behaviors/importexportcontroller/partials/_import_fields.htm b/modules/backend/behaviors/importexportcontroller/partials/_import_columns.htm similarity index 100% rename from modules/backend/behaviors/importexportcontroller/partials/_import_fields.htm rename to modules/backend/behaviors/importexportcontroller/partials/_import_columns.htm diff --git a/modules/backend/behaviors/importexportcontroller/partials/_import_upload.htm b/modules/backend/behaviors/importexportcontroller/partials/_import_upload.htm index f8ab40bad..46b90e3b5 100644 --- a/modules/backend/behaviors/importexportcontroller/partials/_import_upload.htm +++ b/modules/backend/behaviors/importexportcontroller/partials/_import_upload.htm @@ -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) - +render() ?> diff --git a/modules/backend/models/ImportModel.php b/modules/backend/models/ImportModel.php new file mode 100644 index 000000000..c62c44172 --- /dev/null +++ b/modules/backend/models/ImportModel.php @@ -0,0 +1,26 @@ + ['System\Models\File'] + ]; + + /** + * Called when data is being imported. + */ + abstract public function importData(); + +} \ No newline at end of file