Skip to content
This repository has been archived by the owner on Apr 5, 2018. It is now read-only.

Commit

Permalink
Require an asset source, closing #132
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Olde Hampsink committed Sep 14, 2016
1 parent 21455d6 commit acca18e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 34 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion ImportPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getName()
*/
public function getVersion()
{
return '0.8.32';
return '0.8.33';
}

/**
Expand Down
67 changes: 37 additions & 30 deletions controllers/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
13 changes: 10 additions & 3 deletions templates/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@
<div class="field">
<div class="input">
<div class="select">
<select name="import[type]" id="types">
<select name="import[type]" id="types" required>
{% for elementType in elementTypes %}
<option value="{{ elementType.getClassHandle() }}">{{ elementType.getName() }}</option>
{% endfor %}
</select>
</div>
</div>
{% if not elementTypes|length %}
<p class="warning">{{ "No Element Types found. Please create one first."|t }}</p>
{% endif %}
</div>
</td>
</tr>
Expand Down Expand Up @@ -95,15 +98,19 @@
</td>
<td>
<div class="field">
{% set assetSources = craft.import.getAssetSources %}
<div class="input">
<div class="select">
<select name="import[assetsource]" id="assetsources">
{% for source in craft.import.getAssetSources %}
<select name="import[assetsource]" id="assetsources" required>
{% for source in assetSources %}
<option value="{{ source.id }}">{{ source.name|t }}</option>
{% endfor %}
</select>
</div>
</div>
{% if not assetSources|length %}
<p class="warning">{{ "No Asset Sources found. Please create one first."|t }}</p>
{% endif %}
</div>
</td>
</tr>
Expand Down

0 comments on commit acca18e

Please sign in to comment.