Completed
Push — master ( fb242c...3f99e9 )
by Timo
26:23 queued 04:18
created

SuggestFormModifier   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 74.58%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 0
loc 125
ccs 44
cts 59
cp 0.7458
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setParentPlugin() 0 4 1
A modifyForm() 0 19 2
A addSuggestStylesheets() 0 9 3
A addSuggestJavascript() 0 9 1
B getSuggestEidUrl() 0 26 4
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Plugin\Results;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2011-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\Plugin\CommandPluginAware;
28
use ApacheSolrForTypo3\Solr\Plugin\CommandPluginBase;
29
use ApacheSolrForTypo3\Solr\Plugin\FormModifier;
30
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
31
use ApacheSolrForTypo3\Solr\Template;
32
use ApacheSolrForTypo3\Solr\Util;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
35
/**
36
 * Suggest form modifier, suggests queries through auto completion / AJAX.
37
 *
38
 * @author Ingo Renner <[email protected]>
39
 */
40
class SuggestFormModifier implements FormModifier, CommandPluginAware
41
{
42
43
    /**
44
     * Configuration
45
     *
46
     * @var TypoScriptConfiguration
47
     */
48
    protected $configuration;
49
50
    /**
51
     * The currently active plugin
52
     *
53
     * @var CommandPluginBase
54
     */
55
    protected $parentPlugin;
56
57
    /**
58
     * Constructor
59
     *
60
     */
61 25
    public function __construct()
62
    {
63 25
        $this->configuration = Util::getSolrConfiguration();
64 25
    }
65
66
    /**
67
     * Sets the currently active parent plugin.
68
     *
69
     * @param CommandPluginBase $parentPlugin Currently active parent plugin
70
     */
71 25
    public function setParentPlugin(CommandPluginBase $parentPlugin)
72
    {
73 25
        $this->parentPlugin = $parentPlugin;
74 25
    }
75
76
    /**
77
     * Modifies the search form by providing an additional marker providing the
78
     * suggest eID script URL and adding javascript to the page's header.
79
     *
80
     * @param array $markers An array of existing form markers
81
     * @param Template $template An instance of the template engine
82
     * @return array Array with additional markers for suggestions
83
     */
84 25
    public function modifyForm(array $markers, Template $template)
85
    {
86 25
        $suggestionsEnabled = $this->configuration->getSuggest();
87
88 25
        if ($suggestionsEnabled) {
89 25
            $this->addSuggestStylesheets();
90 25
            $this->addSuggestJavascript();
91 25
            $suggestEidUrl = $this->getSuggestEidUrl();
92
93 25
            $markers['suggest_url'] = '<script type="text/javascript">
94
				/*<![CDATA[*/
95 25
				var tx_solr_suggestUrl = \'' . $suggestEidUrl . '\';
96
				/*]]>*/
97
				</script>
98 25
			';
99 25
        }
100
101 25
        return $markers;
102
    }
103
104
    /**
105
     * Adds the stylesheets necessary for the suggestions
106
     *
107
     */
108 25
    protected function addSuggestStylesheets()
109
    {
110 25
        $uiCss = $this->configuration->getCssFileByFileKey('ui');
111 25
        if (trim($uiCss) !== '' && !$GLOBALS['TSFE']->additionalHeaderData['tx_solr-uiCss']) {
112 25
            $cssFile = GeneralUtility::createVersionNumberedFilename($GLOBALS['TSFE']->tmpl->getFileName($uiCss));
113 25
            $GLOBALS['TSFE']->additionalHeaderData['tx_solr-uiCss'] =
114 25
                '<link href="' . $cssFile . '" rel="stylesheet" type="text/css" media="all" />';
115 25
        }
116 25
    }
117
118
    /**
119
     * Adds the Javascript necessary for the suggestions
120
     *
121
     */
122 25
    protected function addSuggestJavascript()
123
    {
124 25
        $javascriptManager = $this->parentPlugin->getJavascriptManager();
125
126 25
        $javascriptManager->loadFile('library');
127 25
        $javascriptManager->loadFile('ui');
128 25
        $javascriptManager->loadFile('ui.autocomplete');
129 25
        $javascriptManager->loadFile('suggest');
130 25
    }
131
132
    /**
133
     * Returns the eID URL for the AJAX suggestion request.
134
     *
135
     * @author Mario Rimann <[email protected]>
136
     * @return string the full URL to the eID script including the needed parameters
137
     */
138 25
    protected function getSuggestEidUrl()
139
    {
140 25
        $suggestUrl = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
141
142 25
        if ($this->configuration->getSuggestForceHttps()) {
143
            $suggestUrl = str_replace('http://', 'https://', $suggestUrl);
144
        }
145
146 25
        $suggestUrl .= '?eID=tx_solr_suggest&id=' . $GLOBALS['TSFE']->id;
147
148
        // add filters
149 25
        $additionalFilters = $this->parentPlugin->getSearchResultSetService()->getAdditionalFilters();
150 25
        if (!empty($additionalFilters)) {
151 2
            $additionalFilters = json_encode($additionalFilters);
152 2
            $additionalFilters = rawurlencode($additionalFilters);
153
154 2
            $suggestUrl .= '&filters=' . $additionalFilters;
155 2
        }
156
157
        // adds the language parameter to the suggest URL
158 25
        if ($GLOBALS['TSFE']->sys_language_uid > 0) {
159
            $suggestUrl .= '&L=' . $GLOBALS['TSFE']->sys_language_uid;
160
        }
161
162 25
        return $suggestUrl;
163
    }
164
}
165