Skip to content

Commit e3ef375

Browse files
wachterjohannesniklasnatter
authored andcommitted
12 - Add a location filter to the events overview page
1 parent 0796a36 commit e3ef375

File tree

7 files changed

+165
-13
lines changed

7 files changed

+165
-13
lines changed

config/services.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ services:
5757
App\Admin\:
5858
resource: '../src/Admin'
5959
tags: ['sulu.admin', {name: 'sulu.context', context: 'admin'}]
60+
61+
App\Repository\:
62+
resource: '../src/Repository'
63+
public: true

config/templates/pages/event_overview.xml

+1-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<key>event_overview</key>
77

88
<view>pages/event_overview</view>
9-
<controller>Sulu\Bundle\WebsiteBundle\Controller\DefaultController::indexAction</controller>
9+
<controller>App\Controller\EventOverviewController:indexAction</controller>
1010
<cacheLifetime>86400</cacheLifetime>
1111

1212
<meta>
@@ -42,16 +42,5 @@
4242
<title lang="de">Artikel</title>
4343
</meta>
4444
</property>
45-
46-
<property name="events" type="smart_content">
47-
<meta>
48-
<title lang="en">Events</title>
49-
<title lang="de">Veranstaltungen</title>
50-
</meta>
51-
52-
<params>
53-
<param name="provider" value="events"/>
54-
</params>
55-
</property>
5645
</properties>
5746
</template>
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Controller;
6+
7+
use App\Repository\EventRepository;
8+
use App\Repository\LocationRepository;
9+
use Sulu\Bundle\WebsiteBundle\Controller\WebsiteController;
10+
use Sulu\Component\Content\Compat\StructureInterface;
11+
use Symfony\Component\HttpFoundation\Request;
12+
use Symfony\Component\HttpFoundation\Response;
13+
14+
class EventOverviewController extends WebsiteController
15+
{
16+
public function indexAction(
17+
Request $request,
18+
StructureInterface $structure,
19+
bool $preview = false,
20+
bool $partial = false
21+
): Response {
22+
/** @var EventRepository $eventRepository */
23+
$eventRepository = $this->get(EventRepository::class);
24+
/** @var LocationRepository $locationRepository */
25+
$locationRepository = $this->get(LocationRepository::class);
26+
27+
$locationId = $request->query->get('location');
28+
if ('' === $locationId) {
29+
$locationId = null;
30+
}
31+
32+
$response = $this->renderStructure(
33+
$structure,
34+
[
35+
'events' => $eventRepository->filterByLocationId(
36+
(int) $locationId,
37+
$request->getLocale()
38+
),
39+
'locations' => $locationRepository->findAll(),
40+
],
41+
$preview,
42+
$partial
43+
);
44+
45+
return $response;
46+
}
47+
48+
/**
49+
* @return mixed[]
50+
*/
51+
public static function getSubscribedServices(): array
52+
{
53+
return array_merge(
54+
parent::getSubscribedServices(),
55+
[
56+
EventRepository::class,
57+
LocationRepository::class,
58+
]
59+
);
60+
}
61+
}

src/DataFixtures/Document/DocumentFixture.php

+6
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ private function loadPages(DocumentManager $documentManager): void
5555
'structureType' => 'default',
5656
'article' => '<p>This is a very good imprint :)</p>',
5757
],
58+
[
59+
'title' => 'Events',
60+
'navigationContexts' => ['main'],
61+
'structureType' => 'event_overview',
62+
'article' => '',
63+
],
5864
];
5965

6066
$pages = [];

src/Repository/EventRepository.php

+18
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@ public function findById(int $id, string $locale): ?Event
6969
return $event;
7070
}
7171

72+
/**
73+
* @return Event[]
74+
*/
75+
public function filterByLocationId(?int $locationId, string $locale): array
76+
{
77+
$criteria = ['enabled' => true];
78+
if ($locationId) {
79+
$criteria['location'] = $locationId;
80+
}
81+
82+
$events = $this->findBy($criteria);
83+
foreach ($events as $event) {
84+
$event->setLocale($locale);
85+
}
86+
87+
return $events;
88+
}
89+
7290
/**
7391
* @param mixed[] $filters
7492
*/

templates/pages/event_overview.html.twig

+22-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,28 @@
1010

