Browse Source

Ajout de plusieurs vidéos fonctionnel

François Drouhard 1 year ago
parent
commit
4b66d41213

+ 9 - 5
assets/js/addCollectionWidget.js

@@ -47,7 +47,9 @@
             var $container = $($(this).attr("data-list-selector"));
             $li = $container.find('li');
             $li.each(function() {
-                $(this).find(':input').prop("readonly", true);
+                if (chemin !== "") {
+                    $(this).find(':input').prop("readonly", true);
+                }
                 addDeleteLink($(this));
             })
       
@@ -70,11 +72,13 @@
                 // create a new list element and add it to the list
                 var newElem = jQuery(list.attr('data-widget-tags')).html(newWidget);
                 
-                requete (chemin, function(output) {
-                    newElem.find(':input').autocomplete({
-                        source: output
+                if (chemin !== "") {
+                    requete (chemin, function(output) {
+                        newElem.find(':input').autocomplete({
+                            source: output
+                        });
                     });
-                })
+                }
                 
                 addDeleteLink(newElem);
 

+ 35 - 0
migrations/Version20230419174011.php

@@ -0,0 +1,35 @@
+<?php
+
+declare(strict_types=1);
+
+namespace DoctrineMigrations;
+
+use Doctrine\DBAL\Schema\Schema;
+use Doctrine\Migrations\AbstractMigration;
+
+/**
+ * Auto-generated Migration: Please modify to your needs!
+ */
+final class Version20230419174011 extends AbstractMigration
+{
+    public function getDescription(): string
+    {
+        return '';
+    }
+
+    public function up(Schema $schema): void
+    {
+        // this up() migration is auto-generated, please modify it to your needs
+        $this->addSql('ALTER TABLE film DROP FOREIGN KEY FK_8244BE2226A02EFC');
+        $this->addSql('DROP INDEX UNIQ_8244BE2226A02EFC ON film');
+        $this->addSql('ALTER TABLE film DROP media_video_id');
+    }
+
+    public function down(Schema $schema): void
+    {
+        // this down() migration is auto-generated, please modify it to your needs
+        $this->addSql('ALTER TABLE film ADD media_video_id INT DEFAULT NULL');
+        $this->addSql('ALTER TABLE film ADD CONSTRAINT FK_8244BE2226A02EFC FOREIGN KEY (media_video_id) REFERENCES media_video (id)');
+        $this->addSql('CREATE UNIQUE INDEX UNIQ_8244BE2226A02EFC ON film (media_video_id)');
+    }
+}

+ 1 - 0
src/Controller/VideothequeController.php

@@ -15,6 +15,7 @@ use App\Form\FilmType;
 use App\Repository\CommentaireRepository;
 use App\Repository\FilmRepository;
 use App\Repository\GenreRepository;
+use App\Repository\MediaVideoRepository;
 use App\Repository\RealisateurRepository;
 use App\Service\CommentaireManager;
 use App\Service\FilmManager;

+ 7 - 6
src/Entity/Film.php

@@ -5,7 +5,6 @@ namespace App\Entity;
 use App\Repository\FilmRepository;
 use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\Common\Collections\Collection;
-use Doctrine\DBAL\Types\DateImmutableType;
 use Doctrine\DBAL\Types\Types;
 use Doctrine\ORM\Mapping as ORM;
 use Symfony\Component\Validator\Constraints as Assert;
@@ -45,11 +44,13 @@ class Film
     
     private ?int $nbComs = null;
 
+    /*
     #[Assert\Valid()]
     #[ORM\OneToOne(targetEntity: MediaVideo::class, cascade: ["persist","remove"])]
     #[ORM\JoinColumn(nullable: true)]
     
     private ?MediaVideo $mediaVideo = null;
+    */
 
     #[ORM\ManyToMany(targetEntity: Realisateur::class, inversedBy: "films", cascade: ["persist"])]
     
@@ -85,8 +86,8 @@ class Film
     #[ORM\Column(type: Types::DATE_IMMUTABLE, nullable: true)]
     private ?\DateTimeImmutable $dateSortie = null;
 
-    #[ORM\OneToMany(mappedBy: 'film', targetEntity: MediaVideo::class, orphanRemoval: true)]
-    private Collection $mediaVideos;
+    #[ORM\OneToMany(mappedBy: 'film', targetEntity: MediaVideo::class, orphanRemoval: true, cascade: ['persist'])]
+    private ?Collection $mediaVideos = null;
 
     public function __construct()
     {
@@ -315,7 +316,7 @@ class Film
         return $this;
     }
 
-    public function getMediaVideo(): ?MediaVideo
+    /*public function getMediaVideo(): ?MediaVideo
     {
         return $this->mediaVideo;
     }
@@ -325,12 +326,12 @@ class Film
         $this->mediaVideo = $mediaVideo;
 
         return $this;
-    }
+    }*/
 
     /**
      * @return Collection<int, MediaVideo>
      */
-    public function getMediaVideos(): Collection
+    public function getMediaVideos(): ?Collection
     {
         return $this->mediaVideos;
     }

+ 11 - 1
src/Entity/MediaVideo.php

@@ -44,6 +44,11 @@ class MediaVideo
         $this->setIdentif($identif);
     }
 
+    public function __toString(): string
+    {
+        return "MediaVideo Entity";
+    }
+
     public function getId(): ?int
     {
         return $this->id;
@@ -242,8 +247,13 @@ class MediaVideo
 
     public function setFilm(?Film $film): self
     {
+        if ($film) {
+            if (!$film->getMediaVideos()->contains($this))
+            {
+                $film->addMediaVideo($this);
+            }
+        }
         $this->film = $film;
-
         return $this;
     }
 }

+ 17 - 12
src/Form/FilmType.php

@@ -2,6 +2,7 @@
 
 namespace App\Form;
 
+use App\Entity\Film;
 use Symfony\Component\Form\Extension\Core\Type\CollectionType;
 use Symfony\Component\Form\Extension\Core\Type\TextType;
 use Symfony\Component\Form\Extension\Core\Type\TextareaType;
@@ -31,31 +32,35 @@ class FilmType extends AbstractType
             ->add('lien', TextType::class, array(
                 'required'  => false
             ))
-            ->add('mediaVideo', MediaVideoType::class,array(
-                'required'  =>  false,
-            ))
+            ->add('mediaVideos',  CollectionType::class, [
+                'entry_type' => MediaVideoType::class,
+                'entry_options' =>  array('label'=>false),
+                'allow_add'     =>  true,
+                'allow_delete'  =>  true,
+                'by_reference'  => false
+            ])
             ->add('dateSortie', DateType::class, [
                 'input'     =>  'datetime_immutable',
                 'html5'     =>  true,
                 'widget'    =>  'single_text',
                 'required'  =>  false
             ])
-            ->add('information', TextareaType::class, array(
+            ->add('information', TextareaType::class, [
                 'required' => false,
-            ))
-            ->add('genres', CollectionType::class, array(
+            ])
+            ->add('genres', CollectionType::class, [
                 'entry_type'    =>  GenreType::class,
                 'entry_options' =>  array('label'=>false),
                 'allow_add'     =>  true,
                 'allow_delete'  =>  true
-            ))
-            ->add('realisateurs', CollectionType::class, array(
+            ])
+            ->add('realisateurs', CollectionType::class, [
                 'entry_type'    =>  RealisateurType::class,
                 'entry_options' =>  array('label'=>false),
                 'allow_add'     =>  true,
                 'allow_delete'  =>  true,
                 'allow_extra_fields'    => true
-            ))
+            ])
 		    ->add('save', SubmitType::class, array('label' => 'Enregistrer'));
     }
     
