Skip to content

Commit 458ce4f

Browse files
wachterjohannesniklasnatter
authored andcommittedJun 20, 2022
09 - Add a list representation for locations in the admin interface
1 parent 84af386 commit 458ce4f

File tree

9 files changed

+224
-2
lines changed

9 files changed

+224
-2
lines changed
 

‎config/lists/locations.xml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" ?>
2+
<list xmlns="http://schemas.sulu.io/list-builder/list">
3+
<key>locations</key>
4+
5+
<properties>
6+
<property name="id" visibility="no" translation="sulu_admin.id">
7+
<field-name>id</field-name>
8+
<entity-name>App\Entity\Location</entity-name>
9+
</property>
10+
11+
<property name="name" visibility="always" searchability="yes" translation="sulu_admin.name">
12+
<field-name>name</field-name>
13+
<entity-name>App\Entity\Location</entity-name>
14+
</property>
15+
16+
<property name="street" visibility="yes" searchability="yes" translation="sulu_contact.street">
17+
<field-name>street</field-name>
18+
<entity-name>App\Entity\Location</entity-name>
19+
</property>
20+
21+
<property name="number" visibility="yes" searchability="yes" translation="sulu_contact.number">
22+
<field-name>number</field-name>
23+
<entity-name>App\Entity\Location</entity-name>
24+
</property>
25+
26+
<property name="postalCode" visibility="yes" searchability="yes" translation="sulu_contact.zip">
27+
<field-name>postalCode</field-name>
28+
<entity-name>App\Entity\Location</entity-name>
29+
</property>
30+
31+
<property name="city" visibility="yes" searchability="yes" translation="sulu_contact.city">
32+
<field-name>city</field-name>
33+
<entity-name>App\Entity\Location</entity-name>
34+
</property>
35+
36+
<property name="countryCode" visibility="yes" searchability="yes" translation="sulu_contact.country">
37+
<field-name>countryCode</field-name>
38+
<entity-name>App\Entity\Location</entity-name>
39+
</property>
40+
</properties>
41+
</list>

‎config/packages/sulu_admin.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ sulu_admin:
1818
routes:
1919
list: app.get_event_registration_list
2020

21+
locations:
22+
routes:
23+
list: app.get_location_list
24+
2125
# Registering Selection Field Types in this section
2226
field_type_options:
2327
selection:

