Skip to content

Commit

Permalink
Merge pull request #17 from kitzberger/typo3-11
Browse files Browse the repository at this point in the history
TYPO3 11 + 12
  • Loading branch information
georgringer authored Jan 28, 2024
2 parents 5d4135a + e2d7514 commit 1af4440
Show file tree
Hide file tree
Showing 16 changed files with 178 additions and 96 deletions.
2 changes: 1 addition & 1 deletion Classes/Domain/Model/Dto/Demand.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace GeorgRinger\NewsFilter\Domain\Model\Dto;
Expand All @@ -7,7 +8,6 @@

class Demand extends NewsDemand
{

/** @var string */
protected string $fromDate = '';

Expand Down
4 changes: 1 addition & 3 deletions Classes/Domain/Model/Dto/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

class Search extends NewsSearch
{

/** @var string */
protected $fromDate = '';

Expand Down Expand Up @@ -82,5 +81,4 @@ public function setFilteredCategories(array $filteredCategories)
{
$this->filteredCategories = $filteredCategories;
}

}
}
13 changes: 6 additions & 7 deletions Classes/Domain/Repository/CategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace GeorgRinger\NewsFilter\Domain\Repository;

class CategoryRepository extends \GeorgRinger\News\Domain\Repository\CategoryRepository {


class CategoryRepository extends \GeorgRinger\News\Domain\Repository\CategoryRepository
{
/**
* Find categories by a given pid
*
Expand Down Expand Up @@ -35,8 +34,8 @@ public function findByIdListWithLanguageSupport(array $idList, array $ordering =

return $query->matching(
$query->logicalAnd(
$conditions
))->execute();
...$conditions
)
)->execute();
}

}
}
64 changes: 39 additions & 25 deletions Classes/EventListener/NewsListActionEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,37 @@

namespace GeorgRinger\NewsFilter\EventListener;

use GeorgRinger\News\Domain\Model\Dto\NewsDemand;
use GeorgRinger\News\Domain\Repository\CategoryRepository;
use GeorgRinger\News\Domain\Repository\TagRepository;
use GeorgRinger\News\Event\NewsListActionEvent;
use GeorgRinger\News\Utility\Page;
use GeorgRinger\NewsFilter\Domain\Model\Dto\Search;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Property\PropertyMapper;

class NewsListActionEventListener
{
/** @var ObjectManager */
protected $objectManager;

public function __construct()
{
$this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
/** @var CategoryRepository */
protected $categoryRepository;

/** @var TagRepository */
protected $tagRepository;

/** @var PropertyMapper */
protected $propertyMapper;

public function __construct(
CategoryRepository $categoryRepository,
TagRepository $tagRepository,
PropertyMapper $propertyMapper
) {
$this->categoryRepository = $categoryRepository;
$this->tagRepository = $tagRepository;
$this->propertyMapper = $propertyMapper;
}

public function __invoke(NewsListActionEvent $event)
Expand All @@ -31,29 +43,27 @@ public function __invoke(NewsListActionEvent $event)
$settings = $data['settings'];

if ($settings['enableFilter'] ?? false) {
$search = $this->objectManager->get(Search::class);
$search = GeneralUtility::makeInstance(Search::class);

$vars = \TYPO3\CMS\Core\Utility\GeneralUtility::_POST('tx_news_pi1');
$vars = GeneralUtility::_POST('tx_news_pi1');
if (isset($vars['search']) && is_array($vars['search'])) {
/** @var Search $search */
$search = $this->objectManager->get(PropertyMapper::class)->convert($vars['search'], Search::class);
$search = $this->propertyMapper->convert($vars['search'], Search::class);
}

$extended = [
'currentDate' => $GLOBALS['EXEC_TIME'],
'currentDate' => GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('date', 'timestamp'),
'searchDemand' => $search,
];

$categories2 = $this->getAllRecordsByPid('sys_category', $settings['filterCategories']);
if (!empty($categories2)) {
$categoryRepository = $this->objectManager->get(CategoryRepository::class);
$extended['categories'] = $categoryRepository->findByIdListWithLanguageSupport($categories2);
$extended['categories'] = $this->categoryRepository->findByIdListWithLanguageSupport($categories2);
}

$tags2 = $this->getAllRecordsByPid('tx_news_domain_model_tag', $settings['filterTags']);
if (!empty($tags2)) {
$tagRepository = $this->objectManager->get(TagRepository::class);
$extended['tags'] = $tagRepository->findByIdList($tags2);
$extended['tags'] = $this->tagRepository->findByIdList($tags2);
}

$data['extendedVariables'] = $extended;
Expand Down Expand Up @@ -90,22 +100,24 @@ protected function getAllRecordsByPid(string $tableName, string $pidList): array
*
* @param array $settings
* @param string $class optional class which must be an instance of \GeorgRinger\News\Domain\Model\Dto\NewsDemand
* @return \GeorgRinger\News\Domain\Model\Dto\NewsDemand
* @return NewsDemand
*/
protected function createDemandObjectFromSettings(
$settings,
$class = 'GeorgRinger\\News\\Domain\\Model\\Dto\\NewsDemand'
)
{
) {
$class = isset($settings['demandClass']) && !empty($settings['demandClass']) ? $settings['demandClass'] : $class;

/* @var $demand \GeorgRinger\News\Domain\Model\Dto\NewsDemand */
$demand = $this->objectManager->get($class, $settings);
if (!$demand instanceof \GeorgRinger\News\Domain\Model\Dto\NewsDemand) {
$demand = GeneralUtility::makeInstance($class, $settings);
if (!$demand instanceof NewsDemand) {
throw new \UnexpectedValueException(
sprintf('The demand object must be an instance of \GeorgRinger\\News\\Domain\\Model\\Dto\\NewsDemand, but %s given!',
$class),
1423157953);
sprintf(
'The demand object must be an instance of \GeorgRinger\\News\\Domain\\Model\\Dto\\NewsDemand, but %s given!',
$class
),
1423157953
);
}

$demand->setCategories(GeneralUtility::trimExplode(',', $settings['categories'], true));
Expand Down Expand Up @@ -135,8 +147,10 @@ protected function createDemandObjectFromSettings(
$demand->setMonth((int)$settings['month']);
$demand->setYear((int)$settings['year']);

$demand->setStoragePage(Page::extendPidListByChildren($settings['startingpoint'],
$settings['recursive']));
$demand->setStoragePage(Page::extendPidListByChildren(
$settings['startingpoint'],
$settings['recursive']
));
return $demand;
}
}
23 changes: 15 additions & 8 deletions Classes/Hooks/EnrichDemandObject.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
<?php

declare(strict_types=1);

namespace GeorgRinger\NewsFilter\Hooks;

use GeorgRinger\NewsFilter\Domain\Model\Dto\Demand;
use GeorgRinger\NewsFilter\Domain\Model\Dto\Search;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Property\PropertyMapper;

class EnrichDemandObject
{
/** @var PropertyMapper */
protected $propertyMapper;

public function __construct(PropertyMapper $propertyMapper)
{
$this->propertyMapper = $propertyMapper;
}

public function run(array &$params): void
{
$demand = $params['demand'];
if (get_class($demand) !== Demand::class) {
return;
}
$settings = $params['settings'];
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);


if ($settings['enableFilter']) {
$vars = \TYPO3\CMS\Core\Utility\GeneralUtility::_POST('tx_news_pi1');
if ($settings['enableFilter'] ?? false) {
$vars = GeneralUtility::_POST('tx_news_pi1');
if (isset($vars['search']) && is_array($vars['search'])) {
/** @var Search $search */
$search = $objectManager->get(PropertyMapper::class)->convert($vars['search'], Search::class);

$search = $this->propertyMapper->convert($vars['search'], Search::class);
$search->setFields($settings['search']['fields']);
$search->setSplitSubjectWords((bool)$settings['search']['splitSearchWord']);
$demand->setSearch($search);
$demand->setFilteredCategories($search->getFilteredCategories());
$demand->setFilteredTags($search->getFilteredTags());
$demand->setFromDate($search->getFromDate());
$demand->setToDate($search->getToDate());

}
}
}
Expand Down
46 changes: 25 additions & 21 deletions Classes/Hooks/FlexFormHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,48 @@

namespace GeorgRinger\NewsFilter\Hooks;

use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Configuration\Event\AfterFlexFormDataStructureParsedEvent;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class FlexFormHook
{
private const CTYPES = ['*,news_pi1', '*,news_newsliststicky'];

const PATH = 'typo3conf/ext/news_filter/Configuration/FlexForms/flexform_newsfilter.xml';

// For 7x

/**
* @param array $dataStructArray
* @param array $conf
* @param array $row
* @param string $table
*/
public function getFlexFormDS_postProcessDS(&$dataStructArray, $conf, $row, $table)
public function __invoke(AfterFlexFormDataStructureParsedEvent $event): void
{
if ($table === 'tt_content' && $row['CType'] === 'list' && $row['list_type'] === 'news_pi1') {
$dataStructArray['sheets']['extraEntryNewsFilter'] = self::PATH;
$dataStructure = $event->getDataStructure();
$identifier = $event->getIdentifier();

if ($identifier['type'] === 'tca' && $identifier['tableName'] === 'tt_content' && in_array($identifier['dataStructureKey'], self::CTYPES)) {
$content = file_get_contents($this->getPath());
if ($content) {
$dataStructure['sheets']['extraEntryNewsFilter'] = GeneralUtility::xml2array($content);
}
}
$event->setDataStructure($dataStructure);
}

// For 8x

/**
* @param array $dataStructure
* @param array $identifier
* @return array
*/
public function parseDataStructureByIdentifierPostProcess(array $dataStructure, array $identifier): array
{
if ($identifier['type'] === 'tca' && $identifier['tableName'] === 'tt_content' && $identifier['dataStructureKey'] === 'news_pi1,list') {
$file = Environment::getPublicPath() . '/' . self::PATH;
$content = file_get_contents($file);
if ($identifier['type'] === 'tca' && $identifier['tableName'] === 'tt_content' && in_array($identifier['dataStructureKey'], self::CTYPES)) {
$content = file_get_contents($this->getPath());
if ($content) {
$dataStructure['sheets']['extraEntryNewsFilter'] = \TYPO3\CMS\Core\Utility\GeneralUtility::xml2array($content);
$dataStructure['sheets']['extraEntryNewsFilter'] = GeneralUtility::xml2array($content);
}
}
return $dataStructure;
}
}

protected function getPath(): string
{
$file = (new Typo3Version())->getMajorVersion() >= 12 ? 'flexform_newsfilter12.xml' : 'flexform_newsfilter.xml';
return ExtensionManagementUtility::extPath('news_filter') . 'Configuration/FlexForms/' . $file;
}
}
6 changes: 2 additions & 4 deletions Classes/Hooks/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

class Repository
{

public function modify(array $params, $newsRepository): void
{
if (!($newsRepository instanceof NewsRepository) || \get_class($params['demand']) !== Demand::class) {
Expand Down Expand Up @@ -49,7 +48,7 @@ protected function updateConstraints(Demand $demand, QueryInterface $query, arra
foreach ($categories as $category) {
$categoryConstraint[] = $query->contains('categories', $category);
}
$constraints['filteredCategories'] = $query->logicalOr($categoryConstraint);
$constraints['filteredCategories'] = $query->logicalOr(...$categoryConstraint);
}

// tags
Expand All @@ -59,8 +58,7 @@ protected function updateConstraints(Demand $demand, QueryInterface $query, arra
foreach ($tags as $tag) {
$tagConstraint[] = $query->contains('tags', $tag);
}
$constraints['filteredTags'] = $query->logicalOr($tagConstraint);
$constraints['filteredTags'] = $query->logicalOr(...$tagConstraint);
}

}
}
6 changes: 2 additions & 4 deletions Configuration/FlexForms/flexform_newsfilter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
</TCEforms>
<type>array</type>
<el>


<settings.enableFilter>
<TCEforms>
<label>Enable filter</label>
<label>LLL:EXT:news_filter/Resources/Private/Language/locallang_ff.xlf:settings.enableFilter</label>
<config>
<type>check</type>
<default>0</default>
Expand Down Expand Up @@ -48,4 +46,4 @@
</settings.filterTags>
</el>
</ROOT>
</extra>
</extra>
41 changes: 41 additions & 0 deletions Configuration/FlexForms/flexform_newsfilter12.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<extra>
<ROOT>
<sheetTitle>NewsFilter</sheetTitle>
<type>array</type>
<el>
<settings.enableFilter>
<label>LLL:EXT:news_filter/Resources/Private/Language/locallang_ff.xlf:settings.enableFilter</label>
<config>
<type>check</type>
<default>0</default>
</config>
</settings.enableFilter>

<!-- Category -->
<settings.filterCategories>
<label>LLL:EXT:news/Resources/Private/Language/locallang_be.xlf:flexforms_general.categories</label>
<config>
<type>group</type>
<internal_type>db</internal_type>
<allowed>pages</allowed>
<size>2</size>
<maxitems>50</maxitems>
<minitems>0</minitems>
</config>
</settings.filterCategories>

<!-- Tags -->
<settings.filterTags>
<label>LLL:EXT:news/Resources/Private/Language/locallang_be.xlf:flexforms_additional.tags</label>
<config>
<type>group</type>
<internal_type>db</internal_type>
<allowed>pages</allowed>
<size>2</size>
<maxitems>50</maxitems>
<minitems>0</minitems>
</config>
</settings.filterTags>
</el>
</ROOT>
</extra>
Loading

0 comments on commit 1af4440

Please sign in to comment.