Conditions | 15 |
Paths | 84 |
Total Lines | 136 |
Code Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 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 |
||
28 | public function __construct($name, CommentingController $controller) |
||
29 | { |
||
30 | $usePreview = $controller->getOption('use_preview'); |
||
31 | $nameRequired = _t('CommentInterface.YOURNAME_MESSAGE_REQUIRED', 'Please enter your name'); |
||
32 | $emailRequired = _t('CommentInterface.EMAILADDRESS_MESSAGE_REQUIRED', 'Please enter your email address'); |
||
33 | $emailInvalid = _t('CommentInterface.EMAILADDRESS_MESSAGE_EMAIL', 'Please enter a valid email address'); |
||
34 | $urlInvalid = _t('CommentInterface.COMMENT_MESSAGE_URL', 'Please enter a valid URL'); |
||
35 | $commentRequired = _t('CommentInterface.COMMENT_MESSAGE_REQUIRED', 'Please enter your comment'); |
||
36 | |||
37 | $fields = FieldList::create( |
||
38 | $dataFields = CompositeField::create( |
||
39 | // Name |
||
40 | $a = TextField::create('Name', _t('CommentInterface.YOURNAME', 'Your name')) |
||
41 | ->setCustomValidationMessage($nameRequired) |
||
42 | ->setAttribute('data-msg-required', $nameRequired), |
||
43 | |||
44 | EmailField::create( |
||
45 | 'Email', |
||
46 | _t( |
||
47 | 'SilverStripe\\Comments\\Controllers\\CommentingController.EMAILADDRESS', |
||
48 | 'Your email address (will not be published)' |
||
49 | ) |
||
50 | ) |
||
51 | ->setCustomValidationMessage($emailRequired) |
||
52 | ->setAttribute('data-msg-required', $emailRequired) |
||
53 | ->setAttribute('data-msg-email', $emailInvalid) |
||
54 | ->setAttribute('data-rule-email', true), |
||
55 | // Url |
||
56 | TextField::create('URL', _t( |
||
57 | 'SilverStripe\\Comments\\Controllers\\CommentingController.WEBSITEURL', |
||
58 | 'Your website URL' |
||
59 | )) |
||
60 | ->setAttribute('data-msg-url', $urlInvalid) |
||
61 | ->setAttribute('data-rule-url', true), |
||
62 | // Comment |
||
63 | TextareaField::create('Comment', _t( |
||
64 | 'SilverStripe\\Comments\\Controllers\\CommentingController.COMMENTS', |
||
65 | 'Comments' |
||
66 | )) |
||
67 | ->setCustomValidationMessage($commentRequired) |
||
68 | ->setAttribute('data-msg-required', $commentRequired) |
||
69 | ), |
||
70 | HiddenField::create('ParentID'), |
||
71 | HiddenField::create('ParentClassName'), |
||
72 | HiddenField::create('ReturnURL'), |
||
73 | HiddenField::create('ParentCommentID') |
||
74 | ); |
||
75 | |||
76 | // Preview formatted comment. Makes most sense when shortcodes or |
||
77 | // limited HTML is allowed. Populated by JS/Ajax. |
||
78 | if ($usePreview) { |
||
79 | $fields->insertAfter( |
||
80 | ReadonlyField::create('PreviewComment', _t('CommentInterface.PREVIEWLABEL', 'Preview')) |
||
81 | ->setAttribute('style', 'display: none'), // enable through JS |
||
82 | 'Comment' |
||
83 | ); |
||
84 | } |
||
85 | |||
86 | $dataFields->addExtraClass('data-fields'); |
||
87 | |||
88 | // save actions |
||
89 | $actions = FieldList::create( |
||
90 | $postAction = new FormAction('doPostComment', _t('CommentInterface.POST', 'Post')) |
||
91 | ); |
||
92 | |||
93 | if ($usePreview) { |
||
94 | $actions->push( |
||
95 | FormAction::create('doPreviewComment', _t('CommentInterface.PREVIEW', 'Preview')) |
||
96 | ->addExtraClass('action-minor') |
||
97 | ->setAttribute('style', 'display: none') // enable through JS |
||
98 | ); |
||
99 | } |
||
100 | |||
101 | $required = RequiredFields::create( |
||
102 | $controller->config()->required_fields |
||
103 | ); |
||
104 | |||
105 | parent::__construct($controller, $name, $fields, $actions, $required); |
||
106 | |||
107 | |||
108 | // if the record exists load the extra required data |
||
109 | if ($record = $controller->getOwnerRecord()) { |
||
110 | // Load member data |
||
111 | $member = Security::getCurrentUser(); |
||
112 | if (($record->CommentsRequireLogin || $record->PostingRequiredPermission) && $member) { |
||
113 | $fields = $this->Fields(); |
||
114 | |||
115 | $fields->removeByName('Name'); |
||
116 | $fields->removeByName('Email'); |
||
117 | $fields->insertBefore( |
||
118 | ReadonlyField::create( |
||
119 | 'NameView', |
||
120 | _t('CommentInterface.YOURNAME', 'Your name'), |
||
121 | $member->getName() |
||
122 | ), |
||
123 | 'URL' |
||
|
|||
124 | ); |
||
125 | $fields->push(HiddenField::create('Name', '', $member->getName())); |
||
126 | $fields->push(HiddenField::create('Email', '', $member->Email)); |
||
127 | } |
||
128 | |||
129 | // we do not want to read a new URL when the form has already been submitted |
||
130 | // which in here, it hasn't been. |
||
131 | $this->loadDataFrom([ |
||
132 | 'ParentID' => $record->ID, |
||
133 | 'ReturnURL' => $controller->getRequest()->getURL(), |
||
134 | 'ParentClassName' => $controller->getParentClass() |
||
135 | ]); |
||
136 | } |
||
137 | |||
138 | // Set it so the user gets redirected back down to the form upon form fail |
||
139 | $this->setRedirectToFormOnValidationError(true); |
||
140 | |||
141 | // load any data from the session |
||
142 | $data = $this->getSessionData(); |
||
143 | if (!is_array($data)) { |
||
144 | return; |
||
145 | } |
||
146 | |||
147 | // load user data from previous form request back into form. |
||
148 | if (array_key_exists('UserData', $data)) { |
||
149 | $formData = json_decode($data['UserData'], true); |
||
150 | |||
151 | $this->loadDataFrom([ |
||
152 | 'Name' => isset($formData['Name']) ? $formData['Name'] : '', |
||
153 | 'URL' => isset($formData['URL']) ? $formData['URL'] : '', |
||
154 | 'Email' => isset($formData['Email']) ? $formData['Email'] : '' |
||
155 | ]); |
||
156 | } |
||
157 | |||
158 | // allow previous value to fill if comment |
||
159 | if (array_key_exists('Comment', $data)) { |
||
160 | $prevComment = $data['Comment']; |
||
161 | |||
162 | if ($prevComment && $prevComment != '') { |
||
163 | $this->loadDataFrom(['Comment' => $prevComment]); |
||
164 | } |
||
300 |