| Conditions | 11 |
| Paths | 64 |
| Total Lines | 77 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 103 | public function updateImportForm(Form $form) |
||
| 104 | { |
||
| 105 | /** @var ModelAdmin $owner */ |
||
| 106 | $owner = $this->owner; |
||
| 107 | $class = $owner->modelClass; |
||
| 108 | $classConfig = $owner->config(); |
||
| 109 | |||
| 110 | $modelSNG = singleton($class); |
||
| 111 | $modelConfig = $modelSNG->config(); |
||
| 112 | $modelName = $modelSNG->i18n_singular_name(); |
||
| 113 | |||
| 114 | $fields = $form->Fields(); |
||
| 115 | |||
| 116 | $downloadSampleLink = $owner->Link(str_replace('\\', '-', $class) . '/downloadsample'); |
||
| 117 | $downloadSample = '<a href="' . $downloadSampleLink . '" class="no-ajax" target="_blank">' . _t( |
||
| 118 | 'ExcelImportExport.DownloadSample', |
||
| 119 | 'Download sample file' |
||
| 120 | ) . '</a>'; |
||
| 121 | |||
| 122 | $file = $fields->dataFieldByName('_CsvFile'); |
||
| 123 | if ($file) { |
||
| 124 | $csvDescription = ExcelImportExport::getValidExtensionsText(); |
||
| 125 | $csvDescription .= '. ' . $downloadSample; |
||
| 126 | $file->setDescription($csvDescription); |
||
| 127 | $file->getValidator()->setAllowedExtensions(ExcelImportExport::getValidExtensions()); |
||
| 128 | } |
||
| 129 | |||
| 130 | // Hide by default, but allow on per class basis (opt-in) |
||
| 131 | if ($classConfig->hide_replace_data && !$modelConfig->show_replace_data) { |
||
| 132 | // This is way too dangerous and customers don't understand what this is most of the time |
||
| 133 | $fields->removeByName("EmptyBeforeImport"); |
||
| 134 | } |
||
| 135 | // If you cannot delete, you cannot empty |
||
| 136 | if (!$modelSNG->canDelete()) { |
||
| 137 | $fields->removeByName('EmptyBeforeImport'); |
||
| 138 | } |
||
| 139 | |||
| 140 | // We moved the specs into a nice to use download sample button |
||
| 141 | $fields->removeByName("SpecFor{$modelName}"); |
||
| 142 | |||
| 143 | // We can implement a custom handler |
||
| 144 | $importHandlers = []; |
||
| 145 | if ($modelSNG->hasMethod('listImportHandlers')) { |
||
| 146 | $importHandlers = array_merge([ |
||
| 147 | 'default' => _t('ExcelImportExport.DefaultHandler', 'Default import handler'), |
||
| 148 | ], $modelSNG->listImportHandlers()); |
||
| 149 | |||
| 150 | $supportOnlyUpdate = []; |
||
| 151 | foreach ($importHandlers as $class => $label) { |
||
| 152 | if (class_exists($class) && method_exists($class, 'setOnlyUpdate')) { |
||
| 153 | $supportOnlyUpdate[] = $class; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | $form->Fields()->push($OnlyUpdateRecords = new CheckboxField("OnlyUpdateRecords", _t('ExcelImportExport.OnlyUpdateRecords', "Only update records"))); |
||
| 158 | $OnlyUpdateRecords->setAttribute("data-handlers", implode(",", $supportOnlyUpdate)); |
||
| 159 | } |
||
| 160 | if (!empty($importHandlers)) { |
||
| 161 | $form->Fields()->push($ImportHandler = new OptionsetField("ImportHandler", _t('ExcelImportExport.PleaseSelectImportHandler', "Please select the import handler"), $importHandlers)); |
||
| 162 | // Simply check of this is supported or not for the given handler (if not, disable it) |
||
| 163 | $js = <<<JS |
||
| 164 | 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'; |
||
| 165 | JS; |
||
| 166 | $ImportHandler->setAttribute("onclick", $js); |
||
| 167 | } |
||
| 168 | |||
| 169 | $actions = $form->Actions(); |
||
| 170 | |||
| 171 | // Update import button |
||
| 172 | $import = $actions->dataFieldByName('action_import'); |
||
| 173 | if ($import) { |
||
| 174 | $import->setTitle(_t( |
||
| 175 | 'ExcelImportExport.ImportExcel', |
||
| 176 | "Import from Excel" |
||
| 177 | )); |
||
| 178 | $import->removeExtraClass('btn-outline-secondary'); |
||
| 179 | $import->addExtraClass('btn-primary'); |
||
| 180 | } |
||
| 183 |