| Conditions | 15 |
| Paths | 144 |
| Total Lines | 91 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 2 |
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 |
||
| 161 | public function updateImportForm(Form $form) |
||
| 162 | { |
||
| 163 | /** @var ModelAdmin $owner */ |
||
| 164 | $owner = $this->owner; |
||
| 165 | $class = $owner->getModelClass(); |
||
| 166 | $classConfig = $owner->config(); |
||
| 167 | |||
| 168 | $modelSNG = singleton($class); |
||
| 169 | $modelConfig = $modelSNG->config(); |
||
| 170 | $modelName = $modelSNG->i18n_singular_name(); |
||
| 171 | |||
| 172 | $fields = $form->Fields(); |
||
| 173 | |||
| 174 | // We can implement a custom handler |
||
| 175 | $importHandlers = []; |
||
| 176 | $htmlDesc = ''; |
||
| 177 | $useDefaultSample = true; |
||
| 178 | if ($modelSNG->hasMethod('listImportHandlers')) { |
||
| 179 | $importHandlers = array_merge([ |
||
| 180 | 'default' => _t('ExcelImportExport.DefaultHandler', 'Default import handler'), |
||
| 181 | ], $modelSNG->listImportHandlers()); |
||
| 182 | |||
| 183 | $supportOnlyUpdate = []; |
||
| 184 | foreach ($importHandlers as $class => $label) { |
||
| 185 | if (!class_exists($class)) { |
||
| 186 | continue; |
||
| 187 | } |
||
| 188 | if (method_exists($class, 'setOnlyUpdate')) { |
||
| 189 | $supportOnlyUpdate[] = $class; |
||
| 190 | } |
||
| 191 | if (method_exists($class, 'getImportDescription')) { |
||
| 192 | $htmlDesc .= '<div class="js-import-desc" data-name="' . $class . '" hidden>' . $class::getImportDescription() . '</div>'; |
||
| 193 | } |
||
| 194 | if (method_exists($class, 'getSampleFileLink')) { |
||
| 195 | $useDefaultSample = false; |
||
| 196 | $htmlDesc .= '<div class="js-import-desc" data-name="' . $class . '" hidden>' . $class::getSampleFileLink() . '</div>'; |
||
| 197 | } |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | /** @var \SilverStripe\Forms\FileField|null $file */ |
||
| 202 | $file = $fields->dataFieldByName('_CsvFile'); |
||
| 203 | if ($file) { |
||
| 204 | $csvDescription = ExcelImportExport::getValidExtensionsText(); |
||
| 205 | if ($useDefaultSample) { |
||
| 206 | $downloadSample = ExcelImportExport::createDownloadSampleLink(); |
||
| 207 | $csvDescription .= '. ' . $downloadSample; |
||
| 208 | } |
||
| 209 | $file->setDescription($csvDescription); |
||
| 210 | $file->getValidator()->setAllowedExtensions(ExcelImportExport::getValidExtensions()); |
||
| 211 | } |
||
| 212 | |||
| 213 | // Hide by default, but allow on per class basis (opt-in) |
||
| 214 | if ($classConfig->hide_replace_data && !$modelConfig->show_replace_data) { |
||
| 215 | // This is way too dangerous and customers don't understand what this is most of the time |
||
| 216 | $fields->removeByName("EmptyBeforeImport"); |
||
| 217 | } |
||
| 218 | // If you cannot delete, you cannot empty |
||
| 219 | if (!$modelSNG->canDelete()) { |
||
| 220 | $fields->removeByName('EmptyBeforeImport'); |
||
| 221 | } |
||
| 222 | |||
| 223 | // We moved the specs into a nice to use download sample button |
||
| 224 | $fields->removeByName("SpecFor{$modelName}"); |
||
| 225 | |||
| 226 | if (!empty($importHandlers)) { |
||
| 227 | $form->Fields()->push($OnlyUpdateRecords = new CheckboxField("OnlyUpdateRecords", _t('ExcelImportExport.OnlyUpdateRecords', "Only update records"))); |
||
| 228 | $OnlyUpdateRecords->setAttribute("data-handlers", implode(",", $supportOnlyUpdate)); |
||
| 229 | |||
| 230 | $form->Fields()->push($ImportHandler = new OptionsetField("ImportHandler", _t('ExcelImportExport.PleaseSelectImportHandler', "Please select the import handler"), $importHandlers)); |
||
| 231 | // Simply check of this is supported or not for the given handler (if not, disable it) |
||
| 232 | $js = <<<JS |
||
| 233 | var desc=document.querySelectorAll('.js-import-desc');var cb=document.querySelector('#OnlyUpdateRecords');var accepted=cb.dataset.handlers.split(',');var item=([...this.querySelectorAll('input')].filter((input) => input.checked)[0]); cb.disabled=(item && accepted.includes(item.value)) ? '': 'disabled';desc.forEach((el)=>el.hidden=!item||el.dataset.name!=item.value);; |
||
| 234 | JS; |
||
| 235 | $ImportHandler->setAttribute("onclick", $js); |
||
| 236 | if ($htmlDesc) { |
||
| 237 | $ImportHandler->setDescription($htmlDesc); // Description is an HTMLFragment |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | $actions = $form->Actions(); |
||
| 242 | |||
| 243 | // Update import button |
||
| 244 | $import = $actions->dataFieldByName('action_import'); |
||
| 245 | if ($import) { |
||
| 246 | $import->setTitle(_t( |
||
| 247 | 'ExcelImportExport.ImportExcel', |
||
| 248 | "Import from Excel" |
||
| 249 | )); |
||
| 250 | $import->removeExtraClass('btn-outline-secondary'); |
||
| 251 | $import->addExtraClass('btn-primary'); |
||
| 252 | } |
||
| 255 |