diff --git a/config/services.yaml b/config/services.yaml index 6ce490d..b5ee599 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -57,3 +57,7 @@ services: App\Admin\: resource: '../src/Admin' tags: ['sulu.admin', {name: 'sulu.context', context: 'admin'}] + + App\Repository\: + resource: '../src/Repository' + public: true diff --git a/config/templates/pages/event_overview.xml b/config/templates/pages/event_overview.xml index e386244..3f576fa 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\EventOverviewController:indexAction 86400 @@ -42,16 +42,5 @@ Artikel - - - - Events - Veranstaltungen - - - - - - diff --git a/src/Controller/EventOverviewController.php b/src/Controller/EventOverviewController.php new file mode 100644 index 0000000..af7690f --- /dev/null +++ b/src/Controller/EventOverviewController.php @@ -0,0 +1,61 @@ +get(EventRepository::class); + /** @var LocationRepository $locationRepository */ + $locationRepository = $this->get(LocationRepository::class); + + $locationId = $request->query->get('location'); + if ('' === $locationId) { + $locationId = null; + } + + $response = $this->renderStructure( + $structure, + [ + 'events' => $eventRepository->filterByLocationId( + (int) $locationId, + $request->getLocale() + ), + 'locations' => $locationRepository->findAll(), + ], + $preview, + $partial + ); + + return $response; + } + + /** + * @return mixed[] + */ + 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 04902d9..73c7f04 100644 --- a/src/DataFixtures/Document/DocumentFixture.php +++ b/src/DataFixtures/Document/DocumentFixture.php @@ -55,6 +55,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 90a473c..3b1bdf9 100644 --- a/src/Repository/EventRepository.php +++ b/src/Repository/EventRepository.php @@ -69,6 +69,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 d3a687b..f699e67 100644 --- a/templates/pages/event_overview.html.twig +++ b/templates/pages/event_overview.html.twig @@ -10,7 +10,28 @@
- {% for event in content.events %} +
+
+ + +
+ + +
+
+
+ +
+
+ {% for event in events %}

{{ event.title }}

{{ event.teaser }}

diff --git a/tests/Functional/Pages/EventOverviewTest.php b/tests/Functional/Pages/EventOverviewTest.php index fbaab12..01636de 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; /** @@ -26,6 +28,7 @@ protected function setUp(): void { $this->client = $this->createWebsiteClient(); $this->initPhpcr(); + $this->purgeDatabase(); } public function testEventOverview(): void @@ -58,6 +61,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->assertSame(Response::HTTP_OK, $response->getStatusCode()); + $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->assertSame(Response::HTTP_OK, $response->getStatusCode()); + $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');