Skip to content

Commit

Permalink
Create Trick page
Browse files Browse the repository at this point in the history
  • Loading branch information
abdounikarim committed Feb 2, 2024
1 parent b67fa0d commit 620d828
Show file tree
Hide file tree
Showing 28 changed files with 394 additions and 122 deletions.
13 changes: 13 additions & 0 deletions assets/controllers/show_medias_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Controller } from '@hotwired/stimulus';

export default class extends Controller {
async show(e) {
const seeMediasBlock = document.getElementById('see-medias-block');
seeMediasBlock.style.display = 'none';
const dNone = document.querySelectorAll('.d-none');
dNone.forEach((element) => {
element.classList.remove('d-none');
});
e.preventDefault();
}
}
26 changes: 26 additions & 0 deletions assets/styles/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ body {
right: 20px;
}

.bi {
font-size: 1.4em;
}

.bi-arrow-down-circle-fill, .bi-arrow-up-circle-fill {
font-size: 36px;
}

.bi-trash {
color: #842029;
}

.image-trick {
width: 100%;
}
Expand All @@ -44,3 +52,21 @@ body {
.trick-content-actions i {
margin-right: 10px;
}

.form-check {
padding-left: 0;
}

.form-check-label {
display: inherit;
}

.form-check input {
position: relative;
left: 20px;
bottom: 10px;
}

textarea.form-control {
height: 200px;
}
26 changes: 26 additions & 0 deletions migrations/Version20240126180522.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20240126180522 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE trick ADD main_image_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE trick ADD CONSTRAINT FK_D8F0A91EE4873418 FOREIGN KEY (main_image_id) REFERENCES image (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('CREATE INDEX IDX_D8F0A91EE4873418 ON trick (main_image_id)');
}

public function down(Schema $schema): void
{
$this->addSql('CREATE SCHEMA public');
$this->addSql('ALTER TABLE trick DROP CONSTRAINT FK_D8F0A91EE4873418');
$this->addSql('DROP INDEX IDX_D8F0A91EE4873418');
$this->addSql('ALTER TABLE trick DROP main_image_id');
}
}
16 changes: 13 additions & 3 deletions src/Controller/Admin/ImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Entity\Image;
use App\Form\ImageType;
use App\Repository\ImageRepository;
use App\Repository\TrickRepository;
use App\Service\FileHandler;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
Expand Down Expand Up @@ -46,9 +47,10 @@ public function new(Request $request, FileHandler $fileHandler, EntityManagerInt
return $this->redirectToRoute('app_image_index', [], Response::HTTP_SEE_OTHER);
}

