From 4ae2f0405455ef8f1ee843d9561ba10cda4324ed Mon Sep 17 00:00:00 2001 From: Niklas Natter Date: Fri, 12 Feb 2021 16:26:24 +0100 Subject: [PATCH] Add additional data tab to contact entity --- config/forms/contact_additional_data.xml | 21 +++++ .../app_additional_contact_data_admin.yaml | 5 ++ config/packages/sulu_contact.yaml | 4 + config/routes_admin.yaml | 6 ++ src/Admin/AppAdmin.php | 48 ++++++++++ .../Admin/ContactAdditionalDataController.php | 88 +++++++++++++++++++ src/Entity/Contact.php | 43 +++++++++ translations/admin.de.json | 5 +- translations/admin.en.json | 5 +- 9 files changed, 223 insertions(+), 2 deletions(-) create mode 100644 config/forms/contact_additional_data.xml create mode 100644 config/packages/app_additional_contact_data_admin.yaml create mode 100644 config/packages/sulu_contact.yaml create mode 100644 src/Admin/AppAdmin.php create mode 100644 src/Controller/Admin/ContactAdditionalDataController.php create mode 100644 src/Entity/Contact.php diff --git a/config/forms/contact_additional_data.xml b/config/forms/contact_additional_data.xml new file mode 100644 index 00000000..3d80abac --- /dev/null +++ b/config/forms/contact_additional_data.xml @@ -0,0 +1,21 @@ + +
+ contact_additional_data + + + + + app.social_security_number + + + + + + app.external_crm_id + + + +
diff --git a/config/packages/app_additional_contact_data_admin.yaml b/config/packages/app_additional_contact_data_admin.yaml new file mode 100644 index 00000000..edd204cf --- /dev/null +++ b/config/packages/app_additional_contact_data_admin.yaml @@ -0,0 +1,5 @@ +sulu_admin: + resources: + contact_additional_data: + routes: + detail: 'app.get_additional-contact-data' diff --git a/config/packages/sulu_contact.yaml b/config/packages/sulu_contact.yaml new file mode 100644 index 00000000..50daad8a --- /dev/null +++ b/config/packages/sulu_contact.yaml @@ -0,0 +1,4 @@ +sulu_contact: + objects: + contact: + model: 'App\Entity\Contact' diff --git a/config/routes_admin.yaml b/config/routes_admin.yaml index d084e01e..571cbfe8 100644 --- a/config/routes_admin.yaml +++ b/config/routes_admin.yaml @@ -4,3 +4,9 @@ app.album_api: prefix: /admin/api resource: App\Controller\Admin\AlbumController name_prefix: app. + +app.contact_additional_data_api: + type: rest + prefix: /admin/api + resource: App\Controller\Admin\ContactAdditionalDataController + name_prefix: app. diff --git a/src/Admin/AppAdmin.php b/src/Admin/AppAdmin.php new file mode 100644 index 00000000..1fc0b66f --- /dev/null +++ b/src/Admin/AppAdmin.php @@ -0,0 +1,48 @@ +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.contact_additional_data_form', '/additional-data') + ->setResourceKey('contact_additional_data') + ->setFormKey('contact_additional_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; + } +} diff --git a/src/Controller/Admin/ContactAdditionalDataController.php b/src/Controller/Admin/ContactAdditionalDataController.php new file mode 100644 index 00000000..efb15c64 --- /dev/null +++ b/src/Controller/Admin/ContactAdditionalDataController.php @@ -0,0 +1,88 @@ +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 + */ + protected function getDataForEntity(Contact $entity): array + { + return [ + 'id' => $entity->getId(), + 'socialSecurityNumber' => $entity->getSocialSecurityNumber(), + 'externalCrmId' => $entity->getExternalCrmId(), + ]; + } + + /** + * @param array $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; + } +} diff --git a/src/Entity/Contact.php b/src/Entity/Contact.php new file mode 100644 index 00000000..4cab8c0e --- /dev/null +++ b/src/Entity/Contact.php @@ -0,0 +1,43 @@ +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; + } +} diff --git a/translations/admin.de.json b/translations/admin.de.json index 4565f68a..2c21af5c 100644 --- a/translations/admin.de.json +++ b/translations/admin.de.json @@ -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" } diff --git a/translations/admin.en.json b/translations/admin.en.json index 2160e113..bcb0645d 100644 --- a/translations/admin.en.json +++ b/translations/admin.en.json @@ -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" }