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