| 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 |
||
| 152 | public function getCMSFields() |
||
| 153 | { |
||
| 154 | Requirements::javascript(USERFORMS_DIR . '/javascript/Recipient.js'); |
||
| 155 | |||
| 156 | // Determine optional field values |
||
| 157 | $form = $this->getFormParent(); |
||
| 158 | |||
| 159 | // predefined choices are also candidates |
||
| 160 | $multiOptionFields = EditableMultipleOptionField::get()->filter('ParentID', $form->ID); |
||
| 161 | |||
| 162 | // if they have email fields then we could send from it |
||
| 163 | $validEmailFromFields = EditableEmailField::get()->filter('ParentID', $form->ID); |
||
| 164 | |||
| 165 | // For the subject, only one-line entry boxes make sense |
||
| 166 | $validSubjectFields = ArrayList::create( |
||
| 167 | EditableTextField::get() |
||
| 168 | ->filter('ParentID', $form->ID) |
||
| 169 | ->exclude('Rows:GreaterThan', 1) |
||
| 170 | ->toArray() |
||
| 171 | ); |
||
| 172 | $validSubjectFields->merge($multiOptionFields); |
||
| 173 | |||
| 174 | |||
| 175 | // Check valid email-recipient fields |
||
| 176 | if ($this->config()->allow_unbound_recipient_fields) { |
||
| 177 | // To address can only be email fields or multi option fields |
||
| 178 | $validEmailToFields = ArrayList::create($validEmailFromFields->toArray()); |
||
| 179 | $validEmailToFields->merge($multiOptionFields); |
||
| 180 | } else { |
||
| 181 | // To address cannot be unbound, so restrict to pre-defined lists |
||
| 182 | $validEmailToFields = $multiOptionFields; |
||
| 183 | } |
||
| 184 | |||
| 185 | // Build fieldlist |
||
| 186 | $fields = FieldList::create(TabSet::create('Root')->addExtraClass('EmailRecipientForm')); |
||
| 187 | |||
| 188 | // Configuration fields |
||
| 189 | $fields->addFieldsToTab('Root.EmailDetails', array( |
||
| 190 | // Subject |
||
| 191 | FieldGroup::create( |
||
| 192 | TextField::create('EmailSubject', _t('UserDefinedForm.TYPESUBJECT', 'Type subject')) |
||
| 193 | ->setAttribute('style', 'min-width: 400px;'), |
||
| 194 | DropdownField::create( |
||
| 195 | 'SendEmailSubjectFieldID', |
||
| 196 | _t('UserDefinedForm.SELECTAFIELDTOSETSUBJECT', '.. or select a field to use as the subject'), |
||
| 197 | $validSubjectFields->map('ID', 'Title') |
||
| 198 | )->setEmptyString('') |
||
| 199 | ) |
||
| 200 | ->setTitle(_t('UserDefinedForm.EMAILSUBJECT', 'Email subject')), |
||
| 201 | |||
| 202 | // To |
||
| 203 | FieldGroup::create( |
||
| 204 | TextField::create('EmailAddress', _t('UserDefinedForm.TYPETO', 'Type to address')) |
||
| 205 | ->setAttribute('style', 'min-width: 400px;'), |
||
| 206 | DropdownField::create( |
||
| 207 | 'SendEmailToFieldID', |
||
| 208 | _t('UserDefinedForm.ORSELECTAFIELDTOUSEASTO', '.. or select a field to use as the to address'), |
||
| 209 | $validEmailToFields->map('ID', 'Title') |
||
| 210 | )->setEmptyString(' ') |
||
| 211 | ) |
||
| 212 | ->setTitle(_t('UserDefinedForm.SENDEMAILTO', 'Send email to')) |
||
| 213 | ->setDescription(_t( |
||
| 214 | 'UserDefinedForm.SENDEMAILTO_DESCRIPTION', |
||
| 215 | 'You may enter multiple email addresses as a comma separated list.' |
||
| 216 | )), |
||
| 217 | |||
| 218 | |||
| 219 | // From |
||
| 220 | TextField::create('EmailFrom', _t('UserDefinedForm.FROMADDRESS', 'Send email from')) |
||
| 221 | ->setDescription(_t( |
||
| 222 | 'UserDefinedForm.EmailFromContent', |
||
| 223 | "The from address allows you to set who the email comes from. On most servers this ". |
||
| 224 | "will need to be set to an email address on the same domain name as your site. ". |
||
| 225 | "For example on yoursite.com the from address may need to be [email protected]. ". |
||
| 226 | "You can however, set any email address you wish as the reply to address." |
||
| 227 | )), |
||
| 228 | |||
| 229 | |||
| 230 | // Reply-To |
||
| 231 | FieldGroup::create( |
||
| 232 | TextField::create('EmailReplyTo', _t('UserDefinedForm.TYPEREPLY', 'Type reply address')) |
||
| 233 | ->setAttribute('style', 'min-width: 400px;'), |
||
| 234 | DropdownField::create( |
||
| 235 | 'SendEmailFromFieldID', |
||
| 236 | _t('UserDefinedForm.ORSELECTAFIELDTOUSEASFROM', '.. or select a field to use as reply to address'), |
||
| 237 | $validEmailFromFields->map('ID', 'Title') |
||
| 238 | )->setEmptyString(' ') |
||
| 239 | ) |
||
| 240 | ->setTitle(_t('UserDefinedForm.REPLYADDRESS', 'Email for reply to')) |
||
| 241 | ->setDescription(_t( |
||
| 242 | 'UserDefinedForm.REPLYADDRESS_DESCRIPTION', |
||
| 243 | 'The email address which the recipient is able to \'reply\' to.' |
||
| 244 | )) |
||
| 245 | )); |
||
| 246 | |||
| 247 | $fields->fieldByName('Root.EmailDetails')->setTitle(_t('UserDefinedForm_EmailRecipient.EMAILDETAILSTAB', 'Email Details')); |
||
| 248 | |||
| 249 | // Only show the preview link if the recipient has been saved. |
||
| 250 | if (!empty($this->EmailTemplate)) { |
||
| 251 | $preview = sprintf( |
||
| 252 | '<p><a href="%s" target="_blank" class="ss-ui-button">%s</a></p><em>%s</em>', |
||
| 253 | "admin/pages/edit/EditForm/field/EmailRecipients/item/{$this->ID}/preview", |
||
| 254 | _t('UserDefinedForm.PREVIEW_EMAIL', 'Preview email'), |
||
| 255 | _t('UserDefinedForm.PREVIEW_EMAIL_DESCRIPTION', 'Note: Unsaved changes will not appear in the preview.') |
||
| 256 | ); |
||
| 257 | } else { |
||
| 258 | $preview = sprintf( |
||
| 259 | '<em>%s</em>', |
||
| 260 | _t( |
||
| 261 | 'UserDefinedForm.PREVIEW_EMAIL_UNAVAILABLE', |
||
| 262 | 'You can preview this email once you have saved the Recipient.' |
||
| 263 | ) |
||
| 264 | ); |
||
| 265 | } |
||
| 266 | |||
| 267 | // Email templates |
||
| 268 | $fields->addFieldsToTab('Root.EmailContent', array( |
||
| 269 | CheckboxField::create('HideFormData', _t('UserDefinedForm.HIDEFORMDATA', 'Hide form data from email?')), |
||
| 270 | CheckboxField::create( |
||
| 271 | 'SendPlain', |
||
| 272 | _t('UserDefinedForm.SENDPLAIN', 'Send email as plain text? (HTML will be stripped)') |
||
| 273 | ), |
||
| 274 | DropdownField::create( |
||
| 275 | 'EmailTemplate', |
||
| 276 | _t('UserDefinedForm.EMAILTEMPLATE', 'Email template'), |
||
| 277 | $this->getEmailTemplateDropdownValues() |
||
| 278 | )->addExtraClass('toggle-html-only'), |
||
| 279 | HTMLEditorField::create('EmailBodyHtml', _t('UserDefinedForm.EMAILBODYHTML', 'Body')) |
||
| 280 | ->addExtraClass('toggle-html-only'), |
||
| 281 | TextareaField::create('EmailBody', _t('UserDefinedForm.EMAILBODY', 'Body')) |
||
| 282 | ->addExtraClass('toggle-plain-only'), |
||
| 283 | LiteralField::create( |
||
| 284 | 'EmailPreview', |
||
| 285 | '<div id="EmailPreview" class="field toggle-html-only">' . $preview . '</div>' |
||
| 286 | ) |
||
| 287 | )); |
||
| 288 | |||
| 289 | $fields->fieldByName('Root.EmailContent')->setTitle(_t('UserDefinedForm_EmailRecipient.EMAILCONTENTTAB', 'Email Content')); |
||
| 290 | |||
| 291 | // Custom rules for sending this field |
||
| 292 | $grid = new GridField( |
||
| 293 | "CustomRules", |
||
| 294 | _t('EditableFormField.CUSTOMRULES', 'Custom Rules'), |
||
| 295 | $this->CustomRules(), |
||
| 296 | $this->getRulesConfig() |
||
| 297 | ); |
||
| 298 | $grid->setDescription(_t( |
||
| 299 | 'UserDefinedForm.RulesDescription', |
||
| 300 | '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.' |
||
| 301 | )); |
||
| 302 | $fields->addFieldsToTab('Root.CustomRules', array( |
||
| 303 | new DropdownField( |
||
| 304 | 'CustomRulesCondition', |
||
| 305 | _t('UserDefinedForm.SENDIF', 'Send condition'), |
||
| 306 | array( |
||
| 307 | 'Or' => _t('UserDefinedForm.SENDIFOR', 'Any conditions are true'), |
||
| 308 | 'And' => _t('UserDefinedForm.SENDIFAND', 'All conditions are true') |
||
| 309 | ) |
||
| 310 | ), |
||
| 311 | $grid |
||
| 312 | )); |
||
| 313 | |||
| 314 | $fields->fieldByName('Root.CustomRules')->setTitle(_t('UserDefinedForm_EmailRecipient.CUSTOMRULESTAB', 'Custom Rules')); |
||
| 315 | |||
| 316 | $this->extend('updateCMSFields', $fields); |
||
| 317 | return $fields; |
||
| 318 | } |
||
| 319 | |||
| 468 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: