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

feat!: add support for multiple file uploads and update code base #5

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
46 changes: 0 additions & 46 deletions .build/phpcs.xml

This file was deleted.

6 changes: 3 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: remindgmbh/[email protected]
phpcs:
static-analysis:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: 8.2
php-version: 8.3
extensions: intl
tools: composer:v2
- run: composer install
- run: composer run-script phpcs
- run: composer run-script static-analysis
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: remindgmbh/[email protected].0
- uses: remindgmbh/[email protected].1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
21 changes: 2 additions & 19 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,19 +1,2 @@
### IDE ###
nbproject/
.vscode/
.idea/

### Composer ###
composer.phar

### General ###
*.log
cache.properties

### CI ###
.build/bin/
.build/logs/
.build/var/
.build/vendor/
.build/web/
coverage.xml
/vendor
/public
8 changes: 8 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"recommendations": [
"benjaminkott.typo3-typoscript",
"devsense.phptools-vscode",
"obliviousharmony.vscode-php-codesniffer"
],
"unwantedRecommendations": []
}
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"phpCodeSniffer.exec.linux": "./vendor/bin/phpcs",
"phpCodeSniffer.standard": "Custom",
"phpCodeSniffer.standardCustom": "./phpcs.xml",
"phpCodeSniffer.exclude": []
}
10 changes: 10 additions & 0 deletions Classes/Backend/ItemsProc.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

class ItemsProc
{
/**
* @param mixed[] $params
*/
public function getFormIdentifiers(array &$params): void
{
$formPersistenceManager = $this->getFormPersistenceManager();
Expand All @@ -29,6 +32,9 @@ public function getFormIdentifiers(array &$params): void
$this->getInvalidItems($selectedValues, $params['items']);
}

/**
* @param mixed[] $params
*/
public function getFormElements(array &$params): void
{
$formIdentifier = $params['row']['form_identifier'][0] ?? null;
Expand Down Expand Up @@ -62,6 +68,10 @@ private function getFormPersistenceManager(): FormPersistenceManagerInterface
);
}

