We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 102 |
| Total Lines | 553 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Search often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Search, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Search extends \Kitodo\Dlf\Common\AbstractPlugin { |
||
| 31 | public $scriptRelPath = 'Classes/Plugins/Search.php'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Adds the JS files necessary for search suggestions |
||
| 35 | * |
||
| 36 | * @access protected |
||
| 37 | * |
||
| 38 | * @return void |
||
| 39 | */ |
||
| 40 | protected function addAutocompleteJS() { |
||
| 41 | // Check if there are any metadata to suggest. |
||
| 42 | $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery( |
||
| 43 | 'tx_dlf_metadata.*', |
||
| 44 | 'tx_dlf_metadata', |
||
| 45 | 'tx_dlf_metadata.index_autocomplete=1' |
||
| 46 | .' AND tx_dlf_metadata.pid='.intval($this->conf['pages']) |
||
| 47 | .Helper::whereClause('tx_dlf_metadata'), |
||
| 48 | '', |
||
| 49 | '', |
||
| 50 | '1' |
||
| 51 | ); |
||
| 52 | if ($GLOBALS['TYPO3_DB']->sql_num_rows($result)) { |
||
| 53 | $GLOBALS['TSFE']->additionalHeaderData[$this->prefixId.'_search_suggest'] = '<script type="text/javascript" src="'.\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::siteRelPath($this->extKey).'Resources/Public/Javascript/Search/Suggester.js"></script>'; |
||
| 54 | } else { |
||
| 55 | Helper::devLog('No metadata fields configured for search suggestions', DEVLOG_SEVERITY_WARNING); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Adds the current collection's UID to the search form |
||
| 61 | * |
||
| 62 | * @access protected |
||
| 63 | * |
||
| 64 | * @return string HTML input fields with current document's UID and parent ID |
||
| 65 | */ |
||
| 66 | protected function addCurrentCollection() { |
||
| 67 | // Load current collection. |
||
| 68 | $list = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(DocumentList::class); |
||
| 69 | if (!empty($list->metadata['options']['source']) |
||
| 70 | && $list->metadata['options']['source'] == 'collection') { |
||
| 71 | // Get collection's UID. |
||
| 72 | return '<input type="hidden" name="'.$this->prefixId.'[collection]" value="'.$list->metadata['options']['select'].'" />'; |
||
| 73 | } elseif (!empty($list->metadata['options']['params']['filterquery'])) { |
||
| 74 | // Get collection's UID from search metadata. |
||
| 75 | foreach ($list->metadata['options']['params']['filterquery'] as $facet) { |
||
| 76 | $facetKeyVal = explode(':', $facet['query'], 2); |
||
| 77 | if ($facetKeyVal[0] == 'collection_faceting' |
||
| 78 | && !strpos($facetKeyVal[1], '" OR "')) { |
||
| 79 | $collectionId = Helper::getUidFromIndexName(trim($facetKeyVal[1], '(")'), 'tx_dlf_collections'); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | return '<input type="hidden" name="'.$this->prefixId.'[collection]" value="'.$collectionId.'" />'; |
||
|
|
|||
| 83 | } |
||
| 84 | return ''; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Adds the current document's UID or parent ID to the search form |
||
| 89 | * |
||
| 90 | * @access protected |
||
| 91 | * |
||
| 92 | * @return string HTML input fields with current document's UID and parent ID |
||
| 93 | */ |
||
| 94 | protected function addCurrentDocument() { |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Adds the encrypted Solr core name to the search form |
||
| 120 | * |
||
| 121 | * @access protected |
||
| 122 | * |
||
| 123 | * @return string HTML input fields with encrypted core name and hash |
||
| 124 | */ |
||
| 125 | protected function addEncryptedCoreName() { |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Returns the extended search form and adds the JS files necessary for extended search. |
||
| 142 | * |
||
| 143 | * @access protected |
||
| 144 | * |
||
| 145 | * @return string The extended search form or an empty string |
||
| 146 | */ |
||
| 147 | protected function addExtendedSearch() { |
||
| 148 | $extendedSearch = ''; |
||
| 149 | // Quit without doing anything if no fields for extended search are selected. |
||
| 150 | if (empty($this->conf['extendedSlotCount']) |
||
| 151 | || empty($this->conf['extendedFields'])) { |
||
| 152 | return $extendedSearch; |
||
| 153 | } |
||
| 154 | // Get operator options. |
||
| 155 | $operatorOptions = ''; |
||
| 156 | foreach (['AND', 'OR', 'NOT'] as $operator) { |
||
| 157 | $operatorOptions .= '<option class="tx-dlf-search-operator-option tx-dlf-search-operator-'.$operator.'" value="'.$operator.'">'.$this->pi_getLL($operator, '', TRUE).'</option>'; |
||
| 158 | } |
||
| 159 | // Get field selector options. |
||
| 160 | $fieldSelectorOptions = ''; |
||
| 161 | $searchFields = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->conf['extendedFields'], TRUE); |
||
| 162 | foreach ($searchFields as $searchField) { |
||
| 163 | $fieldSelectorOptions .= '<option class="tx-dlf-search-field-option tx-dlf-search-field-'.$searchField.'" value="'.$searchField.'">'.Helper::translate($searchField, 'tx_dlf_metadata', $this->conf['pages']).'</option>'; |
||
| 164 | } |
||
| 165 | for ($i = 0; $i < $this->conf['extendedSlotCount']; $i++) { |
||
| 166 | $markerArray = [ |
||
| 167 | '###EXT_SEARCH_OPERATOR###' => '<select class="tx-dlf-search-operator tx-dlf-search-operator-'.$i.'" name="'.$this->prefixId.'[extOperator]['.$i.']">'.$operatorOptions.'</select>', |
||
| 168 | '###EXT_SEARCH_FIELDSELECTOR###' => '<select class="tx-dlf-search-field tx-dlf-search-field-'.$i.'" name="'.$this->prefixId.'[extField]['.$i.']">'.$fieldSelectorOptions.'</select>', |
||
| 169 | '###EXT_SEARCH_FIELDQUERY###' => '<input class="tx-dlf-search-query tx-dlf-search-query-'.$i.'" type="text" name="'.$this->prefixId.'[extQuery]['.$i.']" />' |
||
| 170 | ]; |
||
| 171 | $extendedSearch .= $this->cObj->substituteMarkerArray($this->cObj->getSubpart($this->template, '###EXT_SEARCH_ENTRY###'), $markerArray); |
||
| 172 | } |
||
| 173 | return $extendedSearch; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Adds the facets menu to the search form |
||
| 178 | * |
||
| 179 | * @access protected |
||
| 180 | * |
||
| 181 | * @return string HTML output of facets menu |
||
| 182 | */ |
||
| 183 | protected function addFacetsMenu() { |
||
| 184 | // Check for typoscript configuration to prevent fatal error. |
||
| 185 | if (empty($this->conf['facetsConf.'])) { |
||
| 186 | Helper::devLog('Incomplete plugin configuration', DEVLOG_SEVERITY_WARNING); |
||
| 187 | return ''; |
||
| 188 | } |
||
| 189 | // Quit without doing anything if no facets are selected. |
||
| 190 | if (empty($this->conf['facets'])) { |
||
| 191 | return ''; |
||
| 192 | } |
||
| 193 | // Get facets from plugin configuration. |
||
| 194 | $facets = []; |
||
| 195 | foreach (\TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->conf['facets'], TRUE) as $facet) { |
||
| 196 | $facets[$facet.'_faceting'] = Helper::translate($facet, 'tx_dlf_metadata', $this->conf['pages']); |
||
| 197 | } |
||
| 198 | // Render facets menu. |
||
| 199 | $TSconfig = []; |
||
| 200 | $TSconfig['special'] = 'userfunction'; |
||
| 201 | $TSconfig['special.']['userFunc'] = 'Kitodo\\Dlf\\Plugins\\Search->makeFacetsMenuArray'; |
||
| 202 | $TSconfig['special.']['facets'] = $facets; |
||
| 203 | $TSconfig['special.']['limit'] = max(intval($this->conf['limitFacets']), 1); |
||
| 204 | $TSconfig = \TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule($this->conf['facetsConf.'], $TSconfig); |
||
| 205 | return $this->cObj->HMENU($TSconfig); |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Adds the fulltext switch to the search form |
||
| 210 | * |
||
| 211 | * @access protected |
||
| 212 | * |
||
| 213 | * @param integer $isFulltextSearch: Is fulltext search activated? |
||
| 214 | * |
||
| 215 | * @return string HTML output of fulltext switch |
||
| 216 | */ |
||
| 217 | protected function addFulltextSwitch($isFulltextSearch = 0) { |
||
| 218 | $output = ''; |
||
| 219 | // Check for plugin configuration. |
||
| 220 | if (!empty($this->conf['fulltext'])) { |
||
| 221 | $output .= ' <input class="tx-dlf-search-fulltext" id="tx-dlf-search-fulltext-no" type="radio" name="'.$this->prefixId.'[fulltext]" value="0" '.($isFulltextSearch == 0 ? 'checked="checked"' : '').' />'; |
||
| 222 | $output .= ' <label for="tx-dlf-search-fulltext-no">'.$this->pi_getLL('label.inMetadata', '').'</label>'; |
||
| 223 | $output .= ' <input class="tx-dlf-search-fulltext" id="tx-dlf-search-fulltext-yes" type="radio" name="'.$this->prefixId.'[fulltext]" value="1" '.($isFulltextSearch == 1 ? 'checked="checked"' : '').'/>'; |
||
| 224 | $output .= ' <label for="tx-dlf-search-fulltext-yes">'.$this->pi_getLL('label.inFulltext', '').'</label>'; |
||
| 225 | } |
||
| 226 | return $output; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Adds the logical page field to the search form |
||
| 231 | * |
||
| 232 | * @access protected |
||
| 233 | * |
||
| 234 | * @return string HTML output of logical page field |
||
| 235 | */ |
||
| 236 | protected function addLogicalPage() { |
||
| 237 | $output = ''; |
||
| 238 | // Check for plugin configuration. |
||
| 239 | if (!empty($this->conf['showLogicalPageField'])) { |
||
| 240 | $output .= ' <label for="tx-dlf-search-logical-page">'.$this->pi_getLL('label.logicalPage', '').': </label>'; |
||
| 241 | $output .= ' <input class="tx-dlf-search-logical-page" id="tx-dlf-search-logical-page" type="text" name="'.$this->prefixId.'[logicalPage]" />'; |
||
| 242 | } |
||
| 243 | return $output; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Creates an array for a HMENU entry of a facet value. |
||
| 248 | * |
||
| 249 | * @access protected |
||
| 250 | * |
||
| 251 | * @param string $field: The facet's index_name |
||
| 252 | * @param string $value: The facet's value |
||
| 253 | * @param integer $count: Number of hits for this facet |
||
| 254 | * @param array $search: The parameters of the current search query |
||
| 255 | * @param string &$state: The state of the parent item |
||
| 256 | * |
||
| 257 | * @return array The array for the facet's menu entry |
||
| 258 | */ |
||
| 259 | protected function getFacetsMenuEntry($field, $value, $count, $search, &$state) { |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * The main method of the PlugIn |
||
| 307 | * |
||
| 308 | * @access public |
||
| 309 | * |
||
| 310 | * @param string $content: The PlugIn content |
||
| 311 | * @param array $conf: The PlugIn configuration |
||
| 312 | * |
||
| 313 | * @return string The content that is displayed on the website |
||
| 314 | */ |
||
| 315 | public function main($content, $conf) { |
||
| 493 | } |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * This builds a menu array for HMENU |
||
| 498 | * |
||
| 499 | * @access public |
||
| 500 | * |
||
| 501 | * @param string $content: The PlugIn content |
||
| 502 | * @param array $conf: The PlugIn configuration |
||
| 503 | * |
||
| 504 | * @return array HMENU array |
||
| 505 | */ |
||
| 506 | public function makeFacetsMenuArray($content, $conf) { |
||
| 583 | } |
||
| 584 | } |
||
| 585 |