‎src/Admin/LocationAdmin.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Admin;
6+
7+
use App\Entity\Location;
8+
use Sulu\Bundle\AdminBundle\Admin\Admin;
9+
use Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItem;
10+
use Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItemCollection;
11+
use Sulu\Bundle\AdminBundle\Admin\View\ViewBuilderFactoryInterface;
12+
use Sulu\Bundle\AdminBundle\Admin\View\ViewCollection;
13+
14+
class LocationAdmin extends Admin
15+
{
16+
const LOCATION_LIST_KEY = 'locations';
17+
18+
const LOCATION_LIST_VIEW = 'app.locations_list';
19+
20+
/**
21+
* @var ViewBuilderFactoryInterface
22+
*/
23+
private $viewBuilderFactory;
24+
25+
public function __construct(ViewBuilderFactoryInterface $viewBuilderFactory)
26+
{
27+
$this->viewBuilderFactory = $viewBuilderFactory;
28+
}
29+
30+
public function configureNavigationItems(NavigationItemCollection $navigationItemCollection): void
31+
{
32+
$module = $navigationItemCollection->get('app.events');
33+
34+
$locations = new NavigationItem('app.locations');
35+
$locations->setPosition(10);
36+
$locations->setView(static::LOCATION_LIST_VIEW);
37+
38+
$module->addChild($locations);
39+
}
40+
41+
public function configureViews(ViewCollection $viewCollection): void
42+
{
43+
$listView = $this->viewBuilderFactory->createListViewBuilder(self::LOCATION_LIST_VIEW, '/locations')
44+
->setResourceKey(Location::RESOURCE_KEY)
45+
->setListKey(self::LOCATION_LIST_KEY)
46+
->setTitle('app.locations')
47+
->addListAdapters(['table'])
48+
->addToolbarActions([]);
49+
$viewCollection->add($listView);
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Controller\Admin;
6+
7+
use App\Common\DoctrineListRepresentationFactory;
8+
use App\Entity\Location;
9+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
10+
use Symfony\Component\HttpFoundation\Request;
11+
use Symfony\Component\HttpFoundation\Response;
12+
use Symfony\Component\Routing\Annotation\Route;
13+
14+
class LocationController extends AbstractController
15+
{
16+
/**
17+
* @var DoctrineListRepresentationFactory
18+
*/
19+
private $doctrineListRepresentationFactory;
20+
21+
public function __construct(
22+
DoctrineListRepresentationFactory $doctrineListRepresentationFactory
23+
) {
24+
$this->doctrineListRepresentationFactory = $doctrineListRepresentationFactory;
25+
}
26+
27+
/**
28+
* @Route("/admin/api/locations", methods={"GET"}, name="app.get_location_list")
29+
*/
30+
public function getListAction(Request $request): Response
31+
{
32+
$listRepresentation = $this->doctrineListRepresentationFactory->createDoctrineListRepresentation(
33+
Location::RESOURCE_KEY
34+
);
35+
36+
return $this->json($listRepresentation->toArray());
37+
}
38+
}

‎src/Entity/Location.php

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
*/
1212
class Location
1313
{
14+
const RESOURCE_KEY = 'locations';
15+
1416
/**
1517
* @var int|null
1618
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Tests\Functional\Controller\Admin;
6+
7+
use App\Tests\Functional\Traits\LocationTrait;
8+
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;
9+
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
10+
use Symfony\Component\HttpFoundation\Response;
11+
12+
class LocationControllerTest extends SuluTestCase
13+
{
14+
use LocationTrait;
15+
16+
/**
17+
* @var KernelBrowser
18+
*/
19+
private $client;
20+
21+
protected function setUp(): void
22+
{
23+
$this->client = $this->createAuthenticatedClient();
24+
$this->purgeDatabase();
25+
}
26+
27+
public function testCGet(): void
28+
{
29+
$location1 = $this->createLocation('Sulu');
30+
$location2 = $this->createLocation('Symfony');
31+
32+
$this->client->jsonRequest('GET', '/admin/api/locations');
33+
34+
$response = $this->client->getResponse();
35+
$this->assertInstanceOf(Response::class, $response);
36+
$result = json_decode($response->getContent() ?: '', true);
37+
$this->assertHttpStatusCode(200, $response);
38+
39+
$this->assertSame(2, $result['total']);
40+
$this->assertCount(2, $result['_embedded']['locations']);
41+
$items = $result['_embedded']['locations'];
42+
43+
$this->assertSame($location1->getId(), $items[0]['id']);
44+
$this->assertSame($location2->getId(), $items[1]['id']);
45+
46+
$this->assertSame($location1->getName(), $items[0]['name']);
47+
$this->assertSame($location2->getName(), $items[1]['name']);
48+
}
49+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Tests\Functional\Traits;
6+
7+
use App\Entity\Location;
8+
use App\Repository\LocationRepository;
9+
use Doctrine\ORM\EntityManagerInterface;
10+
11+
trait LocationTrait
12+
{
13+
public function createLocation(string $name): Location
14+
{
15+
$location = $this->getLocationRepository()->create();
16+
$location->setName($name);
17+
$location->setStreet('');
18+
$location->setNumber('');
19+
$location->setPostalCode('');
20+
$location->setCity('');
21+
$location->setCountryCode('');
22+
23+
static::getEntityManager()->persist($location);
24+
static::getEntityManager()->flush();
25+
26+
return $location;
27+
}
28+
29+
protected function getLocationRepository(): LocationRepository
30+
{
31+
return static::getEntityManager()->getRepository(Location::class);
32+
}
33+
34+
abstract protected static function getEntityManager(): EntityManagerInterface;
35+
}

‎translations/admin.de.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
"app.end_date": "Ende",
88
"app.enable_event": "Veranstaltungen aktivieren",
99
"app.enabled": "Aktiviert",
10-
"app.location": "Standort"
10+
"app.location": "Standort",
11+
"app.locations": "Standorte"
1112
}

‎translations/admin.en.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
"app.end_date": "Ende",
88
"app.enable_event": "Enable event",
99
"app.enabled": "Enabled",
10-
"app.location": "Location"
10+
"app.location": "Location",
11+
"app.locations": "Locations"
1112
}

0 commit comments

Comments
 (0)