Skip to content

Commit ea71bcd

Browse files
wachterjohannesniklasnatter
authored andcommitted
11 - Add a relation between events and locations
1 parent e218ec8 commit ea71bcd

File tree

9 files changed

+97
-25
lines changed

9 files changed

+97
-25
lines changed

config/forms/event_details.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
</params>
2525
</property>
2626

27-
<property name="location" type="text_line" colspan="6">
27+
<property name="locationId" type="single_location_selection" mandatory="true" colspan="6">
2828
<meta>
2929
<title>app.location</title>
3030
</meta>

config/packages/sulu_admin.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,17 @@ sulu_admin:
3838
icon: fa-calendar
3939
label: 'app.events'
4040
overlay_title: 'app.events'
41+
42+
single_selection:
43+
single_location_selection:
44+
default_type: list_overlay
45+
resource_key: locations
46+
types:
47+
list_overlay:
48+
adapter: table
49+
list_key: locations
50+
display_properties:
51+
- name
52+
icon: fa-home
53+
empty_text: 'app.location.no_selections'
54+
overlay_title: 'app.locations'

src/Controller/Admin/EventController.php

+16-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Common\DoctrineListRepresentationFactory;
88
use App\Entity\Event;
99
use App\Repository\EventRepository;
10+
use App\Repository\LocationRepository;
1011
use FOS\RestBundle\Controller\Annotations as Rest;
1112
use FOS\RestBundle\Controller\Annotations\RouteResource;
1213
use FOS\RestBundle\Routing\ClassResourceInterface;
@@ -23,6 +24,11 @@
2324
*/
2425
class EventController extends AbstractRestController implements ClassResourceInterface
2526
{
27+
/**
28+
* @var DoctrineListRepresentationFactory
29+
*/
30+
private $doctrineListRepresentationFactory;
31+
2632
/**
2733
* @var EventRepository
2834
*/
@@ -34,20 +40,22 @@ class EventController extends AbstractRestController implements ClassResourceInt
3440
private $mediaRepository;
3541

3642
/**
37-
* @var DoctrineListRepresentationFactory
43+
* @var LocationRepository
3844
*/
39-
private $doctrineListRepresentationFactory;
45+
private $locationRepository;
4046

4147
public function __construct(
48+
DoctrineListRepresentationFactory $doctrineListRepresentationFactory,
4249
EventRepository $repository,
4350
MediaRepositoryInterface $mediaRepository,
44-
DoctrineListRepresentationFactory $doctrineListRepresentationFactory,
51+
LocationRepository $locationRepository,
4552
ViewHandlerInterface $viewHandler,
4653
?TokenStorageInterface $tokenStorage = null
4754
) {
55+
$this->doctrineListRepresentationFactory = $doctrineListRepresentationFactory;
4856
$this->repository = $repository;
4957
$this->mediaRepository = $mediaRepository;
50-
$this->doctrineListRepresentationFactory = $doctrineListRepresentationFactory;
58+
$this->locationRepository = $locationRepository;
5159

5260
parent::__construct($viewHandler, $tokenStorage);
5361
}
@@ -159,8 +167,10 @@ protected function mapDataToEntity(array $data, Event $entity): void
159167
$entity->setEndDate(new \DateTimeImmutable($endDate));
160168
}
161169

162-
if ($location = $data['location'] ?? null) {
163-
$entity->setLocation($location);
170+
if ($locationId = $data['locationId'] ?? null) {
171+
$entity->setLocation(
172+
$this->locationRepository->findById((int) $locationId)
173+
);
164174
}
165175
}
166176

src/DataFixtures/ORM/AppFixtures.php

+18-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace App\DataFixtures\ORM;
66

