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