Skip to content

Commit

Permalink
12 - Add a location filter to the events overview page
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes authored and niklasnatter committed Feb 16, 2021
1 parent e70114a commit 5ff6cdf
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 13 deletions.
4 changes: 4 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 1 addition & 12 deletions config/templates/pages/event_overview.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<key>event_overview</key>

<view>pages/event_overview</view>
<controller>Sulu\Bundle\WebsiteBundle\Controller\DefaultController::indexAction</controller>
<controller>App\Controller\EventOverviewController:indexAction</controller>
<cacheLifetime>86400</cacheLifetime>

<meta>
Expand Down Expand Up @@ -42,16 +42,5 @@
<title lang="de">Artikel</title>
</meta>
</property>

<property name="events" type="smart_content">
<meta>
<title lang="en">Events</title>
<title lang="de">Veranstaltungen</title>
</meta>

<params>
<param name="provider" value="events"/>
</params>
</property>
</properties>
</template>
61 changes: 61 additions & 0 deletions src/Controller/EventOverviewController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace App\Controller;

use App\Repository\EventRepository;
use App\Repository\LocationRepository;
use Sulu\Bundle\WebsiteBundle\Controller\WebsiteController;
use Sulu\Component\Content\Compat\StructureInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class EventOverviewController extends WebsiteController
{
public function indexAction(
Request $request,
StructureInterface $structure,
bool $preview = false,
bool $partial = false
): Response {
/** @var EventRepository $eventRepository */
$eventRepository = $this->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,
]
);
}
}
6 changes: 6 additions & 0 deletions src/DataFixtures/Document/DocumentFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ private function loadPages(DocumentManager $documentManager): void
'structureType' => 'default',
'article' => '<p>This is a very good imprint :)</p>',
],
[
'title' => 'Events',
'navigationContexts' => ['main'],
'structureType' => 'event_overview',
'article' => '',
],
];

$pages = [];
Expand Down
18 changes: 18 additions & 0 deletions src/Repository/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
23 changes: 22 additions & 1 deletion templates/pages/event_overview.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,28 @@

<div class="container marketing">
<div class="row">
{% for event in content.events %}
<form action="{{ sulu_content_path(content.url) }}" method="get" class="col-3">
<div class="form-group">
<label for="location">Location</label>
<select id="location" name="location" class="form-control">
<option value>All ...</option>
{% for location in locations %}
<option value="{{ location.id }}"
{% if app.request.get('location') == location.id %}selected{% endif %}>
{{ location.name }}
</option>
{% endfor %}
</select>
</div>

<button type="submit" id="location_submit" class="btn btn-primary">Filter</button>
</form>
</div>
</div>

<div class="container marketing mt-5">
<div class="row">
{% for event in events %}
<div class="col-lg-4 text-center">
<h2 class="event-title">{{ event.title }}</h2>
<p>{{ event.teaser }}</p>
Expand Down
53 changes: 53 additions & 0 deletions tests/Functional/Pages/EventOverviewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,6 +16,7 @@
class EventOverviewTest extends SuluTestCase
{
use EventTrait;
use LocationTrait;
use PageTrait;

/**
Expand All @@ -26,6 +28,7 @@ public function setUp(): void
{
$this->client = $this->createWebsiteClient();
$this->initPhpcr();
$this->purgeDatabase();
}

public function testEventOverview(): void
Expand Down Expand Up @@ -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 function getDocumentManager(): DocumentManagerInterface
{
return $this->getContainer()->get('sulu_document_manager.document_manager');
Expand Down

0 comments on commit 5ff6cdf

Please sign in to comment.