From 7fafc0550176afa6e3c2dd891952614725b7b3a5 Mon Sep 17 00:00:00 2001 From: Johannes Wachter Date: Thu, 3 Oct 2019 08:45:23 +0200 Subject: [PATCH] 12 - Add a location filter to the events overview page --- config/templates/pages/event_overview.xml | 13 +---- .../Website/EventOverviewController.php | 44 +++++++++++++++ src/DataFixtures/Document/DocumentFixture.php | 6 +++ src/Repository/EventRepository.php | 18 +++++++ templates/pages/event_overview.html.twig | 27 ++++++++-- tests/Functional/Pages/EventOverviewTest.php | 53 +++++++++++++++++++ 6 files changed, 146 insertions(+), 15 deletions(-) create mode 100644 src/Controller/Website/EventOverviewController.php diff --git a/config/templates/pages/event_overview.xml b/config/templates/pages/event_overview.xml index e386244..212f740 100644 --- a/config/templates/pages/event_overview.xml +++ b/config/templates/pages/event_overview.xml @@ -6,7 +6,7 @@ event_overview pages/event_overview - Sulu\Bundle\WebsiteBundle\Controller\DefaultController::indexAction + App\Controller\Website\EventOverviewController::indexAction 86400 @@ -42,16 +42,5 @@ Artikel - - - - Events - Veranstaltungen - - - - - - diff --git a/src/Controller/Website/EventOverviewController.php b/src/Controller/Website/EventOverviewController.php new file mode 100644 index 0000000..4eb4ade --- /dev/null +++ b/src/Controller/Website/EventOverviewController.php @@ -0,0 +1,44 @@ +container->get(EventRepository::class); + /** @var LocationRepository $locationRepository */ + $locationRepository = $this->container->get(LocationRepository::class); + + $request = $this->getRequest(); + $locationId = $request->query->get('location'); + + $attributes = parent::getAttributes($attributes, $structure, $preview); + $attributes['events'] = $eventRepository->filterByLocationId( + $locationId ? (int) $locationId : null, + $request->getLocale(), + ); + $attributes['locations'] = $locationRepository->findAll(); + + return $attributes; + } + + public static function getSubscribedServices(): array + { + return \array_merge( + parent::getSubscribedServices(), + [ + EventRepository::class, + LocationRepository::class, + ], + ); + } +} diff --git a/src/DataFixtures/Document/DocumentFixture.php b/src/DataFixtures/Document/DocumentFixture.php index 249aa8e..a1f4f27 100644 --- a/src/DataFixtures/Document/DocumentFixture.php +++ b/src/DataFixtures/Document/DocumentFixture.php @@ -45,6 +45,12 @@ private function loadPages(DocumentManager $documentManager): void 'structureType' => 'default', 'article' => '

This is a very good imprint :)

', ], + [ + 'title' => 'Events', + 'navigationContexts' => ['main'], + 'structureType' => 'event_overview', + 'article' => '', + ], ]; $pages = []; diff --git a/src/Repository/EventRepository.php b/src/Repository/EventRepository.php index 0f71b13..b6807b6 100644 --- a/src/Repository/EventRepository.php +++ b/src/Repository/EventRepository.php @@ -64,6 +64,24 @@ public function findById(int $id, string $locale): ?Event return $event; } + /** + * @return Event[] + */ + public function filterByLocationId(?int $locationId, string $locale): array + { + $criteria = ['enabled' => true]; + if ($locationId) { + $criteria['location'] = $locationId; + } + + $events = $this->findBy($criteria); + foreach ($events as $event) { + $event->setLocale($locale); + } + + return $events; + } + /** * @param mixed[] $filters */ diff --git a/templates/pages/event_overview.html.twig b/templates/pages/event_overview.html.twig index 7ef4d3c..f699e67 100644 --- a/templates/pages/event_overview.html.twig +++ b/templates/pages/event_overview.html.twig @@ -10,10 +10,31 @@
- {% for event in content.events %} +
+
+ + +
+ + +
+
+
+ +
+
+ {% for event in events %}
-

{{ event.resource.title }}

-

{{ event.resource.teaser }}

+

{{ event.title }}

+

{{ event.teaser }}

View details ยป diff --git a/tests/Functional/Pages/EventOverviewTest.php b/tests/Functional/Pages/EventOverviewTest.php index c56e157..ebb768b 100644 --- a/tests/Functional/Pages/EventOverviewTest.php +++ b/tests/Functional/Pages/EventOverviewTest.php @@ -5,6 +5,7 @@ namespace App\Tests\Functional\Pages; use App\Tests\Functional\Traits\EventTrait; +use App\Tests\Functional\Traits\LocationTrait; use App\Tests\Functional\Traits\PageTrait; use Sulu\Bundle\TestBundle\Testing\SuluTestCase; use Sulu\Component\DocumentManager\DocumentManagerInterface; @@ -15,6 +16,7 @@ class EventOverviewTest extends SuluTestCase { use EventTrait; + use LocationTrait; use PageTrait; private KernelBrowser $client; @@ -23,6 +25,7 @@ protected function setUp(): void { $this->client = $this->createWebsiteClient(); $this->initPhpcr(); + $this->purgeDatabase(); } public function testEventOverview(): void @@ -55,6 +58,56 @@ public function testEventOverview(): void $this->assertStringContainsString($event2->getTitle() ?: '', $content); } + public function testEventOverviewWithLocations(): void + { + $location1 = $this->createLocation('Dornbirn'); + $location2 = $this->createLocation('Berlin'); + + $event1 = $this->createEvent('Sulu is awesome', 'en'); + $event1->setLocation($location1); + $this->enableEvent($event1); + $event2 = $this->createEvent('Symfony Live is awesome', 'en'); + $event2->setLocation($location2); + $this->enableEvent($event2); + $event3 = $this->createEvent('Disabled', 'en'); + + $this->createPage( + 'event_overview', + 'example', + [ + 'title' => 'Symfony Live', + 'url' => '/events', + 'published' => true, + ], + ); + + $crawler = $this->client->request(Request::METHOD_GET, '/en/events'); + + $response = $this->client->getResponse(); + $this->assertInstanceOf(Response::class, $response); + $this->assertResponseIsSuccessful(); + $this->assertCount(2, $crawler->filter('.event-title')); + $this->assertNotNull($content = $crawler->filter('.event-title')->eq(0)->html()); + $this->assertStringContainsString($event1->getTitle() ?: '', $content); + $this->assertNotNull($content = $crawler->filter('.event-title')->eq(1)->html()); + $this->assertStringContainsString($event2->getTitle() ?: '', $content); + + $form = $crawler->filter('#location_submit')->form( + [ + 'location' => $location1->getId(), + ], + ); + + $crawler = $this->client->submit($form); + + $response = $this->client->getResponse(); + $this->assertInstanceOf(Response::class, $response); + $this->assertResponseIsSuccessful(); + $this->assertCount(1, $crawler->filter('.event-title')); + $this->assertNotNull($content = $crawler->filter('.event-title')->eq(0)->html()); + $this->assertStringContainsString($event1->getTitle() ?: '', $content); + } + protected static function getDocumentManager(): DocumentManagerInterface { return static::getContainer()->get('sulu_document_manager.document_manager');