vendor/netgen/information-collection-bundle/bundle/DataCollector/InformationCollectionCollector.php line 34

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Netgen\Bundle\InformationCollectionBundle\DataCollector;
  4. use Ibexa\Contracts\Core\Repository\Repository;
  5. use Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType;
  6. use Ibexa\Core\Helper\TranslationHelper;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpKernel\DataCollector\DataCollector;
  10. use Netgen\Bundle\InformationCollectionBundle\Ibexa\ContentForms\InformationCollectionType;
  11. class InformationCollectionCollector extends DataCollector
  12. {
  13. private Repository $repository;
  14. private TranslationHelper $translationHelper;
  15. public function __construct(Repository $repository, TranslationHelper $translationHelper)
  16. {
  17. $this->repository = $repository;
  18. $this->data = [
  19. 'count' => 0,
  20. 'collections' => [],
  21. 'content_type' => null,
  22. 'content' => null,
  23. ];
  24. $this->translationHelper = $translationHelper;
  25. }
  26. public function collect(Request $request, Response $response, \Throwable $exception = null): void
  27. {
  28. if ($request->get(InformationCollectionType::FORM_BLOCK_PREFIX) !== null) {
  29. $this->mapCollectedData($request);
  30. return;
  31. }
  32. $this->data = [];
  33. }
  34. public function reset(): void
  35. {
  36. $this->data = [];
  37. }
  38. public function getName(): string
  39. {
  40. return 'netgen_information_collection_collector';
  41. }
  42. public function getCollections(): array
  43. {
  44. return $this->data['collections'] ?? [];
  45. }
  46. public function getCollectionCount(): int
  47. {
  48. return $this->data['count'] ?? 0;
  49. }
  50. public function getContent(): string
  51. {
  52. return $this->data['content'];
  53. }
  54. public function getContentId(): int
  55. {
  56. return $this->data['content_id'];
  57. }
  58. public function getContentType(): string
  59. {
  60. return $this->data['content_type'];
  61. }
  62. public function getContentTypeId(): int
  63. {
  64. return $this->data['content_type_id'];
  65. }
  66. public function getAdminSiteaccess(): string
  67. {
  68. return 'admin';
  69. }
  70. public function getContentTypeGroupId(): int
  71. {
  72. return $this->data['content_type_group_id'];
  73. }
  74. private function mapCollectedData(Request $request): void
  75. {
  76. $mapped = [];
  77. $data = $request->get('information_collection');
  78. $contentId = $data['content_id'];
  79. $contentTypeId = intval($data['content_type_id']);
  80. /** @var ContentType $contentType */
  81. $contentType = $this->repository->sudo(
  82. function(Repository $repository) use ($contentTypeId) {
  83. return $repository->getContentTypeService()->loadContentType($contentTypeId);
  84. }
  85. );
  86. $content = $this->repository->sudo(
  87. function(Repository $repository) use ($contentId) {
  88. return $repository->getContentService()->loadContent((int)$contentId);
  89. }
  90. );
  91. foreach ($data as $identifier => $datum) {
  92. if (is_array($datum) && array_key_exists('value', $datum)) {
  93. $fieldDefinition = $contentType->getFieldDefinition($identifier);
  94. if ($fieldDefinition === null) {
  95. continue;
  96. }
  97. $mapped['collections'][] = [
  98. 'identifier' => $identifier,
  99. 'value' => $datum['value'],
  100. 'name' => $this->translationHelper->getTranslatedByMethod($fieldDefinition, 'getName'),
  101. 'type' => $fieldDefinition->fieldTypeIdentifier,
  102. ];
  103. }
  104. }
  105. $mapped['content'] = $this->translationHelper->getTranslatedContentName($content);
  106. $mapped['content_id'] = $content->id;
  107. $mapped['content_type'] = $this->translationHelper->getTranslatedByMethod($contentType, 'getName');
  108. $mapped['content_type_id'] = $contentType->id;
  109. $mapped['content_type_group_id'] = $contentType->getContentTypeGroups()[0]->id;
  110. $mapped['count'] = count($mapped['collections']);
  111. $this->data = $mapped;
  112. }
  113. }