Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

12 - Add a location filter to the events overview page #17

Open
wants to merge 1 commit into
base: assignment/11
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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\Website\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>
44 changes: 44 additions & 0 deletions src/Controller/Website/EventOverviewController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace App\Controller\Website;

use App\Repository\EventRepository;
use App\Repository\LocationRepository;
use Sulu\Bundle\WebsiteBundle\Controller\DefaultController;
use Sulu\Component\Content\Compat\StructureInterface;

class EventOverviewController extends DefaultController
{
protected function getAttributes($attributes, StructureInterface $structure = null, $preview = false)
{
/** @var EventRepository $eventRepository */
$eventRepository = $this->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,
],
);
}
}
6 changes: 6 additions & 0 deletions src/DataFixtures/Document/DocumentFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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 @@ -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
*/
Expand Down
27 changes: 24 additions & 3 deletions templates/pages/event_overview.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,31 @@

<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.resource.title }}</h2>
<p>{{ event.resource.teaser }}</p>
<h2 class="event-title">{{ event.title }}</h2>
<p>{{ event.teaser }}</p>
<p>
<a class="btn btn-secondary" href="{{ path('app.event', {id: event.id}) }}" role="button">
View details »
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;

private KernelBrowser $client;
Expand All @@ -23,6 +25,7 @@ protected function setUp(): void
{
$this->client = $this->createWebsiteClient();
$this->initPhpcr();
$this->purgeDatabase();
}

public function testEventOverview(): void
Expand Down Expand Up @@ -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');
Expand Down