Skip to content
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

refactor Media create.html.twig to correctly render FileHelperType (t… #389

Merged
merged 1 commit into from
Apr 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Bundle/MediaBundle/Controller/MediaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,12 @@ private function createAndRedirect(Request $request, $folderId, $type, $redirect
$media = new Media();
$helper = $handler->getFormHelper($media);

$form = $this->createForm($handler->getFormType(), $helper);
$options = array_merge([
'action' => $this->generateUrl('VictoireMediaBundle_media_create', ['folderId' => $folderId, 'type' => $type])
], $handler->getFormTypeOptions());
$form = $this->createForm($handler->getFormType(), $helper, $options);

if ('POST' == $request->getMethod()) {
if ($request->isMethod('POST')) {
$form->handleRequest($request);
if ($form->isValid()) {
$media = $helper->getMedia();
Expand Down
97 changes: 32 additions & 65 deletions Bundle/MediaBundle/Entity/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Victoire\Bundle\MediaBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;

/**
* Media.
Expand All @@ -13,6 +14,8 @@
*/
class Media
{
use TimestampableEntity;

/**
* @ORM\Id
* @ORM\Column(type="bigint")
Expand All @@ -35,6 +38,13 @@ class Media
*/
protected $name;

/**
* @var string
*
* @ORM\Column(type="string", nullable=true, name="original_filename")
*/
protected $originalFilename;

/**
* @var string
*
Expand All @@ -56,20 +66,6 @@ class Media
*/
protected $metadata = [];

/**
* @var \DateTime
*
* @ORM\Column(type="datetime", name="created_at")
*/
protected $createdAt;

/**
* @var \DateTime
*
* @ORM\Column(type="datetime", name="updated_at")
*/
protected $updatedAt;

/**
* @var Folder
*
Expand Down Expand Up @@ -209,6 +205,23 @@ public function getName()
{
return $this->name;
}
/**
* @param string $originalFilename
*
* @return Media
*/
public function setOriginalFilename($originalFilename)
{
$this->originalFilename = $originalFilename;
return $this;
}
/**
* @return string
*/
public function getOriginalFilename()
{
return $this->originalFilename;
}

/**
* Set location.
Expand Down Expand Up @@ -315,54 +328,6 @@ public function getMetadataValue($key)
return isset($this->metadata[$key]) ? $this->metadata[$key] : null;
}

/**
* Set createdAt.
*
* @param \DateTime $createdAt
*
* @return Media
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;

return $this;
}

/**
* Get createdAt.
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}

/**
* Set updatedAt.
*
* @param \DateTime $updatedAt
*
* @return Media
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;

return $this;
}

/**
* Get updatedAt.
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}

/**
* Set content.
*
Expand Down Expand Up @@ -464,10 +429,12 @@ public function getClassType()
}

/**
* @ORM\PreUpdate
* @ORM\PrePersist
*/
public function preUpdate()
public function prePersist()
{
$this->setUpdatedAt(new \DateTime());
if (empty($this->name)) {
$this->setName($this->getOriginalFilename());
}
}
}
2 changes: 1 addition & 1 deletion Bundle/MediaBundle/Form/File/FileType.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => 'Victoire\Bundle\MediaBundle\Helper\File\FileHelper',
'data_class' => 'Victoire\Bundle\MediaBundle\Helper\File\FileHelper'
]);
}
}
24 changes: 12 additions & 12 deletions Bundle/MediaBundle/Helper/File/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Gaufrette\Adapter\Local;
use Gaufrette\Filesystem;
use Gedmo\Sluggable\Util\Urlizer;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
use Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser;
Expand Down Expand Up @@ -32,6 +33,7 @@ class FileHandler extends AbstractMediaHandler
* @var MimeTypeGuesserInterface
*/
public $mimeTypeGuesser = null;
public $urlizer;

/**
* constructor.
Expand All @@ -41,6 +43,7 @@ public function __construct()
$this->fileSystem = new Filesystem(new \Gaufrette\Adapter\Local('uploads/media/', true));
//we use a specific symfony mimetypeguesser because de default (FileinfoMimeTypeGuesser) is unable to recognize MS documents
$this->mimeTypeGuesser = new FileBinaryMimeTypeGuesser();
$this->urlizer = new Urlizer();
}

/**
Expand All @@ -56,15 +59,15 @@ public function getName()
*/
public function getType()
{
return self::TYPE;
return FileHandler::TYPE;
}

/**
* @return FileType
*/
public function getFormType()
{
return new FileType();
return FileType::class;
}

/**
Expand Down Expand Up @@ -98,33 +101,30 @@ public function getFormHelper(Media $media)
*/
public function prepareMedia(Media $media)
{
if (null == $media->getUuid()) {
if (null === $media->getUuid()) {
$uuid = uniqid();
$media->setUuid($uuid);
}

$content = $media->getContent();
if (empty($content)) {
return;
}

if (!$content instanceof File) {
if (!is_file($content)) {
throw new \RuntimeException('Invalid file');
}

$file = new File($content);
$media->setContent($file);
}
if ($content instanceof UploadedFile) {
$media->setName($content->getClientOriginalName());
$pathInfo = pathinfo($content->getClientOriginalName());
$media->setOriginalFilename($this->urlizer->urlize($pathInfo['filename']).'.'.$pathInfo['extension']);
$name = $media->getName();
if (empty($name)) {
$media->setName($media->getOriginalFilename());
}
}

$metadata = [];

$media->setFileSize(filesize($media->getContent()));
$media->setMetadata($metadata);

$contentType = $this->mimeTypeGuesser->guess($media->getContent()->getPathname());
$media->setContentType($contentType);
$relativePath = sprintf('/%s.%s', $media->getUuid(), ExtensionGuesser::getInstance()->guess($media->getContentType()));
Expand Down
10 changes: 10 additions & 0 deletions Bundle/MediaBundle/Helper/Media/AbstractMediaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
*/
abstract class AbstractMediaHandler
{
/**
* Return the default form type options
*
* @return array
*/
public function getFormTypeOptions()
{
return [];
}

/**
* @return string
*/
Expand Down
10 changes: 6 additions & 4 deletions Bundle/MediaBundle/Resources/views/Media/create.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@
{% block content %}
{% form_theme form 'VictoireMediaBundle:Form:fields.html.twig' %}

<form action="{{ path('VictoireMediaBundle_media_create', { 'folderId' : folder.id, 'type': type } ) }}" method="post" {{ form_enctype(form) }} class="form-horizontal properties">
{{ form_start(form) }}
{{ form_row(form.name) }}
{{ form_widget(form.file) }}
{{ form_rest(form) }}
<div class="prop_wrp">
{{ form_widget(form) }}
<div class="input_prop">
<div class="btn_group">
<button class="btn" onclick="window.location = '{{ path('VictoireMediaBundle_folder_show', { 'folderId' : folder.id } ) }}'" type="reset">{{ 'form.cancel' |trans({}, 'victoire') }}</button>
<button class="btn btn-primary" type="submit">{{ 'media.file.add' |trans({}, 'victoire') }}</button>
</div>
</div>
</div>
</form>
</div>
{{ form_end(form) }}
{% endblock %}