@@ -64,9 +69,9 @@ class FilmType extends AbstractType
      */
     public function configureOptions(OptionsResolver $resolver)
     {
-        $resolver->setDefaults(array(
-            'data_class' => 'App\Entity\Film'
-        ));
+        $resolver->setDefaults([
+            'data_class' => Film::class
+        ]);
     }
 
     /**

+ 8 - 2
src/Service/FilmManager.php

@@ -17,6 +17,7 @@ class FilmManager {
         protected EntityManagerInterface $em,
         protected UniciteCollections $uc,
         protected CommentaireManager $cm,
+        protected MediaVideoManager $mvm,
         Security $security
     )
     {
@@ -41,10 +42,15 @@ class FilmManager {
     {
         $commentaires = $film->getCommentaires();
         /** @var \App\Entity\Commentaire $commentaire */
-        foreach($commentaires as $commentaire)
-        {
+        foreach($commentaires as $commentaire) {
             $this->cm->delCommentaire($commentaire);
         }
+
+        $mediaVideos = $film->getMediaVideos();
+        /** @var \App\Entity\MediaVideo $mediaVideos */
+        foreach($mediaVideos as $media) {
+            $this->mvm->delMediaVideo($media);
+        }
         $this->em->remove($film);
 
         $this->em->flush();

+ 21 - 0
src/Service/MediaVideoManager.php

@@ -0,0 +1,21 @@
+<?php 
+
+namespace App\Service;
+
+use App\Entity\MediaVideo;
+use Doctrine\ORM\EntityManagerInterface;
+
+class MediaVideoManager
+{
+    public function __construct(protected EntityManagerInterface $em)
+    {
+        
+    }
+    public function delMediaVideo(MediaVideo $mediaVideo, bool $flush = false): void
+    {
+        $this->em->remove($mediaVideo);
+        if ($flush) {
+            $this->em->flush();
+        }
+    }
+}

+ 1 - 1
src/Service/TmdbApiService.php

@@ -131,7 +131,7 @@ class TmdbApiService
         if (!empty($videoTmdb)) {
             $video = new MediaVideo();
             $video->setFromTmdb($videoTmdb['type'], $videoTmdb['identif']);
-            $film->setMediaVideo($video);
+            $film->addMediaVideo($video);
         }
 
         $film = $this->uc->assureUniciteCollections($film);

+ 27 - 1
templates/videotheque/form.html.twig

@@ -12,12 +12,15 @@
                     {% form_theme form 'bootstrap_5_layout.html.twig' %}
                     {{ form_row(form.information) }}
 
+                    {#
                     {{ form_label(form.mediaVideo, 'Bande annonce') }}
                     {{ form_errors(form.mediaVideo) }}
                     {{ form_widget(form.mediaVideo) }}
+                    #}
                     
                 </div>
             </div>
+
             <div class="col-md">
                 <div class="card">
                     <div class="card-header">
@@ -65,7 +68,30 @@
                         </ul>
                     </div>
                 </div>
-
+            </div>
+            <div class="">
+                <div class="card">
+                    <div class="card-header">
+                        {{ form_label(form.mediaVideos, 'Bandes annonces') }}
+                    </div>
+                    <div class="card-body">
+                        <button type="button" id="add_media-video" class="btn btn-link add-another-collection-widget mb-3" data-path="" data-list-selector="#media-video-fields-list">Ajouter une bande annonce</button>
+                        <ul id="media-video-fields-list"
+                            data-prototype="{{ form_widget(form.mediaVideos.vars.prototype)|e }}"
+                            data-widget-tags="{{ '<li class="list-group-item mb-3"></li>'|e }}"
+                            data-widget-counter="{{ form.children|length }}">
+                            {% for mediaVideoField in form.mediaVideos %}
+                            <li class="list-group-item mb-3">
+                                {{ form_widget(mediaVideoField) }}
+                                {{ form_errors(mediaVideoField) }}
+                            </li>
+                            {% else %}
+                                {{ form_widget(form.mediaVideos) }}
+                                {{ form_errors(form.mediaVideos) }}
+                            {% endfor %}
+                        </ul>
+                    </div>
+                </div>
             </div>
         </div>
     </div>

+ 5 - 5
templates/videotheque/voirfilm.html.twig

@@ -144,13 +144,13 @@
                     <h5>Bande Annonce</h5>
                 </div>
                 <div class="card-body text-center">
-                    {% if film.mediaVideo.video is defined %}
-                        <div class="ratio ratio-4x3">
-                            {{ film.mediaVideo.video|raw }}
+                    {% for mediaVideo in film.mediaVideos %}
+                        <div class="ratio ratio-4x3 mb-3">
+                            {{ mediaVideo.video|raw }}
                         </div>
-                    {% else %}
+                    {% else %}  
                         <p>Pas encore de bande annonce</p>
-                    {% endif %}
+                    {% endfor %}
                 </div>
             </div>
         </div>