Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4030c29

Browse files
wachterjohannesniklasnatter
authored andcommittedOct 21, 2020
11 - Add a relation between events and locations
1 parent 053fdcc commit 4030c29

File tree

9 files changed

+102
-23
lines changed

9 files changed

+102
-23
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\Routing\ClassResourceInterface;
1213
use FOS\RestBundle\View\ViewHandlerInterface;
@@ -19,6 +20,11 @@
1920

2021
class EventController extends AbstractRestController implements ClassResourceInterface
2122
{
23+
/**
24+
* @var DoctrineListRepresentationFactory
25+
*/
26+
private $doctrineListRepresentationFactory;
27+
2228
/**
2329
* @var EventRepository
2430
*/
@@ -30,20 +36,22 @@ class EventController extends AbstractRestController implements ClassResourceInt
3036
private $mediaRepository;
3137

3238
/**
33-
* @var DoctrineListRepresentationFactory
39+
* @var LocationRepository
3440
*/
35-
private $doctrineListRepresentationFactory;
41+
private $locationRepository;
3642

3743
public function __construct(
44+
DoctrineListRepresentationFactory $doctrineListRepresentationFactory,
3845
EventRepository $repository,
3946
MediaRepositoryInterface $mediaRepository,
40-
DoctrineListRepresentationFactory $doctrineListRepresentationFactory,
47+
LocationRepository $locationRepository,
4148
ViewHandlerInterface $viewHandler,
4249
?TokenStorageInterface $tokenStorage = null
4350
) {
51+
$this->doctrineListRepresentationFactory = $doctrineListRepresentationFactory;
4452
$this->repository = $repository;
4553
$this->mediaRepository = $mediaRepository;
46-
$this->doctrineListRepresentationFactory = $doctrineListRepresentationFactory;
54+
$this->locationRepository = $locationRepository;
4755

4856
parent::__construct($viewHandler, $tokenStorage);
4957
}
@@ -155,8 +163,10 @@ protected function mapDataToEntity(array $data, Event $entity): void
155163
$entity->setEndDate(new \DateTimeImmutable($endDate));
156164
}
157165

158-
if ($location = $data['location'] ?? null) {
159-
$entity->setLocation($location);
166+
if ($locationId = $data['locationId'] ?? null) {
167+
$entity->setLocation(
168+
$this->locationRepository->findById((int) $locationId)
169+
);
160170
}
161171
}
162172

‎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
/**
140153
* @Serializer\VirtualProperty(name="title")
141154
*/

‎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

+21
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,26 @@
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

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

1825
public function setUp(): void
1926
{
27+
$this->location = $this->prophesize(Location::class);
28+
2029
$this->event = new Event();
2130
$this->event->setLocale('de');
2231
}
@@ -87,4 +96,16 @@ public function testDescription(): void
8796
$this->assertSame('de', $this->event->getTranslations()['de']->getLocale());
8897
$this->assertSame('Sulu is awesome', $this->event->getTranslations()['de']->getDescription());
8998
}
99+
100+
public function testLocation(): void
101+
{
102+
$this->location->getId()->willReturn(42);
103+
104+
$this->assertNull($this->event->getLocation());
105+
$this->assertNull($this->event->getLocationId());
106+
$this->assertSame($this->event, $this->event->setLocation($this->location->reveal()));
107+
$this->assertNotNull($this->event->getLocation());
108+
$this->assertSame($this->location->reveal(), $this->event->getLocation());
109+
$this->assertSame(42, $this->event->getLocationId());
110+
}
90111
}

‎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)
Please sign in to comment.