77
use App\Entity\Event;
8+
use App\Entity\Location;
89
use Doctrine\Bundle\FixturesBundle\Fixture;
910
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
1011
use Doctrine\Persistence\ObjectManager;
@@ -56,7 +57,8 @@ public function load(ObjectManager $manager): void
5657
*/
5758
private function loadEvents(ObjectManager $manager, array $images): void
5859
{
59-
$repository = $manager->getRepository(Event::class);
60+
$eventRepository = $manager->getRepository(Event::class);
61+
$locationRepository = $manager->getRepository(Location::class);
6062

6163
$data = [
6264
[
@@ -132,18 +134,30 @@ private function loadEvents(ObjectManager $manager, array $images): void
132134
];
133135

134136
foreach ($data as $item) {
135-
$event = $repository->create(self::LOCALE);
137+
$location = null;
138+
if ($item['location']) {
139+
$location = $locationRepository->create();
140+
$location->setName($item['location']);
141+
$location->setStreet('');
142+
$location->setNumber('');
143+
$location->setCity('');
144+
$location->setCountryCode('');
145+
$location->setPostalCode('');
146+
$locationRepository->save($location);
147+
}
148+
149+
$event = $eventRepository->create(self::LOCALE);
136150

137151
$event->setTitle($item['title']);
138152
$event->setImage($images[$item['image']] ?? null);
139-
$event->setLocation($item['location']);
153+
$event->setLocation($location);
140154
$event->setTeaser($item['teaser']);
141155
$event->setDescription('<p>' . $item['description'] . '</p>');
142156
$event->setStartDate(new \DateTimeImmutable($item['startDate']));
143157
$event->setEndDate(new \DateTimeImmutable($item['endDate']));
144158
$event->setEnabled($item['enabled']);
145159

146-
$repository->save($event);
160+
$eventRepository->save($event);
147161
}
148162
}
149163

src/Entity/Event.php

+17-4
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ class Event
4848
private $endDate;
4949

5050
/**
51-
* @var string|null
51+
* @var Location|null
5252
*
53-
* @ORM\Column(type="string", nullable=true)
53+
* @ORM\ManyToOne(targetEntity="App\Entity\Location")
54+
* @ORM\JoinColumn(onDelete="SET NULL")
5455
*/
5556
private $location;
5657

@@ -124,18 +125,30 @@ public function setEndDate(?\DateTimeImmutable $endDate): self
124125
return $this;
125126
}
126127

127-
public function getLocation(): ?string
128+
public function getLocation(): ?Location
128129
{
129130
return $this->location;
130131
}
131132

132-
public function setLocation(?string $location): self
133+
public function setLocation(?Location $location): self
133134
{
134135
$this->location = $location;
135136

136137
return $this;
137138
}
138139

