Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EmailRecipient often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EmailRecipient, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class EmailRecipient extends DataObject |
||
| 46 | { |
||
| 47 | private static $db = [ |
||
| 48 | 'EmailAddress' => 'Varchar(200)', |
||
| 49 | 'EmailSubject' => 'Varchar(200)', |
||
| 50 | 'EmailFrom' => 'Varchar(200)', |
||
| 51 | 'EmailReplyTo' => 'Varchar(200)', |
||
| 52 | 'EmailBody' => 'Text', |
||
| 53 | 'EmailBodyHtml' => 'HTMLText', |
||
| 54 | 'EmailTemplate' => 'Varchar', |
||
| 55 | 'SendPlain' => 'Boolean', |
||
| 56 | 'HideFormData' => 'Boolean', |
||
| 57 | 'CustomRulesCondition' => 'Enum("And,Or")' |
||
| 58 | ]; |
||
| 59 | |||
| 60 | private static $has_one = [ |
||
| 61 | 'Form' => UserDefinedForm::class, |
||
| 62 | 'SendEmailFromField' => EditableFormField::class, |
||
| 63 | 'SendEmailToField' => EditableFormField::class, |
||
| 64 | 'SendEmailSubjectField' => EditableFormField::class |
||
| 65 | ]; |
||
| 66 | |||
| 67 | private static $has_many = [ |
||
| 68 | 'CustomRules' => EmailRecipientCondition::class, |
||
| 69 | ]; |
||
| 70 | |||
| 71 | private static $owns = [ |
||
| 72 | 'CustomRules', |
||
| 73 | ]; |
||
| 74 | |||
| 75 | private static $cascade_deetes = [ |
||
| 76 | 'CustomRules', |
||
| 77 | ]; |
||
| 78 | |||
| 79 | private static $summary_fields = [ |
||
| 80 | 'EmailAddress', |
||
| 81 | 'EmailSubject', |
||
| 82 | 'EmailFrom' |
||
| 83 | ]; |
||
| 84 | |||
| 85 | private static $table_name = 'UserDefinedForm_EmailRecipient'; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Setting this to true will allow you to select "risky" fields as |
||
| 89 | * email recipient, such as free-text entry fields. |
||
| 90 | * |
||
| 91 | * It's advisable to leave this off. |
||
| 92 | * |
||
| 93 | * @config |
||
| 94 | * @var bool |
||
| 95 | */ |
||
| 96 | private static $allow_unbound_recipient_fields = false; |
||
| 97 | |||
| 98 | public function summaryFields() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Get instance of UserDefinedForm when editing in getCMSFields |
||
| 115 | * |
||
| 116 | * @return UserDefinedFrom |
||
| 117 | */ |
||
| 118 | protected function getFormParent() |
||
| 125 | |||
| 126 | public function getTitle() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Generate a gridfield config for editing filter rules |
||
| 139 | * |
||
| 140 | * @return GridFieldConfig |
||
| 141 | */ |
||
| 142 | protected function getRulesConfig() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @return FieldList |
||
| 173 | */ |
||
| 174 | public function getCMSFields() |
||
| 175 | { |
||
| 176 | Requirements::javascript( |
||
| 177 | ModuleLoader::getModule('silverstripe/userforms')->getRelativeResourcePath('javascript/Recipient.js') |
||
| 178 | ); |
||
| 179 | |||
| 180 | // Determine optional field values |
||
| 181 | $form = $this->getFormParent(); |
||
| 182 | |||
| 183 | // predefined choices are also candidates |
||
| 184 | $multiOptionFields = EditableMultipleOptionField::get()->filter('ParentID', $form->ID); |
||
| 185 | |||
| 186 | // if they have email fields then we could send from it |
||
| 187 | $validEmailFromFields = EditableEmailField::get()->filter('ParentID', $form->ID); |
||
| 188 | |||
| 189 | // For the subject, only one-line entry boxes make sense |
||
| 190 | $validSubjectFields = ArrayList::create( |
||
| 191 | EditableTextField::get() |
||
| 192 | ->filter('ParentID', $form->ID) |
||
| 193 | ->exclude('Rows:GreaterThan', 1) |
||
| 194 | ->toArray() |
||
| 195 | ); |
||
| 196 | $validSubjectFields->merge($multiOptionFields); |
||
| 197 | |||
| 198 | |||
| 199 | // Check valid email-recipient fields |
||
| 200 | if ($this->config()->get('allow_unbound_recipient_fields')) { |
||
| 201 | // To address can only be email fields or multi option fields |
||
| 202 | $validEmailToFields = ArrayList::create($validEmailFromFields->toArray()); |
||
| 203 | $validEmailToFields->merge($multiOptionFields); |
||
| 204 | } else { |
||
| 205 | // To address cannot be unbound, so restrict to pre-defined lists |
||
| 206 | $validEmailToFields = $multiOptionFields; |
||
| 207 | } |
||
| 208 | |||
| 209 | // Build fieldlist |
||
| 210 | $fields = FieldList::create(Tabset::create('Root')->addExtraClass('EmailRecipientForm')); |
||
| 211 | |||
| 212 | // Configuration fields |
||
| 213 | $fields->addFieldsToTab('Root.EmailDetails', [ |
||
| 214 | // Subject |
||
| 215 | FieldGroup::create( |
||
| 216 | TextField::create( |
||
| 217 | 'EmailSubject', |
||
| 218 | _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.TYPESUBJECT', 'Type subject') |
||
| 219 | ) |
||
| 220 | ->setAttribute('style', 'min-width: 400px;'), |
||
| 221 | DropdownField::create( |
||
| 222 | 'SendEmailSubjectFieldID', |
||
| 223 | _t( |
||
| 224 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.SELECTAFIELDTOSETSUBJECT', |
||
| 225 | '.. or select a field to use as the subject' |
||
| 226 | ), |
||
| 227 | $validSubjectFields->map('ID', 'Title') |
||
| 228 | )->setEmptyString('') |
||
| 229 | ) |
||
| 230 | ->setTitle(_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILSUBJECT', 'Email subject')), |
||
| 231 | |||
| 232 | // To |
||
| 233 | FieldGroup::create( |
||
| 234 | TextField::create( |
||
| 235 | 'EmailAddress', |
||
| 236 | _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.TYPETO', 'Type to address') |
||
| 237 | ) |
||
| 238 | ->setAttribute('style', 'min-width: 400px;'), |
||
| 239 | DropdownField::create( |
||
| 240 | 'SendEmailToFieldID', |
||
| 241 | _t( |
||
| 242 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.ORSELECTAFIELDTOUSEASTO', |
||
| 243 | '.. or select a field to use as the to address' |
||
| 244 | ), |
||
| 245 | $validEmailToFields->map('ID', 'Title') |
||
| 246 | )->setEmptyString(' ') |
||
| 247 | ) |
||
| 248 | ->setTitle(_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.SENDEMAILTO', 'Send email to')) |
||
| 249 | ->setDescription(_t( |
||
| 250 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.SENDEMAILTO_DESCRIPTION', |
||
| 251 | 'You may enter multiple email addresses as a comma separated list.' |
||
| 252 | )), |
||
| 253 | |||
| 254 | |||
| 255 | // From |
||
| 256 | TextField::create( |
||
| 257 | 'EmailFrom', |
||
| 258 | _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.FROMADDRESS', 'Send email from') |
||
| 259 | ) |
||
| 260 | ->setDescription(_t( |
||
| 261 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.EmailFromContent', |
||
| 262 | "The from address allows you to set who the email comes from. On most servers this ". |
||
| 263 | "will need to be set to an email address on the same domain name as your site. ". |
||
| 264 | "For example on yoursite.com the from address may need to be [email protected]. ". |
||
| 265 | "You can however, set any email address you wish as the reply to address." |
||
| 266 | )), |
||
| 267 | |||
| 268 | |||
| 269 | // Reply-To |
||
| 270 | FieldGroup::create( |
||
| 271 | TextField::create('EmailReplyTo', _t( |
||
| 272 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.TYPEREPLY', |
||
| 273 | 'Type reply address' |
||
| 274 | )) |
||
| 275 | ->setAttribute('style', 'min-width: 400px;'), |
||
| 276 | DropdownField::create( |
||
| 277 | 'SendEmailFromFieldID', |
||
| 278 | _t( |
||
| 279 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.ORSELECTAFIELDTOUSEASFROM', |
||
| 280 | '.. or select a field to use as reply to address' |
||
| 281 | ), |
||
| 282 | $validEmailFromFields->map('ID', 'Title') |
||
| 283 | )->setEmptyString(' ') |
||
| 284 | ) |
||
| 285 | ->setTitle(_t( |
||
| 286 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.REPLYADDRESS', |
||
| 287 | 'Email for reply to' |
||
| 288 | )) |
||
| 289 | ->setDescription(_t( |
||
| 290 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.REPLYADDRESS_DESCRIPTION', |
||
| 291 | 'The email address which the recipient is able to \'reply\' to.' |
||
| 292 | )) |
||
| 293 | ]); |
||
| 294 | |||
| 295 | $fields->fieldByName('Root.EmailDetails')->setTitle(_t(__CLASS__.'.EMAILDETAILSTAB', 'Email Details')); |
||
| 296 | |||
| 297 | // Only show the preview link if the recipient has been saved. |
||
| 298 | if (!empty($this->EmailTemplate)) { |
||
| 299 | $preview = sprintf( |
||
| 300 | '<p><a href="%s" target="_blank" class="ss-ui-button">%s</a></p><em>%s</em>', |
||
| 301 | Controller::join_links( |
||
| 302 | singleton(CMSPageEditController::class)->getEditForm()->FormAction(), |
||
| 303 | "field/EmailRecipients/item/{$this->ID}/preview" |
||
| 304 | ), |
||
| 305 | _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.PREVIEW_EMAIL', 'Preview email'), |
||
| 306 | _t( |
||
| 307 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.PREVIEW_EMAIL_DESCRIPTION', |
||
| 308 | 'Note: Unsaved changes will not appear in the preview.' |
||
| 309 | ) |
||
| 310 | ); |
||
| 311 | } else { |
||
| 312 | $preview = sprintf( |
||
| 313 | '<em>%s</em>', |
||
| 314 | _t( |
||
| 315 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.PREVIEW_EMAIL_UNAVAILABLE', |
||
| 316 | 'You can preview this email once you have saved the Recipient.' |
||
| 317 | ) |
||
| 318 | ); |
||
| 319 | } |
||
| 320 | |||
| 321 | // Email templates |
||
| 322 | $fields->addFieldsToTab('Root.EmailContent', [ |
||
| 323 | CheckboxField::create( |
||
| 324 | 'HideFormData', |
||
| 325 | _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.HIDEFORMDATA', 'Hide form data from email?') |
||
| 326 | ), |
||
| 327 | CheckboxField::create( |
||
| 328 | 'SendPlain', |
||
| 329 | _t( |
||
| 330 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.SENDPLAIN', |
||
| 331 | 'Send email as plain text? (HTML will be stripped)' |
||
| 332 | ) |
||
| 333 | ), |
||
| 334 | DropdownField::create( |
||
| 335 | 'EmailTemplate', |
||
| 336 | _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILTEMPLATE', 'Email template'), |
||
| 337 | $this->getEmailTemplateDropdownValues() |
||
| 338 | )->addExtraClass('toggle-html-only'), |
||
| 339 | HTMLEditorField::create( |
||
| 340 | 'EmailBodyHtml', |
||
| 341 | _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILBODYHTML', 'Body') |
||
| 342 | ) |
||
| 343 | ->addExtraClass('toggle-html-only'), |
||
| 344 | TextareaField::create( |
||
| 345 | 'EmailBody', |
||
| 346 | _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILBODY', 'Body') |
||
| 347 | ) |
||
| 348 | ->addExtraClass('toggle-plain-only'), |
||
| 349 | LiteralField::create('EmailPreview', $preview) |
||
| 350 | ]); |
||
| 351 | |||
| 352 | $fields->fieldByName('Root.EmailContent')->setTitle(_t(__CLASS__.'.EMAILCONTENTTAB', 'Email Content')); |
||
| 353 | |||
| 354 | // Custom rules for sending this field |
||
| 355 | $grid = GridField::create( |
||
| 356 | 'CustomRules', |
||
| 357 | _t('SilverStripe\\UserForms\\Model\\EditableFormField.CUSTOMRULES', 'Custom Rules'), |
||
| 358 | $this->CustomRules(), |
||
| 359 | $this->getRulesConfig() |
||
| 360 | ); |
||
| 361 | $grid->setDescription(_t( |
||
| 362 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.RulesDescription', |
||
| 363 | 'Emails will only be sent to the recipient if the custom rules are met. If no rules are defined, this receipient will receive notifications for every submission.' |
||
| 364 | )); |
||
| 365 | $fields->addFieldsToTab('Root.CustomRules', [ |
||
| 366 | DropdownField::create( |
||
| 367 | 'CustomRulesCondition', |
||
| 368 | _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.SENDIF', 'Send condition'), |
||
| 369 | [ |
||
| 370 | 'Or' => _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.SENDIFOR', 'Any conditions are true'), |
||
| 371 | 'And' => _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.SENDIFAND', 'All conditions are true') |
||
| 372 | ] |
||
| 373 | ), |
||
| 374 | $grid |
||
| 375 | ]); |
||
| 376 | |||
| 377 | $fields->fieldByName('Root.CustomRules')->setTitle(_t(__CLASS__.'.CUSTOMRULESTAB', 'Custom Rules')); |
||
| 378 | |||
| 379 | $this->extend('updateCMSFields', $fields); |
||
| 380 | return $fields; |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Return whether a user can create an object of this type |
||
| 385 | * |
||
| 386 | * @param Member $member |
||
| 387 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
| 388 | * @return bool |
||
| 389 | */ |
||
| 390 | View Code Duplication | public function canCreate($member = null, $context = []) |
|
| 391 | { |
||
| 392 | // Check parent page |
||
| 393 | $parent = $this->getCanCreateContext(func_get_args()); |
||
| 394 | if ($parent) { |
||
| 395 | return $parent->canEdit($member); |
||
| 396 | } |
||
| 397 | |||
| 398 | // Fall back to secure admin permissions |
||
| 399 | return parent::canCreate($member); |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Helper method to check the parent for this object |
||
| 404 | * |
||
| 405 | * @param array $args List of arguments passed to canCreate |
||
| 406 | * @return SiteTree Parent page instance |
||
| 407 | */ |
||
| 408 | protected function getCanCreateContext($args) |
||
| 409 | { |
||
| 410 | // Inspect second parameter to canCreate for a 'Parent' context |
||
| 411 | if (isset($args[1][Form::class])) { |
||
| 412 | return $args[1][Form::class]; |
||
| 413 | } |
||
| 414 | // Hack in currently edited page if context is missing |
||
| 415 | if (Controller::has_curr() && Controller::curr() instanceof CMSMain) { |
||
| 416 | return Controller::curr()->currentPage(); |
||
| 417 | } |
||
| 418 | |||
| 419 | // No page being edited |
||
| 420 | return null; |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param Member |
||
| 425 | * |
||
| 426 | * @return boolean |
||
| 427 | */ |
||
| 428 | public function canView($member = null) |
||
| 429 | { |
||
| 430 | return $this->Form()->canView($member); |
||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @param Member |
||
| 435 | * |
||
| 436 | * @return boolean |
||
| 437 | */ |
||
| 438 | public function canEdit($member = null) |
||
| 439 | { |
||
| 440 | return $this->Form()->canEdit($member); |
||
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @param Member |
||
| 445 | * |
||
| 446 | * @return boolean |
||
| 447 | */ |
||
| 448 | public function canDelete($member = null) |
||
| 449 | { |
||
| 450 | return $this->canEdit($member); |
||
| 451 | } |
||
| 452 | |||
| 453 | /* |
||
| 454 | * Determine if this recipient may receive notifications for this submission |
||
| 455 | * |
||
| 456 | * @param array $data |
||
| 457 | * @param Form $form |
||
| 458 | * @return bool |
||
| 459 | */ |
||
| 460 | public function canSend($data, $form) |
||
| 461 | { |
||
| 462 | // Skip if no rules configured |
||
| 463 | $customRules = $this->CustomRules(); |
||
| 464 | if (!$customRules->count()) { |
||
| 465 | return true; |
||
| 466 | } |
||
| 467 | |||
| 468 | // Check all rules |
||
| 469 | $isAnd = $this->CustomRulesCondition === 'And'; |
||
| 470 | foreach ($customRules as $customRule) { |
||
| 471 | /** @var EmailRecipientCondition $customRule */ |
||
| 472 | $matches = $customRule->matches($data); |
||
| 473 | if ($isAnd && !$matches) { |
||
| 474 | return false; |
||
| 475 | } |
||
| 476 | if (!$isAnd && $matches) { |
||
| 477 | return true; |
||
| 478 | } |
||
| 479 | } |
||
| 480 | |||
| 481 | // Once all rules are checked |
||
| 482 | return $isAnd; |
||
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Make sure the email template saved against the recipient exists on the file system. |
||
| 487 | * |
||
| 488 | * @param string |
||
| 489 | * |
||
| 490 | * @return boolean |
||
| 491 | */ |
||
| 492 | public function emailTemplateExists($template = '') |
||
| 493 | { |
||
| 494 | $t = ($template ? $template : $this->EmailTemplate); |
||
| 495 | |||
| 496 | return in_array($t, $this->getEmailTemplateDropdownValues()); |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Get the email body for the current email format |
||
| 501 | * |
||
| 502 | * @return string |
||
| 503 | */ |
||
| 504 | public function getEmailBodyContent() |
||
| 505 | { |
||
| 506 | if ($this->SendPlain) { |
||
| 507 | return DBField::create_field('HTMLText', $this->EmailBody)->NoHTML(); |
||
| 508 | } |
||
| 509 | return DBField::create_field('HTMLText', $this->EmailBodyHtml)->RAW(); |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Gets a list of email templates suitable for populating the email template dropdown. |
||
| 514 | * |
||
| 515 | * @return array |
||
| 516 | */ |
||
| 517 | public function getEmailTemplateDropdownValues() |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Validate that valid email addresses are being used |
||
| 542 | * |
||
| 543 | * @return ValidationResult |
||
| 544 | */ |
||
| 545 | public function validate() |
||
| 571 | } |
||
| 572 |