| Conditions | 3 |
| Paths | 4 |
| Total Lines | 208 |
| Code Lines | 134 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 175 | public function getCMSFields() |
||
| 176 | { |
||
| 177 | Requirements::javascript( |
||
| 178 | ModuleLoader::getModule('silverstripe/userforms')->getRelativeResourcePath('javascript/Recipient.js') |
||
| 179 | ); |
||
| 180 | |||
| 181 | // Determine optional field values |
||
| 182 | $form = $this->getFormParent(); |
||
| 183 | |||
| 184 | // predefined choices are also candidates |
||
| 185 | $multiOptionFields = EditableMultipleOptionField::get()->filter('ParentID', $form->ID); |
||
| 186 | |||
| 187 | // if they have email fields then we could send from it |
||
| 188 | $validEmailFromFields = EditableEmailField::get()->filter('ParentID', $form->ID); |
||
| 189 | |||
| 190 | // For the subject, only one-line entry boxes make sense |
||
| 191 | $validSubjectFields = ArrayList::create( |
||
| 192 | EditableTextField::get() |
||
| 193 | ->filter('ParentID', $form->ID) |
||
| 194 | ->exclude('Rows:GreaterThan', 1) |
||
| 195 | ->toArray() |
||
| 196 | ); |
||
| 197 | $validSubjectFields->merge($multiOptionFields); |
||
| 198 | |||
| 199 | |||
| 200 | // Check valid email-recipient fields |
||
| 201 | if ($this->config()->get('allow_unbound_recipient_fields')) { |
||
| 202 | // To address can only be email fields or multi option fields |
||
| 203 | $validEmailToFields = ArrayList::create($validEmailFromFields->toArray()); |
||
| 204 | $validEmailToFields->merge($multiOptionFields); |
||
| 205 | } else { |
||
| 206 | // To address cannot be unbound, so restrict to pre-defined lists |
||
| 207 | $validEmailToFields = $multiOptionFields; |
||
| 208 | } |
||
| 209 | |||
| 210 | // Build fieldlist |
||
| 211 | $fields = FieldList::create(Tabset::create('Root')->addExtraClass('EmailRecipientForm')); |
||
| 212 | |||
| 213 | // Configuration fields |
||
| 214 | $fields->addFieldsToTab('Root.EmailDetails', [ |
||
| 215 | // Subject |
||
| 216 | FieldGroup::create( |
||
| 217 | TextField::create( |
||
| 218 | 'EmailSubject', |
||
| 219 | _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.TYPESUBJECT', 'Type subject') |
||
| 220 | ) |
||
| 221 | ->setAttribute('style', 'min-width: 400px;'), |
||
| 222 | DropdownField::create( |
||
| 223 | 'SendEmailSubjectFieldID', |
||
| 224 | _t( |
||
| 225 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.SELECTAFIELDTOSETSUBJECT', |
||
| 226 | '.. or select a field to use as the subject' |
||
| 227 | ), |
||
| 228 | $validSubjectFields->map('ID', 'Title') |
||
| 229 | )->setEmptyString('') |
||
| 230 | ) |
||
| 231 | ->setTitle(_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILSUBJECT', 'Email subject')), |
||
| 232 | |||
| 233 | // To |
||
| 234 | FieldGroup::create( |
||
| 235 | TextField::create( |
||
| 236 | 'EmailAddress', |
||
| 237 | _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.TYPETO', 'Type to address') |
||
| 238 | ) |
||
| 239 | ->setAttribute('style', 'min-width: 400px;'), |
||
| 240 | DropdownField::create( |
||
| 241 | 'SendEmailToFieldID', |
||
| 242 | _t( |
||
| 243 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.ORSELECTAFIELDTOUSEASTO', |
||
| 244 | '.. or select a field to use as the to address' |
||
| 245 | ), |
||
| 246 | $validEmailToFields->map('ID', 'Title') |
||
| 247 | )->setEmptyString(' ') |
||
| 248 | ) |
||
| 249 | ->setTitle(_t('SilverStripe\\UserForms\\Model\\UserDefinedForm.SENDEMAILTO', 'Send email to')) |
||
| 250 | ->setDescription(_t( |
||
| 251 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.SENDEMAILTO_DESCRIPTION', |
||
| 252 | 'You may enter multiple email addresses as a comma separated list.' |
||
| 253 | )), |
||
| 254 | |||
| 255 | |||
| 256 | // From |
||
| 257 | TextField::create( |
||
| 258 | 'EmailFrom', |
||
| 259 | _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.FROMADDRESS', 'Send email from') |
||
| 260 | ) |
||
| 261 | ->setDescription(_t( |
||
| 262 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.EmailFromContent', |
||
| 263 | "The from address allows you to set who the email comes from. On most servers this ". |
||
| 264 | "will need to be set to an email address on the same domain name as your site. ". |
||
| 265 | "For example on yoursite.com the from address may need to be [email protected]. ". |
||
| 266 | "You can however, set any email address you wish as the reply to address." |
||
| 267 | )), |
||
| 268 | |||
| 269 | |||
| 270 | // Reply-To |
||
| 271 | FieldGroup::create( |
||
| 272 | TextField::create('EmailReplyTo', _t( |
||
| 273 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.TYPEREPLY', |
||
| 274 | 'Type reply address' |
||
| 275 | )) |
||
| 276 | ->setAttribute('style', 'min-width: 400px;'), |
||
| 277 | DropdownField::create( |
||
| 278 | 'SendEmailFromFieldID', |
||
| 279 | _t( |
||
| 280 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.ORSELECTAFIELDTOUSEASFROM', |
||
| 281 | '.. or select a field to use as reply to address' |
||
| 282 | ), |
||
| 283 | $validEmailFromFields->map('ID', 'Title') |
||
| 284 | )->setEmptyString(' ') |
||
| 285 | ) |
||
| 286 | ->setTitle(_t( |
||
| 287 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.REPLYADDRESS', |
||
| 288 | 'Email for reply to' |
||
| 289 | )) |
||
| 290 | ->setDescription(_t( |
||
| 291 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.REPLYADDRESS_DESCRIPTION', |
||
| 292 | 'The email address which the recipient is able to \'reply\' to.' |
||
| 293 | )) |
||
| 294 | ]); |
||
| 295 | |||
| 296 | $fields->fieldByName('Root.EmailDetails')->setTitle(_t(__CLASS__.'.EMAILDETAILSTAB', 'Email Details')); |
||
| 297 | |||
| 298 | // Only show the preview link if the recipient has been saved. |
||
| 299 | if (!empty($this->EmailTemplate)) { |
||
| 300 | $preview = sprintf( |
||
| 301 | '<p><a href="%s" target="_blank" class="ss-ui-button">%s</a></p><em>%s</em>', |
||
| 302 | Controller::join_links( |
||
| 303 | singleton(CMSPageEditController::class)->getEditForm()->FormAction(), |
||
| 304 | "field/EmailRecipients/item/{$this->ID}/preview" |
||
| 305 | ), |
||
| 306 | _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.PREVIEW_EMAIL', 'Preview email'), |
||
| 307 | _t( |
||
| 308 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.PREVIEW_EMAIL_DESCRIPTION', |
||
| 309 | 'Note: Unsaved changes will not appear in the preview.' |
||
| 310 | ) |
||
| 311 | ); |
||
| 312 | } else { |
||
| 313 | $preview = sprintf( |
||
| 314 | '<em>%s</em>', |
||
| 315 | _t( |
||
| 316 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.PREVIEW_EMAIL_UNAVAILABLE', |
||
| 317 | 'You can preview this email once you have saved the Recipient.' |
||
| 318 | ) |
||
| 319 | ); |
||
| 320 | } |
||
| 321 | |||
| 322 | // Email templates |
||
| 323 | $fields->addFieldsToTab('Root.EmailContent', [ |
||
| 324 | CheckboxField::create( |
||
| 325 | 'HideFormData', |
||
| 326 | _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.HIDEFORMDATA', 'Hide form data from email?') |
||
| 327 | ), |
||
| 328 | CheckboxField::create( |
||
| 329 | 'SendPlain', |
||
| 330 | _t( |
||
| 331 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.SENDPLAIN', |
||
| 332 | 'Send email as plain text? (HTML will be stripped)' |
||
| 333 | ) |
||
| 334 | ), |
||
| 335 | DropdownField::create( |
||
| 336 | 'EmailTemplate', |
||
| 337 | _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILTEMPLATE', 'Email template'), |
||
| 338 | $this->getEmailTemplateDropdownValues() |
||
| 339 | )->addExtraClass('toggle-html-only'), |
||
| 340 | HTMLEditorField::create( |
||
| 341 | 'EmailBodyHtml', |
||
| 342 | _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILBODYHTML', 'Body') |
||
| 343 | ) |
||
| 344 | ->addExtraClass('toggle-html-only'), |
||
| 345 | TextareaField::create( |
||
| 346 | 'EmailBody', |
||
| 347 | _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.EMAILBODY', 'Body') |
||
| 348 | ) |
||
| 349 | ->addExtraClass('toggle-plain-only'), |
||
| 350 | LiteralField::create('EmailPreview', $preview) |
||
| 351 | ]); |
||
| 352 | |||
| 353 | $fields->fieldByName('Root.EmailContent')->setTitle(_t(__CLASS__.'.EMAILCONTENTTAB', 'Email Content')); |
||
| 354 | |||
| 355 | // Custom rules for sending this field |
||
| 356 | $grid = GridField::create( |
||
| 357 | 'CustomRules', |
||
| 358 | _t('SilverStripe\\UserForms\\Model\\EditableFormField.CUSTOMRULES', 'Custom Rules'), |
||
| 359 | $this->CustomRules(), |
||
| 360 | $this->getRulesConfig() |
||
| 361 | ); |
||
| 362 | $grid->setDescription(_t( |
||
| 363 | 'SilverStripe\\UserForms\\Model\\UserDefinedForm.RulesDescription', |
||
| 364 | '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.' |
||
| 365 | )); |
||
| 366 | $fields->addFieldsToTab('Root.CustomRules', [ |
||
| 367 | DropdownField::create( |
||
| 368 | 'CustomRulesCondition', |
||
| 369 | _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.SENDIF', 'Send condition'), |
||
| 370 | [ |
||
| 371 | 'Or' => _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.SENDIFOR', 'Any conditions are true'), |
||
| 372 | 'And' => _t('SilverStripe\\UserForms\\Model\\UserDefinedForm.SENDIFAND', 'All conditions are true') |
||
| 373 | ] |
||
| 374 | ), |
||
| 375 | $grid |
||
| 376 | ]); |
||
| 377 | |||
| 378 | $fields->fieldByName('Root.CustomRules')->setTitle(_t(__CLASS__.'.CUSTOMRULESTAB', 'Custom Rules')); |
||
| 379 | |||
| 380 | $this->extend('updateCMSFields', $fields); |
||
| 381 | return $fields; |
||
| 382 | } |
||
| 383 | |||
| 573 |