140+
/**
141+
* @Serializer\VirtualProperty
142+
*/
143+
public function getLocationId(): ?int
144+
{
145+
if (!$this->location) {
146+
return null;
147+
}
148+
149+
return $this->location->getId();
150+
}
151+
139152
public function getImage(): ?MediaInterface
140153
{
141154
return $this->image;

tests/Functional/Controller/Admin/EventControllerTest.php

+11-6
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
namespace App\Tests\Functional\Controller\Admin;
66

77
use App\Tests\Functional\Traits\EventTrait;
8+
use App\Tests\Functional\Traits\LocationTrait;
89
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;
910
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
1011
use Symfony\Component\HttpFoundation\Response;
1112

1213
class EventControllerTest extends SuluTestCase
1314
{
1415
use EventTrait;
16+
use LocationTrait;
1517

1618
/**
1719
* @var KernelBrowser
@@ -64,6 +66,8 @@ public function testGet(): void
6466

6567
public function testPost(): void
6668
{
69+
$location = $this->createLocation('Sulu HQ');
70+
6771
$this->client->request(
6872
'POST',
6973
'/admin/api/events?locale=de',
@@ -73,7 +77,7 @@ public function testPost(): void
7377
'startDate' => '2019-01-01 12:00',
7478
'endDate' => '2019-01-02 12:00',
7579
'description' => 'Sulu is really awesome',
76-
'location' => 'Dornbirn',
80+
'locationId' => $location->getId(),
7781
]
7882
);
7983

@@ -90,7 +94,7 @@ public function testPost(): void
9094
$this->assertSame('2019-01-01T12:00:00', $result['startDate']);
9195
$this->assertSame('2019-01-02T12:00:00', $result['endDate']);
9296
$this->assertSame('Sulu is really awesome', $result['description']);
93-
$this->assertSame('Dornbirn', $result['location']);
97+
$this->assertSame($location->getId(), $result['locationId']);
9498

9599
$result = $this->findEventById($result['id'], 'de');
96100

@@ -103,7 +107,7 @@ public function testPost(): void
103107
$this->assertNotNull($result->getEndDate());
104108
$this->assertSame('2019-01-02T12:00:00', $result->getEndDate()->format('Y-m-d\TH:i:s'));
105109
$this->assertSame('Sulu is really awesome', $result->getDescription());
106-
$this->assertSame('Dornbirn', $result->getLocation());
110+
$this->assertSame($location->getId(), $result->getLocationId());
107111
}
108112

109113
public function testPostNullValues(): void
@@ -146,6 +150,7 @@ public function testPostNullValues(): void
146150
public function testPut(): void
147151
{
148152
$event = $this->createEvent('Symfony', 'de');
153+
$location = $this->createLocation('Sulu HQ');
149154

150155
$this->client->request(
151156
'PUT',
@@ -156,7 +161,7 @@ public function testPut(): void
156161
'startDate' => '2019-01-01 12:00',
157162
'endDate' => '2019-01-02 12:00',
158163
'description' => 'Symfony Live is really awesome',
159-
'location' => 'Dornbirn',
164+
'locationId' => $location->getId(),
160165
]
161166
);
162167

@@ -173,7 +178,7 @@ public function testPut(): void
173178
$this->assertSame('2019-01-01T12:00:00', $result['startDate']);
174179
$this->assertSame('2019-01-02T12:00:00', $result['endDate']);
175180
$this->assertSame('Symfony Live is really awesome', $result['description']);
176-
$this->assertSame('Dornbirn', $result['location']);
181+
$this->assertSame($location->getId(), $result['locationId']);
177182

178183
$result = $this->findEventById($result['id'], 'de');
179184

@@ -186,7 +191,7 @@ public function testPut(): void
186191
$this->assertNotNull($result->getEndDate());
187192
$this->assertSame('2019-01-02T12:00:00', $result->getEndDate()->format('Y-m-d\TH:i:s'));
188193
$this->assertSame('Symfony Live is really awesome', $result->getDescription());
189-
$this->assertSame('Dornbirn', $result->getLocation());
194+
$this->assertSame($location->getId(), $result->getLocationId());
190195
}
191196

192197
public function testPutNullValues(): void

tests/Unit/Entity/EventTest.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,27 @@
66

77
use App\Entity\Event;
88
use App\Entity\EventTranslation;
9+
use App\Entity\Location;
910
use PHPUnit\Framework\TestCase;
11+
use Prophecy\Prophecy\ObjectProphecy;
1012
use Sulu\Bundle\MediaBundle\Entity\MediaInterface;
1113

1214
class EventTest extends TestCase
1315
{
16+
/**
17+
* @var Location|ObjectProphecy
18+
*/
19+
private $location;
20+
1421
/**
1522
* @var Event
1623
*/
1724
private $event;
1825

1926
protected function setUp(): void
2027
{
28+
$this->location = $this->prophesize(Location::class);
29+
2130
$this->event = new Event();
2231
$this->event->setLocale('de');
2332
}
@@ -51,9 +60,14 @@ public function testEndDate(): void
5160

5261
public function testLocation(): void
5362
{
63+
$this->location->getId()->willReturn(42);
64+
5465
$this->assertNull($this->event->getLocation());
55-
$this->assertSame($this->event, $this->event->setLocation('Amsterdam'));
56-
$this->assertSame('Amsterdam', $this->event->getLocation());
66+
$this->assertNull($this->event->getLocationId());
67+
$this->assertSame($this->event, $this->event->setLocation($this->location->reveal()));
68+
$this->assertNotNull($this->event->getLocation());
69+
$this->assertSame($this->location->reveal(), $this->event->getLocation());
70+
$this->assertSame(42, $this->event->getLocationId());
5771
}
5872

5973
public function testImage(): void

translations/admin.de.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
"app.enable_event": "Veranstaltungen aktivieren",
99
"app.enabled": "Aktiviert",
1010
"app.location": "Standort",
11-
"app.locations": "Standorte"
11+
"app.locations": "Standorte",
12+
"app.location.no_selections": "Kein Standord ausgewählt"
1213
}

translations/admin.en.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
"app.enable_event": "Enable event",
99
"app.enabled": "Enabled",
1010
"app.location": "Location",
11-
"app.locations": "Locations"
11+
"app.locations": "Locations",
12+
"app.location.no_selections": "No location selected"
1213
}

0 commit comments

Comments
 (0)