Skip to content

Commit

Permalink
[TASK] Refactor LastSearches and FrequentlySearched widgets
Browse files Browse the repository at this point in the history
Fixes: #3091
Relates: #2976
  • Loading branch information
FamousWolf authored and dkd-kaehm committed Nov 29, 2021
1 parent 3bb3dad commit 2bfc641
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 191 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace ApacheSolrForTypo3\Solr\ViewHelpers\Widget\Controller;
namespace ApacheSolrForTypo3\Solr\ViewHelpers;

/*
* This file is part of the TYPO3 CMS project.
Expand All @@ -15,72 +15,82 @@
*/

use ApacheSolrForTypo3\Solr\Domain\Search\FrequentSearches\FrequentSearchesService;
use ApacheSolrForTypo3\Solr\Widget\AbstractWidgetController;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;

/**
* Class FrequentlySearchedController
* Class LastSearchesViewHelper
*
* @author Frans Saris <frans@beech.it>
* @author Timo Hund <timo.hund@dkd.de>
* @author Rudy Gnodde <rudy.gnodde@beech.it>
*/
class FrequentlySearchedController extends AbstractWidgetController implements LoggerAwareInterface
class FrequentlySearchedViewHelper extends AbstractSolrViewHelper
{
use LoggerAwareTrait;
/**
* @var bool
*/
protected $escapeChildren = false;

/**
* @var bool
*/
protected $escapeOutput = false;

/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return mixed|void
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
/** @var TypoScriptFrontendController $tsfe */
$tsfe = $GLOBALS['TSFE'];
$cache = self::getInitializedCache();
/** @var ConfigurationManager $configurationManager */
$configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
$typoScriptConfiguration = $configurationManager->getTypoScriptConfiguration();
/* @var FrequentSearchesService $frequentSearchesService */
$frequentSearchesService = GeneralUtility::makeInstance(
FrequentSearchesService::class,
$typoScriptConfiguration,
$cache,
$tsfe
);

$frequentSearches = $frequentSearchesService->getFrequentSearchTerms();
$minimumSize = $typoScriptConfiguration->getSearchFrequentSearchesMinSize();
$maximumSize = $typoScriptConfiguration->getSearchFrequentSearchesMaxSize();

$templateVariableContainer = $renderingContext->getVariableProvider();
$templateVariableContainer->add('frequentSearches', self::enrichFrequentSearchesInfo($frequentSearches, $minimumSize, $maximumSize));
$output = $renderChildrenClosure();
$templateVariableContainer->remove('frequentSearches');
return $output;
}

/**
* Initializes the cache for this command.
*
* @return FrontendInterface|null
*/
protected function getInitializedCache(): ?FrontendInterface
protected static function getInitializedCache(): ?FrontendInterface
{
$cacheIdentifier = 'tx_solr';
/* @var FrontendInterface $cacheInstance */
try {
$cacheInstance = GeneralUtility::makeInstance(CacheManager::class)->getCache($cacheIdentifier);
} catch (NoSuchCacheException $exception) {
$this->logger->error('Getting cache failed: ' . $exception->getMessage());
return null;
}

return $cacheInstance;
}

/**
* Last searches
*/
public function indexAction()
{
$tsfe = $GLOBALS['TSFE'];
$cache = $this->getInitializedCache();
$configuration = $this->controllerContext->getTypoScriptConfiguration();

/* @var FrequentSearchesService $frequentSearchesService */
$frequentSearchesService = GeneralUtility::makeInstance(
FrequentSearchesService::class,
/** @scrutinizer ignore-type */ $configuration,
/** @scrutinizer ignore-type */ $cache,
/** @scrutinizer ignore-type */ $tsfe
);

$frequentSearches = $frequentSearchesService->getFrequentSearchTerms();
$minimumSize = $configuration->getSearchFrequentSearchesMinSize();
$maximumSize = $configuration->getSearchFrequentSearchesMaxSize();

$this->view->assign(
'contentArguments',
[
'frequentSearches' => $this->enrichFrequentSearchesInfo($frequentSearches, $minimumSize, $maximumSize)
]
);
}

/**
* Enrich the frequentSearches
*
Expand All @@ -89,7 +99,7 @@ public function indexAction()
* @param int $maximumSize
* @return array An array with content for the frequent terms markers
*/
protected function enrichFrequentSearchesInfo(array $frequentSearchTerms, int $minimumSize, int $maximumSize): array
protected static function enrichFrequentSearchesInfo(array $frequentSearchTerms, int $minimumSize, int $maximumSize): array
{
$frequentSearches = [];
if (count($frequentSearchTerms)) {
Expand Down
61 changes: 61 additions & 0 deletions Classes/ViewHelpers/LastSearchesViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
namespace ApacheSolrForTypo3\Solr\ViewHelpers;

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

use ApacheSolrForTypo3\Solr\Domain\Search\LastSearches\LastSearchesService;
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;

/**
* Class LastSearchesViewHelper
*
* @author Rudy Gnodde <rudy.gnodde@beech.it>
*/
class LastSearchesViewHelper extends AbstractSolrViewHelper
{

/**
* @var bool
*/
protected $escapeChildren = false;

/**
* @var bool
*/
protected $escapeOutput = false;

/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return mixed|void
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
/** @var ConfigurationManager $configurationManager */
$configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
$typoScriptConfiguration = $configurationManager->getTypoScriptConfiguration();
$lastSearchesService = GeneralUtility::makeInstance(
LastSearchesService::class,
$typoScriptConfiguration
);
$templateVariableContainer = $renderingContext->getVariableProvider();
$templateVariableContainer->add('lastSearches', $lastSearchesService->getLastSearches());
$output = $renderChildrenClosure();
$templateVariableContainer->remove('lastSearches');
return $output;
}
}
41 changes: 0 additions & 41 deletions Classes/ViewHelpers/Widget/Controller/LastSearchesController.php

This file was deleted.

50 changes: 0 additions & 50 deletions Classes/ViewHelpers/Widget/FrequentlySearchedViewHelper.php

This file was deleted.

50 changes: 0 additions & 50 deletions Classes/ViewHelpers/Widget/LastSearchesViewHelper.php

This file was deleted.

6 changes: 3 additions & 3 deletions Resources/Private/Partials/Search/FrequentlySearched.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<f:section name="FrequentlySearched">

<s:widget.frequentlySearched>
<s:frequentlySearched>
<f:if condition="{frequentSearches}">
<div id="tx-solr-frequent-searches" class="secondaryContentSection panel">
<div class="panel-heading">
Expand All @@ -29,8 +29,8 @@ <h3 class="panel-title">
</div>

</f:if>
</s:widget.frequentlySearched>
</s:frequentlySearched>

</f:section>

</html>
</html>
6 changes: 3 additions & 3 deletions Resources/Private/Partials/Search/LastSearches.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<f:section name="LastSearches">

<s:widget.lastSearches>
<s:lastSearches>
<f:if condition="{lastSearches}">

<div id="tx-solr-lastsearches" class="secondaryContentSection panel">
Expand All @@ -30,8 +30,8 @@ <h3 class="panel-title">
</div>

</f:if>
</s:widget.lastSearches>
</s:lastSearches>

</f:section>

</html>
</html>
Loading

0 comments on commit 2bfc641

Please sign in to comment.