| Conditions | 3 |
| Paths | 4 |
| Total Lines | 167 |
| Code Lines | 108 |
| 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 |
||
| 130 | public function getCMSFields() |
||
| 131 | { |
||
| 132 | Requirements::javascript(USERFORMS_DIR . '/javascript/Recipient.js'); |
||
| 133 | |||
| 134 | // Determine optional field values |
||
| 135 | $form = $this->getFormParent(); |
||
| 136 | |||
| 137 | // predefined choices are also candidates |
||
| 138 | $multiOptionFields = EditableMultipleOptionField::get()->filter('ParentID', $form->ID); |
||
| 139 | |||
| 140 | // if they have email fields then we could send from it |
||
| 141 | $validEmailFromFields = EditableEmailField::get()->filter('ParentID', $form->ID); |
||
| 142 | |||
| 143 | // For the subject, only one-line entry boxes make sense |
||
| 144 | $validSubjectFields = ArrayList::create( |
||
| 145 | EditableTextField::get() |
||
| 146 | ->filter('ParentID', $form->ID) |
||
| 147 | ->exclude('Rows:GreaterThan', 1) |
||
| 148 | ->toArray() |
||
| 149 | ); |
||
| 150 | $validSubjectFields->merge($multiOptionFields); |
||
| 151 | |||
| 152 | |||
| 153 | // Check valid email-recipient fields |
||
| 154 | if ($this->config()->allow_unbound_recipient_fields) { |
||
| 155 | // To address can only be email fields or multi option fields |
||
| 156 | $validEmailToFields = ArrayList::create($validEmailFromFields->toArray()); |
||
| 157 | $validEmailToFields->merge($multiOptionFields); |
||
| 158 | } else { |
||
| 159 | // To address cannot be unbound, so restrict to pre-defined lists |
||
| 160 | $validEmailToFields = $multiOptionFields; |
||
| 161 | } |
||
| 162 | |||
| 163 | // Build fieldlist |
||
| 164 | $fields = FieldList::create(Tabset::create('Root')->addExtraClass('EmailRecipientForm')); |
||
| 165 | |||
| 166 | // Configuration fields |
||
| 167 | $fields->addFieldsToTab('Root.EmailDetails', array( |
||
| 168 | // Subject |
||
| 169 | FieldGroup::create( |
||
| 170 | TextField::create('EmailSubject', _t('UserDefinedForm.TYPESUBJECT', 'Type subject')) |
||
| 171 | ->setAttribute('style', 'min-width: 400px;'), |
||
| 172 | DropdownField::create( |
||
| 173 | 'SendEmailSubjectFieldID', |
||
| 174 | _t('UserDefinedForm.SELECTAFIELDTOSETSUBJECT', '.. or select a field to use as the subject'), |
||
| 175 | $validSubjectFields->map('ID', 'Title') |
||
| 176 | )->setEmptyString('') |
||
| 177 | ) |
||
| 178 | ->setTitle(_t('UserDefinedForm.EMAILSUBJECT', 'Email subject')), |
||
| 179 | |||
| 180 | // To |
||
| 181 | FieldGroup::create( |
||
| 182 | TextField::create('EmailAddress', _t('UserDefinedForm.TYPETO', 'Type to address')) |
||
| 183 | ->setAttribute('style', 'min-width: 400px;'), |
||
| 184 | DropdownField::create( |
||
| 185 | 'SendEmailToFieldID', |
||
| 186 | _t('UserDefinedForm.ORSELECTAFIELDTOUSEASTO', '.. or select a field to use as the to address'), |
||
| 187 | $validEmailToFields->map('ID', 'Title') |
||
| 188 | )->setEmptyString(' ') |
||
| 189 | ) |
||
| 190 | ->setTitle(_t('UserDefinedForm.SENDEMAILTO', 'Send email to')) |
||
| 191 | ->setDescription(_t( |
||
| 192 | 'UserDefinedForm.SENDEMAILTO_DESCRIPTION', |
||
| 193 | 'You may enter multiple email addresses as a comma separated list.' |
||
| 194 | )), |
||
| 195 | |||
| 196 | |||
| 197 | // From |
||
| 198 | TextField::create('EmailFrom', _t('UserDefinedForm.FROMADDRESS', 'Send email from')) |
||
| 199 | ->setDescription(_t( |
||
| 200 | 'UserDefinedForm.EmailFromContent', |
||
| 201 | "The from address allows you to set who the email comes from. On most servers this ". |
||
| 202 | "will need to be set to an email address on the same domain name as your site. ". |
||
| 203 | "For example on yoursite.com the from address may need to be [email protected]. ". |
||
| 204 | "You can however, set any email address you wish as the reply to address." |
||
| 205 | )), |
||
| 206 | |||
| 207 | |||
| 208 | // Reply-To |
||
| 209 | FieldGroup::create( |
||
| 210 | TextField::create('EmailReplyTo', _t('UserDefinedForm.TYPEREPLY', 'Type reply address')) |
||
| 211 | ->setAttribute('style', 'min-width: 400px;'), |
||
| 212 | DropdownField::create( |
||
| 213 | 'SendEmailFromFieldID', |
||
| 214 | _t('UserDefinedForm.ORSELECTAFIELDTOUSEASFROM', '.. or select a field to use as reply to address'), |
||
| 215 | $validEmailFromFields->map('ID', 'Title') |
||
| 216 | )->setEmptyString(' ') |
||
| 217 | ) |
||
| 218 | ->setTitle(_t('UserDefinedForm.REPLYADDRESS', 'Email for reply to')) |
||
| 219 | ->setDescription(_t( |
||
| 220 | 'UserDefinedForm.REPLYADDRESS_DESCRIPTION', |
||
| 221 | 'The email address which the recipient is able to \'reply\' to.' |
||
| 222 | )) |
||
| 223 | )); |
||
| 224 | |||
| 225 | $fields->fieldByName('Root.EmailDetails')->setTitle(_t('UserDefinedForm_EmailRecipient.EMAILDETAILSTAB', 'Email Details')); |
||
| 226 | |||
| 227 | // Only show the preview link if the recipient has been saved. |
||
| 228 | if (!empty($this->EmailTemplate)) { |
||
| 229 | $preview = sprintf( |
||
| 230 | '<p><a href="%s" target="_blank" class="ss-ui-button">%s</a></p><em>%s</em>', |
||
| 231 | "admin/pages/edit/EditForm/field/EmailRecipients/item/{$this->ID}/preview", |
||
| 232 | _t('UserDefinedForm.PREVIEW_EMAIL', 'Preview email'), |
||
| 233 | _t('UserDefinedForm.PREVIEW_EMAIL_DESCRIPTION', 'Note: Unsaved changes will not appear in the preview.') |
||
| 234 | ); |
||
| 235 | } else { |
||
| 236 | $preview = sprintf( |
||
| 237 | '<em>%s</em>', |
||
| 238 | _t( |
||
| 239 | 'UserDefinedForm.PREVIEW_EMAIL_UNAVAILABLE', |
||
| 240 | 'You can preview this email once you have saved the Recipient.' |
||
| 241 | ) |
||
| 242 | ); |
||
| 243 | } |
||
| 244 | |||
| 245 | // Email templates |
||
| 246 | $fields->addFieldsToTab('Root.EmailContent', array( |
||
| 247 | CheckboxField::create('HideFormData', _t('UserDefinedForm.HIDEFORMDATA', 'Hide form data from email?')), |
||
| 248 | CheckboxField::create( |
||
| 249 | 'SendPlain', |
||
| 250 | _t('UserDefinedForm.SENDPLAIN', 'Send email as plain text? (HTML will be stripped)') |
||
| 251 | ), |
||
| 252 | DropdownField::create( |
||
| 253 | 'EmailTemplate', |
||
| 254 | _t('UserDefinedForm.EMAILTEMPLATE', 'Email template'), |
||
| 255 | $this->getEmailTemplateDropdownValues() |
||
| 256 | )->addExtraClass('toggle-html-only'), |
||
| 257 | HTMLEditorField::create('EmailBodyHtml', _t('UserDefinedForm.EMAILBODYHTML', 'Body')) |
||
| 258 | ->addExtraClass('toggle-html-only'), |
||
| 259 | TextareaField::create('EmailBody', _t('UserDefinedForm.EMAILBODY', 'Body')) |
||
| 260 | ->addExtraClass('toggle-plain-only'), |
||
| 261 | LiteralField::create( |
||
| 262 | 'EmailPreview', |
||
| 263 | '<div id="EmailPreview" class="field toggle-html-only">' . $preview . '</div>' |
||
| 264 | ) |
||
| 265 | )); |
||
| 266 | |||
| 267 | $fields->fieldByName('Root.EmailContent')->setTitle(_t('UserDefinedForm_EmailRecipient.EMAILCONTENTTAB', 'Email Content')); |
||
| 268 | |||
| 269 | // Custom rules for sending this field |
||
| 270 | $grid = new GridField( |
||
| 271 | "CustomRules", |
||
| 272 | _t('EditableFormField.CUSTOMRULES', 'Custom Rules'), |
||
| 273 | $this->CustomRules(), |
||
| 274 | $this->getRulesConfig() |
||
| 275 | ); |
||
| 276 | $grid->setDescription(_t( |
||
| 277 | 'UserDefinedForm.RulesDescription', |
||
| 278 | '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.' |
||
| 279 | )); |
||
| 280 | $fields->addFieldsToTab('Root.CustomRules', array( |
||
| 281 | new DropdownField( |
||
| 282 | 'CustomRulesCondition', |
||
| 283 | _t('UserDefinedForm.SENDIF', 'Send condition'), |
||
| 284 | array( |
||
| 285 | 'Or' => _t('UserDefinedForm.SENDIFOR', 'Any conditions are true'), |
||
| 286 | 'And' => _t('UserDefinedForm.SENDIFAND', 'All conditions are true') |
||
| 287 | ) |
||
| 288 | ), |
||
| 289 | $grid |
||
| 290 | )); |
||
| 291 | |||
| 292 | $fields->fieldByName('Root.CustomRules')->setTitle(_t('UserDefinedForm_EmailRecipient.CUSTOMRULESTAB', 'Custom Rules')); |
||
| 293 | |||
| 294 | $this->extend('updateCMSFields', $fields); |
||
| 295 | return $fields; |
||
| 296 | } |
||
| 297 | |||
| 475 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.