/**
* @param mixed[] $selectedValues
* @param mixed[] $items
*/
private function getInvalidItems(array $selectedValues, array &$items): void
{
$availableValues = array_map(function (array $item) {
Expand Down
6 changes: 3 additions & 3 deletions Classes/Command/DeleteLogCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DeleteLogCommand extends Command
private const ARGUMENT_DAYS = 'days';
private const ARGUMENT_STORAGE_PAGES = 'storage-pages';
private const OPTION_SOFT_DELETE = 'soft-delete';
private DataMapper $dataMapper;

private string $tableName;

public function __construct(
Expand All @@ -36,7 +36,7 @@ public function __construct(
/**
* Configure the command by defining the name, options and arguments
*/
protected function configure()
protected function configure(): void
{
$this
->setDescription('Delete log entries saved in database after specififed amount of days.')
Expand Down Expand Up @@ -98,7 +98,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ($softDelete) {
$this->logEntryRepository->remove($entry);
} else {
$this->dataHandler->deleteRecord($this->tableName, $entry->getUid(), true, true);
$this->dataHandler->deleteRecord($this->tableName, (int) $entry->getUid(), true, true);
}
}

Expand Down
24 changes: 16 additions & 8 deletions Classes/Controller/LogModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Remind\FormLog\Domain\Repository\ConfigurationRepository;
use Remind\FormLog\Domain\Repository\LogEntryRepository;
use Remind\FormLog\Utility\FormUtility;
use RuntimeException;
use TYPO3\CMS\Backend\Template\ModuleTemplateFactory;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Localization\LanguageService;
Expand Down Expand Up @@ -47,9 +48,9 @@ public function listAction(?string $formIdentifier = null, int $currentPage = 1)

$formIdentifiers = array_map(function (string $formIdentifier) use ($currentFormIdentifier) {
return [
'name' => $formIdentifier,
'link' => $this->uriBuilder->uriFor(null, ['formIdentifier' => $formIdentifier]),
'active' => $currentFormIdentifier === $formIdentifier,
'link' => $this->uriBuilder->uriFor(null, ['formIdentifier' => $formIdentifier]),
'name' => $formIdentifier,
];
}, $formIdentifiers);

Expand Down Expand Up @@ -100,6 +101,10 @@ public function downloadCsvAction(string $formIdentifier): ResponseInterface

$resource = fopen('php://temp', 'w+');

if (!$resource) {
throw new RuntimeException('Could not open temporary file handle');
}

foreach ($queryResult as $entry) {
$json = json_decode($entry->getFormData(), true);
fputcsv($resource, $json);
Expand All @@ -115,6 +120,10 @@ public function downloadCsvAction(string $formIdentifier): ResponseInterface
->withHeader('Content-Disposition', 'attachment;filename="' . $filename . '.csv"');
}

/**
* @param mixed[] $elements
* @return mixed[]
*/
private function formatLogEntry(LogEntry $logEntry, array $elements): array
{
$entry = [];
Expand All @@ -124,11 +133,10 @@ private function formatLogEntry(LogEntry $logEntry, array $elements): array
$additionalData = json_decode($logEntry->getAdditionalData(), true) ?? [];

foreach ($formData as $key => $value) {
if (in_array($elements[$key]['type'], self::ELEMENTS_WITH_OPTIONS)) {
$entry['formData'][$key] = $elements[$key]['properties']['options'][$value];
} else {
$entry['formData'][$key] = $value;
}
$entry['formData'][$key] =
isset($elements[$key]) &&
in_array($elements[$key]['type'], self::ELEMENTS_WITH_OPTIONS)
? $elements[$key]['properties']['options'][$value] ?? null : $value;
}

foreach ($additionalData as $key => $value) {
Expand Down Expand Up @@ -159,7 +167,7 @@ private function getAvailableFormIdentifiers(): array
->where(
$queryBuilder->expr()->eq('pid', $storagePid)
);
$queryResult = $queryBuilder->execute();
$queryResult = $queryBuilder->executeQuery();
return $queryResult->fetchFirstColumn();
}

Expand Down
41 changes: 33 additions & 8 deletions Classes/Domain/Finishers/LogFinisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Remind\FormLog\Domain\Finishers;

use InvalidArgumentException;
use Remind\FormLog\Domain\Model\LogEntry;
use Remind\FormLog\Domain\Repository\LogEntryRepository;
use Remind\FormLog\Event\ModifyLogEntryEvent;
Expand All @@ -22,6 +23,11 @@ class LogFinisher extends AbstractFinisher
'Honeypot',
'StaticText',
];

/**
* @var mixed[]
*/
// phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint
protected $defaultOptions = [
'storagePid' => 0,
];
Expand All @@ -32,6 +38,11 @@ public function __construct(
) {
}


/**
* @return string|null
*/
// phpcs:ignore SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint
protected function executeInternal()
{
$formRuntime = $this->finisherContext->getFormRuntime();
Expand All @@ -45,42 +56,56 @@ protected function executeInternal()
$element = $formDefinition->getElementByIdentifier($identifier);

// Process element if type is not excluded
if ($element instanceof GenericFormElement && !in_array($element->getType(), self::EXCLUDED_TYPES)) {
if (
$element instanceof GenericFormElement &&
!in_array($element->getType(), self::EXCLUDED_TYPES)
) {
// Build form data
$formData[$element->getIdentifier()] = $elementValue ?? null;
}

// Process attachments
if (
$element instanceof FileUpload &&
$elementValue instanceof FileReference
$element instanceof FileUpload
) {
// Process file path from FileReference
$filePath = $elementValue->getOriginalResource()->getOriginalFile()->getPublicUrl();
$formData['attachments'][] = $filePath;
$files = is_array($elementValue) ? $elementValue : [$elementValue];

foreach ($files as $file) {
if (!$file instanceof FileReference) {
// Process file path from FileReference
$filePath = $elementValue->getOriginalResource()->getOriginalFile()->getPublicUrl();
$formData[$element->getIdentifier()][] = $filePath;
}
}
}
}

// Get storage page from finisher option
$storagePid = (int) $this->parseOption('storagePid');

if ($storagePid < 0) {
throw new InvalidArgumentException('Invalid storagePid');
}

$formIdentifier = $formDefinition->getRenderingOptions()['_originalIdentifier'];

$logEntry = new LogEntry();

$logEntry->setPid($storagePid);
$logEntry->setFormIdentifier($formIdentifier);
$logEntry->setFormData(json_encode($formData));
$logEntry->setFormData(json_encode($formData) ?: '');

/** @var ModifyLogEntryEvent $event */
$event = $this->eventDispatcher->dispatch(new ModifyLogEntryEvent($logEntry, $this->finisherContext));

$additionalData = $event->getAdditionalData();

if (!empty($additionalData)) {
$logEntry->setAdditionalData(json_encode($additionalData));
$logEntry->setAdditionalData(json_encode($additionalData) ?: 'false');
}

$this->logEntryRepository->add($logEntry);

return null;
}
}
2 changes: 2 additions & 0 deletions Classes/Domain/Model/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
class Configuration extends AbstractEntity
{
protected string $formIdentifier = '';

protected string $headerElements = '';

protected int $itemsPerPage = 25;

public function getFormIdentifier(): string
Expand Down
3 changes: 3 additions & 0 deletions Classes/Domain/Model/LogEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
class LogEntry extends AbstractEntity
{
protected string $formIdentifier = '';

protected string $formData = '';

protected string $additionalData = '';

protected ?int $crdate = null;

public function getFormIdentifier(): string
Expand Down
3 changes: 3 additions & 0 deletions Classes/Domain/Repository/ConfigurationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
use TYPO3\CMS\Extbase\Persistence\Repository;

/**
* @extends Repository<Configuration>
*/
class ConfigurationRepository extends Repository
{
public function findByFormIdentifier(string $formIdentifier): ?Configuration
Expand Down
Loading