| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace ApacheSolrForTypo3\Solr\ViewHelpers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * Class SearchFormViewHelper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @author Frans Saris <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @author Timo Hund <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @package ApacheSolrForTypo3\Solr\ViewHelpers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | class SearchFormViewHelper extends AbstractSolrFrontendTagBasedViewHelper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     protected $tagName = 'form'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @var TypoScriptFrontendController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     protected $frontendController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     protected $escapeChildren = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     protected $escapeOutput = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * Constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 31 |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 31 |  |         parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 31 |  |         $this->frontendController = $GLOBALS['TSFE']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 31 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * Initialize arguments. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 31 |  |     public function initializeArguments() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 31 |  |         parent::initializeArguments(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 31 |  |         $this->registerTagAttribute('enctype', 'string', 'MIME type with which the form is submitted'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 31 |  |         $this->registerTagAttribute('method', 'string', 'Transfer type (GET or POST)', false, 'get'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 31 |  |         $this->registerTagAttribute('name', 'string', 'Name of form'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 31 |  |         $this->registerTagAttribute('onreset', 'string', 'JavaScript: On reset of the form'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 31 |  |         $this->registerTagAttribute('onsubmit', 'string', 'JavaScript: On submit of the form'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 31 |  |         $this->registerUniversalTagAttributes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 31 |  |         $this->registerArgument('pageUid', 'integer', 'When not set current page is used', false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 31 |  |         $this->registerArgument('additionalFilters', 'array', 'Additional filters', false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 31 |  |         $this->registerArgument('additionalParams', 'array', 'Query parameters to be attached to the resulting URI', false, []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 31 |  |         $this->registerArgument('pageType', 'integer', 'Type of the target page. See typolink.parameter', false, 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 31 |  |         $this->registerArgument('noCache', 'boolean', 'Set this to disable caching for the target page. You should not need this.', false, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 31 |  |         $this->registerArgument('noCacheHash', 'boolean', 'Set this to supress the cHash query parameter created by TypoLink. You should not need this.', false, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 31 |  |         $this->registerArgument('section', 'string', 'The anchor to be added to the action URI (only active if $actionUri is not set)', false, ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 31 |  |         $this->registerArgument('absolute', 'boolean', 'If set, the URI of the rendered link is absolute', false, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 31 |  |         $this->registerArgument('addQueryString', 'boolean', 'If set, the current query parameters will be kept in the URI', false, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 31 |  |         $this->registerArgument('argumentsToBeExcludedFromQueryString', 'array', 'arguments to be removed from the URI. Only active if $addQueryString = TRUE', false, []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 31 |  |         $this->registerArgument('addQueryStringMethod', 'string', 'Set which parameters will be kept. Only active if $addQueryString = TRUE', false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 31 |  |         $this->registerArgument('addSuggestUrl', 'boolean', 'Indicates if suggestUrl should be rendered or not', false, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 31 |  |         $this->registerArgument('suggestHeader', 'string', 'The header for the top results', false, 'Top Results'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 31 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * Render search form tag | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 31 |  |     public function render() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 31 |  |         $pageUid = $this->arguments['pageUid']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 31 |  |         if ($pageUid === null && !empty($this->getTypoScriptConfiguration()->getSearchTargetPage())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 31 |  |             $pageUid = $this->getTypoScriptConfiguration()->getSearchTargetPage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 31 |  |         $uri = $this->buildUriFromArguments(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 31 |  |         $this->tag->addAttribute('action', trim($uri)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 31 |  |         if ($this->arguments['addSuggestUrl']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 31 |  |             $this->tag->addAttribute('data-suggest', $this->getSuggestEidUrl($this->arguments['additionalFilters'], $pageUid)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 31 |  |         $this->tag->addAttribute('data-suggest-header', htmlspecialchars($this->arguments['suggestHeader'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 31 |  |         $this->tag->addAttribute('accept-charset', $this->frontendController->metaCharset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         // Get search term | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 31 |  |         $this->templateVariableContainer->add('q', $this->getQueryString()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 31 |  |         $this->templateVariableContainer->add('pageUid', $pageUid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 31 |  |         $this->templateVariableContainer->add('languageUid', $this->frontendController->sys_language_uid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 31 |  |         $formContent = $this->renderChildren(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 31 |  |         $this->templateVariableContainer->remove('q'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 31 |  |         $this->templateVariableContainer->remove('pageUid'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 31 |  |         $this->templateVariableContainer->remove('languageUid'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 31 |  |         $this->tag->setContent($formContent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 31 |  |         return $this->tag->render(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 31 |  |     protected function getQueryString() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 31 |  |         $resultSet = $this->getSearchResultSet(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 31 |  |         if ($resultSet === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 2 |  |             return ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 29 |  |         return trim($this->getSearchResultSet()->getUsedSearchRequest()->getRawUserQuery()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @param NULL|array $additionalFilters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @param int $pageUid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 141 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 142 | 31 |  |     protected function getSuggestEidUrl($additionalFilters, $pageUid) | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 144 | 31 |  |         $suggestUrl = $this->frontendController->absRefPrefix; | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 146 | 31 |  |         $suggestUrl .= '?type=7384&id=' . $pageUid; | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |         // add filters | 
            
                                                                        
                            
            
                                    
            
            
                | 149 | 31 |  |         if (!empty($additionalFilters)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 150 | 1 |  |             $additionalFilters = json_encode($additionalFilters); | 
            
                                                                        
                            
            
                                    
            
            
                | 151 | 1 |  |             $additionalFilters = rawurlencode($additionalFilters); | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 153 | 1 |  |             $suggestUrl .= '&filters=' . $additionalFilters; | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |         // adds the language parameter to the suggest URL | 
            
                                                                        
                            
            
                                    
            
            
                | 157 | 31 |  |         if ($this->frontendController->sys_language_uid > 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |             $suggestUrl .= '&L=' . $this->frontendController->sys_language_uid; | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 161 | 31 |  |         return $suggestUrl; | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 31 |  |     protected function buildUriFromArguments(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 31 |  |         $uriBuilder = $this->getControllerContext()->getUriBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 31 |  |         $uri = $uriBuilder->reset()->setTargetPageUid($this->arguments['pageUid'])->setTargetPageType($this->arguments['pageType'])->setNoCache($this->arguments['noCache'])->setUseCacheHash(!$this->arguments['noCacheHash'])->setArguments($this->arguments['additionalParams'])->setCreateAbsoluteUri($this->arguments['absolute'])->setAddQueryString($this->arguments['addQueryString'])->setArgumentsToBeExcludedFromQueryString($this->arguments['argumentsToBeExcludedFromQueryString'])->setAddQueryStringMethod($this->arguments['addQueryStringMethod'])->setSection($this->arguments['section'])->build(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 31 |  |         return $uri; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 173 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 174 |  |  |  |