Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CED-1124 NGSTACK-831 edit infocollection #89

Open
wants to merge 87 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 64 commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
82983ad
CED-1124 WIP
Jan 11, 2024
a325b2c
CED-1124 WIP
Jan 11, 2024
1078320
CED-1124 enable information collection editing
Jan 12, 2024
0feeea3
CED-1124 use form theme from bundle
Jan 12, 2024
9837590
CED-1124 add configuration for EnhancedSelectionFieldHandler and fix …
Jan 12, 2024
b571290
CED-1124 remove dump from form.html.twig
Jan 12, 2024
8ae1fc8
CED-1124 Add minimal styling for ic edit form and translations for ed…
vjeran Jan 12, 2024
577d9e8
CED-1124 check edit permissions
Jan 15, 2024
3f85b70
CED-1124 add return type in createUpdateFormForLocation method
Jan 15, 2024
f9cf7a1
CED-1124 remove uneccessary comments
Jan 15, 2024
5ddfd75
CED-1124 set languages with config resolver
Jan 15, 2024
11c12af
CED-1124 add edit link on collection list
Jan 18, 2024
39638ea
CS Fixes
ludwig031 Jan 23, 2024
11079f5
CED-1124 add ibexa-forms-bundle package to composer.json
Feb 21, 2024
2dedd9f
CED-1124 refactor fromLegacyValue method
Feb 21, 2024
d44fc88
CED-1124 add fromLegacyValue method to interface
Feb 21, 2024
0b4f820
CED-1124 rename method fromLegacyValue to fromAttribute
Feb 21, 2024
2d33a49
CED-1124 use spritf when creating name for formbuilder
Feb 21, 2024
ac40a59
CED-1124 change the php version in composer.json
Feb 21, 2024
a5f8e50
CED-1124 fix code style and remove not necessary use statements in In…
Feb 21, 2024
0ff2402
CED-1124 require php 8.1 or greater
Feb 21, 2024
6fc74a5
CED-1124 InformationCollectionUpdateType.php code cleaning
Feb 23, 2024
1d67a7c
CED-1124 InformationCollectionUpdateMapper.php code cleaning
Feb 23, 2024
7faf13c
CED-1124 FormBuilder.php code cleaning
Feb 23, 2024
c34cdb5
CED-1124 code cleanup FormBuilder.php
Feb 23, 2024
251ce38
CED-1124 code cleanup AdminController.php
Feb 23, 2024
316673c
CED-1124 code cleanup field handlers
Feb 23, 2024
7182dd9
CED-1124 add findByCollectionIdAndFieldDefinitionId method to EzInfoC…
Feb 23, 2024
2f1bdff
CED-1024 rename variable in AdminController editAction
Feb 23, 2024
34ebdf2
CED-1024 add return value to CountryFieldHandler
Feb 23, 2024
4dc434f
CED-1124 typehint ValueObject instead of Value to make code more robust
Feb 27, 2024
d9710ed
CED-1124 use $this->createNotFoundException() in AdminController.php
Feb 27, 2024
36d5f6a
CED-1124 check if fielddefinition is null
Feb 27, 2024
2f9f33f
CED-1124 check if location is not null
Feb 27, 2024
58bc405
CED-1124 instead of type hinting ValueObject type hint Value Interface
Feb 27, 2024
e00a98d
CED-1124 rename method fromAttribute to getFieldValueFromAttribute
Feb 27, 2024
9d63353
CED-1124 use getFieldValueFromAttribute instead of fromAttribute
Feb 27, 2024
89702d5
CED-1124 typehint right InformationCollectionStruct
Mar 18, 2024
0c50e28
CED-1124 throw NotFoundException
Mar 18, 2024
1d3bd95
CED-1124 remove dependency on ibexa-forms-bundle
Mar 18, 2024
6f0f394
CED-1124 set right arguments
Mar 18, 2024
760b953
CED-1124 add form_fieldtype_handlers.yaml
Mar 18, 2024
1deef6b
CED-1124 add FieldTypeHandlerRegistryPass.php
Mar 18, 2024
852d03a
CED-1124 register FieldTypeHandlerRegistryPass.php
Mar 18, 2024
bdf85a9
CED-1124 adjustments to work with old FieldTypeHandler
Mar 19, 2024
d42fe13
CED-1124 clean up DataMapper.php
Mar 19, 2024
12dcfa7
CED-1124 clean up FieldTypeHandler.php
Mar 19, 2024
b540b6f
CED-1124 clean up Checkbox.php
Mar 19, 2024
691a39a
CED-1124 clean up Country.php
Mar 19, 2024
bba4273
CED-1124 clean up Selection.php
Mar 19, 2024
5a1554d
CED-1124 clean up FieldTypeHandlerInterface.php
Mar 19, 2024
65ef538
CED-1124 clean up field type handlers
Mar 19, 2024
932f303
CED-1124 remove User.php class
Mar 19, 2024
8aa2661
CED-1124 remove User from field type config
Mar 19, 2024
a0d2b5e
CED-1124 fix BinaryFile return type
Mar 19, 2024
83093de
CED-1124 sync parameter name with interface in mapDataToForms method
Mar 20, 2024
6580c58
CED-1124 sync parameter name with interface in mapFormsToData method
Mar 20, 2024
f574457
CED-1124 fix alias in Checkbox.php
Mar 20, 2024
75d831c
CED-1124 fix aliases and return types in field type handlers
Mar 20, 2024
fac3dfd
CED-1124 add docblock type hint to Country.php
Mar 20, 2024
e0905a8
CED-1124 match variable with interface Selection.php
Mar 20, 2024
df6111a
CED-1124 add docblock type hint to field type handler
Mar 20, 2024
6bec7a9
CED-1124 update code to PHP 8.1 standards
Mar 20, 2024
37edb6f
CED-1124 change name of variable in DataMapper.php
Mar 20, 2024
bc480a8
CED-1124 remove redundant inline type hint
Mar 20, 2024
90f8ad9
CED-1124 remove redundant inline type hint
Mar 20, 2024
54aa265
CED-1124 add return type to getCollectedValue method
Mar 20, 2024
cc986d9
CED-1124 add return type for value variable in setCollectedValue method
Mar 20, 2024
aec51eb
CED-1124 change alias in DateAndTime.php
Mar 20, 2024
a6d9a0e
CED-1124 fixed wrong variable name in DataMapper.php
Mar 20, 2024
1ab8f5d
CED-1124 remove UserType.php
Mar 20, 2024
7822313
CED-1124 remove UserUpdateType.php
Mar 20, 2024
bfce0ac
CED-1124 remove UserCreateType.php
Mar 20, 2024
6266154
CED-1124 add type hints to DataWrapper.php
Mar 20, 2024
4ccfd43
CED-1124 update docs in DataWrapper.php
Mar 20, 2024
706fed5
CED-1124 remove callable from parameter docs in FieldTypeHandlerRegis…
Mar 20, 2024
d2abeb6
CED-1124 type hint FieldTypeHandlerInterface in register method
Mar 20, 2024
38530c5
CED-1124 remove handling callable from get method in FieldTypeHandler…
Mar 20, 2024
49dc73c
CED-1124 move form types to Type folder
Mar 20, 2024
f52577d
CED-1124 adapt tag name for form_fieldtype_handlers.yaml
Mar 20, 2024
9f56103
CED-1124 adapt tag in FieldTypeHandlerRegistryPass.php
Mar 20, 2024
137ab03
CED-1124 add Birthday type
Mar 20, 2024
e46037b
CED-1124 remove FieldTypeHandlerInterface from Ibexa Forms bundle in …
Mar 20, 2024
9fb2e01
CED-1124 change class namespace in configuration
Mar 20, 2024
fc5f4d2
CED-1124 add enhanced selection handler
Mar 21, 2024
2c79357
CED-1124 standardize EnhancedSelection.php and Birthday.php with Fiel…
Mar 21, 2024
ffdd8eb
CED-1124 change input type and remove constraint in Birthday.php
Mar 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 142 additions & 30 deletions bundle/Controller/Admin/AdminController.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,45 @@
<?php

