| Conditions | 3 |
| Paths | 2 |
| Total Lines | 188 |
| Code Lines | 138 |
| Lines | 0 |
| Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 84 | public function getCMSFields() { |
||
| 85 | Requirements::javascript('elastica/javascript/elasticaedit.js'); |
||
| 86 | $fields = parent::getCMSFields(); |
||
| 87 | |||
| 88 | |||
| 89 | $fields->addFieldToTab("Root", new TabSet('Search', |
||
| 90 | new Tab('SearchFor'), |
||
| 91 | new Tab('Fields'), |
||
| 92 | new Tab('AutoComplete'), |
||
| 93 | new Tab('Aggregations'), |
||
| 94 | new Tab('Similarity') |
||
| 95 | )); |
||
| 96 | |||
| 97 | |||
| 98 | // ---- similarity tab ---- |
||
| 99 | $html = '<button class="ui-button-text-alternate ui-button-text" |
||
| 100 | id="MoreLikeThisDefaultsButton" |
||
| 101 | style="display: block;float: right;">Restore Defaults</button>'; |
||
| 102 | $defaultsButton = new LiteralField('DefaultsButton', $html); |
||
| 103 | $fields->addFieldToTab("Root.Search.Similarity", $defaultsButton); |
||
| 104 | $sortedWords = $this->SimilarityStopWords; |
||
| 105 | |||
| 106 | $stopwordsField = StringTagField::create( |
||
| 107 | 'SimilarityStopWords', |
||
| 108 | 'Stop Words for Similar Search', |
||
| 109 | $sortedWords, |
||
| 110 | $sortedWords |
||
| 111 | ); |
||
| 112 | |||
| 113 | $stopwordsField->setShouldLazyLoad(true); // tags should be lazy loaded |
||
| 114 | |||
| 115 | $fields->addFieldToTab("Root.Search.Similarity", $stopwordsField); |
||
| 116 | |||
| 117 | $lf = new LiteralField('SimilarityNotes', _t('Elastica.SIMILARITY_NOTES', |
||
| 118 | 'Default values are those used by Elastica')); |
||
| 119 | $fields->addFieldToTab("Root.Search.Similarity", $lf); |
||
| 120 | $fields->addFieldToTab("Root.Search.Similarity", new TextField('MinTermFreq', |
||
| 121 | 'The minimum term frequency below which the terms will be ignored from the input ' . |
||
| 122 | 'document. Defaults to 2.')); |
||
| 123 | $fields->addFieldToTab("Root.Search.Similarity", new TextField('MaxTermFreq', |
||
| 124 | 'The maximum number of query terms that will be selected. Increasing this value gives ' . |
||
| 125 | 'greater accuracy at the expense of query execution speed. Defaults to 25.')); |
||
| 126 | $fields->addFieldToTab("Root.Search.Similarity", new TextField('MinWordLength', |
||
| 127 | 'The minimum word length below which the terms will be ignored. Defaults to 0.')); |
||
| 128 | $fields->addFieldToTab("Root.Search.Similarity", new TextField('MinDocFreq', |
||
| 129 | 'The minimum document frequency below which the terms will be ignored from the input ' . |
||
| 130 | 'document. Defaults to 5.')); |
||
| 131 | $fields->addFieldToTab("Root.Search.Similarity", new TextField('MaxDocFreq', |
||
| 132 | 'The maximum document frequency above which the terms will be ignored from the input ' . |
||
| 133 | 'document. This could be useful in order to ignore highly frequent words such as stop ' . |
||
| 134 | 'words. Defaults to unbounded (0).')); |
||
| 135 | $fields->addFieldToTab("Root.Search.Similarity", new TextField('MinWordLength', |
||
| 136 | 'The minimum word length below which the terms will be ignored. The old name min_' . |
||
| 137 | 'word_len is deprecated. Defaults to 0.')); |
||
| 138 | $fields->addFieldToTab("Root.Search.Similarity", new TextField('MaxWordLength', |
||
| 139 | 'The maximum word length above which the terms will be ignored. The old name max_word_' . |
||
| 140 | 'len is deprecated. Defaults to unbounded (0).')); |
||
| 141 | $fields->addFieldToTab("Root.Search.Similarity", new TextField('MinShouldMatch', |
||
| 142 | 'This parameter controls the number of terms that must match. This can be either a ' . |
||
| 143 | 'number or a percentage. See ' . |
||
| 144 | 'https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html')); |
||
| 145 | |||
| 146 | // ---- search details tab ---- |
||
| 147 | $identifierField = new TextField('Identifier', |
||
| 148 | 'Identifier to allow this page to be found in form templates'); |
||
| 149 | $fields->addFieldToTab('Root.Main', $identifierField, 'Content'); |
||
| 150 | $fields->addFieldToTab('Root.Search.SearchFor', new CheckboxField('SiteTreeOnly', 'Show search results for all SiteTree objects only')); |
||
| 151 | |||
| 152 | $sql = "SELECT DISTINCT ClassName from SiteTree_Live UNION " |
||
| 153 | . "SELECT DISTINCT ClassName from SiteTree " |
||
| 154 | . "WHERE ClassName != 'ErrorPage'" |
||
| 155 | . "ORDER BY ClassName" |
||
| 156 | ; |
||
| 157 | |||
| 158 | $classes = array(); |
||
| 159 | $records = DB::query($sql); |
||
| 160 | foreach($records as $record) { |
||
| 161 | array_push($classes, $record['ClassName']); |
||
| 162 | } |
||
| 163 | $list = implode(',', $classes); |
||
| 164 | |||
| 165 | $clazzes = ''; |
||
| 166 | $clazzes = $this->ClassesToSearch; |
||
| 167 | $allSearchableClasses = SearchableClass::get()->sort('Name')->map('Name')->toArray(); |
||
| 168 | $classesToSearchField = StringTagField::create( |
||
| 169 | 'ClassesToSearch', |
||
| 170 | 'Choose which SilverStripe classes to search', |
||
| 171 | $allSearchableClasses, |
||
| 172 | $clazzes |
||
| 173 | ); |
||
| 174 | |||
| 175 | $fields->addFieldToTab('Root.Search.SearchFor', $classesToSearchField); |
||
| 176 | |||
| 177 | |||
| 178 | $html = '<div class="field text" id="SiteTreeOnlyInfo">'; |
||
| 179 | $html .= "<p>Copy the following into the above field to ensure that all SiteTree classes are searched</p>"; |
||
| 180 | $html .= '<p class="message">' . $list; |
||
| 181 | $html .= "</p></div>"; |
||
| 182 | $infoField = new LiteralField('InfoField', $html); |
||
| 183 | $fields->addFieldToTab('Root.Search.SearchFor', $infoField); |
||
| 184 | |||
| 185 | $fields->addFieldToTab('Root.Main', new HTMLEditorField('ContentForEmptySearch')); |
||
| 186 | |||
| 187 | |||
| 188 | $fields->addFieldToTab('Root.Search.SearchFor', new NumericField('ResultsPerPage', |
||
| 189 | 'The number of results to return on a page')); |
||
| 190 | $fields->addFieldToTab('Root.Search.Aggregations', new TextField('SearchHelper', |
||
| 191 | 'ClassName of object to manipulate search details and results. Leave blank for standard search')); |
||
| 192 | |||
| 193 | $ottos = AutoCompleteOption::get()->Filter('Locale', $this->Locale)->map('ID', 'Name')-> |
||
| 194 | toArray(); |
||
| 195 | $df = DropdownField::create('AutoCompleteFunctionID', 'Autocomplete Function')-> |
||
| 196 | setSource($ottos); |
||
| 197 | $df->setEmptyString('-- Please select what do do after find as you type has occurred --'); |
||
| 198 | |||
| 199 | $ottos = $this->ElasticaSearchableFields()->filter('EnableAutocomplete', 1)->Map('ID', 'Name')->toArray(); |
||
| 200 | $autoCompleteFieldDF = DropDownField::create('AutoCompleteFieldID', 'Field to use for autocomplete')->setSource($ottos); |
||
| 201 | $autoCompleteFieldDF->setEmptyString('-- Please select which field to use for autocomplete --'); |
||
| 202 | |||
| 203 | $fields->addFieldToTab("Root.Search.AutoComplete", |
||
| 204 | FieldGroup::create( |
||
| 205 | $autoCompleteFieldDF, |
||
| 206 | $df |
||
| 207 | )->setTitle('Autocomplete') |
||
| 208 | ); |
||
| 209 | |||
| 210 | // ---- grid of searchable fields ---- |
||
| 211 | $html = '<p id="SearchFieldIntro">' . _t('SiteConfig.ELASTICA_SEARCH_INFO', |
||
| 212 | "Select a field to edit it's properties") . '</p>'; |
||
| 213 | $fields->addFieldToTab('Root.Search.Fields', $h1 = new LiteralField('SearchInfo', $html)); |
||
| 214 | $searchPicker = new PickerField('ElasticaSearchableFields', 'Searchable Fields', |
||
| 215 | $this->ElasticaSearchableFields()->filter('Active', 1)->sort('Name')); |
||
| 216 | |||
| 217 | $fields->addFieldToTab('Root.Search.Fields', $searchPicker); |
||
| 218 | |||
| 219 | $pickerConfig = $searchPicker->getConfig(); |
||
| 220 | |||
| 221 | $pickerConfig->removeComponentsByType(new GridFieldAddNewButton()); |
||
| 222 | $pickerConfig->removeComponentsByType(new GridFieldDeleteAction()); |
||
| 223 | $pickerConfig->removeComponentsByType(new PickerFieldAddExistingSearchButton()); |
||
| 224 | $pickerConfig->getComponentByType('GridFieldPaginator')->setItemsPerPage(100); |
||
| 225 | |||
| 226 | $searchPicker->enableEdit(); |
||
| 227 | $edittest = $pickerConfig->getComponentByType('GridFieldDetailForm'); |
||
| 228 | $edittest->setFields(FieldList::create( |
||
| 229 | TextField::create('Name', 'Field Name'), |
||
| 230 | TextField::create('ClazzName', 'Class'), |
||
| 231 | HiddenField::create('Autocomplete', 'This can be autocompleted'), |
||
| 232 | CheckboxField::create('ManyMany[Searchable]', 'Use for normal searching'), |
||
| 233 | CheckboxField::create('ManyMany[SimilarSearchable]', 'Use for similar search'), |
||
| 234 | NumericField::create('ManyMany[Weight]', 'Weighting'), |
||
| 235 | CheckboxField::create('ShowHighlights', 'Show highlights from search in results for this field'), |
||
| 236 | CheckboxField::create('ManyMany[EnableAutocomplete]', 'Enable Autocomplete') |
||
| 237 | )); |
||
| 238 | |||
| 239 | $edittest->setItemEditFormCallback(function($form) { |
||
| 240 | $fields = $form->Fields(); |
||
| 241 | $fieldAutocomplete = $fields->dataFieldByName('Autocomplete'); |
||
| 242 | $fieldEnableAutcomplete = $fields->dataFieldByName('ManyMany[EnableAutocomplete]'); |
||
| 243 | |||
| 244 | $fields->dataFieldByName('ClazzName')->setReadOnly(true); |
||
| 245 | $fields->dataFieldByName('ClazzName')->setDisabled(true); |
||
| 246 | $fields->dataFieldByName('Name')->setReadOnly(true); |
||
| 247 | $fields->dataFieldByName('Name')->setDisabled(true); |
||
| 248 | |||
| 249 | if(!$fieldAutocomplete->Value() == '1') { |
||
| 250 | $fieldEnableAutcomplete->setDisabled(true); |
||
| 251 | $fieldEnableAutcomplete->setReadOnly(true); |
||
| 252 | $fieldEnableAutcomplete->setTitle("Autcomplete is not available for this field"); |
||
| 253 | } |
||
| 254 | |||
| 255 | }); |
||
| 256 | |||
| 257 | |||
| 258 | // What do display on the grid of searchable fields |
||
| 259 | $dataColumns = $pickerConfig->getComponentByType('GridFieldDataColumns'); |
||
| 260 | $dataColumns->setDisplayFields(array( |
||
| 261 | 'Name' => 'Name', |
||
| 262 | 'ClazzName' => 'Class', |
||
| 263 | 'Type' => 'Type', |
||
| 264 | 'Searchable' => 'Use for Search?', |
||
| 265 | 'SimilarSearchable' => 'Use for Similar Search?', |
||
| 266 | 'ShowHighlights' => 'Show Search Highlights', |
||
| 267 | 'Weight' => 'Weighting' |
||
| 268 | )); |
||
| 269 | |||
| 270 | return $fields; |
||
| 271 | } |
||
| 272 | |||
| 430 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.