Skip to content

Commit

Permalink
Add event to modify categories during destination data one import
Browse files Browse the repository at this point in the history
A new PSR-14 event is added that allows to modify the categories to be
assigned to an event.
The event itself (including already existing categories) as well as the
list of categories to be used after import are available.
It is possible to change the categories to be assigned, e.g. keep some
of the existing categories.

That way it is possible for installations to add custom categories to
events.

Relates: #10623
  • Loading branch information
DanielSiepmann committed Aug 10, 2023
1 parent 00946af commit 2e1d313
Show file tree
Hide file tree
Showing 14 changed files with 549 additions and 5 deletions.
19 changes: 17 additions & 2 deletions Classes/Service/DestinationDataImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Wrm\Events\Service;

use Exception;
use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
use TYPO3\CMS\Core\Log\Logger;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Expand All @@ -22,6 +23,7 @@
use Wrm\Events\Service\DestinationDataImportService\CategoriesAssignment\Import as CategoryImport;
use Wrm\Events\Service\DestinationDataImportService\DataFetcher;
use Wrm\Events\Service\DestinationDataImportService\DatesFactory;
use Wrm\Events\Service\DestinationDataImportService\Events\CategoriesAssignEvent;
use Wrm\Events\Service\DestinationDataImportService\FilesAssignment;
use Wrm\Events\Service\DestinationDataImportService\LocationAssignment;
use Wrm\Events\Service\DestinationDataImportService\Slugger;
Expand Down Expand Up @@ -108,6 +110,11 @@ class DestinationDataImportService
*/
private $cacheManager;

/**
* @var EventDispatcher
*/
private $eventDispatcher;

/**
* ImportService constructor.
*
Expand All @@ -123,6 +130,7 @@ class DestinationDataImportService
* @param LocationAssignment $locationAssignment
* @param Slugger $slugger
* @param CacheManager $cacheManager
* @param EventDispatcher $eventDispatcher
*/
public function __construct(
EventRepository $eventRepository,
Expand All @@ -137,7 +145,8 @@ public function __construct(
CategoriesAssignment $categoriesAssignment,
LocationAssignment $locationAssignment,
Slugger $slugger,
CacheManager $cacheManager
CacheManager $cacheManager,
EventDispatcher $eventDispatcher
) {
$this->eventRepository = $eventRepository;
$this->organizerRepository = $organizerRepository;
Expand All @@ -152,6 +161,7 @@ public function __construct(
$this->locationAssignment = $locationAssignment;
$this->slugger = $slugger;
$this->cacheManager = $cacheManager;
$this->eventDispatcher = $eventDispatcher;
}

public function import(
Expand Down Expand Up @@ -290,7 +300,12 @@ private function setCategories(array $categories): void
$categories
));

$this->tmpCurrentEvent->setCategories($categories);
$event = $this->eventDispatcher->dispatch(new CategoriesAssignEvent(
$this->tmpCurrentEvent,
$categories
));

$this->tmpCurrentEvent->setCategories($event->getCategories());

Check failure on line 308 in Classes/Service/DestinationDataImportService.php

View workflow job for this annotation

GitHub Actions / code-quality (7.2, ^10.4)

Call to an undefined method object::getCategories().

Check failure on line 308 in Classes/Service/DestinationDataImportService.php

View workflow job for this annotation

GitHub Actions / code-quality (7.3, ^10.4)

Call to an undefined method object::getCategories().

Check failure on line 308 in Classes/Service/DestinationDataImportService.php

View workflow job for this annotation

GitHub Actions / code-quality (7.4, ^10.4)

Call to an undefined method object::getCategories().

Check failure on line 308 in Classes/Service/DestinationDataImportService.php

View workflow job for this annotation

GitHub Actions / code-quality (7.4, ^11.5)

Call to an undefined method object::getCategories().

Check failure on line 308 in Classes/Service/DestinationDataImportService.php

View workflow job for this annotation

GitHub Actions / code-quality (8.0, ^11.5)

Call to an undefined method object::getCategories().

Check failure on line 308 in Classes/Service/DestinationDataImportService.php

View workflow job for this annotation

GitHub Actions / code-quality (8.1, ^11.5)

Call to an undefined method object::getCategories().

Check failure on line 308 in Classes/Service/DestinationDataImportService.php

View workflow job for this annotation

GitHub Actions / code-quality (8.2, ^11.5)

Call to an undefined method object::getCategories().
}

private function setFeatures(array $features): void
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

/*
* Copyright (C) 2023 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

namespace Wrm\Events\Service\DestinationDataImportService\Events;

use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use Wrm\Events\Domain\Model\Category;
use Wrm\Events\Domain\Model\Event;

final class CategoriesAssignEvent
{
/**
* @var Event
*/
private $event;

/**
* @var ObjectStorage<Category>
*/
private $categories;

/**
* @param ObjectStorage<Category> $categories
*/
public function __construct(
Event $event,
ObjectStorage $categories
) {
$this->event = $event;
$this->setCategories($categories);
}

