Explorar el Código

Ajout d'une entité page pour la page contacts

Sangfroid hace 2 meses
padre
commit
1ad39e0251

+ 1 - 1
config/packages/security.yaml

@@ -38,7 +38,7 @@ security:
         # - { path: ^/profile, roles: ROLE_USER }
 
     role_hierarchy:
-        ROLE_ADMIN: ROLE_AUTHOR, ROLE_MODERATOR
+        ROLE_ADMIN: ROLE_AUTHOR, ROLE_MODERATOR, ROLE_CONTACT
 
 when@test:
     security:

+ 36 - 0
migrations/Version20241024211711.php

@@ -0,0 +1,36 @@
+<?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 Version20241024211711 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('CREATE TABLE page (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(255) NOT NULL, content LONGTEXT DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
+        
+        $this->addSql('INSERT INTO page (title, content) VALUES (:title, :content)', [
+            'title' => 'Contact',
+            'content' => 'Ceci est le contenu initial de la page contact.',
+        ]);
+    }
+
+    public function down(Schema $schema): void
+    {
+        // this down() migration is auto-generated, please modify it to your needs
+        $this->addSql('DROP TABLE page');
+    }
+}

+ 52 - 0
src/Controller/PageController.php

@@ -0,0 +1,52 @@
+<?php
+
+namespace App\Controller;
+
+use App\Form\PageType;
+use App\Repository\PageRepository;
+use Doctrine\ORM\EntityManagerInterface;
+use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\Routing\Attribute\Route;
+use Symfony\Component\Security\Http\Attribute\IsGranted;
+
+class PageController extends AbstractController
+{
+    #[Route('/contact', name: 'app_page_contact')]
+    public function index(
+        PageRepository $pageRepository
+    ): Response
+    {
+        $page = $pageRepository->find('1');
+
+        return $this->render('page/index.html.twig', [
+            'page' => $page
+        ]);
+    }
+
+    #[Route('/page/contact/edit', name: 'app_page_contact_edit')]
+    #[IsGranted('ROLE_CONTACT')]
+    public function contactEdit(
+        Request $request,
+        PageRepository $pageRepository,
+        EntityManagerInterface $entityManager
+    ): Response
+    {
+        $page = $pageRepository->find('1');
+        $form = $this->createForm(PageType::class, $page);
+
+        $form->handleRequest($request);
+
+        if ($form->isSubmitted() && $form->isValid()) {
+            $entityManager->flush();
+
+            return $this->redirectToRoute('app_page_contact', [], Response::HTTP_SEE_OTHER);
+        }
+
+        return $this->render('page/edit.html.twig', [
+            'page' => $page,
+            'form' => $form,
+        ]);
+    }
+}

+ 51 - 0
src/Entity/Page.php

@@ -0,0 +1,51 @@
+<?php
+
+namespace App\Entity;
+
+use App\Repository\PageRepository;
+use Doctrine\DBAL\Types\Types;
+use Doctrine\ORM\Mapping as ORM;
+
+#[ORM\Entity(repositoryClass: PageRepository::class)]
+class Page
+{
+    #[ORM\Id]
+    #[ORM\GeneratedValue]
+    #[ORM\Column]
+    private ?int $id = null;
+
+    #[ORM\Column(length: 255)]
+    private ?string $title = null;
+
+    #[ORM\Column(type: Types::TEXT, nullable: true)]
+    private ?string $content = null;
+
+    public function getId(): ?int
+    {
+        return $this->id;
+    }
+
+    public function getTitle(): ?string
+    {
+        return $this->title;
+    }
+
+    public function setTitle(string $title): static
+    {
+        $this->title = $title;
+
+        return $this;
+    }
+
+    public function getContent(): ?string
+    {
+        return $this->content;
+    }
+
+    public function setContent(?string $content): static
+    {
+        $this->content = $content;
+
+        return $this;
+    }
+}

+ 27 - 0
src/Form/PageType.php

@@ -0,0 +1,27 @@
+<?php
+
+namespace App\Form;
+
+use App\Entity\Page;
+use FOS\CKEditorBundle\Form\Type\CKEditorType;
+use Symfony\Component\Form\AbstractType;
+use Symfony\Component\Form\FormBuilderInterface;
+use Symfony\Component\OptionsResolver\OptionsResolver;
+
+class PageType extends AbstractType
+{
+    public function buildForm(FormBuilderInterface $builder, array $options): void
+    {
+        $builder
+            ->add('title')
+            ->add('content', CKEditorType::class)
+        ;
+    }
+
+    public function configureOptions(OptionsResolver $resolver): void
+    {
+        $resolver->setDefaults([
+            'data_class' => Page::class,
+        ]);
+    }
+}

+ 43 - 0
src/Repository/PageRepository.php

@@ -0,0 +1,43 @@
+<?php
+
+namespace App\Repository;
+
+use App\Entity\Page;
+use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
+use Doctrine\Persistence\ManagerRegistry;
+
+/**
+ * @extends ServiceEntityRepository<Page>
+ */
+class PageRepository extends ServiceEntityRepository
+{
+    public function __construct(ManagerRegistry $registry)
+    {
+        parent::__construct($registry, Page::class);
+    }
+
+    //    /**
+    //     * @return Page[] Returns an array of Page objects
+    //     */
+    //    public function findByExampleField($value): array
+    //    {
+    //        return $this->createQueryBuilder('p')
+    //            ->andWhere('p.exampleField = :val')
+    //            ->setParameter('val', $value)
+    //            ->orderBy('p.id', 'ASC')
+    //            ->setMaxResults(10)
+    //            ->getQuery()
+    //            ->getResult()
+    //        ;
+    //    }
+
+    //    public function findOneBySomeField($value): ?Page
+    //    {
+    //        return $this->createQueryBuilder('p')
+    //            ->andWhere('p.exampleField = :val')
+    //            ->setParameter('val', $value)
+    //            ->getQuery()
+    //            ->getOneOrNullResult()
+    //        ;
+    //    }
+}

+ 1 - 1
templates/_menu.html.twig

@@ -4,7 +4,7 @@
         <div class="nav-links" data-menuburger-target="navlinks">
             <ul>
                 <li><a href="{{ path('app_index') }}">Blog</a></li>
-                <li><a href="#">Contact</a></li>
+                <li><a href="{{ path('app_page_contact') }}">Contact</a></li>
                 {% if is_granted('ROLE_AUTHOR') %}
                 <li>
                     <a href="#">Administation</a>

+ 18 - 0
templates/page/edit.html.twig

@@ -0,0 +1,18 @@
+{% extends 'base.html.twig' %}
+
+{% block title %}Gestion de la page contact{% endblock %}
+
+{% block body %}
+
+<h1>Gestion de la page contacts</h1>
+
+{{ form_start(form) }}
+    {{ form_widget(form) }}
+    <button class="btn btn-green">Sauvegarder</button>
+{{ form_end(form) }}
+
+<a href="{{ path('app_page_contact') }}">Retour à la page Contacts</a>
+
+{{ include('article/_bouton_agrandir.html.twig') }}
+
+{% endblock %}

+ 16 - 0
templates/page/index.html.twig

@@ -0,0 +1,16 @@
+{% extends 'base.html.twig' %}
+
+{% block title %}Page contact{% endblock %}
+
+{% block body %}
+
+{% if is_granted('ROLE_CONTACT') %}
+    <p><a class="btn btn-blue" href="{{ path('app_page_contact_edit') }}">Modifier cette page</a></p>
+{% endif %}
+<h1>{{ page.title }}</h1>
+<div class="contenu">
+    {{ page.content | raw }}
+</div>
+
+
+{% endblock %}