diff --git a/Listener/BlogListener.php b/Listener/BlogListener.php index 43c982f..c7f39b2 100644 --- a/Listener/BlogListener.php +++ b/Listener/BlogListener.php @@ -82,8 +82,7 @@ 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()); + $this->container->get('icap_blog.manager.blog')->delete($blog); $event->stopPropagation(); } diff --git a/Manager/BlogManager.php b/Manager/BlogManager.php index a4242ee..150adae 100644 --- a/Manager/BlogManager.php +++ b/Manager/BlogManager.php @@ -27,7 +27,7 @@ class BlogManager /** * @DI\InjectParams({ * "objectManager" = @DI\Inject("claroline.persistence.object_manager"), - * "uploadDir" = @DI\Inject("%icap.blog.banner_directory%") + * "uploadDir" = @DI\Inject("%icap.blog.banner_directory%") * }) */ public function __construct(ObjectManager $objectManager, $uploadDir) @@ -270,4 +270,17 @@ public function getPanelInfos() ); } + public function delete(Blog $blog) + { + $options = $blog->getOptions(); + //Removing widgets that are referencing the blog. The cascade delete doesn't work properly for some reason. + @unlink($this->uploadDir . DIRECTORY_SEPARATOR . $options->getBannerBackgroundImage()); + $widgetBlogs = $this->objectManager->getRepository('Icap\BlogBundle\Entity\WidgetBlog')->findByResourceNode($blog->getResourceNode()); + $this->objectManager->remove($blog); + + foreach ($widgetBlogs as $widget) { + $this->objectManager->remove($widget); + $this->objectManager->remove($widget->getWidgetInstance()); + } + } }