From e1a4872fd9142f5761f2675cda363f882eda062d Mon Sep 17 00:00:00 2001 From: Nicolas Godfraind Date: Tue, 6 Oct 2015 11:49:45 +0200 Subject: [PATCH] Refactoring how the banner is handled. --- Controller/BlogController.php | 4 + Entity/BlogOptions.php | 100 ------------------------- Form/BlogBannerType.php | 3 - Listener/BlogListener.php | 3 + Manager/BlogManager.php | 31 +++++++- Resources/config/services/services.yml | 4 +- Resources/views/layout.html.twig | 8 +- Twig/IcapBlogExtension.php | 29 ++++++- 8 files changed, 69 insertions(+), 113 deletions(-) diff --git a/Controller/BlogController.php b/Controller/BlogController.php index f7c0714..ebc52e6 100644 --- a/Controller/BlogController.php +++ b/Controller/BlogController.php @@ -228,6 +228,10 @@ public function configureBannerAction(Request $request, Blog $blog) $form->submit($request); if ($form->isValid()) { + $this->container->get('icap_blog.manager.blog')->updateBanner( + $form->get('file')->getData(), + $blogOptions + ); $entityManager = $this->getDoctrine()->getManager(); $translator = $this->get('translator'); $flashBag = $this->get('session')->getFlashBag(); diff --git a/Entity/BlogOptions.php b/Entity/BlogOptions.php index 8d85b27..c70a50f 100644 --- a/Entity/BlogOptions.php +++ b/Entity/BlogOptions.php @@ -339,9 +339,6 @@ public function getBannerHeight() */ public function setBannerBackgroundImage($bannerBackgroundImage) { - if (null !== $this->bannerBackgroundImage) { - $this->oldFileName = $this->bannerBackgroundImage; - } $this->bannerBackgroundImage = $bannerBackgroundImage; return $this; @@ -422,12 +419,6 @@ public function isBannerActivate() */ public function setFile(UploadedFile $file) { - $newFileName = $file->getClientOriginalName(); - - if ($this->bannerBackgroundImage !== $newFileName) { - $this->oldFileName = $this->bannerBackgroundImage; - $this->bannerBackgroundImage = null; - } $this->file = $file; return $this; @@ -480,95 +471,4 @@ public function getDisplayPostViewCounter() { return $this->displayPostViewCounter; } - - /** - * @return null|string - */ - public function getBannerBackgroundImageAbsolutePath() - { - return (null === $this->bannerBackgroundImage) ? null : $this->getUploadRootDir() . DIRECTORY_SEPARATOR . $this->bannerBackgroundImage; - } - - /** - * @return null|string - */ - public function getBannerBackgroundImageWebPath() - { - return (null === $this->bannerBackgroundImage) ? null : $this->getUploadDir() . DIRECTORY_SEPARATOR . $this->bannerBackgroundImage; - } - - /** - * @throws \Exception - * @return string - */ - protected function getUploadRootDir() - { - $ds = DIRECTORY_SEPARATOR; - - $uploadRootDir = sprintf('%s%s..%s..%s..%s..%s..%s..%sweb%s%s', __DIR__, $ds, $ds, $ds, $ds, $ds, $ds, $ds, $ds, $this->getUploadDir()); - - if (!is_dir($uploadRootDir)) { - if (false === mkdir($uploadRootDir)) { - throw new \Exception(sprintf("Unable to create the upload directory '%s' for blog banner.", $uploadRootDir)); - } - } - - $realpathUploadRootDir = realpath($uploadRootDir); - - if (false === $realpathUploadRootDir) { - throw new \Exception(sprintf("Invalid upload root dir '%s'for uploading blog banner background images.", $uploadRootDir)); - } - - return $realpathUploadRootDir; - } - - /** - * @return string - */ - public function getUploadDir() - { - return sprintf("uploads%sblogs", DIRECTORY_SEPARATOR); - } - - /** - * @ORM\PreUpdate() - */ - public function preUpdate(PreUpdateEventArgs $event) - { - if (null !== $this->file) { - $this->bannerBackgroundImage = $this->file->getClientOriginalName(); - } - } - - /** - * @ORM\PostUpdate() - */ - public function postUpdate() - { - if (null === $this->file && null === $this->oldFileName) { - return; - } - - if (null !== $this->bannerBackgroundImage) { - $this->file->move($this->getUploadRootDir(), $this->bannerBackgroundImage); - } - - if (null !== $this->oldFileName) { - unlink($this->getUploadRootDir() . DIRECTORY_SEPARATOR . $this->oldFileName); - $this->oldFileName = null; - } - - $this->file = null; - } - - /** - * @ORM\PostRemove() - */ - public function postRemove() - { - $filePath = $this->getBannerBackgroundImageAbsolutePath(); - if (null !== $filePath) { - unlink($filePath); - } - } } diff --git a/Form/BlogBannerType.php b/Form/BlogBannerType.php index d7ad685..a50552d 100755 --- a/Form/BlogBannerType.php +++ b/Form/BlogBannerType.php @@ -29,9 +29,6 @@ public function buildForm(FormBuilderInterface $builder, array $options) 'theme_options' => array('label_width' => ''), 'required' => false )) - ->add('banner_background_image', 'hidden', array( - 'required' => false - )) ->add('banner_background_image_position', 'text', array( 'theme_options' => array('label_width' => ''), 'required' => false diff --git a/Listener/BlogListener.php b/Listener/BlogListener.php index 2cc3392..43c982f 100644 --- a/Listener/BlogListener.php +++ b/Listener/BlogListener.php @@ -81,6 +81,9 @@ public function onOpen(OpenResourceEvent $event) */ public function onDelete(DeleteResourceEvent $event) { + $blog = $event->getResource(); + $options = $blog->getOptions(); + @unlink($this->container->getParameter('icap.blog.banner_directory') . DIRECTORY_SEPARATOR . $options->getBannerBackgroundImage()); $event->stopPropagation(); } diff --git a/Manager/BlogManager.php b/Manager/BlogManager.php index 76afbb6..a85466c 100644 --- a/Manager/BlogManager.php +++ b/Manager/BlogManager.php @@ -12,6 +12,7 @@ use Icap\BlogBundle\Entity\Post; use Icap\BlogBundle\Entity\Tag; use JMS\DiExtraBundle\Annotation as DI; +use Symfony\Component\HttpFoundation\File\UploadedFile; /** * @DI\Service("icap_blog.manager.blog") @@ -25,12 +26,14 @@ class BlogManager /** * @DI\InjectParams({ - * "objectManager" = @DI\Inject("claroline.persistence.object_manager") + * "objectManager" = @DI\Inject("claroline.persistence.object_manager"), + * "uploadDir" = @DI\Inject("%icap.blog.banner_directory%") * }) */ - public function __construct(ObjectManager $objectManager) + public function __construct(ObjectManager $objectManager, $uploadDir) { $this->objectManager = $objectManager; + $this->uploadDir = $uploadDir; } /** @@ -229,4 +232,28 @@ protected function retrieveTag($name) return $tag; } + + /** + * @param UploadedFile $file + * @param BlogOptions $options + */ + public function updateBanner(UploadedFile $file = null, BlogOptions $options) + { + $ds = DIRECTORY_SEPARATOR; + + if (file_exists($this->uploadDir . $ds . $options->getBannerBackgroundImage()) || $file === null) { + @unlink($this->uploadDir . $ds . $options->getBannerBackgroundImage()); + } + + if ($file) { + $uniqid = uniqid(); + $options->setBannerBackgroundImage($uniqid); + $file->move($this->uploadDir, $uniqid); + } else { + $options->setBannerBackgroundImage(null); + } + + $this->objectManager->persist($options); + $this->objectManager->flush(); + } } diff --git a/Resources/config/services/services.yml b/Resources/config/services/services.yml index 72ca20d..6650c44 100644 --- a/Resources/config/services/services.yml +++ b/Resources/config/services/services.yml @@ -6,6 +6,8 @@ parameters: icap.blog.tag_repository.class: Icap\BlogBundle\Repository\TagRepository icap.blog.post.class: Icap\BlogBundle\Entity\Post icap.blog.post_repository.class: Icap\BlogBundle\Repository\PostRepository + icap.blog.banner_upload_directory: "uploads/blogs" + icap.blog.banner_directory: "%claroline.param.web_directory%/%icap.blog.banner_upload_directory%" services: icap.blog.tag_repository: @@ -32,6 +34,6 @@ services: icap.blog.twig.extension: class: "%icap.blog.twig.extension.class%" - arguments: [@icap.blog.manager.tag, @icap.blog.post_repository] + arguments: [@icap.blog.manager.tag, @icap.blog.post_repository, %icap.blog.banner_directory%, %icap.blog.banner_upload_directory%] tags: - { name: twig.extension } diff --git a/Resources/views/layout.html.twig b/Resources/views/layout.html.twig index 280d510..293be25 100644 --- a/Resources/views/layout.html.twig +++ b/Resources/views/layout.html.twig @@ -25,7 +25,7 @@
-
+
{{ form_label(bannerForm.file) }} {{ form_widget(bannerForm.file) }}
@@ -42,9 +42,9 @@
- +
-