Skip to content

Commit

Permalink
[FEATURE] Introduce formatters for columns in content element
Browse files Browse the repository at this point in the history
Related: #10
  • Loading branch information
brotkrueml committed Nov 29, 2021
1 parent 050fd59 commit 30d3a99
Show file tree
Hide file tree
Showing 36 changed files with 901 additions and 717 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- Extensible formatters for table content in content element (#10)

## [0.14.0] - 2021-11-21

### Added
Expand Down
30 changes: 24 additions & 6 deletions Classes/DataProcessing/TableProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Brotkrueml\JobRouterData\DataProcessing;

use Brotkrueml\JobRouterData\Cache\Cache;
use Brotkrueml\JobRouterData\Domain\Converter\DatasetConverter;
use Brotkrueml\JobRouterData\Domain\Model\Table;
use Brotkrueml\JobRouterData\Domain\Repository\TableRepository;
use TYPO3\CMS\Core\Service\FlexFormService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Expand All @@ -23,6 +25,11 @@
*/
final class TableProcessor implements DataProcessorInterface
{
/**
* @var DatasetConverter
*/
private $datasetConverter;

/**
* @var FlexFormService
*/
Expand All @@ -33,8 +40,12 @@ final class TableProcessor implements DataProcessorInterface
*/
private $tableRepository;

public function __construct(FlexFormService $flexFormService = null, TableRepository $tableRepository = null)
{
public function __construct(
DatasetConverter $datasetConverter = null,
FlexFormService $flexFormService = null,
TableRepository $tableRepository = null
) {
$this->datasetConverter = $datasetConverter ?? GeneralUtility::makeInstance(DatasetConverter::class);
$this->flexFormService = $flexFormService ?? GeneralUtility::makeInstance(FlexFormService::class);
$this->tableRepository = $tableRepository ?? GeneralUtility::makeInstance(TableRepository::class);
}
Expand All @@ -43,17 +54,24 @@ public function __construct(FlexFormService $flexFormService = null, TableReposi
* @return mixed[]
*/
public function process(
ContentObjectRenderer $contentObjectRenderer,
ContentObjectRenderer $cObj,
array $contentObjectConfiguration,
array $processorConfiguration,
array $processedData
): array {
$flexForm = $this->flexFormService->convertFlexFormContentToArray($contentObjectRenderer->data['pi_flexform']);
$flexForm = $this->flexFormService->convertFlexFormContentToArray($cObj->data['pi_flexform']);

$tableUid = (int)($flexForm['table'] ?? 0);

if ($tableUid > 0) {
$processedData['table'] = $this->tableRepository->findByIdentifier($tableUid);
$locale = $cObj->getRequest()->getAttribute('language')->getHrefLang();
/** @var Table $table */
$table = $this->tableRepository->findByIdentifier($tableUid);
$processedData['table'] = $table;
$processedData['rows'] = $this->datasetConverter->convertFromJsonToArray(
$table->getColumns()->getArray(),
$table->getDatasets()->toArray(),
$locale
);
Cache::addCacheTagByTable($tableUid);
}

Expand Down
66 changes: 66 additions & 0 deletions Classes/Domain/Converter/DatasetConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

/*
* This file is part of the "jobrouter_data" extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

namespace Brotkrueml\JobRouterData\Domain\Converter;

use Brotkrueml\JobRouterData\Domain\Model\Column;
use Brotkrueml\JobRouterData\Domain\Model\Dataset;
use Brotkrueml\JobRouterData\Event\ModifyColumnContentEvent;
use Psr\EventDispatcher\EventDispatcherInterface;

/**
* @internal
*/
final class DatasetConverter
{
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;

public function __construct(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}

/**
* @param Column[] $columns
* @param Dataset[] $datasets
* @return list<array<string, float|int|string|null>>
*/
public function convertFromJsonToArray(array $columns, array $datasets, string $locale): array
{
$rows = [];
foreach ($datasets as $datasetJson) {
$datasetArray = \json_decode($datasetJson->getDataset(), true, 512, \JSON_THROW_ON_ERROR);

$row = [];
foreach ($columns as $column) {
if ($datasetArray[$column->getName()] ?? false) {
/** @var float|int|string|null $content */
$content = $datasetArray[$column->getName()];
if ($content === null || $content === '') {
$row[$column->getName()] = '';
continue;
}

$event = new ModifyColumnContentEvent($column, $content, $locale);
/** @var ModifyColumnContentEvent $event */
$event = $this->eventDispatcher->dispatch($event);
$row[$column->getName()] = $event->getContent();
}
}
$rows[] = $row;
}

return $rows;
}
}
40 changes: 0 additions & 40 deletions Classes/Domain/Model/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@

namespace Brotkrueml\JobRouterData\Domain\Model;

use Brotkrueml\JobRouterBase\Enumeration\FieldTypeEnumeration;
use Brotkrueml\JobRouterConnector\Domain\Model\Connection;
use Brotkrueml\JobRouterData\Domain\Model\Table\Cell;
use Brotkrueml\JobRouterData\Domain\Model\Table\Row;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;

Expand Down Expand Up @@ -215,43 +212,6 @@ public function setDatasets(ObjectStorage $datasets): void
$this->datasets = $datasets;
}

public function getCountRows(): int
{
return count($this->datasets);
}

/**
* @return Row[]
*/
public function getRows(): array
{
$rows = [];
foreach ($this->datasets as $dataset) {
$datasetArray = \json_decode($dataset->getDataset(), true, 512, \JSON_THROW_ON_ERROR);

$row = new Row();
foreach ($this->columns as $column) {
$cell = new Cell();
$cell->setName($column->getName());

if ($column->getName() === 'jrid') {
$cell->setContent($dataset->getJrid());
$cell->setType(FieldTypeEnumeration::INTEGER);
} else {
$cell->setContent($datasetArray[$column->getName()] ?? '');
$cell->setType($column->getType());
$cell->setDecimalPlaces($column->getDecimalPlaces());
}

$row->addCell($cell);
}

$rows[] = $row;
}

return $rows;
}

public function isDisabled(): bool
{
return $this->disabled;
Expand Down
75 changes: 0 additions & 75 deletions Classes/Domain/Model/Table/Cell.php

This file was deleted.

41 changes: 0 additions & 41 deletions Classes/Domain/Model/Table/Row.php

This file was deleted.

2 changes: 1 addition & 1 deletion Classes/Domain/Repository/TableRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function findAllByTypeWithHidden(int $type)
return $query->execute();
}

public function findByIdentifierWithHidden(int $identifier): object
public function findByIdentifierWithHidden(int $identifier): ?object
{
$query = $this->createQuery();
$query->getQuerySettings()->setIgnoreEnableFields(true);
Expand Down

0 comments on commit 30d3a99

Please sign in to comment.