| 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 |
||
| 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 | |||
| 572 |