declare(strict_types=1);

namespace Netgen\Bundle\InformationCollectionBundle\Controller\Admin;

use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Ibexa\Core\MVC\Symfony\Security\Authorization\Attribute;
use Doctrine\ORM\NonUniqueResultException;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use Ibexa\Bundle\Core\Controller;
use Ibexa\Contracts\Core\Repository\ContentService;
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
use Ibexa\Core\MVC\Symfony\Security\Authorization\Attribute;
use Netgen\Bundle\InformationCollectionBundle\Form\Payload\InformationCollectionStruct;
use Netgen\Bundle\InformationCollectionBundle\Form\Builder\FormBuilder;
use Netgen\InformationCollection\API\Persistence\Anonymizer\Anonymizer;
use Netgen\InformationCollection\API\Service\InformationCollection;
use Netgen\InformationCollection\API\Value\Collection;
use Netgen\InformationCollection\API\Value\Filter\CollectionFields;
use Netgen\InformationCollection\API\Value\Filter\CollectionId;
use Netgen\InformationCollection\API\Value\Filter\Collections;
use Netgen\InformationCollection\API\Value\Filter\ContentId;
use Netgen\InformationCollection\API\Value\Filter\Contents;
use Netgen\InformationCollection\API\Value\Filter\Query;
use Netgen\InformationCollection\API\Value\Filter\SearchQuery;
use Netgen\InformationCollection\Core\Factory\FieldDataFactory;
use Netgen\InformationCollection\Core\Pagination\InformationCollectionCollectionListAdapter;
use Netgen\InformationCollection\Core\Pagination\InformationCollectionCollectionListSearchAdapter;
use Netgen\InformationCollection\Core\Pagination\InformationCollectionContentsAdapter;
use Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionAttributeRepository;
use Netgen\InformationCollection\Doctrine\Repository\EzInfoCollectionRepository;
use Pagerfanta\Adapter\AdapterInterface;
use Pagerfanta\Pagerfanta;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;

