From acca18ea054132fe3c9be567e59aaa51afa34283 Mon Sep 17 00:00:00 2001 From: Bob Olde Hampsink Date: Wed, 14 Sep 2016 08:40:56 +0200 Subject: [PATCH] Require an asset source, closing #132 --- CHANGELOG.md | 3 ++ ImportPlugin.php | 2 +- controllers/ImportController.php | 67 ++++++++++++++++++-------------- templates/index.twig | 13 +++++-- 4 files changed, 51 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5b7e44..86e433a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ Changelog ================= +###0.8.33### + - Require an asset source, closing #132 + ###0.8.32### - Fixed broken relative link for user (thanks to @timkelty) - Fix for assets giving 404 on download (thanks to @MRolefes) diff --git a/ImportPlugin.php b/ImportPlugin.php index 1f84212..0c53b4a 100644 --- a/ImportPlugin.php +++ b/ImportPlugin.php @@ -30,7 +30,7 @@ public function getName() */ public function getVersion() { - return '0.8.32'; + return '0.8.33'; } /** diff --git a/controllers/ImportController.php b/controllers/ImportController.php index 7062127..589dd56 100644 --- a/controllers/ImportController.php +++ b/controllers/ImportController.php @@ -49,46 +49,53 @@ public function actionUpload() // Is file valid? if (!is_null($file)) { - // Get source - $source = craft()->assetSources->getSourceTypeById($import['assetsource']); + // Is asset source valid? + if (isset($import['assetsource']) && !empty($import['assetsource'])) { - // Get folder to save to - $folderId = craft()->assets->getRootFolderBySourceId($import['assetsource']); + // Get source + $source = craft()->assetSources->getSourceTypeById($import['assetsource']); - // Save file to Craft's temp folder for later use - $fileName = AssetsHelper::cleanAssetName($file->name); - $filePath = AssetsHelper::getTempFilePath($file->extensionName); - $file->saveAs($filePath); + // Get folder to save to + $folderId = craft()->assets->getRootFolderBySourceId($import['assetsource']); - // Move the file by source type implementation - $response = $source->insertFileByPath($filePath, $folderId, $fileName, true); + // Save file to Craft's temp folder for later use + $fileName = AssetsHelper::cleanAssetName($file->name); + $filePath = AssetsHelper::getTempFilePath($file->extensionName); + $file->saveAs($filePath); - // Prevent sensitive information leak. Just in case. - $response->deleteDataItem('filePath'); + // Move the file by source type implementation + $response = $source->insertFileByPath($filePath, $folderId, $fileName, true); - // Get file id - $fileId = $response->getDataItem('fileId'); + // Prevent sensitive information leak. Just in case. + $response->deleteDataItem('filePath'); - // Put vars in model - $model = new ImportModel(); - $model->filetype = $file->getType(); + // Get file id + $fileId = $response->getDataItem('fileId'); - // Validate filetype - if ($model->validate()) { + // Put vars in model + $model = new ImportModel(); + $model->filetype = $file->getType(); - // Get columns - $columns = craft()->import->columns($fileId); + // Validate filetype + if ($model->validate()) { - // Send variables to template and display - $this->renderTemplate('import/_map', array( - 'import' => $import, - 'file' => $fileId, - 'columns' => $columns, - )); - } else { + // Get columns + $columns = craft()->import->columns($fileId); + + // Send variables to template and display + $this->renderTemplate('import/_map', array( + 'import' => $import, + 'file' => $fileId, + 'columns' => $columns, + )); + } else { - // Not validated, show error - craft()->userSession->setError(Craft::t('This filetype is not valid').': '.$model->filetype); + // Not validated, show error + craft()->userSession->setError(Craft::t('This filetype is not valid').': '.$model->filetype); + } + } else { + // No asset source selected + craft()->userSession->setError(Craft::t('Please select an asset source.')); } } else { diff --git a/templates/index.twig b/templates/index.twig index 65eb850..67aeef8 100644 --- a/templates/index.twig +++ b/templates/index.twig @@ -40,13 +40,16 @@
- {% for elementType in elementTypes %} {% endfor %}
+ {% if not elementTypes|length %} +

{{ "No Element Types found. Please create one first."|t }}

+ {% endif %}
@@ -95,15 +98,19 @@
+ {% set assetSources = craft.import.getAssetSources %}
- + {% for source in assetSources %} {% endfor %}
+ {% if not assetSources|length %} +

{{ "No Asset Sources found. Please create one first."|t }}

+ {% endif %}