Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
10 | class UserDefinedForm_EmailRecipient extends DataObject |
||
|
|||
11 | { |
||
12 | |||
13 | private static $db = array( |
||
14 | 'EmailAddress' => 'Varchar(200)', |
||
15 | 'EmailSubject' => 'Varchar(200)', |
||
16 | 'EmailFrom' => 'Varchar(200)', |
||
17 | 'EmailReplyTo' => 'Varchar(200)', |
||
18 | 'EmailBody' => 'Text', |
||
19 | 'EmailBodyHtml' => 'HTMLText', |
||
20 | 'EmailTemplate' => 'Varchar', |
||
21 | 'SendPlain' => 'Boolean', |
||
22 | 'HideFormData' => 'Boolean', |
||
23 | 'CustomRulesCondition' => 'Enum("And,Or")' |
||
24 | ); |
||
25 | |||
26 | private static $has_one = array( |
||
27 | 'Form' => 'UserDefinedForm', |
||
28 | 'SendEmailFromField' => 'EditableFormField', |
||
29 | 'SendEmailToField' => 'EditableFormField', |
||
30 | 'SendEmailSubjectField' => 'EditableFormField' |
||
31 | ); |
||
32 | |||
33 | private static $has_many = array( |
||
34 | 'CustomRules' => 'UserDefinedForm_EmailRecipientCondition' |
||
35 | ); |
||
36 | |||
37 | private static $summary_fields = array( |
||
38 | 'EmailAddress', |
||
39 | 'EmailSubject', |
||
40 | 'EmailFrom' |
||
41 | ); |
||
42 | |||
43 | /** |
||
44 | * Setting this to true will allow you to select "risky" fields as |
||
45 | * email recipient, such as free-text entry fields. |
||
46 | * |
||
47 | * It's advisable to leave this off. |
||
48 | * |
||
49 | * @config |
||
50 | * @var bool |
||
51 | */ |
||
52 | private static $allow_unbound_recipient_fields = false; |
||
53 | |||
54 | public function summaryFields() |
||
55 | { |
||
56 | $fields = parent::summaryFields(); |
||
57 | if (isset($fields['EmailAddress'])) { |
||
58 | $fields['EmailAddress'] = _t('UserDefinedForm.EMAILADDRESS', 'Email'); |
||
59 | } |
||
60 | if (isset($fields['EmailSubject'])) { |
||
61 | $fields['EmailSubject'] = _t('UserDefinedForm.EMAILSUBJECT', 'Subject'); |
||
62 | } |
||
63 | if (isset($fields['EmailFrom'])) { |
||
64 | $fields['EmailFrom'] = _t('UserDefinedForm.EMAILFROM', 'From'); |
||
65 | } |
||
66 | return $fields; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Get instance of UserDefinedForm when editing in getCMSFields |
||
71 | * |
||
72 | * @return UserDefinedFrom |
||
73 | */ |
||
74 | 1 | protected function getFormParent() |
|
81 | |||
82 | public function getTitle() |
||
92 | |||
93 | /** |
||
94 | * Generate a gridfield config for editing filter rules |
||
95 | * |
||
96 | * @return GridFieldConfig |
||
97 | */ |
||
98 | 1 | protected function getRulesConfig() |
|
126 | |||
127 | /** |
||
128 | * @return FieldList |
||
129 | */ |
||
130 | 1 | public function getCMSFields() |
|
131 | { |
||
132 | 1 | Requirements::javascript(USERFORMS_DIR . '/javascript/Recipient.js'); |
|
133 | |||
134 | // Determine optional field values |
||
135 | 1 | $form = $this->getFormParent(); |
|
136 | |||
137 | // predefined choices are also candidates |
||
138 | 1 | $multiOptionFields = EditableMultipleOptionField::get()->filter('ParentID', $form->ID); |
|
139 | |||
140 | // if they have email fields then we could send from it |
||
141 | 1 | $validEmailFromFields = EditableEmailField::get()->filter('ParentID', $form->ID); |
|
142 | |||
143 | // For the subject, only one-line entry boxes make sense |
||
144 | 1 | $validSubjectFields = ArrayList::create( |
|
145 | 1 | EditableTextField::get() |
|
146 | 1 | ->filter('ParentID', $form->ID) |
|
147 | 1 | ->exclude('Rows:GreaterThan', 1) |
|
148 | 1 | ->toArray() |
|
149 | ); |
||
150 | 1 | $validSubjectFields->merge($multiOptionFields); |
|
151 | |||
152 | |||
153 | // Check valid email-recipient fields |
||
154 | 1 | if ($this->config()->allow_unbound_recipient_fields) { |
|
155 | // To address can only be email fields or multi option fields |
||
156 | $validEmailToFields = ArrayList::create($validEmailFromFields->toArray()); |
||
157 | $validEmailToFields->merge($multiOptionFields); |
||
158 | } else { |
||
159 | // To address cannot be unbound, so restrict to pre-defined lists |
||
160 | 1 | $validEmailToFields = $multiOptionFields; |
|
161 | } |
||
162 | |||
163 | // Build fieldlist |
||
164 | 1 | $fields = FieldList::create(Tabset::create('Root')->addExtraClass('EmailRecipientForm')); |
|
165 | |||
166 | // Configuration fields |
||
167 | 1 | $fields->addFieldsToTab('Root.EmailDetails', array( |
|
168 | // Subject |
||
169 | 1 | FieldGroup::create( |
|
170 | 1 | TextField::create('EmailSubject', _t('UserDefinedForm.TYPESUBJECT', 'Type subject')) |
|
171 | 1 | ->setAttribute('style', 'min-width: 400px;'), |
|
172 | 1 | DropdownField::create( |
|
173 | 1 | 'SendEmailSubjectFieldID', |
|
174 | 1 | _t('UserDefinedForm.SELECTAFIELDTOSETSUBJECT', '.. or select a field to use as the subject'), |
|
175 | 1 | $validSubjectFields->map('ID', 'Title') |
|
176 | 1 | )->setEmptyString('') |
|
177 | ) |
||
178 | 1 | ->setTitle(_t('UserDefinedForm.EMAILSUBJECT', 'Email subject')), |
|
179 | |||
180 | // To |
||
181 | 1 | FieldGroup::create( |
|
182 | 1 | TextField::create('EmailAddress', _t('UserDefinedForm.TYPETO', 'Type to address')) |
|
183 | 1 | ->setAttribute('style', 'min-width: 400px;'), |
|
184 | 1 | DropdownField::create( |
|
185 | 1 | 'SendEmailToFieldID', |
|
186 | 1 | _t('UserDefinedForm.ORSELECTAFIELDTOUSEASTO', '.. or select a field to use as the to address'), |
|
187 | 1 | $validEmailToFields->map('ID', 'Title') |
|
188 | 1 | )->setEmptyString(' ') |
|
189 | ) |
||
190 | 1 | ->setTitle(_t('UserDefinedForm.SENDEMAILTO', 'Send email to')) |
|
191 | 1 | ->setDescription(_t( |
|
192 | 1 | 'UserDefinedForm.SENDEMAILTO_DESCRIPTION', |
|
193 | 1 | 'You may enter multiple email addresses as a comma separated list.' |
|
194 | )), |
||
195 | |||
196 | |||
197 | // From |
||
198 | 1 | TextField::create('EmailFrom', _t('UserDefinedForm.FROMADDRESS', 'Send email from')) |
|
199 | 1 | ->setDescription(_t( |
|
200 | 1 | 'UserDefinedForm.EmailFromContent', |
|
201 | "The from address allows you to set who the email comes from. On most servers this ". |
||
202 | "will need to be set to an email address on the same domain name as your site. ". |
||
203 | "For example on yoursite.com the from address may need to be [email protected]. ". |
||
204 | 1 | "You can however, set any email address you wish as the reply to address." |
|
205 | )), |
||
206 | |||
207 | |||
208 | // Reply-To |
||
209 | 1 | FieldGroup::create( |
|
210 | 1 | TextField::create('EmailReplyTo', _t('UserDefinedForm.TYPEREPLY', 'Type reply address')) |
|
211 | 1 | ->setAttribute('style', 'min-width: 400px;'), |
|
212 | 1 | DropdownField::create( |
|
213 | 1 | 'SendEmailFromFieldID', |
|
214 | 1 | _t('UserDefinedForm.ORSELECTAFIELDTOUSEASFROM', '.. or select a field to use as reply to address'), |
|
215 | 1 | $validEmailFromFields->map('ID', 'Title') |
|
216 | 1 | )->setEmptyString(' ') |
|
217 | ) |
||
218 | 1 | ->setTitle(_t('UserDefinedForm.REPLYADDRESS', 'Email for reply to')) |
|
219 | 1 | ->setDescription(_t( |
|
220 | 1 | 'UserDefinedForm.REPLYADDRESS_DESCRIPTION', |
|
221 | 1 | 'The email address which the recipient is able to \'reply\' to.' |
|
222 | )) |
||
223 | )); |
||
224 | |||
225 | 1 | $fields->fieldByName('Root.EmailDetails')->setTitle(_t('UserDefinedForm_EmailRecipient.EMAILDETAILSTAB', 'Email Details')); |
|
226 | |||
227 | // Only show the preview link if the recipient has been saved. |
||
228 | 1 | if (!empty($this->EmailTemplate)) { |
|
229 | $preview = sprintf( |
||
230 | '<p><a href="%s" target="_blank" class="ss-ui-button">%s</a></p><em>%s</em>', |
||
231 | Controller::join_links( |
||
232 | singleton('CMSPageEditController')->getEditForm()->FormAction(), |
||
233 | "field/EmailRecipients/item/{$this->ID}/preview" |
||
234 | ), |
||
235 | _t('UserDefinedForm.PREVIEW_EMAIL', 'Preview email'), |
||
236 | _t('UserDefinedForm.PREVIEW_EMAIL_DESCRIPTION', 'Note: Unsaved changes will not appear in the preview.') |
||
237 | ); |
||
238 | } else { |
||
239 | 1 | $preview = sprintf( |
|
240 | 1 | '<em>%s</em>', |
|
241 | 1 | _t( |
|
242 | 1 | 'UserDefinedForm.PREVIEW_EMAIL_UNAVAILABLE', |
|
243 | 1 | 'You can preview this email once you have saved the Recipient.' |
|
244 | ) |
||
245 | ); |
||
246 | } |
||
247 | |||
248 | // Email templates |
||
249 | 1 | $fields->addFieldsToTab('Root.EmailContent', array( |
|
250 | 1 | CheckboxField::create('HideFormData', _t('UserDefinedForm.HIDEFORMDATA', 'Hide form data from email?')), |
|
251 | 1 | CheckboxField::create( |
|
252 | 1 | 'SendPlain', |
|
253 | 1 | _t('UserDefinedForm.SENDPLAIN', 'Send email as plain text? (HTML will be stripped)') |
|
254 | ), |
||
255 | 1 | DropdownField::create( |
|
256 | 1 | 'EmailTemplate', |
|
257 | 1 | _t('UserDefinedForm.EMAILTEMPLATE', 'Email template'), |
|
258 | 1 | $this->getEmailTemplateDropdownValues() |
|
259 | 1 | )->addExtraClass('toggle-html-only'), |
|
260 | 1 | HTMLEditorField::create('EmailBodyHtml', _t('UserDefinedForm.EMAILBODYHTML', 'Body')) |
|
261 | 1 | ->addExtraClass('toggle-html-only'), |
|
262 | 1 | TextareaField::create('EmailBody', _t('UserDefinedForm.EMAILBODY', 'Body')) |
|
263 | 1 | ->addExtraClass('toggle-plain-only'), |
|
264 | 1 | LiteralField::create( |
|
265 | 1 | 'EmailPreview', |
|
266 | 1 | '<div id="EmailPreview" class="field toggle-html-only">' . $preview . '</div>' |
|
267 | ) |
||
268 | )); |
||
269 | |||
270 | 1 | $fields->fieldByName('Root.EmailContent')->setTitle(_t('UserDefinedForm_EmailRecipient.EMAILCONTENTTAB', 'Email Content')); |
|
271 | |||
272 | // Custom rules for sending this field |
||
273 | 1 | $grid = new GridField( |
|
274 | 1 | "CustomRules", |
|
275 | 1 | _t('EditableFormField.CUSTOMRULES', 'Custom Rules'), |
|
276 | 1 | $this->CustomRules(), |
|
277 | 1 | $this->getRulesConfig() |
|
278 | ); |
||
279 | 1 | $grid->setDescription(_t( |
|
280 | 1 | 'UserDefinedForm.RulesDescription', |
|
281 | 1 | '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.' |
|
282 | )); |
||
283 | 1 | $fields->addFieldsToTab('Root.CustomRules', array( |
|
284 | 1 | new DropdownField( |
|
285 | 1 | 'CustomRulesCondition', |
|
286 | 1 | _t('UserDefinedForm.SENDIF', 'Send condition'), |
|
287 | array( |
||
288 | 1 | 'Or' => _t('UserDefinedForm.SENDIFOR', 'Any conditions are true'), |
|
289 | 1 | 'And' => _t('UserDefinedForm.SENDIFAND', 'All conditions are true') |
|
290 | ) |
||
291 | ), |
||
292 | 1 | $grid |
|
293 | )); |
||
294 | |||
295 | 1 | $fields->fieldByName('Root.CustomRules')->setTitle(_t('UserDefinedForm_EmailRecipient.CUSTOMRULESTAB', 'Custom Rules')); |
|
296 | |||
297 | 1 | $this->extend('updateCMSFields', $fields); |
|
298 | 1 | return $fields; |
|
299 | } |
||
300 | |||
301 | /** |
||
302 | * Return whether a user can create an object of this type |
||
303 | * |
||
304 | * @param Member $member |
||
305 | * @param array $context Virtual parameter to allow context to be passed in to check |
||
306 | * @return bool |
||
307 | */ |
||
308 | View Code Duplication | public function canCreate($member = null) |
|
309 | { |
||
310 | // Check parent page |
||
311 | $parent = $this->getCanCreateContext(func_get_args()); |
||
312 | if ($parent) { |
||
313 | return $parent->canEdit($member); |
||
314 | } |
||
315 | |||
316 | // Fall back to secure admin permissions |
||
317 | return parent::canCreate($member); |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * Helper method to check the parent for this object |
||
322 | * |
||
323 | * @param array $args List of arguments passed to canCreate |
||
324 | * @return SiteTree Parent page instance |
||
325 | */ |
||
326 | View Code Duplication | protected function getCanCreateContext($args) |
|
327 | { |
||
328 | // Inspect second parameter to canCreate for a 'Parent' context |
||
329 | if (isset($args[1]['Form'])) { |
||
330 | return $args[1]['Form']; |
||
331 | } |
||
332 | // Hack in currently edited page if context is missing |
||
333 | if (Controller::has_curr() && Controller::curr() instanceof CMSMain) { |
||
334 | return Controller::curr()->currentPage(); |
||
335 | } |
||
336 | |||
337 | // No page being edited |
||
338 | return null; |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * @param Member |
||
343 | * |
||
344 | * @return boolean |
||
345 | */ |
||
346 | public function canView($member = null) |
||
347 | { |
||
348 | return $this->Form()->canView($member); |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * @param Member |
||
353 | * |
||
354 | * @return boolean |
||
355 | */ |
||
356 | 1 | public function canEdit($member = null) |
|
357 | { |
||
358 | 1 | return $this->Form()->canEdit($member); |
|
359 | } |
||
360 | |||
361 | /** |
||
362 | * @param Member |
||
363 | * |
||
364 | * @return boolean |
||
365 | */ |
||
366 | 1 | public function canDelete($member = null) |
|
367 | { |
||
368 | 1 | return $this->canEdit($member); |
|
369 | } |
||
370 | |||
371 | /* |
||
372 | * Determine if this recipient may receive notifications for this submission |
||
373 | * |
||
374 | * @param array $data |
||
375 | * @param Form $form |
||
376 | * @return bool |
||
377 | */ |
||
378 | 2 | public function canSend($data, $form) |
|
402 | |||
403 | /** |
||
404 | * Make sure the email template saved against the recipient exists on the file system. |
||
405 | * |
||
406 | * @param string |
||
407 | * |
||
408 | * @return boolean |
||
409 | */ |
||
410 | 2 | public function emailTemplateExists($template = '') |
|
411 | { |
||
412 | 2 | $t = ($template ? $template : $this->EmailTemplate); |
|
413 | |||
414 | 2 | return in_array($t, $this->getEmailTemplateDropdownValues()); |
|
415 | } |
||
416 | |||
417 | /** |
||
418 | * Get the email body for the current email format |
||
419 | * |
||
420 | * @return string |
||
421 | */ |
||
422 | 2 | public function getEmailBodyContent() |
|
423 | { |
||
424 | 2 | return $this->SendPlain ? $this->EmailBody : $this->EmailBodyHtml; |
|
425 | } |
||
426 | |||
427 | /** |
||
428 | * Gets a list of email templates suitable for populating the email template dropdown. |
||
429 | * |
||
430 | * @return array |
||
431 | */ |
||
432 | 4 | public function getEmailTemplateDropdownValues() |
|
449 | } |
||
450 |
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.