use function array_merge;
use function count;
use function time;

class AdminController extends Controller
{
protected InformationCollection $service;
Expand All @@ -36,25 +50,39 @@ class AdminController extends Controller

protected Anonymizer $anonymizer;

protected FieldDataFactory $factory;
protected EzInfoCollectionRepository $infoCollectionRepository;

protected EzInfoCollectionAttributeRepository $infoCollectionAttributeRepository;

protected FormBuilder $builder;

private TranslatorInterface $translator;

public function __construct(
InformationCollection $service,
Anonymizer $anonymizer,
ContentService $contentService,
ConfigResolverInterface $configResolver,
TranslatorInterface $translator
)
{
TranslatorInterface $translator,
FieldDataFactory $factory,
EzInfoCollectionRepository $infoCollectionRepository,
EzInfoCollectionAttributeRepository $infoCollectionAttributeRepository,
FormBuilder $builder
) {
$this->service = $service;
$this->contentService = $contentService;
$this->configResolver = $configResolver;
$this->anonymizer = $anonymizer;
$this->translator = $translator;
$this->factory = $factory;
$this->infoCollectionRepository = $infoCollectionRepository;
$this->infoCollectionAttributeRepository = $infoCollectionAttributeRepository;
$this->builder = $builder;
}

/**
* Displays overview page
* Displays overview page.
*/
public function overviewAction(Request $request): Response
{
Expand All @@ -63,27 +91,27 @@ public function overviewAction(Request $request): Response
$adapter = new InformationCollectionContentsAdapter($this->service, Query::countQuery());
$pager = $this->getPager($adapter, (int) $request->query->get('page'));

return $this->render("@NetgenInformationCollection/admin/overview.html.twig", ['objects' => $pager]);
return $this->render('@NetgenInformationCollection/admin/overview.html.twig', ['objects' => $pager]);
}

/**
* Displays list of collection for selected Content
* Displays list of collection for selected Content.
*/
public function collectionListAction(Request $request, Content $content): Response
{
$this->checkReadPermissions();

$adapter = new InformationCollectionCollectionListAdapter($this->service, ContentId::withContentId($content->id));
$pager = $this->getPager($adapter, (int)$request->query->get('page'));
$pager = $this->getPager($adapter, (int) $request->query->get('page'));

return $this->render("@NetgenInformationCollection/admin/collection_list.html.twig", [
return $this->render('@NetgenInformationCollection/admin/collection_list.html.twig', [
'objects' => $pager,
'content' => $content,
]);
}

/**
* Handles collection search
* Handles collection search.
*/
public function searchAction(Request $request, Content $content): Response
{
Expand All @@ -92,9 +120,10 @@ public function searchAction(Request $request, Content $content): Response
$query = SearchQuery::withContentAndSearchText($content->id, $request->query->get('searchText'));

$adapter = new InformationCollectionCollectionListSearchAdapter($this->service, $query);
$pager = $this->getPager($adapter, (int)$request->query->get('page'));
$pager = $this->getPager($adapter, (int) $request->query->get('page'));

return $this->render("@NetgenInformationCollection/admin/collection_list.html.twig",
return $this->render(
'@NetgenInformationCollection/admin/collection_list.html.twig',
[
'objects' => $pager,
'content' => $content,
Expand All @@ -103,20 +132,20 @@ public function searchAction(Request $request, Content $content): Response
}

/**
* Displays individual collection details
* Displays individual collection details.
*/
public function viewAction(Collection $collection): Response
{
$this->checkReadPermissions();

return $this->render("@NetgenInformationCollection/admin/view.html.twig", [
return $this->render('@NetgenInformationCollection/admin/view.html.twig', [
'collection' => $collection,
'content' => $collection->getContent(),
]);
}

/**
* Handles actions performed on overview page
* Handles actions performed on overview page.
*/
public function handleContentsAction(Request $request): RedirectResponse
{
Expand All @@ -132,7 +161,6 @@ public function handleContentsAction(Request $request): RedirectResponse
}

if ($request->request->has('DeleteCollectionByContentAction')) {

$this->checkDeletePermissions();

$query = new Contents($contents);
Expand All @@ -150,7 +178,7 @@ public function handleContentsAction(Request $request): RedirectResponse
}

/**
* Handles actions performed on collection list page
* Handles actions performed on collection list page.
*/
public function handleCollectionListAction(Request $request): RedirectResponse
{
Expand All @@ -167,7 +195,6 @@ public function handleCollectionListAction(Request $request): RedirectResponse
}

if ($request->request->has('DeleteCollectionAction')) {

$this->checkDeletePermissions();

$query = new Collections($contentId, $collections);
Expand All @@ -180,7 +207,6 @@ public function handleCollectionListAction(Request $request): RedirectResponse
}

if ($request->request->has('AnonymizeCollectionAction')) {

$this->checkAnonymizePermissions();

foreach ($collections as $collection) {
Expand All @@ -198,7 +224,7 @@ public function handleCollectionListAction(Request $request): RedirectResponse
}

/**
* Handles action on collection details page
* Handles action on collection details page.
*/
public function handleCollectionAction(Request $request): RedirectResponse
{
Expand All @@ -219,7 +245,6 @@ public function handleCollectionAction(Request $request): RedirectResponse
}

if ($request->request->has('DeleteFieldAction')) {

$this->checkDeletePermissions();

$query = new CollectionFields($contentId, $collectionId, $fields);
Expand All @@ -232,7 +257,6 @@ public function handleCollectionAction(Request $request): RedirectResponse
}

if ($request->request->has('AnonymizeFieldAction')) {

$this->checkAnonymizePermissions();

$this->anonymizer->anonymizeCollection($collectionId, $fields);
Expand All @@ -243,25 +267,22 @@ public function handleCollectionAction(Request $request): RedirectResponse
}

if ($request->request->has('DeleteCollectionAction')) {

$this->checkDeletePermissions();

$query = new Collections($contentId, [$collectionId]);
$this->service->deleteCollections($query);

$this->addFlashMessage("success", "collection_removed");
$this->addFlashMessage('success', 'collection_removed');

return $this->redirectToRoute('netgen_information_collection.route.admin.collection_list', ['contentId' => $contentId]);

}

if ($request->request->has('AnonymizeCollectionAction')) {

$this->checkAnonymizePermissions();

$this->anonymizer->anonymizeCollection($collectionId);

$this->addFlashMessage("success", "collection_anonymized");
$this->addFlashMessage('success', 'collection_anonymized');

return $this->redirectToRoute('netgen_information_collection.route.admin.view', ['collectionId' => $collectionId]);
}
Expand All @@ -271,10 +292,95 @@ public function handleCollectionAction(Request $request): RedirectResponse
return $this->redirectToRoute('netgen_information_collection.route.admin.view', ['collectionId' => $collectionId]);
}

/**
* @throws NonUniqueResultException
*/
public function editAction(Request $request, int $collectionId): RedirectResponse|Response
{
$this->checkEditPermissions();

$collection = $this->service->getCollection(new CollectionId($collectionId));

$location = $collection->getContent()->contentInfo->getMainLocation();
pspanja marked this conversation as resolved.
Show resolved Hide resolved

if ($location === null) {
throw $this->createNotFoundException();
}

$form = $this->builder->createUpdateFormForLocation($location, $collection)->getForm();

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
/** @var InformationCollectionStruct $struct */
$struct = $form->getData()->payload;
pspanja marked this conversation as resolved.
Show resolved Hide resolved

/** @var ContentType $contentType */
$contentType = $form->getData()->definition;
pspanja marked this conversation as resolved.
Show resolved Hide resolved

$ezInfoCollection = $this->infoCollectionRepository->find($collectionId);

if ($ezInfoCollection === null) {
throw $this->createNotFoundException();
}

$ezInfoCollection->setModified(time());

$this->infoCollectionRepository->save($ezInfoCollection);

foreach ($struct->getCollectedFields() as $fieldDefIdentifier => $value) {
if ($value === null) {
continue;
}

$fieldDefinition = $contentType->getFieldDefinition($fieldDefIdentifier);
pspanja marked this conversation as resolved.
Show resolved Hide resolved

if ($fieldDefinition === null) {
continue;
}

$legacyValue = $this->factory->getLegacyValue($value, $fieldDefinition);
pspanja marked this conversation as resolved.
Show resolved Hide resolved

$ezInfoAttribute = $this->infoCollectionAttributeRepository->findByCollectionIdAndFieldDefinitionId(
$collectionId,
$fieldDefinition->id
);

if ($ezInfoAttribute === null) {
$ezInfoAttribute = $this->infoCollectionAttributeRepository->getInstance();
$ezInfoAttribute->setContentObjectId($collection->getContent()->id);
$ezInfoAttribute->setContentObjectAttributeId($collection->getContent()->getField($fieldDefinition->identifier)->id);
$ezInfoAttribute->setContentClassAttributeId($fieldDefinition->id);
$ezInfoAttribute->setInformationCollectionId($collection->getId());
}

$ezInfoAttribute->setDataInt($legacyValue->getDataInt());
$ezInfoAttribute->setDataFloat($legacyValue->getDataFloat());
$ezInfoAttribute->setDataText($legacyValue->getDataText());

$this->infoCollectionAttributeRepository->save($ezInfoAttribute);
}

return $this->redirectToRoute(
'netgen_information_collection.route.admin.view',
[
'contentId' => $location->contentInfo->id,
'collectionId' => $collection->getId(),
]
);
}

return $this->render('@NetgenInformationCollection/admin/edit.html.twig', [
'collection' => $collection,
'content' => $location->getContent(),
'form' => $form->createView(),
]);
}

/**
* Adds a flash message with specified parameters.
*/
protected function addFlashMessage(string $messageType, string $message, int $count = 1, array $parameters = array()): void
protected function addFlashMessage(string $messageType, string $message, int $count = 1, array $parameters = []): void
{
$parameters = array_merge($parameters, ['count' => $count]);

Expand All @@ -289,7 +395,7 @@ protected function addFlashMessage(string $messageType, string $message, int $co
}

/**
* Returns configured instance of Pagerfanta
* Returns configured instance of Pagerfanta.
*/
protected function getPager(AdapterInterface $adapter, int $currentPage): Pagerfanta
{
Expand Down Expand Up @@ -320,4 +426,10 @@ protected function checkAnonymizePermissions(): void
$attribute = new Attribute('infocollector', 'anonymize');
$this->denyAccessUnlessGranted($attribute);
}

protected function checkEditPermissions(): void
{
$attribute = new Attribute('infocollector', 'edit');
$this->denyAccessUnlessGranted($attribute);
}
}
Loading