Skip to content

Commit

Permalink
feat:(UpdateMedias-#49): Update medias
Browse files Browse the repository at this point in the history
  • Loading branch information
Gael committed Mar 20, 2023
1 parent 649a61f commit 644a978
Show file tree
Hide file tree
Showing 20 changed files with 213 additions and 55 deletions.
12 changes: 8 additions & 4 deletions public/assets/css/sass/pages/home.scss
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@
}

#confirm-modal,
#confirm-modal-update {
#confirm-modal-update-image,
#confirm-modal-update-video {
position: fixed;
display: none !important;
z-index: 9999;
Expand All @@ -138,7 +139,8 @@
}

#confirm-modal::before,
#confirm-modal-update::before {
#confirm-modal-update-image::before,
#confirm-modal-update-video::before {
content: "";
display: none;
position: absolute;
Expand All @@ -151,7 +153,8 @@
}

#confirm-modal .modal-content,
#confirm-modal-update .modal-content {
#confirm-modal-update-image .modal-content,
#confirm-modal-update-video .modal-content {
position: relative;
z-index: 1;
background-color: #fff;
Expand All @@ -166,7 +169,8 @@
left: 50%;
}

#confirm-modal-update .modal-content {
#confirm-modal-update-image .modal-content,
#confirm-modal-update-video .modal-content {
text-align: left;
}

