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