Skip to content

Commit

Permalink
Add additional data tab to contact entity
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasnatter committed Feb 16, 2021
1 parent 98453fc commit 852d806
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 2 deletions.
21 changes: 21 additions & 0 deletions config/forms/additional_contact_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" ?>
<form xmlns="http://schemas.sulu.io/template/template"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schemas.sulu.io/template/template http://schemas.sulu.io/template/form-1.0.xsd"
>
<key>additional_contact_data</key>

<properties>
<property name="socialSecurityNumber" type="text_line" colspan="6">
<meta>
<title>app.social_security_number</title>
</meta>
</property>

<property name="externalCrmId" type="text_line" colspan="6">
<meta>
<title>app.external_crm_id</title>
</meta>
</property>
</properties>
</form>
5 changes: 5 additions & 0 deletions config/packages/app_additional_contact_data_admin.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sulu_admin:
resources:
additional_contact_data:
routes:
detail: 'app.get_additional-contact-data'
4 changes: 4 additions & 0 deletions config/packages/sulu_contact.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sulu_contact:
objects:
contact:
model: 'App\Entity\Contact'
6 changes: 6 additions & 0 deletions config/routes_admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ app_albums_api:
prefix: /admin/api
resource: App\Controller\Admin\AlbumController
name_prefix: app.

app_additional_contact_data_api:
type: rest
prefix: /admin/api
resource: App\Controller\Admin\AdditionalContactDataController
name_prefix: app.
48 changes: 48 additions & 0 deletions src/Admin/AdditionalContactDataAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace App\Admin;

use Sulu\Bundle\AdminBundle\Admin\Admin;
use Sulu\Bundle\AdminBundle\Admin\View\ToolbarAction;
use Sulu\Bundle\AdminBundle\Admin\View\ViewBuilderFactoryInterface;
use Sulu\Bundle\AdminBundle\Admin\View\ViewCollection;
use Sulu\Bundle\ContactBundle\Admin\ContactAdmin;

class AdditionalContactDataAdmin extends Admin
{
/**
* @var ViewBuilderFactoryInterface
*/
private $viewBuilderFactory;

public function __construct(
ViewBuilderFactoryInterface $viewBuilderFactory
) {
$this->viewBuilderFactory = $viewBuilderFactory;
}

public function configureViews(ViewCollection $viewCollection): void
{
if ($viewCollection->has('sulu_contact.contact_edit_form.details')) {
$contactDetailsFormView = $viewCollection->get('sulu_contact.contact_edit_form.details')->getView();

$viewCollection->add(
$this->viewBuilderFactory
->createFormViewBuilder('app.additional_contact_data_form', '/additional-data')
->setResourceKey('additional_contact_data')
->setFormKey('additional_contact_data')
->setTabTitle('app.additional_data')
->addToolbarActions([new ToolbarAction('sulu_admin.save')])
->setTabOrder($contactDetailsFormView->getOption('tabOrder') + 1)
->setParent(ContactAdmin::CONTACT_EDIT_FORM_VIEW)
);
}
}

public static function getPriority(): int
{
return ContactAdmin::getPriority() - 1;
}
}
88 changes: 88 additions & 0 deletions src/Controller/Admin/AdditionalContactDataController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use App\Entity\Contact;
use Doctrine\ORM\EntityManagerInterface;
use FOS\RestBundle\View\ViewHandlerInterface;
use HandcraftedInTheAlps\RestRoutingBundle\Controller\Annotations\RouteResource;
use HandcraftedInTheAlps\RestRoutingBundle\Routing\ClassResourceInterface;
use Sulu\Bundle\ContactBundle\Admin\ContactAdmin;
use Sulu\Bundle\ContactBundle\Entity\ContactInterface;
use Sulu\Component\Rest\AbstractRestController;
use Sulu\Component\Security\SecuredControllerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

/**
* @RouteResource("additional-contact-data")
*/
class AdditionalContactDataController extends AbstractRestController implements ClassResourceInterface, SecuredControllerInterface
{
private EntityManagerInterface $entityManager;

public function __construct(
EntityManagerInterface $entityManager,
ViewHandlerInterface $viewHandler,
?TokenStorageInterface $tokenStorage = null
) {
$this->entityManager = $entityManager;

parent::__construct($viewHandler, $tokenStorage);
}

public function getAction(int $id): Response
{
/** @var Contact|null $contact */
$contact = $this->entityManager->getRepository(ContactInterface::class)->find($id);
if (!$contact) {
throw new NotFoundHttpException();
}

return $this->handleView($this->view($this->getDataForEntity($contact)));
}

public function putAction(Request $request, int $id): Response
{
/** @var Contact|null $contact */
$contact = $this->entityManager->getRepository(ContactInterface::class)->find($id);
if (!$contact) {
throw new NotFoundHttpException();
}

$this->mapDataToEntity($request->request->all(), $contact);
$this->entityManager->flush();

return $this->handleView($this->view($this->getDataForEntity($contact)));
}

/**
* @return array<string, mixed>
*/
protected function getDataForEntity(Contact $entity): array
{
return [
'id' => $entity->getId(),
'socialSecurityNumber' => $entity->getSocialSecurityNumber(),
'externalCrmId' => $entity->getExternalCrmId(),
];
}

/**
* @param array<string, mixed> $data
*/
protected function mapDataToEntity(array $data, Contact $entity): void
{
$entity->setSocialSecurityNumber($data['socialSecurityNumber']);
$entity->setExternalCrmId($data['externalCrmId']);
}

public function getSecurityContext(): string
{
return ContactAdmin::CONTACT_SECURITY_CONTEXT;
}
}
43 changes: 43 additions & 0 deletions src/Entity/Contact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Sulu\Bundle\ContactBundle\Entity\Contact as SuluContact;

/**
* @ORM\Entity()
* @ORM\Table(name="co_contacts")
*/
class Contact extends SuluContact
{
/**
* @ORM\Column(type="string", length=63, nullable=true)
*/
private ?string $socialSecurityNumber;

/**
* @ORM\Column(type="string", length=63, nullable=true)
*/
private ?string $externalCrmId;

public function getSocialSecurityNumber(): ?string
{
return $this->socialSecurityNumber;
}

public function setSocialSecurityNumber(?string $socialSecurityNumber): void
{
$this->socialSecurityNumber = $socialSecurityNumber;
}

public function getExternalCrmId(): ?string
{
return $this->externalCrmId;
}

public function setExternalCrmId(?string $externalCrmId): void
{
$this->externalCrmId = $externalCrmId;
}
}
5 changes: 4 additions & 1 deletion translations/admin.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"app.album_selection_label": "{count} {count, plural, =1 {Album} other {Alben}} ausgewählt",
"app.select_albums": "Alben auswählen",
"app.no_album_selected": "Kein Album ausgewählt",
"app.select_album": "Album auswählen"
"app.select_album": "Album auswählen",
"app.additional_data": "Zusätzliche Daten",
"app.social_security_number": "Sozialversicherungsnummer",
"app.external_crm_id": "Externe CRM ID"
}
5 changes: 4 additions & 1 deletion translations/admin.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"app.album_selection_label": "{count} {count, plural, =1 {album} other {albums}} selected",
"app.select_albums": "Select albums",
"app.no_album_selected": "No album selected",
"app.select_album": "Select album"
"app.select_album": "Select album",
"app.additional_data": "Additional Data",
"app.social_security_number": "Social Security number",
"app.external_crm_id": "External CRM ID"
}

0 comments on commit 852d806

Please sign in to comment.