| Conditions | 3 |
| Paths | 1 |
| Total Lines | 170 |
| Code Lines | 122 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 151 | public function getCMSFields() |
||
| 152 | { |
||
| 153 | $this->beforeUpdateCMSFields(function (FieldList $fields) { |
||
| 154 | $fields->removeByName([ |
||
| 155 | 'Images', |
||
| 156 | 'WeightModifier', |
||
| 157 | 'CodeModifier', |
||
| 158 | 'PriceModifier', |
||
| 159 | 'WeightModifierAction', |
||
| 160 | 'CodeModifierAction', |
||
| 161 | 'PriceModifierAction', |
||
| 162 | 'Available', |
||
| 163 | 'Type', |
||
| 164 | 'OptionModifierKey', |
||
| 165 | 'SortOrder', |
||
| 166 | 'ProductID', |
||
| 167 | 'FinalPrice', |
||
| 168 | 'FinalWeight', |
||
| 169 | 'FinalCode', |
||
| 170 | ]); |
||
| 171 | |||
| 172 | $fields->insertBefore( |
||
| 173 | 'Content', |
||
| 174 | CheckboxField::create('Available') |
||
| 175 | ->setTitle('Available for purchase') |
||
| 176 | ); |
||
| 177 | |||
| 178 | $fields->insertBefore( |
||
| 179 | 'Content', |
||
| 180 | $fields->dataFieldByName('VariationTypeID') |
||
| 181 | ); |
||
| 182 | |||
| 183 | $fields->insertBefore( |
||
| 184 | 'Content', |
||
| 185 | $fields->dataFieldByName('UseProductContent') |
||
| 186 | ); |
||
| 187 | |||
| 188 | $content = $fields->dataFieldByName('Content'); |
||
| 189 | |||
| 190 | $content->hideUnless('UseProductContent')->isNotChecked()->end(); |
||
| 191 | |||
| 192 | if ($this->exists()) { |
||
| 193 | $fields->addFieldToTab( |
||
| 194 | 'Root.ProductModifications', |
||
| 195 | ReadonlyField::create('OptionModifierKey') |
||
| 196 | ->setTitle(_t('Variation.ModifierKey', 'Modifier Key')) |
||
| 197 | ); |
||
| 198 | } |
||
| 199 | |||
| 200 | if ($this->Product()->hasDatabaseField('Weight')) { |
||
|
|
|||
| 201 | $fields->addFieldtoTab( |
||
| 202 | 'Root.ProductModifications', |
||
| 203 | FieldGroup::create( |
||
| 204 | DropdownField::create( |
||
| 205 | 'WeightModifierAction', |
||
| 206 | _t('Variation.WeightModifierAction', 'Weight Modification Type'), |
||
| 207 | [ |
||
| 208 | 'Add' => _t( |
||
| 209 | 'Variation.WeightAdd', |
||
| 210 | 'Add to Base Weight', |
||
| 211 | 'Add to weight' |
||
| 212 | ), |
||
| 213 | 'Subtract' => _t( |
||
| 214 | 'Variation.WeightSubtract', |
||
| 215 | 'Subtract from Base Weight', |
||
| 216 | 'Subtract from weight' |
||
| 217 | ), |
||
| 218 | 'Set' => _t('Variation.WeightSet', 'Set as a new Weight'), |
||
| 219 | ] |
||
| 220 | ) |
||
| 221 | ->setEmptyString('') |
||
| 222 | ->setDescription(_t( |
||
| 223 | 'Variation.WeightDescription', |
||
| 224 | 'Does weight modify or replace base weight?' |
||
| 225 | )), |
||
| 226 | NumericField::create("WeightModifier") |
||
| 227 | ->setTitle(_t('Variation.WeightModifier', 'Weight')) |
||
| 228 | ->setScale(3) |
||
| 229 | ->setDescription(_t( |
||
| 230 | 'Variation.WeightDescription', |
||
| 231 | 'Only supports up to 3 decimal places' |
||
| 232 | ))->displayIf('WeightModifierAction')->isNotEmpty()->end(), |
||
| 233 | NumericField::create('FinalWeight') |
||
| 234 | ->setTitle('Final Modified Weight') |
||
| 235 | ->setDescription("Product's weight is {$this->Product()->Weight}") |
||
| 236 | ->performDisabledTransformation() |
||
| 237 | )->setTitle('Weight Modification') |
||
| 238 | ); |
||
| 239 | } |
||
| 240 | |||
| 241 | $fields->addFieldsToTab( |
||
| 242 | 'Root.ProductModifications', |
||
| 243 | [ |
||
| 244 | // Price Modifier Fields |
||
| 245 | //HeaderField::create('PriceHD', _t('Variation.PriceHD', 'Modify Price'), 4), |
||
| 246 | FieldGroup::create( |
||
| 247 | DropdownField::create( |
||
| 248 | 'PriceModifierAction', |
||
| 249 | _t('Variation.PriceModifierAction', 'Price Modification Type'), |
||
| 250 | [ |
||
| 251 | 'Add' => _t( |
||
| 252 | 'Variation.PriceAdd', |
||
| 253 | 'Add to Base Price', |
||
| 254 | 'Add to price' |
||
| 255 | ), |
||
| 256 | 'Subtract' => _t( |
||
| 257 | 'Variation.PriceSubtract', |
||
| 258 | 'Subtract from Base Price', |
||
| 259 | 'Subtract from price' |
||
| 260 | ), |
||
| 261 | 'Set' => _t('Variation.PriceSet', 'Set as a new Price'), |
||
| 262 | ] |
||
| 263 | ) |
||
| 264 | ->setEmptyString('') |
||
| 265 | ->setDescription(_t('Variation.PriceDescription', 'Does price modify or replace base price?')), |
||
| 266 | CurrencyField::create('PriceModifier') |
||
| 267 | ->setTitle(_t('Variation.PriceModifier', 'Price')) |
||
| 268 | ->displayIf('PriceModifierAction')->isNotEmpty()->end(), |
||
| 269 | CurrencyField::create('FinalPrice') |
||
| 270 | ->setTitle('Final Modified Price') |
||
| 271 | ->setDescription("Product's price is {$this->Product()->Price}") |
||
| 272 | ->performDisabledTransformation() |
||
| 273 | )->setTitle('Price Modifications'), |
||
| 274 | |||
| 275 | // Code Modifier Fields |
||
| 276 | //HeaderField::create('CodeHD', _t('Variation.CodeHD', 'Modify Code'), 4), |
||
| 277 | FieldGroup::create( |
||
| 278 | DropdownField::create( |
||
| 279 | 'CodeModifierAction', |
||
| 280 | _t('Variation.CodeModifierAction', 'Code Modification Type'), |
||
| 281 | [ |
||
| 282 | 'Add' => _t( |
||
| 283 | 'Variation.CodeAdd', |
||
| 284 | 'Add to Base Code', |
||
| 285 | 'Add to code' |
||
| 286 | ), |
||
| 287 | 'Subtract' => _t( |
||
| 288 | 'Variation.CodeSubtract', |
||
| 289 | 'Subtract from Base Code', |
||
| 290 | 'Subtract from code' |
||
| 291 | ), |
||
| 292 | 'Set' => _t('Variation.CodeSet', 'Set as a new Code'), |
||
| 293 | ] |
||
| 294 | ) |
||
| 295 | ->setEmptyString('') |
||
| 296 | ->setDescription(_t('Variation.CodeDescription', 'Does code modify or replace base code?')), |
||
| 297 | TextField::create('CodeModifier') |
||
| 298 | ->setTitle(_t('Variation.CodeModifier', 'Code')) |
||
| 299 | ->displayIf('CodeModifierAction')->isNotEmpty()->end(), |
||
| 300 | TextField::create('FinalCode') |
||
| 301 | ->setTitle('Final Modified Code') |
||
| 302 | ->setDescription("Product's code is {$this->Product()->Code}") |
||
| 303 | ->performDisabledTransformation() |
||
| 304 | )->setTitle('Code Modification'), |
||
| 305 | ] |
||
| 306 | ); |
||
| 307 | |||
| 308 | // Images tab |
||
| 309 | $images = SortableUploadField::create('Images') |
||
| 310 | ->setSortColumn('SortOrder') |
||
| 311 | ->setIsMultiUpload(true) |
||
| 312 | ->setAllowedExtensions($this->config()->get('allowed_images_extensions')) |
||
| 313 | ->setFolderName('Uploads/Products/Images'); |
||
| 314 | |||
| 315 | $fields->addFieldsToTab('Root.Images', [ |
||
| 316 | $images, |
||
| 317 | ]); |
||
| 318 | }); |
||
| 319 | |||
| 320 | return parent::getCMSFields(); |
||
| 321 | } |
||
| 581 |