jun 3
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,5 +1,6 @@
|
||||
|
||||
###> symfony/framework-bundle ###
|
||||
/public/uploads
|
||||
/.env.local
|
||||
/.env.local.php
|
||||
/.env.*.local
|
||||
|
||||
1
.idea/guestbook.iml
generated
1
.idea/guestbook.iml
generated
@@ -3,7 +3,6 @@
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" packagePrefix="App\" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" packagePrefix="App\Tests\" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/var" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/blackfire/php-sdk" />
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
# Put parameters here that don't need to change on each machine where the app is deployed
|
||||
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
|
||||
parameters:
|
||||
|
||||
photo_dir: "%kernel.project_dir%/public/uploads/photos"
|
||||
services:
|
||||
# default configuration for services in *this* file
|
||||
_defaults:
|
||||
|
||||
34
migrations/Version20240703092113.php
Normal file
34
migrations/Version20240703092113.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?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 Version20240703092113 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 conference ADD slug VARCHAR(255)');
|
||||
$this->addSql("UPDATE conference SET slug=CONCAT(LOWER(city), '-', year)");
|
||||
$this->addSql('ALTER TABLE conference ALTER COLUMN slug SET NOT NULL');
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
// this down() migration is auto-generated, please modify it to your needs
|
||||
$this->addSql('CREATE SCHEMA public');
|
||||
$this->addSql('ALTER TABLE conference DROP slug');
|
||||
}
|
||||
}
|
||||
32
migrations/Version20240703092601.php
Normal file
32
migrations/Version20240703092601.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?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 Version20240703092601 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 UNIQUE INDEX UNIQ_911533C8989D9B62 ON conference (slug)');
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
// this down() migration is auto-generated, please modify it to your needs
|
||||
$this->addSql('CREATE SCHEMA public');
|
||||
$this->addSql('DROP INDEX UNIQ_911533C8989D9B62');
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
|
||||
@@ -46,7 +47,9 @@ class CommentCrudController extends AbstractCrudController
|
||||
yield TextareaField::new('text')
|
||||
->hideOnIndex()
|
||||
;
|
||||
yield TextField::new('photoFilename')
|
||||
yield ImageField::new('photoFilename')
|
||||
->setBasePath('/uploads/photos')
|
||||
->setLabel('Photo')
|
||||
->onlyOnIndex()
|
||||
;
|
||||
|
||||
@@ -56,9 +59,8 @@ class CommentCrudController extends AbstractCrudController
|
||||
]);
|
||||
if (Crud::PAGE_EDIT === $pageName) {
|
||||
yield $createdAt->setFormTypeOption('disabled', true);
|
||||
} else {
|
||||
yield $createdAt;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,10 +2,15 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
|
||||
use App\Entity\Comment;
|
||||
use App\Entity\Conference;
|
||||
use App\Form\CommentType;
|
||||
use App\Repository\CommentRepository;
|
||||
use App\Repository\ConferenceRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
@@ -13,6 +18,10 @@ use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
class ConferenceController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManagerInterface $entityManager,
|
||||
) {
|
||||
}
|
||||
#[Route('/', name: 'homepage')]
|
||||
public function index(ConferenceRepository $conferenceRepository): Response
|
||||
{
|
||||
@@ -21,17 +30,41 @@ class ConferenceController extends AbstractController
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/conference/{id}', name: 'conference')]
|
||||
public function show(Request $request, Conference $conference, CommentRepository $commentRepository): Response
|
||||
{
|
||||
#[Route('/conference/{slug}', name: 'conference')]
|
||||
public function show(
|
||||
Request $request,
|
||||
Conference $conference,
|
||||
CommentRepository $commentRepository,
|
||||
ConferenceRepository $conferenceRepository,
|
||||
#[Autowire('%photo_dir%')] string $photoDir,
|
||||
): Response {
|
||||
$comment = new Comment();
|
||||
$form = $this->createForm(CommentType::class, $comment);
|
||||
|
||||
$form->handleRequest($request);
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$comment->setConference($conference);
|
||||
if ($photo = $form['photo']->getData()) {
|
||||
$filename = bin2hex(random_bytes(6)).'.'.$photo->guessExtension();
|
||||
$photo->move($photoDir, $filename);
|
||||
$comment->setPhotoFilename($filename);
|
||||
}
|
||||
$this->entityManager->persist($comment);
|
||||
$this->entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('conference', ['slug' => $conference->getSlug()]);
|
||||
}
|
||||
|
||||
$offset = max(0, $request->query->getInt('offset', 0));
|
||||
$paginator = $commentRepository->getCommentPaginator($conference, $offset);
|
||||
|
||||
return $this->render('conference/show.html.twig', [
|
||||
'conferences' => $conferenceRepository->findAll(),
|
||||
'conference' => $conference,
|
||||
'comments' => $paginator,
|
||||
'previous' => $offset - CommentRepository::COMMENTS_PER_PAGE,
|
||||
'next' => min(count($paginator), $offset + CommentRepository::COMMENTS_PER_PAGE),
|
||||
'comment_form' => $form,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,10 @@ namespace App\Entity;
|
||||
use App\Repository\CommentRepository;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
#[ORM\Entity(repositoryClass: CommentRepository::class)]
|
||||
#[ORM\HasLifecycleCallbacks]
|
||||
class Comment
|
||||
{
|
||||
#[ORM\Id]
|
||||
@@ -15,12 +17,16 @@ class Comment
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(length: 255)]
|
||||
#[Assert\NotBlank]
|
||||
private ?string $author = null;
|
||||
|
||||
#[ORM\Column(type: Types::TEXT)]
|
||||
#[Assert\NotBlank]
|
||||
private ?string $text = null;
|
||||
|
||||
#[ORM\Column(length: 255)]
|
||||
#[Assert\NotBlank]
|
||||
#[Assert\Email]
|
||||
private ?string $email = null;
|
||||
|
||||
#[ORM\Column]
|
||||
@@ -86,6 +92,11 @@ class Comment
|
||||
return $this;
|
||||
}
|
||||
|
||||
#[ORM\PrePersist]
|
||||
public function setCreatedAtValue()
|
||||
{
|
||||
$this->createdAt = new \DateTimeImmutable();
|
||||
}
|
||||
public function getConference(): ?Conference
|
||||
{
|
||||
return $this->conference;
|
||||
|
||||
@@ -6,8 +6,11 @@ use App\Repository\ConferenceRepository;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
||||
use Symfony\Component\String\Slugger\SluggerInterface;
|
||||
|
||||
#[ORM\Entity(repositoryClass: ConferenceRepository::class)]
|
||||
#[UniqueEntity('slug')]
|
||||
class Conference
|
||||
{
|
||||
#[ORM\Id]
|
||||
@@ -30,6 +33,9 @@ class Conference
|
||||
#[ORM\OneToMany(targetEntity: Comment::class, mappedBy: 'conference', orphanRemoval: true)]
|
||||
private Collection $comments;
|
||||
|
||||
#[ORM\Column(length: 255, unique: true)]
|
||||
private ?string $slug = null;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->comments = new ArrayCollection();
|
||||
@@ -44,6 +50,13 @@ class Conference
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function computeSlug(SluggerInterface $slugger)
|
||||
{
|
||||
if (!$this->slug || '-' === $this->slug) {
|
||||
$this->slug = (string) $slugger->slug((string) $this)->lower();
|
||||
}
|
||||
}
|
||||
|
||||
public function getCity(): ?string
|
||||
{
|
||||
return $this->city;
|
||||
@@ -109,4 +122,16 @@ class Conference
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getSlug(): ?string
|
||||
{
|
||||
return $this->slug;
|
||||
}
|
||||
|
||||
public function setSlug(string $slug): static
|
||||
{
|
||||
$this->slug = $slug;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
29
src/EntityListener/ConferenceEntityListener.php
Normal file
29
src/EntityListener/ConferenceEntityListener.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\EntityListener;
|
||||
|
||||
use App\Entity\Conference;
|
||||
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
|
||||
use Doctrine\ORM\Events;
|
||||
use Doctrine\Persistence\Event\LifecycleEventArgs;
|
||||
use Symfony\Component\String\Slugger\SluggerInterface;
|
||||
|
||||
#[AsEntityListener(event: Events::prePersist, entity: Conference::class)]
|
||||
#[AsEntityListener(event: Events::preUpdate, entity: Conference::class)]
|
||||
class ConferenceEntityListener
|
||||
{
|
||||
public function __construct(
|
||||
private SluggerInterface $slugger,
|
||||
) {
|
||||
}
|
||||
|
||||
public function prePersist(Conference $conference, LifecycleEventArgs $event)
|
||||
{
|
||||
$conference->computeSlug($this->slugger);
|
||||
}
|
||||
|
||||
public function preUpdate(Conference $conference, LifecycleEventArgs $event)
|
||||
{
|
||||
$conference->computeSlug($this->slugger);
|
||||
}
|
||||
}
|
||||
33
src/EventSubscriber/TwigEventSubscriber.php
Normal file
33
src/EventSubscriber/TwigEventSubscriber.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\EventSubscriber;
|
||||
|
||||
use App\Repository\ConferenceRepository;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\HttpKernel\Event\ControllerEvent;
|
||||
use Twig\Environment;
|
||||
|
||||
class TwigEventSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
|
||||
private $twig;
|
||||
private $conferenceRepository;
|
||||
|
||||
public function __construct(Environment $twig, ConferenceRepository $conferenceRepository)
|
||||
{
|
||||
$this->twig = $twig;
|
||||
$this->conferenceRepository = $conferenceRepository;
|
||||
}
|
||||
|
||||
public function onControllerEvent(ControllerEvent $event): void
|
||||
{
|
||||
$this->twig->addGlobal('conferences', $this->conferenceRepository->findAll());
|
||||
}
|
||||
|
||||
public static function getSubscribedEvents(): array
|
||||
{
|
||||
return [
|
||||
ControllerEvent::class => 'onControllerEvent',
|
||||
];
|
||||
}
|
||||
}
|
||||
43
src/Form/CommentType.php
Normal file
43
src/Form/CommentType.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
use App\Entity\Comment;
|
||||
use App\Entity\Conference;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\EmailType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FileType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Validator\Constraints\Image;
|
||||
|
||||
class CommentType extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder
|
||||
->add('author', null, [
|
||||
'label' => 'Your name',
|
||||
])
|
||||
->add('text')
|
||||
->add('email', EmailType::class)
|
||||
->add('photo', FileType::class, [
|
||||
'required' => false,
|
||||
'mapped' => false,
|
||||
'constraints' => [
|
||||
new Image(['maxSize' => '1024k'])
|
||||
],
|
||||
])
|
||||
->add('submit', SubmitType::class)
|
||||
;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => Comment::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,11 @@ class ConferenceRepository extends ServiceEntityRepository
|
||||
parent::__construct($registry, Conference::class);
|
||||
}
|
||||
|
||||
public function findAll(): array
|
||||
{
|
||||
return $this->findBy([], ['year' => 'ASC', 'city' => 'ASC']);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @return Conference[] Returns an array of Conference objects
|
||||
// */
|
||||
|
||||
@@ -12,6 +12,15 @@
|
||||
{% endblock %}
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1><a href="{{ path('homepage') }}">Guestbook</a></h1>
|
||||
<ul>
|
||||
{% for conference in conferences %}
|
||||
<li><a href="{{ path('conference', { slug: conference.slug }) }}">{{ conference }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<hr />
|
||||
</header>
|
||||
{% block body %}{% endblock %}
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
{% for conference in conferences %}
|
||||
<h4>{{ conference }}</h4>
|
||||
<p>
|
||||
<a href="{{ path('conference', { id: conference.id }) }}">View</a>
|
||||
<a href="{{ path('conference', { slug: conference.slug }) }}">View</a>
|
||||
</p>
|
||||
{% endfor %}
|
||||
{% endblock %}
|
||||
|
||||
@@ -20,12 +20,15 @@
|
||||
<p>{{ comment.text }}</p>
|
||||
{% endfor %}
|
||||
{% if previous >= 0 %}
|
||||
<a href="{{ path('conference', { id: conference.id, offset: previous }) }}">Previous</a>
|
||||
<a href="{{ path('conference', { slug: conference.slug, offset: previous }) }}">Previous</a>
|
||||
{% endif %}
|
||||
{% if next < comments|length %}
|
||||
<a href="{{ path('conference', { id: conference.id, offset: next }) }}">Next</a>
|
||||
<a href="{{ path('conference', { slug: conference.slug, offset: next }) }}">Next</a>
|
||||
{% endif %}
|
||||
{% else %}
|
||||
<div>No comments have been posted yet for this conference.</div>
|
||||
{% endif %}
|
||||
<h2>Add your own feedback</h2>
|
||||
|
||||
{{ form(comment_form) }}
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user