Expand Down
2 changes: 1 addition & 1 deletion public/assets/css/style.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/assets/css/style.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion public/assets/js/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Scroll to tricks section in the homepage
function scrollToTricks() {
const element = document.getElementById("tricks");
element.scrollIntoView({ behavior: "smooth" });
Expand Down
11 changes: 5 additions & 6 deletions public/assets/js/newMedia.js → public/assets/js/updateImage.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
// Modal to confirm trick deleted
window.addEventListener('load', () => {
const updateButtons = document.querySelectorAll('.update-trick-modal');
const modalImage = document.getElementById('modal-image');
const trickId = document.getElementById('trick_image_form_trickId');
const currentImage = document.getElementById('trick_image_form_currentImage');
const confirmModal = document.getElementById('confirm-modal-update');
const confirmYesButton = document.getElementById('confirm-yes-update');
const confirmNoButton = document.getElementById('confirm-no-update');
const oldImage = document.getElementById('trick_image_form_oldImage');
const confirmModal = document.getElementById('confirm-modal-update-image');
const confirmYesButton = document.getElementById('confirm-yes-update-image');
const confirmNoButton = document.getElementById('confirm-no-update-image');

updateButtons.forEach(button => {
button.addEventListener('click', event => {
const image = button.getAttribute('data-image-name');
modalImage.src = '/assets/uploads/tricks/mini/300x300-' + image;
currentImage.value = image;
oldImage.value = image;
trickId.value = button.getAttribute('data-trick-id');
confirmModal.style.setProperty("display", "block", "important");
});
Expand Down
1 change: 1 addition & 0 deletions public/assets/js/updateVideo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('updateVideo.js loaded');
104 changes: 95 additions & 9 deletions src/Controller/TrickController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Form\CreateCommentFormType;
use App\Form\TrickFormType;
use App\Form\TrickImageFormType;
use App\Form\TrickVideoFormType;
use App\Repository\CommentRepository;
use App\Repository\TrickImageRepository;
use App\Repository\TrickVideoRepository;
Expand Down Expand Up @@ -231,36 +232,121 @@ public function update(
]);
}

// Form to add a new image
// Form to update image
$formNewImage = $this->createForm(TrickImageFormType::class);
$formNewImage->handleRequest($request);

if ($formNewImage->isSubmitted() && $formNewImage->isValid()) {
$currentImage = $formNewImage->get('currentImage')->getData();
$newImage = $formNewImage->get('newImage')->getData();
$oldImage = $formNewImage->get('oldImage')->getData();
$trickId = $formNewImage->get('trickId')->getData();

// Check if currentImage exist and is associated with the trick
$currentImageExist = $em->getRepository(TrickImage::class)->findOneBy([
'name' => $currentImage,
// Check if newImage is valid image
if ($newImage === null) {
$this->addFlash('danger', "Vous devez sélectionner une nouvelle image");
return $this->redirectToRoute('trick_update', [
'slug' => $trick->getSlug()
]);
}

$trickImage = $em->getRepository(TrickImage::class)->findOneBy([
'name' => $oldImage,
'trick' => $trickId
]);
if (!$currentImageExist) {

// Check if oldImage exist and is associated with the trick
if (!$trickImage) {
$this->addFlash('danger', "L'image n'existe pas");
return $this->redirectToRoute('trick_update', [
'slug' => $trick->getSlug()
]);
}

// Delete current image
$imageService->delete($currentImage);
// Check if oldImage is not the default image
if ($oldImage === 'default.png') {
$this->addFlash('danger', "L'image par défaut ne peut pas être supprimée");
return $this->redirectToRoute('trick_update', [
'slug' => $trick->getSlug()
]);
}

// Delete old image
$imageService->delete($oldImage, 'tricks', 300, 300);
$em->remove($trickImage);

dd('wip');
// Add new image
$file = $imageService->add($newImage, 'tricks', 300, 300);
$image = new TrickImage();
$image->setName($file);
$trick->addImage($image);
$em->persist($trick);

// Save changes and redirect
$em->flush();
$this->addFlash('success', "L'image a été modifiée avec succès");
return $this->redirectToRoute('trick_update', [
'slug' => $trick->getSlug()
]);
}

// Form to update video
$formNewVideo = $this->createForm(TrickVideoFormType::class);
$formNewVideo->handleRequest($request);

if ($formNewVideo->isSubmitted() && $formNewVideo->isValid()) {
$newVideo = $formNewVideo->get('newVideo')->getData();
$oldVideo = $formNewVideo->get('oldVideo')->getData();
$trickId = $formNewVideo->get('trickId')->getData();

// Check if newVideo is valid video
if ($newVideo === null) {
$this->addFlash('danger', "Vous devez sélectionner une nouvelle vidéo");
return $this->redirectToRoute('trick_update', [
'slug' => $trick->getSlug()
]);
}

$trickVideo = $em->getRepository(TrickVideo::class)->findOneBy([
'url' => $oldVideo,
'trick' => $trickId
]);

// Check if oldVideo exist and is associated with the trick
if (!$trickVideo) {
$this->addFlash('danger', "La vidéo n'existe pas");
return $this->redirectToRoute('trick_update', [
'slug' => $trick->getSlug()
]);
}

// Construct embed url
$url = $urlService->constructEmbedUrl($urlService->getUrlInfos($newVideo));

// Check if embed url is valid
if (!$urlService->isEmbedUrlValid($url)) {
$this->addFlash('danger', "'{$newVideo}' n'est pas une URL de vidéo valide");
return $this->redirectToRoute('trick_update', [
'slug' => $trick->getSlug()
]);
}

// Update video
$trickVideo->setUrl($url);
$em->persist($trickVideo);

// Save changes and redirect
$em->flush();
$this->addFlash('success', "La vidéo a été modifiée avec succès");
return $this->redirectToRoute('trick_update', [
'slug' => $trick->getSlug()
]);
}

return $this->render('trick/update.html.twig', [
'trick' => $trick,
'trickForm' => $form->createView(),
'trickImageForm' => $formNewImage->createView(),
'trickVideoForm' => $formNewVideo->createView(),
'isAdmin' => $trickService->userIsAdmin()
]);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Form/TrickImageFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class TrickImageFormType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('image', FileType::class, [
->add('newImage', FileType::class, [
'label' => 'Nouvelle image',
'help' => "Formats acceptés : jpg, jpeg, png, webp. Taille max : 1Mo",
'required' => false,
'required' => true,
'mapped' => false,
'constraints' => [
new Image([
Expand All @@ -34,7 +34,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'class' => 'form-control'
]
])
->add('currentImage', HiddenType::class, [
->add('oldImage', HiddenType::class, [
'mapped' => false,
'attr' => [
'readonly' => true
Expand Down
47 changes: 47 additions & 0 deletions src/Form/TrickVideoFormType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Form;

use App\Entity\TrickVideo;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class TrickVideoFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('newVideo', TextType::class, [
'label' => 'Vidéo',
'help' => 'Lien de la vidéo (Youtube, Dailymotion, Vimeo)',
'required' => true,
'mapped' => false,
'attr' => [
'class' => 'form-control'
]
])
->add('oldVideo', HiddenType::class, [
'mapped' => false,
'attr' => [
'readonly' => true
],
])
->add('trickId', HiddenType::class, [
'mapped' => false,
'attr' => [
'readonly' => true
],
]);
;
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => TrickVideo::class,
]);
}
}
2 changes: 1 addition & 1 deletion templates/main/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@

{% block javascripts %}
<script src="{{ asset('assets/js/index.js') }}" defer></script>
<script src="{{ asset('assets/js/modal.js') }}" defer></script>
<script src="{{ asset('assets/js/deleteTrickConfirm.js') }}" defer></script>
{% endblock %}
10 changes: 6 additions & 4 deletions templates/trick/_form_media.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@
<img height="250" class="card-img-top" src="{{ asset('assets/uploads/tricks/mini/300x300-' ~ image.name) }}" alt="Image de {{ trick.title }}">
</div>
<div class="mobile-medias-links d-flex flex-row justify-content-between align-items-center">
<a href="#">
<a class="update-trick-modal"
data-image-name="{{ image.name }}"
data-trick-id="{{ trick.id }}">
<i class="fa-sharp fa-solid fa-pencil"></i>
</a>
<a href="{{ path('trick_image_delete', {slug: trick.slug, id: image.id}) }}"
data-delete
data-confirm-message="Êtes-vous sûr de vouloir supprimer cette vidéo ?"
data-token="{{ csrf_token('delete' ~ image.id) }}">
data-delete
data-confirm-message="Êtes-vous sûr de vouloir supprimer cette vidéo ?"
data-token="{{ csrf_token('delete' ~ image.id) }}">
<i class="fa-regular fa-trash-can"></i>
</a>
</div>
Expand Down
16 changes: 0 additions & 16 deletions templates/trick/_modal_update.html.twig

This file was deleted.

16 changes: 16 additions & 0 deletions templates/trick/_modal_update_image.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div id="confirm-modal-update-image">
<div class="modal-content">
<h5 class="modal-title">Modifier une image</h5>
<div class="mt-2">
<img id="modal-image" height="250" class="card-img-top" src="">
</div>
<p id="confirm-message-update"></p>
{{ form_start(trickImageForm) }}
{{ form_row(trickImageForm.newImage) }}
<div class="d-flex flex-row justify-content-center">
<button id="confirm-yes-update-image" type="submit" class="confirm-modal-validate mx-2">Modifier</button>
<button id="confirm-no-update-image" type="button" class="btn confirm-modal-cancel mx-2">Annuler</button>
</div>
{{ form_end(trickImageForm) }}
</div>
</div>
Loading

0 comments on commit 644a978

Please sign in to comment.