public function getEvent(): Event
{
return clone $this->event;
}

/**
* @return ObjectStorage<Category>
*/
public function getCategories(): ObjectStorage
{
return clone $this->categories;
}

/**
* @param ObjectStorage<Category> $categories
*/
public function setCategories(ObjectStorage $categories): void
{
$this->categories = $categories;
}
}
2 changes: 1 addition & 1 deletion Classes/Updates/MigrateDuplicateLocations.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private function getMatchingLocation(
return 0;
}

return (int) $uid;
return (int)$uid;
}

private function buildObject(array $location): Location
Expand Down
31 changes: 31 additions & 0 deletions Documentation/Changelog/3.5.0.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
3.5.0
=====

Breaking
--------

Nothing

Features
--------

* Add PSR-14 Events to Destination Data One import.
The following PSR-14 events were added.
They allow individual installation to alter the import.

See :ref:`psr14` for an overview of PSR-14 Events.

Fixes
-----

Nothing

Tasks
-----

Nothing

Deprecation
-----------

Nothing
13 changes: 13 additions & 0 deletions Documentation/Features/PSR14Events.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.. index:: single: PSR-14 Events
.. _psr14:

PSR-14 Events
=============

.. index:: single: PSR-14 Events; Destination Data One Import: Categories Assign

Destination Data One Import: ``CategoriesAssignEvent``
------------------------------------------------------

Executed during Destination Data One Import.
Allows to alter the categories to assign to an event.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

return [
'tx_events_domain_model_import' => [
[
'uid' => '1',
'pid' => '2',
'title' => 'Example import configuration',
'storage_pid' => 2,
'files_folder' => '1:/staedte/beispielstadt/events/',
'region' => '1',
'rest_experience' => 'beispielstadt',
'categories_pid' => 2,
'category_parent' => 2,
],
],
'sys_category' => [
[
'uid' => 1,
'pid' => 2,
'parent' => 0,
'title' => 'Events Root',
],
[
'uid' => 2,
'pid' => 2,
'parent' => 1,
'title' => 'Events Categories',
],
[
'uid' => 3,
'pid' => 2,
'parent' => 1,
'title' => 'Custom Parent',
],
[
'uid' => 4,
'pid' => 2,
'parent' => 3,
'title' => 'Custom Category',
],
[
'uid' => 5,
'pid' => 2,
'parent' => 2,
'title' => 'Konzerte, Festivals, Show & Tanz',
],
[
'uid' => 6,
'pid' => 2,
'parent' => 2,
'title' => 'Weihnachten',
],
],
'tx_events_domain_model_event' => [
[
'uid' => 1,
'pid' => 2,
'title' => 'Event for categories event',
'global_id' => 'e_100350503',
'categories' => 3,
],
],
'sys_category_record_mm' => [
[
'uid_local' => 4,
'uid_foreign' => 1,
'tablenames' => 'tx_events_domain_model_event',
'fieldname' => 'categories',
'sorting' => 0,
'sorting_foreign' => 3,
],
[
'uid_local' => 5,
'uid_foreign' => 1,
'tablenames' => 'tx_events_domain_model_event',
'fieldname' => 'categories',
'sorting' => 0,
'sorting_foreign' => 1,
],
[
'uid_local' => 6,
'uid_foreign' => 1,
'tablenames' => 'tx_events_domain_model_event',
'fieldname' => 'categories',
'sorting' => 0,
'sorting_foreign' => 2,
],
],
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

/*
* Copyright (C) 2023 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/

namespace Wrm\Events\Tests\Functional\Psr14Events\DestinationDataImport;

use GuzzleHttp\Psr7\Response;
use Wrm\Events\Tests\Functional\Import\DestinationDataTest\AbstractTest;

final class CategoriesAssignEventTest extends AbstractTest
{
protected function setUp(): void
{
$this->testExtensionsToLoad[] = 'typo3conf/ext/events/Tests/Functional/Psr14Events/DestinationDataImport/Fixtures/Extensions/custom_categories/';

parent::setUp();

$this->setUpConfiguration([
'restUrl = https://example.com/some-path/',
'license = example-license',
'restType = Event',
'restLimit = 3',
'restMode = next_months,12',
'restTemplate = ET2014A.json',
]);
}

/**
* @test
*/
public function registeredEventHandlerCanKeepCustomCategoriesAssigned(): void
{
$this->importPHPDataSet(__DIR__ . '/Fixtures/Database/RegisteredEventHandlerCanKeepCustomCategoriesAssigned.php');
$this->setUpResponses([new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/Responses/RegisteredEventHandlerCanKeepCustomCategoriesAssigned.json') ?: '')]);

$this->executeCommand();

$this->assertPHPDataSet(__DIR__ . '/Assertions/RegisteredEventHandlerCanKeepCustomCategoriesAssigned.php');
}
}
Loading

0 comments on commit 2e1d313

Please sign in to comment.