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