Skip to content

Commit

Permalink
Merge pull request #57 from ngodfraind/bugfixes
Browse files Browse the repository at this point in the history
Refactoring how the banner is handled.
  • Loading branch information
maxailloud committed Oct 6, 2015
2 parents 82e0cec + 688ebdc commit 3336114
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 113 deletions.
4 changes: 4 additions & 0 deletions Controller/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
100 changes: 0 additions & 100 deletions Entity/BlogOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,6 @@ public function getBannerHeight()
*/
public function setBannerBackgroundImage($bannerBackgroundImage)
{
if (null !== $this->bannerBackgroundImage) {
$this->oldFileName = $this->bannerBackgroundImage;
}
$this->bannerBackgroundImage = $bannerBackgroundImage;

return $this;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
3 changes: 0 additions & 3 deletions Form/BlogBannerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Listener/BlogListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
31 changes: 29 additions & 2 deletions Manager/BlogManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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();
}
}
4 changes: 3 additions & 1 deletion Resources/config/services/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 }
8 changes: 4 additions & 4 deletions Resources/views/layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</label>
</div>
<div class="row">
<div class="col-md-4" id="icap_blog_banner_form_banner_background_image_container" data-image-path="{{ asset(blogOptions.uploadDir) }}">
<div class="col-md-4" id="icap_blog_banner_form_banner_background_image_container" data-image-path="{{ asset(get_blog_upload_dir()) }}">
{{ form_label(bannerForm.file) }}
{{ form_widget(bannerForm.file) }}
</div>
Expand All @@ -42,9 +42,9 @@
</div>
</div>
<div class="row">
<button type="button" class="btn btn-danger btn-xs{% if null == blogOptions.bannerBackgroundImageWebPath %} hidden{% endif %} remove_banner_background_image" id="remove_banner_background_image">Réinitialiser l'image d'arrière plan</button>
<button type="button" class="btn btn-danger btn-xs{% if null == get_blog_banner(_resource) %} hidden{% endif %} remove_banner_background_image" id="remove_banner_background_image">Réinitialiser l'image d'arrière plan</button>
</div>
<div class="row{% if null == blogOptions.bannerBackgroundImageWebPath %} hidden{% endif %}" id="banner_background_image_parameters">
<div class="row{% if null == get_blog_banner(_resource) %} hidden{% endif %}" id="banner_background_image_parameters">
<div class="col-md-2">
{{ form_label(bannerForm.banner_background_image_position) }}
<table class="position_table">
Expand Down Expand Up @@ -95,7 +95,7 @@
{% endif %}
{% if blogOptions.isBannerActivate or (false == blogOptions.isBannerActivate and is_granted('ADMINISTRATE', _resource)) %}
<a href="{{ path('icap_blog_view', {'blogId': _resource.id}) }}" title="{{ _resource.resourceNode.name }}">
<div id="blog_banner" class="col-md-12 well blog_banner{% if false == blogOptions.isBannerActivate %} hidden{% endif %}" style="height: {{ blogOptions.bannerHeight }}px; background-color: {{ blogOptions.bannerBackgroundColor }}; background-image: {% if null == blogOptions.bannerBackgroundImageWebPath %}none{% else %}url('{{ asset(blogOptions.bannerBackgroundImageWebPath) }}'){% endif %}; background-position: {{ blogOptions.bannerBackgroundImagePosition }}; background-repeat: {{ blogOptions.bannerBackgroundImageRepeat }};">
<div id="blog_banner" class="col-md-12 well blog_banner{% if false == blogOptions.isBannerActivate %} hidden{% endif %}" style="height: {{ blogOptions.bannerHeight }}px; background-color: {{ blogOptions.bannerBackgroundColor }}; background-image: {% if null == get_blog_banner(_resource) %}none{% else %}url('{{ asset(get_blog_banner_web_path(_resource)) }}'){% endif %}; background-position: {{ blogOptions.bannerBackgroundImagePosition }}; background-repeat: {{ blogOptions.bannerBackgroundImageRepeat }};">
</div>
</a>
{% endif %}
Expand Down
29 changes: 26 additions & 3 deletions Twig/IcapBlogExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ class IcapBlogExtension extends \Twig_Extension
/** @var \Icap\BlogBundle\Repository\PostRepository */
protected $postRepository;

public function __construct(TagManager $tagManager, PostRepository $postManager)
protected $uploadDir;
protected $webDirectory;

public function __construct(TagManager $tagManager, PostRepository $postManager, $uploadDir, $webDirectory)
{
$this->tagManager = $tagManager;
$this->postRepository = $postManager;
$this->uploadDir = $uploadDir;
$this->webDirectory = $webDirectory;
}

/**
Expand All @@ -43,8 +48,11 @@ public function getName()
public function getFunctions()
{
return array(
'blog_tags' => new \Twig_Function_Method($this, 'getTagsByBlog'),
'blog_authors' => new \Twig_Function_Method($this, 'getAuthorsByBlog')
'blog_tags' => new \Twig_Function_Method($this, 'getTagsByBlog'),
'blog_authors' => new \Twig_Function_Method($this, 'getAuthorsByBlog'),
'get_blog_banner' => new \Twig_Function_Method($this, 'getBlogBanner'),
'get_blog_upload_dir' => new \Twig_Function_Method($this, 'getBlogUploadDir'),
'get_blog_banner_web_path' => new \Twig_Function_Method($this, 'getBlogBannerWebPath')
);
}

Expand Down Expand Up @@ -77,6 +85,21 @@ public function getTagnames($tags)
return $tagNames;
}

public function getBlogBanner(Blog $blog)
{
return realpath($this->uploadDir . '/' . $blog->getOptions()->getBannerBackgroundImage());
}

public function getBlogUploadDir()
{
return $this->webDirectory;
}

public function getBlogBannerWebPath(Blog $blog)
{
return $this->webDirectory . '/' . $blog->getOptions()->getBannerBackgroundImage();
}

public function highlight($sentence, $search)
{
$searchParameters = explode(' ', trim($search));
Expand Down

0 comments on commit 3336114

Please sign in to comment.