| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace ApacheSolrForTypo3\Solr\ViewHelpers\Widget\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * This file is part of the TYPO3 CMS project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * It is free software; you can redistribute it and/or modify it under | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * the terms of the GNU General Public License, either version 2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * of the License, or any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * For the full copyright and license information, please read the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * LICENSE.txt file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * The TYPO3 project - inspiring people to share! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use ApacheSolrForTypo3\Solr\Domain\Search\FrequentSearches\FrequentSearchesService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use ApacheSolrForTypo3\Solr\Widget\AbstractWidgetController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use TYPO3\CMS\Core\Cache\CacheManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use TYPO3\CMS\Core\Database\DatabaseConnection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use TYPO3\CMS\Core\Utility\GeneralUtility; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * Class FrequentlySearchedController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @author Frans Saris <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @author Timo Hund <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * @package ApacheSolrForTypo3\Solr\ViewHelpers\Widget\Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | class FrequentlySearchedController extends AbstractWidgetController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * Initializes the cache for this command. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @return \TYPO3\CMS\Core\Cache\AbstractFrontend | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     protected function getInitializedCache() | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $cacheIdentifier = 'tx_solr'; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             $cacheInstance = GeneralUtility::makeInstance(CacheManager::class)->getCache($cacheIdentifier); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         } catch (NoSuchCacheException $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |             /** @var t3lib_cache_Factory $typo3CacheFactory */ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             $typo3CacheFactory = $GLOBALS['typo3CacheFactory']; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |             $cacheInstance = $typo3CacheFactory->create($cacheIdentifier, $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$cacheIdentifier]['frontend'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$cacheIdentifier]['backend'], $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations'][$cacheIdentifier]['options']); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         return $cacheInstance; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Last searches | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function indexAction() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $databaseConnection = $GLOBALS['TYPO3_DB']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $tsfe = $GLOBALS['TSFE']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $cache = $this->getInitializedCache(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $configuration = $this->controllerContext->getTypoScriptConfiguration(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $frequentSearchesService = GeneralUtility::makeInstance(FrequentSearchesService::class, $configuration, $cache, $tsfe, $databaseConnection); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $frequentSearches = $frequentSearchesService->getFrequentSearchTerms(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $minimumSize = $configuration->getSearchFrequentSearchesMinSize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $maximumSize = $configuration->getSearchFrequentSearchesMaxSize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->view->assign('contentArguments', ['frequentSearches' => $this->enrichFrequentSearchesInfo($frequentSearches, $minimumSize, $maximumSize)]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * Enrich the frequentSearches | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param array Frequent search terms as array with terms as keys and hits as the value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param integer $minimumSize | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param integer $maximumSize | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @return array An array with content for the frequent terms markers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     protected function enrichFrequentSearchesInfo(array $frequentSearchTerms, $minimumSize, $maximumSize) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $frequentSearches = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         if (count($frequentSearchTerms)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $maximumHits = max(array_values($frequentSearchTerms)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $minimumHits = min(array_values($frequentSearchTerms)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $spread = $maximumHits - $minimumHits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $step = ($spread == 0) ? 1 : ($maximumSize - $minimumSize) / $spread; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             foreach ($frequentSearchTerms as $term => $hits) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 $size = round($minimumSize + (($hits - $minimumHits) * $step)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 $frequentSearches[] = ['q' => htmlspecialchars_decode($term), 'hits' => $hits, 'style' => 'font-size: ' . $size . 'px', 'class' => 'tx-solr-frequent-term-' . $size, 'size' => $size]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         return $frequentSearches; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |  |