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