From 17a35c9e24260cabd05aecd90b9f49669a7ca79f Mon Sep 17 00:00:00 2001 From: Wim Vandersmissen Date: Wed, 6 Aug 2014 11:23:02 +0200 Subject: [PATCH 1/2] extra fields for media (description & copyright) --- Entity/Media.php | 77 ++++++++++++++++++++++++++++++++++++++++++ Form/File/FileType.php | 28 ++++++++++++--- 2 files changed, 101 insertions(+), 4 deletions(-) diff --git a/Entity/Media.php b/Entity/Media.php index d2ebf294..8721a9be 100644 --- a/Entity/Media.php +++ b/Entity/Media.php @@ -3,6 +3,7 @@ namespace Kunstmaan\MediaBundle\Entity; use Doctrine\ORM\Mapping as ORM; +use Gedmo\Mapping\Annotation as Gedmo; use Kunstmaan\AdminBundle\Entity\AbstractEntity; /** @@ -14,6 +15,14 @@ */ class Media extends AbstractEntity { + /** + * @var string + * + * @Gedmo\Locale + * Used locale to override Translation listener`s locale + * this is not a mapped field of entity metadata, just a simple property + */ + protected $locale; /** * @var string @@ -30,6 +39,22 @@ class Media extends AbstractEntity */ protected $name; + /** + * @var string + * + * @ORM\Column(name="description", type="text", nullable=true) + * @Gedmo\Translatable + */ + protected $description; + + /** + * @var string + * + * @ORM\Column(name="copyright", type="string", nullable=true) + * @Gedmo\Translatable + */ + protected $copyright; + /** * @var string * @@ -110,6 +135,18 @@ public function __construct() $this->deleted = false; } + /** + * @param string $locale + * + * @return Media + */ + public function setTranslatableLocale($locale) + { + $this->locale = $locale; + + return $this; + } + /** * @return string */ @@ -438,6 +475,46 @@ public function setUrl($url) return $this; } + /** + * @param string $copyright + * + * @return Media + */ + public function setCopyright($copyright) + { + $this->copyright = $copyright; + + return $this; + } + + /** + * @return string + */ + public function getCopyright() + { + return $this->copyright; + } + + /** + * @param string $description + * + * @return Media + */ + public function setDescription($description) + { + $this->description = $description; + + return $this; + } + + /** + * @return string + */ + public function getDescription() + { + return $this->description; + } + /** * @return string */ diff --git a/Form/File/FileType.php b/Form/File/FileType.php index c3edc98c..86e3ac86 100644 --- a/Form/File/FileType.php +++ b/Form/File/FileType.php @@ -25,8 +25,28 @@ class FileType extends AbstractType */ public function buildForm(FormBuilderInterface $builder, array $options) { - $builder->add('name', 'text', array('required' => false)); + $builder->add( + 'name', + 'text', + array( + 'required' => false + ) + ); $builder->add('file', 'file'); + $builder->add( + 'copyright', + 'text', + array( + 'required' => false + ) + ); + $builder->add( + 'description', + 'textarea', + array( + 'required' => false + ) + ); } /** @@ -47,9 +67,9 @@ public function getName() public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults( - array( - 'data_class' => 'Kunstmaan\MediaBundle\Helper\File\FileHelper', - ) + array( + 'data_class' => 'Kunstmaan\MediaBundle\Helper\File\FileHelper', + ) ); } } \ No newline at end of file From b1a42e5b4ea6c5233afbc1e49b37775622a322b6 Mon Sep 17 00:00:00 2001 From: Wim Vandersmissen Date: Wed, 6 Aug 2014 16:57:50 +0200 Subject: [PATCH 2/2] copyright & description fields added to media --- Form/BulkUploadType.php | 20 ++--- Form/File/FileType.php | 30 ++++++- Form/FolderType.php | 66 +++++++-------- Form/RemoteAudio/RemoteAudioType.php | 53 +++++++++--- Form/RemoteSlide/RemoteSlideType.php | 53 +++++++++--- Form/RemoteVideo/RemoteVideoType.php | 53 +++++++++--- Helper/File/FileHelper.php | 32 +++++++ Helper/Remote/AbstractRemoteHelper.php | 42 +++++++++ Helper/Remote/RemoteInterface.php | 4 + Helper/RemoteAudio/RemoteAudioHelper.php | 8 +- Helper/RemoteSlide/RemoteSlideHelper.php | 103 +---------------------- Helper/RemoteVideo/RemoteVideoHelper.php | 102 +--------------------- 12 files changed, 285 insertions(+), 281 deletions(-) diff --git a/Form/BulkUploadType.php b/Form/BulkUploadType.php index d9fa3d88..4df477a4 100644 --- a/Form/BulkUploadType.php +++ b/Form/BulkUploadType.php @@ -40,16 +40,16 @@ public function __construct($accept = null) public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add( - 'files', - 'file', - array( - 'required' => false, - 'attr' => array( - 'accept' => $this->accept, - 'multiple' => 'multiple', - ), - 'data_class' => null - ) + 'files', + 'file', + array( + 'required' => false, + 'attr' => array( + 'accept' => $this->accept, + 'multiple' => 'multiple', + ), + 'data_class' => null + ) ); } diff --git a/Form/File/FileType.php b/Form/File/FileType.php index 86e3ac86..5ac697f9 100644 --- a/Form/File/FileType.php +++ b/Form/File/FileType.php @@ -4,7 +4,10 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\FormEvents; use Symfony\Component\OptionsResolver\OptionsResolverInterface; +use Symfony\Component\Validator\Constraints\NotBlank; /** * FileType @@ -32,7 +35,13 @@ public function buildForm(FormBuilderInterface $builder, array $options) 'required' => false ) ); - $builder->add('file', 'file'); + $builder->add( + 'file', + 'file', + array( + 'required' => false + ) + ); $builder->add( 'copyright', 'text', @@ -47,6 +56,25 @@ public function buildForm(FormBuilderInterface $builder, array $options) 'required' => false ) ); + $builder->addEventListener( + FormEvents::PRE_SET_DATA, + function (FormEvent $event) { + $helper = $event->getData(); + $form = $event->getForm(); + + // Only add file field as required field when creating new objects + if (!$helper || null === $helper->getMedia()->getId()) { + $form->add( + 'file', + 'file', + array( + 'constraints' => array(new NotBlank()), + 'required' => true + ) + ); + } + } + ); } /** diff --git a/Form/FolderType.php b/Form/FolderType.php index c2f27d46..caf41e54 100644 --- a/Form/FolderType.php +++ b/Form/FolderType.php @@ -43,39 +43,39 @@ public function buildForm(FormBuilderInterface $builder, array $options) $folder = $this->folder; $type = $this; $builder - ->add('name') - ->add( - 'rel', - 'choice', - array( - 'choices' => array( - 'media' => 'media', - 'image' => 'image', - 'slideshow' => 'slideshow', - 'video' => 'video' - ), + ->add('name') + ->add( + 'rel', + 'choice', + array( + 'choices' => array( + 'media' => 'media', + 'image' => 'image', + 'slideshow' => 'slideshow', + 'video' => 'video' + ), + ) ) - ) - ->add( - 'parent', - 'entity', - array( - 'class' => 'Kunstmaan\MediaBundle\Entity\Folder', - 'required' => true, - 'query_builder' => function (\Doctrine\ORM\EntityRepository $er) use ($folder, $type) { - $qb = $er->createQueryBuilder('folder'); + ->add( + 'parent', + 'entity', + array( + 'class' => 'Kunstmaan\MediaBundle\Entity\Folder', + 'required' => true, + 'query_builder' => function (\Doctrine\ORM\EntityRepository $er) use ($folder, $type) { + $qb = $er->createQueryBuilder('folder'); - if ($folder != null && $folder->getId() != null) { - $ids = "folder.id != " . $folder->getId(); - $ids .= $type->addChildren($folder); - $qb->andwhere($ids); - } - $qb->andWhere('folder.deleted != true'); + if ($folder != null && $folder->getId() != null) { + $ids = "folder.id != " . $folder->getId(); + $ids .= $type->addChildren($folder); + $qb->andwhere($ids); + } + $qb->andWhere('folder.deleted != true'); - return $qb; - } - ) - ); + return $qb; + } + ) + ); } /** @@ -112,9 +112,9 @@ public function getName() public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults( - array( - 'data_class' => 'Kunstmaan\MediaBundle\Entity\Folder', - ) + array( + 'data_class' => 'Kunstmaan\MediaBundle\Entity\Folder', + ) ); } } \ No newline at end of file diff --git a/Form/RemoteAudio/RemoteAudioType.php b/Form/RemoteAudio/RemoteAudioType.php index 0d6b0bbe..c4480d7b 100644 --- a/Form/RemoteAudio/RemoteAudioType.php +++ b/Form/RemoteAudio/RemoteAudioType.php @@ -5,6 +5,7 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; +use Symfony\Component\Validator\Constraints\NotBlank; /** * RemoteAudioType @@ -26,15 +27,45 @@ class RemoteAudioType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { $builder - ->add('name', 'text') - ->add('code', 'text') - ->add( - 'type', - 'choice', - array( - 'choices' => array('soundcloud' => 'soundcloud') + ->add( + 'name', + 'text', + array( + 'constraints' => array(new NotBlank()), + 'required' => true + ) + ) + ->add( + 'code', + 'text', + array( + 'constraints' => array(new NotBlank()), + 'required' => true + ) + ) + ->add( + 'type', + 'choice', + array( + 'choices' => array('soundcloud' => 'soundcloud'), + 'constraints' => array(new NotBlank()), + 'required' => true + ) ) - ); + ->add( + 'copyright', + 'text', + array( + 'required' => false + ) + ) + ->add( + 'description', + 'textarea', + array( + 'required' => false + ) + ); } /** @@ -55,9 +86,9 @@ public function getName() public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults( - array( - 'data_class' => 'Kunstmaan\MediaBundle\Helper\RemoteAudio\RemoteAudioHelper', - ) + array( + 'data_class' => 'Kunstmaan\MediaBundle\Helper\RemoteAudio\RemoteAudioHelper', + ) ); } } \ No newline at end of file diff --git a/Form/RemoteSlide/RemoteSlideType.php b/Form/RemoteSlide/RemoteSlideType.php index 2386dd43..3c5913ad 100644 --- a/Form/RemoteSlide/RemoteSlideType.php +++ b/Form/RemoteSlide/RemoteSlideType.php @@ -5,6 +5,7 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; +use Symfony\Component\Validator\Constraints\NotBlank; /** * RemoteSlideType @@ -26,15 +27,45 @@ class RemoteSlideType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { $builder - ->add('name', 'text') - ->add('code', 'text') - ->add( - 'type', - 'choice', - array( - 'choices' => array('slideshare' => 'slideshare') + ->add( + 'name', + 'text', + array( + 'constraints' => array(new NotBlank()), + 'required' => true + ) + ) + ->add( + 'code', + 'text', + array( + 'constraints' => array(new NotBlank()), + 'required' => true + ) + ) + ->add( + 'type', + 'choice', + array( + 'choices' => array('slideshare' => 'slideshare'), + 'constraints' => array(new NotBlank()), + 'required' => true + ) ) - ); + ->add( + 'copyright', + 'text', + array( + 'required' => false + ) + ) + ->add( + 'description', + 'textarea', + array( + 'required' => false + ) + ); } /** @@ -55,9 +86,9 @@ public function getName() public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults( - array( - 'data_class' => 'Kunstmaan\MediaBundle\Helper\RemoteSlide\RemoteSlideHelper', - ) + array( + 'data_class' => 'Kunstmaan\MediaBundle\Helper\RemoteSlide\RemoteSlideHelper', + ) ); } } \ No newline at end of file diff --git a/Form/RemoteVideo/RemoteVideoType.php b/Form/RemoteVideo/RemoteVideoType.php index dccb1e0a..406938d0 100644 --- a/Form/RemoteVideo/RemoteVideoType.php +++ b/Form/RemoteVideo/RemoteVideoType.php @@ -5,6 +5,7 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; +use Symfony\Component\Validator\Constraints\NotBlank; /** * RemoteVideoType @@ -26,15 +27,45 @@ class RemoteVideoType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { $builder - ->add('name', 'text') - ->add('code', 'text') - ->add( - 'type', - 'choice', - array( - 'choices' => array('youtube' => 'youtube', 'vimeo' => 'vimeo', 'dailymotion' => 'dailymotion') + ->add( + 'name', + 'text', + array( + 'constraints' => array(new NotBlank()), + 'required' => true + ) + ) + ->add( + 'code', + 'text', + array( + 'constraints' => array(new NotBlank()), + 'required' => true + ) + ) + ->add( + 'type', + 'choice', + array( + 'choices' => array('youtube' => 'youtube', 'vimeo' => 'vimeo', 'dailymotion' => 'dailymotion'), + 'constraints' => array(new NotBlank()), + 'required' => true + ) ) - ); + ->add( + 'copyright', + 'text', + array( + 'required' => false + ) + ) + ->add( + 'description', + 'textarea', + array( + 'required' => false + ) + ); } /** @@ -55,9 +86,9 @@ public function getName() public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults( - array( - 'data_class' => 'Kunstmaan\MediaBundle\Helper\RemoteVideo\RemoteVideoHelper', - ) + array( + 'data_class' => 'Kunstmaan\MediaBundle\Helper\RemoteVideo\RemoteVideoHelper', + ) ); } } \ No newline at end of file diff --git a/Helper/File/FileHelper.php b/Helper/File/FileHelper.php index 1e740bd4..2ce38c92 100644 --- a/Helper/File/FileHelper.php +++ b/Helper/File/FileHelper.php @@ -54,6 +54,38 @@ public function setName($name) $this->media->setName($name); } + /** + * @return string + */ + public function getCopyright() + { + return $this->media->getCopyright(); + } + + /** + * @param string $copyright + */ + public function setCopyright($copyright) + { + $this->media->setCopyright($copyright); + } + + /** + * @return string + */ + public function getDescription() + { + return $this->media->getDescription(); + } + + /** + * @param string $description + */ + public function setDescription($description) + { + $this->media->setDescription($description); + } + /** * @return UploadedFile */ diff --git a/Helper/Remote/AbstractRemoteHelper.php b/Helper/Remote/AbstractRemoteHelper.php index ddc0aa3c..97016f81 100644 --- a/Helper/Remote/AbstractRemoteHelper.php +++ b/Helper/Remote/AbstractRemoteHelper.php @@ -10,6 +10,16 @@ */ abstract class AbstractRemoteHelper { + /** @var Media $media */ + protected $media; + + /** + * @param Media $media + */ + public function __construct(Media $media) + { + $this->media = $media; + } /** * @return string @@ -27,6 +37,38 @@ public function setName($name) $this->media->setName($name); } + /** + * @return string + */ + public function getCopyright() + { + return $this->media->getCopyright(); + } + + /** + * @param string $copyright + */ + public function setCopyright($copyright) + { + $this->media->setCopyright($copyright); + } + + /** + * @return string + */ + public function getDescription() + { + return $this->media->getDescription(); + } + + /** + * @param string $description + */ + public function setDescription($description) + { + $this->media->setDescription($description); + } + /** * @return Media */ diff --git a/Helper/Remote/RemoteInterface.php b/Helper/Remote/RemoteInterface.php index 0edccabe..1b97f181 100644 --- a/Helper/Remote/RemoteInterface.php +++ b/Helper/Remote/RemoteInterface.php @@ -6,6 +6,10 @@ interface RemoteInterface { public function getName(); public function setName($name); + public function getCopyright(); + public function setCopyright($copyright); + public function getDescription(); + public function setDescription($description); public function getCode(); public function setCode($code); public function getThumbnailUrl(); diff --git a/Helper/RemoteAudio/RemoteAudioHelper.php b/Helper/RemoteAudio/RemoteAudioHelper.php index 3a7911cf..441e9e94 100644 --- a/Helper/RemoteAudio/RemoteAudioHelper.php +++ b/Helper/RemoteAudio/RemoteAudioHelper.php @@ -12,18 +12,12 @@ */ class RemoteAudioHelper extends AbstractRemoteHelper implements RemoteInterface { - - /** - * @var Media - */ - protected $media; - /** * @param Media $media */ public function __construct(Media $media) { - $this->media = $media; + parent::__construct($media); $this->media->setContentType(RemoteAudioHandler::CONTENT_TYPE); } } diff --git a/Helper/RemoteSlide/RemoteSlideHelper.php b/Helper/RemoteSlide/RemoteSlideHelper.php index 60cf3bbe..b80fda96 100644 --- a/Helper/RemoteSlide/RemoteSlideHelper.php +++ b/Helper/RemoteSlide/RemoteSlideHelper.php @@ -2,117 +2,22 @@ namespace Kunstmaan\MediaBundle\Helper\RemoteSlide; -use Kunstmaan\MediaBundle\Helper\RemoteSlide\RemoteSlideHandler; +use Kunstmaan\MediaBundle\Helper\Remote\AbstractRemoteHelper; +use Kunstmaan\MediaBundle\Helper\Remote\RemoteInterface; use Kunstmaan\MediaBundle\Entity\Media; /** * Kunstmaan\MediaBundle\Entity\Video * Class that defines a video in the system */ -class RemoteSlideHelper +class RemoteSlideHelper extends AbstractRemoteHelper implements RemoteInterface { - - /** - * @var Media - */ - protected $media; - /** * @param Media $media */ public function __construct(Media $media) { - $this->media = $media; + parent::__construct($media); $this->media->setContentType(RemoteSlideHandler::CONTENT_TYPE); } - - /** - * @return string - */ - public function getName() - { - return $this->media->getName(); - } - - /** - * @param string $name - */ - public function setName($name) - { - $this->media->setName($name); - } - - /** - * @return Media - */ - public function getMedia() - { - return $this->media; - } - - /** - * @return string - */ - public function getCode() - { - return $this->media->getMetadataValue('code'); - } - - /** - * Set code - * @param string $code - * - * @return RemoteSlideHelper - */ - public function setCode($code) - { - $this->media->setMetadataValue('code', $code); - - return $this; - } - - /** - * @return string - */ - public function getThumbnailUrl() - { - return $this->media->getMetadataValue('thumbnail_url'); - } - - /** - * Set thumbnail url - * @param string $url - * - * @return RemoteSlideHelper - */ - public function setThumbnailUrl($url) - { - $this->media->setMetadataValue('thumbnail_url', $url); - - return $this; - } - - /** - * Get type - * - * @return string - */ - public function getType() - { - return $this->media->getMetadataValue('type'); - } - - /** - * Set type - * @param string $type - * - * @return RemoteSlideHelper - */ - public function setType($type) - { - $this->media->setMetadataValue('type', $type); - - return $this; - } - } diff --git a/Helper/RemoteVideo/RemoteVideoHelper.php b/Helper/RemoteVideo/RemoteVideoHelper.php index c3a51f0f..b254c8f8 100644 --- a/Helper/RemoteVideo/RemoteVideoHelper.php +++ b/Helper/RemoteVideo/RemoteVideoHelper.php @@ -3,115 +3,21 @@ namespace Kunstmaan\MediaBundle\Helper\RemoteVideo; use Kunstmaan\MediaBundle\Entity\Media; +use Kunstmaan\MediaBundle\Helper\Remote\AbstractRemoteHelper; +use Kunstmaan\MediaBundle\Helper\Remote\RemoteInterface; /** * Kunstmaan\MediaBundle\Entity\Video * Class that defines a video in the system */ -class RemoteVideoHelper +class RemoteVideoHelper extends AbstractRemoteHelper implements RemoteInterface { - - /** - * @var Media - */ - protected $media; - /** * @param Media $media */ public function __construct(Media $media) { - $this->media = $media; + parent::__construct($media); $this->media->setContentType(RemoteVideoHandler::CONTENT_TYPE); } - - /** - * @return string - */ - public function getName() - { - return $this->media->getName(); - } - - /** - * @param string $name - */ - public function setName($name) - { - $this->media->setName($name); - } - - /** - * @return Media - */ - public function getMedia() - { - return $this->media; - } - - /** - * @return string - */ - public function getCode() - { - return $this->media->getMetadataValue('code'); - } - - /** - * Set code - * @param string $code - * - * @return RemoteVideoHelper - */ - public function setCode($code) - { - $this->media->setMetadataValue('code', $code); - - return $this; - } - - /** - * @return string|null - */ - public function getThumbnailUrl() - { - return $this->media->getMetadataValue('thumbnail_url'); - } - - /** - * Set thumbnail url - * @param string $url - * - * @return RemoteVideoHelper - */ - public function setThumbnailUrl($url) - { - $this->media->setMetadataValue('thumbnail_url', $url); - - return $this; - } - - /** - * Get type - * - * @return string|null - */ - public function getType() - { - return $this->media->getMetadataValue('type'); - } - - /** - * Set type - * @param string $type - * - * @return RemoteVideoHelper - */ - public function setType($type) - { - $this->media->setMetadataValue('type', $type); - - return $this; - } - }