Skip to content

Commit

Permalink
Merge pull request #32 from netlogix/feature/improve-typo3-v12-support
Browse files Browse the repository at this point in the history
  • Loading branch information
saschanowak authored Oct 22, 2024
2 parents 92da947 + c781647 commit bac98ea
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 43 deletions.
5 changes: 1 addition & 4 deletions Classes/Command/BatchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface;

#[AsCommand(
name: 'nxgooglelocations:runscheduledjobs',
description: 'Run batch processing for Google Locations'
)]
#[AsCommand(name: 'nxgooglelocations:runscheduledjobs', description: 'Run batch processing for Google Locations')]
class BatchCommand extends Command
{
public function __construct(
Expand Down
41 changes: 27 additions & 14 deletions Classes/Controller/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
use Netlogix\Nxgooglelocations\Domain\Model\Batch;
use Netlogix\Nxgooglelocations\Domain\Repository\BatchRepository;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use SJBR\StaticInfoTables\Domain\Model\Country;
use SJBR\StaticInfoTables\Domain\Repository\CountryRepository;
use TYPO3\CMS\Backend\Attribute\Controller;
use TYPO3\CMS\Backend\Attribute\AsController;
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
use TYPO3\CMS\Backend\Template\ModuleTemplate;
use TYPO3\CMS\Backend\Template\ModuleTemplateFactory;
Expand All @@ -20,13 +21,13 @@
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Messaging\AbstractMessage;
use TYPO3\CMS\Core\Type\Bitmask\Permission;
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;

#[Controller]
#[AsController]
abstract class ModuleController extends ActionController
{
protected array $pageRecord = [];
Expand All @@ -45,11 +46,12 @@ protected function initializeAction(): void
{
parent::initializeAction();

$id = (int)($this->request->getQueryParams()['id'] ?? 0);
$id = (int) ($this->request->getQueryParams()['id'] ?? 0);

$this->pageRecord = BackendUtility::readPageAccess(
$id,
$this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW)
$this->getBackendUser()
->getPagePermsClause(Permission::PAGE_SHOW)
) ?: [];

$this->moduleTemplate = $this->moduleTemplateFactory->create($this->request);
Expand All @@ -62,9 +64,8 @@ protected function initializeAction(): void
$refreshButton = $buttonBar->makeLinkButton()
->setHref($this->request->getUri())
->setTitle(
$this->getLanguageService()->sL(
'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.reload'
)
$this->getLanguageService()
->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.reload')
)
->setIcon($this->iconFactory->getIcon('actions-refresh', Icon::SIZE_SMALL));

Expand All @@ -74,6 +75,7 @@ protected function initializeAction(): void
protected function getModuleTemplateResponse(): ResponseInterface
{
$this->moduleTemplate->setContent($this->view->render());

return $this->htmlResponse($this->moduleTemplate->renderContent());
}

Expand Down Expand Up @@ -117,19 +119,28 @@ public function indexAction(int $id): ResponseInterface
return $this->getModuleTemplateResponse();
}

public function importAction(int $id, bool $deleteUnused, bool $cancelPrevious, Country $country = null): ResponseInterface
{
public function importAction(
int $id,
bool $deleteUnused,
bool $cancelPrevious,
Country $country = null
): ResponseInterface {
$file = $this->request->getUploadedFiles()['excelFile'] ?? null;
if ($file === null) {
throw new \RuntimeException('Uploading file failed.', 1702385736);
throw new RuntimeException('Uploading file failed.', 1702385736);
}

$batch = $this->mapRequestToBatch($id, $file, $deleteUnused, $cancelPrevious, $country);

try {
$batch->validate();
} catch (Exception $exception) {
$this->addFlashMessage($exception->getMessage(), '', \TYPO3\CMS\Core\Type\ContextualFeedbackSeverity::ERROR);
$this->addFlashMessage(
$exception->getMessage(),
'',
ContextualFeedbackSeverity::ERROR
);

return $this->redirect('index');
}

Expand All @@ -154,10 +165,11 @@ public function importAction(int $id, bool $deleteUnused, bool $cancelPrevious,
$this->addFlashMessage(
LocalizationUtility::translate('module.flash-messages.new-job-scheduled.content', $extensionName)
);

return $this->redirect('index');
}

public function exportAction(): ResponseInterface
public function exportAction(int $id): ResponseInterface
{
return $this->htmlResponse();
}
Expand All @@ -174,8 +186,9 @@ protected function forwardToErrorWithCannedMessage(string $reason): ResponseInte
$this->addFlashMessage(
LocalizationUtility::translate(sprintf('module.flash-messages.%s.content', $reason), $extensionName),
LocalizationUtility::translate(sprintf('module.flash-messages.%s.title', $reason), $extensionName),
\TYPO3\CMS\Core\Type\ContextualFeedbackSeverity::ERROR
ContextualFeedbackSeverity::ERROR
);

return $this->redirect('error');
}

Expand Down
20 changes: 17 additions & 3 deletions Classes/Domain/Model/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,26 +158,37 @@ protected function collectTcaRecords(): array
return $tcaRecords;
}

protected function executeDataHandler(array $tcaRecords): void
protected function executeDataHandler(array $tcaRecords): array
{
$backendUserId = $this->backendUserId;
$storagePageId = $this->storagePageId;
$deleteUnused = $this->deleteUnused;

$recordUids = [];

$importer = $this->getImporter();

$recordTableName = $this->getLocationFactory()
->getRecordTableName();

$this->impersonator->runAsBackendUser(
$backendUserId,
static function () use ($importer, $recordTableName, $tcaRecords, $storagePageId, $deleteUnused): void {
static function () use (
$importer,
$recordTableName,
$tcaRecords,
$storagePageId,
$deleteUnused,
&$recordUids
): void {
$recordUids = $importer->import($recordTableName, $storagePageId, $tcaRecords);
if ($deleteUnused) {
$importer->removeRecordsExcept($recordTableName, $storagePageId, $recordUids);
}
}
);

return $recordUids;
}

protected function mapTcaRecord($tcaRecord): array
Expand Down Expand Up @@ -249,7 +260,10 @@ protected function initializeServices(): void
}

if (!$this->importer instanceof \Netlogix\Nxgooglelocations\Service\Importer) {
$this->importer = GeneralUtility::makeInstance($this->serviceClasses[Importer::class], $this->storagePageId);
$this->importer = GeneralUtility::makeInstance(
$this->serviceClasses[Importer::class],
$this->storagePageId
);
}

if (!$this->locationFactory instanceof \Netlogix\Nxgooglelocations\Service\LocationFactory) {
Expand Down
10 changes: 5 additions & 5 deletions Classes/Domain/Model/CodingResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Netlogix\Nxgooglelocations\Domain\Model;

use Netlogix\Nxgooglelocations\Enumerations\CodingResultProbability;
use Netlogix\Nxgooglelocations\Service\GeoCoderStatus;
use Netlogix\Nxgooglelocations\Enumerations\GeoCoderStatus;
use TYPO3\CMS\Extbase\Property\Exception\InvalidSourceException;
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;

Expand Down Expand Up @@ -36,13 +36,13 @@ public function __get($propertyName)
{
return match ($propertyName) {
'rawData' => $this->rawData,
'status' => (string)ObjectAccess::getPropertyPath($this->rawData, 'status'),
'formattedAddress', 'addressResultFromGeocoding' => (string)ObjectAccess::getPropertyPath(
'status' => (string) ObjectAccess::getPropertyPath($this->rawData, 'status'),
'formattedAddress', 'addressResultFromGeocoding' => (string) ObjectAccess::getPropertyPath(
$this->rawData,
'results.0.formatted_address'
),
'latitude' => (float)ObjectAccess::getPropertyPath($this->rawData, 'results.0.geometry.location.lat'),
'longitude' => (float)ObjectAccess::getPropertyPath($this->rawData, 'results.0.geometry.location.lng'),
'latitude' => (float) ObjectAccess::getPropertyPath($this->rawData, 'results.0.geometry.location.lat'),
'longitude' => (float) ObjectAccess::getPropertyPath($this->rawData, 'results.0.geometry.location.lng'),
'position' => [
'latitude' => $this->latitude,
'longitude' => $this->longitude,
Expand Down
2 changes: 1 addition & 1 deletion Classes/Domain/Model/FieldMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class FieldMap
'probability' => 'tx_nxgooglelocations_probability',
];

public function __get($propertyName): string
public function __get($propertyName): ?string
{
return $this->fieldMap[$propertyName];
}
Expand Down
8 changes: 5 additions & 3 deletions Classes/Domain/Repository/BatchRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Netlogix\Nxgooglelocations\Domain\Repository;

use Netlogix\Nxgooglelocations\Enumerations\BatchState;
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
use Netlogix\Nxgooglelocations\Domain\Model\Batch;
use Netlogix\Nxgooglelocations\Enumerations\BatchState;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface;
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Extbase\Persistence\Repository;

Expand Down Expand Up @@ -39,6 +39,8 @@ public function findOpenInFolder(int $storagePageId): QueryResultInterface

public function findOneByState(string $state): ?Batch
{
return $this->findOneBy(['state' => $state]);
return $this->findOneBy([
'state' => $state,
]);
}
}
5 changes: 5 additions & 0 deletions Classes/Enumerations/BatchState.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ final class BatchState extends Enumeration
*/
public const PERSISTING = 'persisting';

/**
* @var string
*/
public const ERROR = 'error';

/**
* @var string
*/
Expand Down
9 changes: 5 additions & 4 deletions Classes/Service/GeoCoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

abstract class GeoCoder
{

final public const FETCH_URL = 'https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=%s';

protected FieldMap $fieldMap;
Expand All @@ -35,12 +34,13 @@ public function __construct(

public function getGeoCodingAddress(array $tcaRecord): string
{
return $tcaRecord[$this->fieldMap->addressToGeocode] ?: $tcaRecord[$this->fieldMap->addressToDisplay];
return $tcaRecord[$this->fieldMap->addressToGeocode] ?? $tcaRecord[$this->fieldMap->addressToDisplay] ?? '';
}

public function needsToBeGeoCoded(array $tcaRecord): bool
{
return (!$tcaRecord[$this->fieldMap->latitude] && !$tcaRecord[$this->fieldMap->longitude]) || ($tcaRecord[$this->fieldMap->probability] > $this->probabilityThreshold);
return (!($tcaRecord[$this->fieldMap->latitude] ?? false) && !($tcaRecord[$this->fieldMap->longitude] ?? false))
|| (($tcaRecord[$this->fieldMap->probability] ?? PHP_INT_MAX) > $this->probabilityThreshold);
}

public function setProbabilityToManually(array $tcaRecord): array
Expand All @@ -58,13 +58,14 @@ public function fetchCoordinatesForAddress($address): CodingResult
$urlWithApiKey = sprintf(self::FETCH_URL, urlencode((string) $address), urlencode($this->apiKey));
$geocode = json_decode((string) GeneralUtility::getUrl($urlWithApiKey), true, 512, JSON_THROW_ON_ERROR);
$status = ObjectAccess::getPropertyPath($geocode, 'status');

return match ($status) {
GeoCoderStatus::OK, GeoCoderStatus::ZERO_RESULTS => new CodingResult($geocode),
default => throw new Exception(
'An error occurred: ' . json_encode(
array_filter(
[$status, ObjectAccess::getPropertyPath($geocode, 'error_message')],
static fn($value): bool => (bool)$value
static fn ($value): bool => (bool) $value
),
JSON_THROW_ON_ERROR
)
Expand Down
4 changes: 3 additions & 1 deletion Classes/Service/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public function import(string $recordTableName, int $storagePageId, array $tcaRe
$count = 0;
foreach ($tcaRecords as $tcaRecord) {
++$count;
$uid = array_key_exists('uid', $tcaRecord) ? $tcaRecord['uid'] : sprintf('NEW%s', substr(md5(self::class . $count), 0, 10));
$uid = array_key_exists('uid', $tcaRecord)
? $tcaRecord['uid']
: sprintf('NEW%s', substr(md5(self::class . $count), 0, 10));
$data[$recordTableName][$uid] = $tcaRecord;
$data[$recordTableName][$uid]['pid'] = $storagePageId;
$data[$recordTableName][$uid]['sys_language_uid'] = -1;
Expand Down
23 changes: 16 additions & 7 deletions Classes/ViewHelpers/Be/TableListViewHelper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Netlogix\Nxgooglelocations\ViewHelpers\Be;

use Psr\Http\Message\ServerRequestInterface;
Expand Down Expand Up @@ -135,21 +137,25 @@ protected function render(): string
$table = $request->getParsedBody()['table'] ?? $request->getQueryParams()['table'] ?? '';
$preventPointer = $tableName !== $table;

$this->getPageRenderer()->loadJavaScriptModule('@typo3/backend/recordlist.js');
$this->getPageRenderer()
->loadJavaScriptModule('@typo3/backend/recordlist.js');
// Removed to disable the download button
// $this->getPageRenderer()->loadJavaScriptModule('@typo3/backend/record-download-button.js');
$this->getPageRenderer()->loadJavaScriptModule('@typo3/backend/action-dispatcher.js');
$this->getPageRenderer()
->loadJavaScriptModule('@typo3/backend/action-dispatcher.js');
if ($enableControlPanels === true) {
$this->getPageRenderer()->loadJavaScriptModule('@typo3/backend/multi-record-selection.js');
$this->getPageRenderer()->loadJavaScriptModule('@typo3/backend/context-menu.js');
$this->getPageRenderer()
->loadJavaScriptModule('@typo3/backend/multi-record-selection.js');
$this->getPageRenderer()
->loadJavaScriptModule('@typo3/backend/context-menu.js');
}

$pageId = (int)($request->getParsedBody()['id'] ?? $request->getQueryParams()['id'] ?? 0);
$pageId = (int) ($request->getParsedBody()['id'] ?? $request->getQueryParams()['id'] ?? 0);

// Added to fix the issue with the table pointer
$pointer = $preventPointer
? 0
: (int)($request->getParsedBody()['pointer'] ?? $request->getQueryParams()['pointer'] ?? 0);
: (int) ($request->getParsedBody()['pointer'] ?? $request->getQueryParams()['pointer'] ?? 0);
$pageInfo = BackendUtility::readPageAccess(
$pageId,
$backendUser->getPagePermsClause(Permission::PAGE_SHOW)
Expand Down Expand Up @@ -183,10 +189,13 @@ protected function render(): string
$dbList->start($storagePid, $tableName, $pointer, $filter, $levels, $recordsPerPage);
// Column selector is disabled since fields are defined by the "fieldList" argument
$dbList->displayColumnSelector = false;
$dbList->setFields = [$tableName => $fieldList];
$dbList->setFields = [
$tableName => $fieldList,
];
$dbList->noControlPanels = !$enableControlPanels;
$dbList->sortField = $sortField;
$dbList->sortRev = $sortDescending;

return $dbList->generateList();
}

Expand Down
4 changes: 3 additions & 1 deletion Configuration/Icons.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

declare(strict_types=1);

use TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider;

return [
'ext-nxgooglelocations-batch-type-default'=> [
'ext-nxgooglelocations-batch-type-default' => [
'provider' => SvgIconProvider::class,
'source' => 'EXT:nxgooglelocations/Resources/Public/Icons/tx_nxgooglelocations_domain_model_batch.svg',
],
Expand Down

0 comments on commit bac98ea

Please sign in to comment.