1111
<div class="container marketing">
1212
<div class="row">
13-
{% for event in content.events %}
13+
<form action="{{ sulu_content_path(content.url) }}" method="get" class="col-3">
14+
<div class="form-group">
15+
<label for="location">Location</label>
16+
<select id="location" name="location" class="form-control">
17+
<option value>All ...</option>
18+
{% for location in locations %}
19+
<option value="{{ location.id }}"
20+
{% if app.request.get('location') == location.id %}selected{% endif %}>
21+
{{ location.name }}
22+
</option>
23+
{% endfor %}
24+
</select>
25+
</div>
26+
27+
<button type="submit" id="location_submit" class="btn btn-primary">Filter</button>
28+
</form>
29+
</div>
30+
</div>
31+
32+
<div class="container marketing mt-5">
33+
<div class="row">
34+
{% for event in events %}
1435
<div class="col-lg-4 text-center">
1536
<h2 class="event-title">{{ event.title }}</h2>
1637
<p>{{ event.teaser }}</p>

tests/Functional/Pages/EventOverviewTest.php

+53
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\Tests\Functional\Pages;
66

77
use App\Tests\Functional\Traits\EventTrait;
8+
use App\Tests\Functional\Traits\LocationTrait;
89
use App\Tests\Functional\Traits\PageTrait;
910
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;
1011
use Sulu\Component\DocumentManager\DocumentManagerInterface;
@@ -15,6 +16,7 @@
1516
class EventOverviewTest extends SuluTestCase
1617
{
1718
use EventTrait;
19+
use LocationTrait;
1820
use PageTrait;
1921

2022
/**
@@ -26,6 +28,7 @@ public function setUp(): void
2628
{
2729
$this->client = $this->createWebsiteClient();
2830
$this->initPhpcr();
31+
$this->purgeDatabase();
2932
}
3033

3134
public function testEventOverview(): void
@@ -58,6 +61,56 @@ public function testEventOverview(): void
5861
$this->assertStringContainsString($event2->getTitle() ?: '', $content);
5962
}
6063

64+
public function testEventOverviewWithLocations(): void
65+
{
66+
$location1 = $this->createLocation('Dornbirn');
67+
$location2 = $this->createLocation('Berlin');
68+
69+
$event1 = $this->createEvent('Sulu is awesome', 'en');
70+
$event1->setLocation($location1);
71+
$this->enableEvent($event1);
72+
$event2 = $this->createEvent('Symfony Live is awesome', 'en');
73+
$event2->setLocation($location2);
74+
$this->enableEvent($event2);
75+
$event3 = $this->createEvent('Disabled', 'en');
76+
77+
$this->createPage(
78+
'event_overview',
79+
'example',
80+
[
81+
'title' => 'Symfony Live',
82+
'url' => '/events',
83+
'published' => true,
84+
]
85+
);
86+
87+
$crawler = $this->client->request(Request::METHOD_GET, '/en/events');
88+
89+
$response = $this->client->getResponse();
90+
$this->assertInstanceOf(Response::class, $response);
91+
$this->assertSame(Response::HTTP_OK, $response->getStatusCode());
92+
$this->assertCount(2, $crawler->filter('.event-title'));
93+
$this->assertNotNull($content = $crawler->filter('.event-title')->eq(0)->html());
94+
$this->assertStringContainsString($event1->getTitle() ?: '', $content);
95+
$this->assertNotNull($content = $crawler->filter('.event-title')->eq(1)->html());
96+
$this->assertStringContainsString($event2->getTitle() ?: '', $content);
97+
98+
$form = $crawler->filter('#location_submit')->form(
99+
[
100+
'location' => $location1->getId(),
101+
]
102+
);
103+
104+
$crawler = $this->client->submit($form);
105+
106+
$response = $this->client->getResponse();
107+
$this->assertInstanceOf(Response::class, $response);
108+
$this->assertSame(Response::HTTP_OK, $response->getStatusCode());
109+
$this->assertCount(1, $crawler->filter('.event-title'));
110+
$this->assertNotNull($content = $crawler->filter('.event-title')->eq(0)->html());
111+
$this->assertStringContainsString($event1->getTitle() ?: '', $content);
112+
}
113+
61114
protected function getDocumentManager(): DocumentManagerInterface
62115
{
63116
return $this->getContainer()->get('sulu_document_manager.document_manager');

0 commit comments

Comments
 (0)