return $this->render('image/new.html.twig', [
return $this->render('generic/new.html.twig', [
'image' => $image,
'form' => $form,
'name' => 'image',
]);
}

Expand Down Expand Up @@ -78,17 +80,25 @@ public function edit(Request $request, Image $image, FileHandler $fileHandler, E
return $this->redirectToRoute('app_image_index', [], Response::HTTP_SEE_OTHER);
}

return $this->render('image/edit.html.twig', [
return $this->render('generic/edit.html.twig', [
'image' => $image,
'form' => $form,
'name' => 'image',
]);
}

#[Route('/{id}', name: 'app_image_delete', methods: ['POST'])]
public function delete(Request $request, Image $image, EntityManagerInterface $entityManager): Response
public function delete(Request $request, Image $image, EntityManagerInterface $entityManager, TrickRepository $trickRepository): Response
{
$token = $request->request->getString('_token');
if ($this->isCsrfTokenValid('delete'.$image->getId(), $token)) {
$tricks = $trickRepository->findAll();
foreach ($tricks as $trick) {
if ($trick->getMainImage() === $image) {
$trick->setMainImage(null);
}
$trick->removeImage($image);
}
$entityManager->remove($image);
$entityManager->flush();
}
Expand Down
6 changes: 4 additions & 2 deletions src/Controller/User/TrickController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public function new(Request $request, EntityManagerInterface $entityManager): Re
return $this->redirectToRoute('app_trick_index', [], Response::HTTP_SEE_OTHER);
}

return $this->render('trick/new.html.twig', [
return $this->render('generic/new.html.twig', [
'trick' => $trick,
'form' => $form,
'name' => 'trick',
]);
}

Expand All @@ -63,9 +64,10 @@ public function edit(Request $request, Trick $trick, EntityManagerInterface $ent
return $this->redirectToRoute('app_trick_index', [], Response::HTTP_SEE_OTHER);
}

return $this->render('trick/edit.html.twig', [
return $this->render('generic/edit.html.twig', [
'trick' => $trick,
'form' => $form,
'name' => 'trick',
]);
}

Expand Down
6 changes: 4 additions & 2 deletions src/Controller/User/VideoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ public function new(Request $request, EntityManagerInterface $entityManager): Re
return $this->redirectToRoute('app_video_index', [], Response::HTTP_SEE_OTHER);
}

return $this->render('video/new.html.twig', [
return $this->render('generic/new.html.twig', [
'video' => $video,
'form' => $form,
'name' => 'video',
]);
}

Expand All @@ -64,9 +65,10 @@ public function edit(Request $request, Video $video, EntityManagerInterface $ent
return $this->redirectToRoute('app_video_index', [], Response::HTTP_SEE_OTHER);
}

return $this->render('video/edit.html.twig', [
return $this->render('generic/edit.html.twig', [
'video' => $video,
'form' => $form,
'name' => 'video',
]);
}

Expand Down
1 change: 1 addition & 0 deletions src/DataFixtures/TrickFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function load(ObjectManager $manager): void
$trick1->setCategory($grab);
$trick1->addImage($image1);
$trick1->addImage($image2);
$trick1->setMainImage($image1);
$manager->persist($trick1);

$trick2 = new Trick();
Expand Down
15 changes: 15 additions & 0 deletions src/Entity/Trick.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class Trick
#[ORM\ManyToMany(targetEntity: Video::class, inversedBy: 'tricks')]
private Collection $videos;

#[ORM\ManyToOne]
private ?Image $mainImage = null;

public function __construct()
{
$this->comments = new ArrayCollection();
Expand Down Expand Up @@ -186,4 +189,16 @@ public function removeVideo(Video $video): static

return $this;
}

public function getMainImage(): ?Image
{
return $this->mainImage;
}

public function setMainImage(?Image $mainImage): static
{
$this->mainImage = $mainImage;

return $this;
}
}
20 changes: 19 additions & 1 deletion src/Form/TrickType.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Entity\Video;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand All @@ -19,18 +20,35 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('name', TextType::class, [
'attr' => [
'placeholder' => 'Name',
],
'constraints' => [
new NotBlank(),
],
'label' => false,
])
->add('description', TextareaType::class, [
'attr' => [
'placeholder' => 'Description',
],
'label' => false,
])
->add('description')
->add('category', EntityType::class, [
'class' => Category::class,
'choice_label' => 'name',
'constraints' => [
new NotBlank(),
],
'expanded' => false,
'label' => false,
'multiple' => false,
])
->add('mainImage', EntityType::class, [
'class' => Image::class,
'choice_label' => 'name',
'expanded' => true,
'label' => 'Image principale',
'multiple' => false,
])
->add('images', EntityType::class, [
Expand Down
4 changes: 4 additions & 0 deletions templates/_inc/_delete_form.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<form method="post" action="{{ action }}">
<input type="hidden" name="_token" value="{{ value }}">
<button class="btn btn-danger">{{ content }}</button>
</form>
5 changes: 5 additions & 0 deletions templates/_inc/_load_more_tricks.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
id: trick.id,
htmlName: 'trick',
name: trick.name,
includeForm: include('_inc/_delete_form.html.twig', {
action: path('app_trick_delete', {'id': trick.id}),
content: 'Delete',
value: csrf_token('delete' ~ trick.id),
})
}) }}
</div>
{% endif %}
Expand Down
4 changes: 1 addition & 3 deletions templates/_inc/_modal.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
{{ include('trick/_delete_form.html.twig', {
content: 'Delete',
}) }}
{{ includeForm|raw }}
</div>
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions templates/_inc/_modal_button.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<button type="button" class="btn {{ btn_class }}" data-bs-toggle="modal" data-bs-target="#{{ target }}">
{{ content }}
</button>
18 changes: 18 additions & 0 deletions templates/generic/edit.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends 'base.html.twig' %}

{% block title %}Edit {{ name|capitalize }}{% endblock %}

{% block body %}
<div class="container">
<h1>Edit {{ name|capitalize }}</h1>

{{ include(name ~ '/_form.html.twig', {
'button_label': 'Update',
delete: true
}) }}

<p class="text-center">
<a href="{{ path('app_' ~ name ~ '_index') }}">back to list</a>
</p>
</div>
{% endblock %}
15 changes: 15 additions & 0 deletions templates/generic/new.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends 'base.html.twig' %}

{% block title %}New {{ name|capitalize }}{% endblock %}

{% block body %}
<div class="container">
<h1>Create new {{ name|capitalize }}</h1>

{{ include(name ~ '/_form.html.twig') }}

<p class="text-center">
<a href="{{ path('app_' ~ name ~ '_index') }}">back to list</a>
</p>
</div>
{% endblock %}
4 changes: 0 additions & 4 deletions templates/image/_delete_form.html.twig

This file was deleted.

25 changes: 24 additions & 1 deletion templates/image/_form.html.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
{{ form_start(form) }}
{{ form_widget(form) }}
<button class="btn">{{ button_label|default('Save') }}</button>

<div class="d-flex justify-content-between">
<button class="btn btn-{% if delete is defined %}warning{% else %}primary{% endif %}">{{ button_label|default('Save') }}</button>
{% if delete is defined %}
{{ include('_inc/_modal_button.html.twig', {
btn_class: 'btn-danger',
content: 'Delete',
target: 'image-' ~ image.id
}) }}
{% endif %}
</div>
{{ form_end(form) }}

{% if delete is defined %}
{{ include('_inc/_modal.html.twig', {
id: image.id,
htmlName: 'image',
name: image.name,
includeForm: include('_inc/_delete_form.html.twig', {
action: path('app_image_delete', {'id': image.id}),
content: 'Delete',
value: csrf_token('delete' ~ image.id),
})
}) }}
{% endif %}
13 changes: 0 additions & 13 deletions templates/image/edit.html.twig

This file was deleted.

11 changes: 0 additions & 11 deletions templates/image/new.html.twig

This file was deleted.

4 changes: 0 additions & 4 deletions templates/trick/_delete_form.html.twig

This file was deleted.

Loading

0 comments on commit 620d828

Please sign in to comment.