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 15, 2021
1 parent 98453fc commit 78e2662
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 2 deletions.
21 changes: 21 additions & 0 deletions config/forms/contact_extensions.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_contact_extension_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_contact-extension'
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.
1 change: 1 addition & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
Sulu\Bundle\SnippetBundle\Snippet\DefaultSnippetManagerInterface: '@sulu_snippet.default_snippet.manager'
Sulu\Component\PHPCR\PathCleanupInterface: '@sulu.content.path_cleaner'
Doctrine\ORM\EntityManagerInterface: '@doctrine.orm.entity_manager'
Sulu\Bundle\ContactBundle\Controller\ContactController: '@sulu_contact.contact_controller'

# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
Expand Down
47 changes: 47 additions & 0 deletions src/Admin/AdditionalContactDataAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace App\Admin;

use Sulu\Bundle\AdminBundle\Admin\Admin;
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', '/extensions')
->setResourceKey('additional_contact_data')
->setFormKey('additional_contact_data')
->setTabTitle('app.additional_data')
->addToolbarActions($contactDetailsFormView->getOption('toolbarActions'))
->setTabOrder($contactDetailsFormView->getOption('tabOrder') + 1)
->setParent(ContactAdmin::CONTACT_EDIT_FORM_VIEW)
);
}
}

public static function getPriority(): int
{
return ContactAdmin::getPriority() - 1;
}
}
93 changes: 93 additions & 0 deletions src/Controller/Admin/AdditionalContactDataController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?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\Controller\ContactController;
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("contact-extension")
*/
class AdditionalContactDataController extends AbstractRestController implements ClassResourceInterface, SecuredControllerInterface
{
private EntityManagerInterface $entityManager;
private ContactController $contactController;

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

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

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

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

public function putAction(Request $request, int $id): Response
{
$contact = $this->entityManager->getRepository(Contact::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)));
}

public function deleteAction(int $id): Response
{
return $this->contactController->deleteAction($id);
}

/**
* @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 $this->contactController->getSecurityContext();
}
}
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 78e2662

Please sign in to comment.