Skip to content

Commit

Permalink
Add event to modify categories during destination.one import (#34)
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 14, 2023
1 parent de38d80 commit 27ee70d
Show file tree
Hide file tree
Showing 19 changed files with 626 additions and 23 deletions.
48 changes: 44 additions & 4 deletions Classes/Domain/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@

namespace Wrm\Events\Domain\Model;

use TYPO3\CMS\Extbase\Domain\Model\Category as ExtbaseCategory;
use TYPO3\CMS\Extbase\Annotation as Extbase;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
use TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy;

/**
* Extend original model to include furher properties.
*
* Used for Plugins and Import.
*/
class Category extends ExtbaseCategory
class Category extends AbstractEntity
{
/**
* @var string
*/
protected $title = '';

/**
* @var int
*/
Expand All @@ -21,13 +28,46 @@ class Category extends ExtbaseCategory
*/
protected $hidden = false;

/**
* @var Category|null
*
* @Extbase\ORM\Lazy
*/
protected $parent;

/**
* @param Category|null $parent
*/
public function __construct(
$parent,
int $pid,
string $title,
bool $hidden
) {
$this->parent = $parent;
$this->pid = $pid;
$this->title = $title;
$this->hidden = $hidden;
}

public function getTitle(): string
{
return $this->title;
}

public function getSorting(): int
{
return $this->sorting;
}

public function hide(): void
/**
* @return Category|null
*/
public function getParent()
{
$this->hidden = true;
if ($this->parent instanceof LazyLoadingProxy) {
$this->parent->_loadRealInstance();
}
return $this->parent;
}
}
2 changes: 1 addition & 1 deletion Classes/Domain/Repository/CategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
*/

use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Extbase\Domain\Model\Category;
use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper;
use TYPO3\CMS\Extbase\Persistence\Repository;
use Wrm\Events\Domain\Model\Category;

class CategoryRepository extends Repository
{
Expand Down
9 changes: 8 additions & 1 deletion Classes/Service/DestinationDataImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,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\Events\EventImportEvent;
use Wrm\Events\Service\DestinationDataImportService\FilesAssignment;
use Wrm\Events\Service\DestinationDataImportService\LocationAssignment;
Expand Down Expand Up @@ -310,7 +311,13 @@ private function setCategories(array $categories): void
$categories
));

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

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

private function setFeatures(array $features): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ public function getCategories(
);

if (!$category instanceof Category) {
$category = new Category();
$category->setParent($import->getParentCategory());
$category->setPid($import->getPid());
$category->setTitle($categoryTitle);
if ($import->getHideByDefault()) {
$category->hide();
}
$category = new Category(
$import->getParentCategory(),
$import->getPid(),
$categoryTitle,
$import->getHideByDefault() ? true : false
);
$this->repository->add($category);
}

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;
}
}
11 changes: 10 additions & 1 deletion Documentation/Changelog/3.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
Breaking
--------

Nothing
* We migrated away from Extbase Category model.
This is technically breaking, but we don't consider it breaking as this should be an internal detail.
Still this might break if you have type checks.

Features
--------
Expand All @@ -16,6 +18,8 @@ Features

Added Events:

* Allow to modify TYPO3 ``sys_categories`` before adding them to an event during import.

* Allow to modify an event object before importing.

* Add source to events.
Expand All @@ -38,6 +42,11 @@ Tasks
* Renaming different Destination cases to destination.one throughout documentation and text.
Code is left untouched in order to not break things.

* Migrate away from Extbase Category to custom Category.
The extension already provided its own Category model.
TYPO3 deprecated and will remove the default Models.
We consider this none breaking as this is considered to be internal API.

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

Expand Down
8 changes: 8 additions & 0 deletions Documentation/Features/PSR14Events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
PSR-14 Events
=============

.. index:: single: PSR-14 Events; destination.one Import: Categories Assign

destination.one Import: ``CategoriesAssignEvent``
-------------------------------------------------

Executed during destination.one Import.
Allows to alter the categories to assign to an event.

.. index:: single: PSR-14 Events; destination.one Import: Event Import

destination.one Import: ``EventImportEvent``
Expand Down
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 27ee70d

Please sign in to comment.