| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ApacheSolrForTypo3\Solr\Domain\Search\LastSearches; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /*************************************************************** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  *  Copyright notice | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  *  (c) 2015-2016 Timo Schmidt <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  *  All rights reserved | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  *  This script is part of the TYPO3 project. The TYPO3 project is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  *  free software; you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  *  it under the terms of the GNU General Public License as published by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  *  the Free Software Foundation; either version 2 of the License, or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  *  (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  *  The GNU General Public License can be found at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  *  http://www.gnu.org/copyleft/gpl.html. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  *  This script is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  *  GNU General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  *  This copyright notice MUST APPEAR in all copies of the script! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  ***************************************************************/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use ApacheSolrForTypo3\Solr\System\Session\FrontendUserSession; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use TYPO3\CMS\Core\Utility\GeneralUtility; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use TYPO3\CMS\Extbase\Persistence\Generic\Session; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * The LastSearchesService is responsible to return the LastSearches from the session or database, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * depending on the configuration. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  * @author Timo Schmidt <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | class LastSearchesService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @var TypoScriptConfiguration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     protected $configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @var FrontendUserSession | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     protected $session; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @var LastSearchesRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     protected $lastSearchesRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param TypoScriptConfiguration $typoscriptConfiguration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param FrontendUserSession|null $session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 30 |  |      * @param LastSearchesRepository|null $lastSearchesRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 30 |  |     public function __construct(TypoScriptConfiguration $typoscriptConfiguration, FrontendUserSession $session = null, LastSearchesRepository $lastSearchesRepository = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 30 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 30 |  |         $this->configuration = $typoscriptConfiguration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 30 |  |         $this->session = isset($session) ? $session : GeneralUtility::makeInstance(FrontendUserSession::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->lastSearchesRepository = isset($lastSearchesRepository) ? $lastSearchesRepository : GeneralUtility::makeInstance(LastSearchesRepository::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * Retrieves the last searches from the session or database depending on the configuration. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 29 |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 29 |  |     public function getLastSearches() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 29 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 29 |  |         $lastSearchesKeywords = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $mode   = $this->configuration->getSearchLastSearchesMode(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $limit  = $this->configuration->getSearchLastSearchesLimit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 29 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 26 |  |         switch ($mode) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 26 |  |             case 'user': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 3 |  |                 $lastSearchesKeywords = $this->getLastSearchesFromSession($limit); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 3 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 3 |  |             case 'global': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 $lastSearchesKeywords = $this->lastSearchesRepository->findAllKeywords($limit); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 29 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         return $lastSearchesKeywords; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * Saves the keywords to the last searches in the database or session depending on the configuration. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @param string $keywords | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 19 |  |      * @throws \UnexpectedValueException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 99 | 19 |  |     public function addToLastSearches($keywords) | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 101 | 19 |  |         $mode = $this->configuration->getSearchLastSearchesMode(); | 
            
                                                                        
                            
            
                                    
            
            
                | 102 | 18 |  |         switch ($mode) { | 
            
                                                                        
                            
            
                                    
            
            
                | 103 | 18 |  |             case 'user': | 
            
                                                                        
                            
            
                                    
            
            
                | 104 | 1 |  |                 $this->storeKeywordsToSession($keywords); | 
            
                                                                        
                            
            
                                    
            
            
                | 105 | 1 |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 106 | 1 |  |             case 'global': | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |                 $this->lastSearchesRepository->add($keywords, (int)$this->configuration->getSearchLastSearchesLimit()); | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |             default: | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |                 throw new \UnexpectedValueException( | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |                     'Unknown mode for plugin.tx_solr.search.lastSearches.mode, valid modes are "user" or "global".', | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |                     1342456570 | 
            
                                                                        
                            
            
                                    
            
            
                | 113 | 19 |  |                 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * Gets the last searched keywords from the user's session | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @param int $limit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 26 |  |      * @return array An array containing the last searches of the current user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 26 |  |     protected function getLastSearchesFromSession($limit) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 26 |  |         $lastSearches = $this->session->getLastSearches(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 8 |  |         $lastSearches = array_slice(array_reverse(array_unique($lastSearches)), 0, $limit); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         return $lastSearches; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 18 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 18 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * Stores the keywords from the current query to the user's session. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @param string $keywords The current query's keywords | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     protected function storeKeywordsToSession($keywords) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $currentLastSearches = $this->session->getLastSearches(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 3 |  |         $lastSearches = $currentLastSearches; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $newLastSearchesCount = array_push($lastSearches, $keywords); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 3 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 3 |  |         while ($newLastSearchesCount > $this->configuration->getSearchLastSearchesLimit()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 3 |  |             array_shift($lastSearches); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 3 |  |             $newLastSearchesCount = count($lastSearches); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 3 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 3 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $this->session->setLastSearches($lastSearches); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 150 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 151 |  |  |  |