-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add media field to custom entity #172
Open
simoncarre
wants to merge
2
commits into
FriendsOfAkeneo:master
Choose a base branch
from
ClickAndMortar:feature-add-media-on-custom-entity
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace Pim\Bundle\CustomEntityBundle\Controller; | ||
|
||
use Akeneo\Component\FileStorage\PathGeneratorInterface; | ||
use Symfony\Component\HttpFoundation\File\Exception\FileException; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
/** | ||
* Media REST controller | ||
* | ||
* @author Simon CARRE <[email protected]> | ||
* @package Pim\Bundle\CustomEntityBundle\Controller | ||
*/ | ||
class MediaRestController | ||
{ | ||
/** | ||
* Validator interface | ||
* | ||
* @var ValidatorInterface | ||
*/ | ||
protected $validator; | ||
|
||
/** | ||
* Path generator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not needed comment line |
||
* | ||
* @var PathGeneratorInterface | ||
*/ | ||
protected $pathGenerator; | ||
|
||
/** | ||
* Upload directory | ||
* | ||
* @var string | ||
*/ | ||
protected $uploadDir; | ||
|
||
/** | ||
* @param ValidatorInterface $validator | ||
* @param PathGeneratorInterface $pathGenerator | ||
* @param string $uploadDir | ||
*/ | ||
public function __construct(ValidatorInterface $validator, PathGeneratorInterface $pathGenerator, $uploadDir) | ||
{ | ||
$this->validator = $validator; | ||
$this->pathGenerator = $pathGenerator; | ||
$this->uploadDir = $uploadDir; | ||
} | ||
|
||
/** | ||
* Post a new media and return original filename and path | ||
* | ||
* @param Request $request | ||
* | ||
* @return JsonResponse | ||
*/ | ||
public function postAction(Request $request) | ||
{ | ||
/** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */ | ||
$file = $request->files->get('file'); | ||
$violations = $this->validator->validate($file); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add an example of file validation in the AcmeCustomBundle ? |
||
|
||
if (count($violations) > 0) { | ||
$errors = []; | ||
foreach ($violations as $violation) { | ||
$errors[$violation->getPropertyPath()] = [ | ||
'message' => $violation->getMessage(), | ||
'invalid_value' => $violation->getInvalidValue(), | ||
]; | ||
} | ||
|
||
return new JsonResponse($errors, 400); | ||
} | ||
$pathData = $this->pathGenerator->generate($file); | ||
|
||
try { | ||
$movedFile = $file->move( | ||
$this->uploadDir . DIRECTORY_SEPARATOR . $pathData['path'] . DIRECTORY_SEPARATOR . $pathData['uuid'], | ||
$file->getClientOriginalName() | ||
); | ||
} catch (FileException $e) { | ||
return new JsonResponse("Unable to create target-directory, or moving file.", 400); | ||
} | ||
$filePath = $movedFile->getPathname(); | ||
$filePathWithoutDirectory = str_replace($this->uploadDir . '/', '', $filePath); | ||
|
||
return new JsonResponse( | ||
[ | ||
'originalFilename' => $file->getClientOriginalName(), | ||
'filePath' => $filePath, | ||
'shortFilePath' => $filePathWithoutDirectory | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
'use strict'; | ||
|
||
define([ | ||
'jquery', | ||
'underscore', | ||
'pim/form/common/fields/field', | ||
'pim/media-url-generator', | ||
'routing', | ||
'custom_entity/template/form/common/fields/media' | ||
], | ||
function ($, | ||
_, | ||
BaseField, | ||
MediaUrlGenerator, | ||
Routing, | ||
template) { | ||
return BaseField.extend({ | ||
template: _.template(template), | ||
|
||
events: { | ||
'change input[type="file"]': function (event) { | ||
this.errors = []; | ||
this.updateModel(this.getFieldValue(event.target)); | ||
this.render(); | ||
}, | ||
'click .clear-field': 'clearField' | ||
}, | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
renderInput: function (templateContext) { | ||
var modelValue = this.getModelValue(); | ||
var originalFilename = null; | ||
var filePath = null; | ||
var shortFilePath = null; | ||
if (modelValue != null) { | ||
var modelValueAsJson = JSON.parse(modelValue); | ||
originalFilename = modelValueAsJson.originalFilename; | ||
filePath = modelValueAsJson.filePath; | ||
shortFilePath = modelValueAsJson.shortFilePath; | ||
} | ||
|
||
return this.template(_.extend(templateContext, { | ||
value: modelValue, | ||
originalFilename: originalFilename, | ||
filePath: filePath, | ||
shortFilePath: shortFilePath, | ||
mediaUrlGenerator: MediaUrlGenerator | ||
})); | ||
}, | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
getFieldValue: function (field) { | ||
var self = this; | ||
var fieldValue = null; | ||
|
||
var input = this.$(field).get(0); | ||
if (!input || 0 === input.files.length) { | ||
return null; | ||
} | ||
var formData = new FormData(); | ||
formData.append('file', input.files[0]); | ||
|
||
$.ajax({ | ||
url: Routing.generate('pim_customentity_media_rest_post'), | ||
type: 'POST', | ||
data: formData, | ||
contentType: false, | ||
cache: false, | ||
processData: false, | ||
async: false | ||
}).done(function (data) { | ||
fieldValue = JSON.stringify(data); | ||
}).fail(function () { | ||
self.errors.push({ | ||
code: self.fieldName, | ||
message: _.__('pim_enrich.entity.product.error.upload'), | ||
global: false | ||
}); | ||
}); | ||
|
||
return fieldValue; | ||
}, | ||
|
||
/** | ||
* Clear media field | ||
*/ | ||
clearField: function () { | ||
this.updateModel(null); | ||
this.render(); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<div class="AknMediaField <%- filePath ? 'has-file' : '' %>" > | ||
<% if (!filePath) { %> | ||
<input class="AknMediaField-fileUploaderInput" id="upload_field_<%- fieldId %>" type="file"/> | ||
<div class="AknMediaField-emptyContainer"> | ||
<span title="upload icon" class="AknMediaField-uploadIcon"/> | ||
<span><%- _.__('pim_enrich.entity.product.media.upload')%></span> | ||
</div> | ||
<% } else { %> | ||
<div class="AknMediaField-preview preview"> | ||
<% mediaThumbnailUrl = mediaUrlGenerator.getMediaShowUrl(filePath, 'thumbnail_small') %> | ||
<% mediaDownloadUrl = mediaUrlGenerator.getMediaDownloadUrl(shortFilePath) %> | ||
<div class="AknMediaField-thumb file"><img src="<%- mediaThumbnailUrl %>" class="AknMediaField-image"/></div> | ||
<div class="AknMediaField-info info"> | ||
<div class="filename" title="<%- originalFilename %>"><%- originalFilename %></div> | ||
<div class="AknButtonList AknButtonList--centered actions"> | ||
<a href="<%- mediaDownloadUrl %>" class="AknButtonList-item AknIconButton AknIconButton--grey download-file" download><i class="icon icon-cloud-download"></i></a> | ||
<span class="AknButtonList-item AknIconButton AknIconButton--grey clear-field"><i class="icon icon-trash"></i></span> | ||
</div> | ||
</div> | ||
</div> | ||
<% } %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not needed comment line