Conditions | 9 |
Paths | 2 |
Total Lines | 158 |
Code Lines | 98 |
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 | 2 | public function getCMSFields() |
|
179 | { |
||
180 | Requirements::css( |
||
181 | ModuleLoader::getModule('silverstripe/userforms') |
||
182 | ->getRelativeResourcePath('client/dist/styles/userforms-cms.css') |
||
183 | 2 | ); |
|
184 | |||
185 | $this->beforeUpdateCMSFields(function ($fields) { |
||
186 | // define tabs |
||
187 | $fields->findOrMakeTab('Root.FormOptions', _t(__CLASS__.'.CONFIGURATION', 'Configuration')); |
||
188 | $fields->findOrMakeTab('Root.Recipients', _t(__CLASS__.'.RECIPIENTS', 'Recipients')); |
||
189 | $fields->findOrMakeTab('Root.Submissions', _t(__CLASS__.'.SUBMISSIONS', 'Submissions')); |
||
190 | |||
191 | 2 | // text to show on complete |
|
192 | 2 | $onCompleteFieldSet = CompositeField::create( |
|
193 | 2 | $label = LabelField::create( |
|
194 | 'OnCompleteMessageLabel', |
||
195 | 2 | _t(__CLASS__.'.ONCOMPLETELABEL', 'Show on completion') |
|
196 | 2 | ), |
|
197 | $editor = HTMLEditorField::create( |
||
198 | 2 | 'OnCompleteMessage', |
|
199 | '', |
||
200 | 2 | _t(__CLASS__.'.ONCOMPLETEMESSAGE', $this->OnCompleteMessage) |
|
201 | 2 | ) |
|
202 | 2 | ); |
|
203 | 2 | ||
204 | 2 | $onCompleteFieldSet->addExtraClass('field'); |
|
205 | 2 | ||
206 | 2 | $editor->setRows(3); |
|
207 | 2 | $label->addExtraClass('left'); |
|
208 | 2 | ||
209 | 2 | // Define config for email recipients |
|
210 | 2 | $emailRecipientsConfig = GridFieldConfig_RecordEditor::create(10); |
|
211 | 2 | $emailRecipientsConfig->getComponentByType(GridFieldAddNewButton::class) |
|
212 | 2 | ->setButtonName( |
|
213 | _t(__CLASS__.'.ADDEMAILRECIPIENT', 'Add Email Recipient') |
||
214 | ); |
||
215 | |||
216 | 2 | // who do we email on submission |
|
217 | 2 | $emailRecipients = GridField::create( |
|
218 | 'EmailRecipients', |
||
219 | 2 | _t(__CLASS__.'.EMAILRECIPIENTS', 'Email Recipients'), |
|
220 | 2 | $this->EmailRecipients(), |
|
221 | 2 | $emailRecipientsConfig |
|
222 | 1 | ); |
|
223 | 1 | $emailRecipients |
|
224 | 2 | ->getConfig() |
|
225 | ->getComponentByType(GridFieldDetailForm::class) |
||
226 | 2 | ->setItemRequestClass(UserFormRecipientItemRequest::class); |
|
227 | |||
228 | $fields->addFieldsToTab('Root.FormOptions', $onCompleteFieldSet); |
||
229 | $fields->addFieldToTab('Root.Recipients', $emailRecipients); |
||
230 | $fields->addFieldsToTab('Root.FormOptions', $this->getFormOptions()); |
||
231 | 2 | ||
232 | |||
233 | // view the submissions |
||
234 | // make sure a numeric not a empty string is checked against this int column for SQL server |
||
235 | 2 | $parentID = (!empty($this->ID)) ? (int) $this->ID : 0; |
|
236 | 2 | ||
237 | 2 | // get a list of all field names and values used for print and export CSV views of the GridField below. |
|
238 | $columnSQL = <<<SQL |
||
239 | SELECT "SubmittedFormField"."Name" as "Name", COALESCE("EditableFormField"."Title", "SubmittedFormField"."Title") as "Title", COALESCE("EditableFormField"."Sort", 999) AS "Sort" |
||
240 | 2 | FROM "SubmittedFormField" |
|
241 | 2 | LEFT JOIN "SubmittedForm" ON "SubmittedForm"."ID" = "SubmittedFormField"."ParentID" |
|
242 | 2 | LEFT JOIN "EditableFormField" ON "EditableFormField"."Name" = "SubmittedFormField"."Name" AND "EditableFormField"."ParentID" = '$parentID' |
|
243 | WHERE "SubmittedForm"."ParentID" = '$parentID' |
||
244 | ORDER BY "Sort", "Title" |
||
245 | SQL; |
||
246 | 2 | // Sanitise periods in title |
|
247 | 2 | $columns = array(); |
|
248 | foreach (DB::query($columnSQL)->map() as $name => $title) { |
||
249 | $columns[$name] = trim(strtr($title, '.', ' ')); |
||
250 | 2 | } |
|
251 | 2 | ||
252 | $config = GridFieldConfig::create(); |
||
253 | 2 | $config->addComponent(new GridFieldToolbarHeader()); |
|
254 | 2 | $config->addComponent($sort = new GridFieldSortableHeader()); |
|
255 | 2 | $config->addComponent($filter = new UserFormsGridFieldFilterHeader()); |
|
256 | 2 | $config->addComponent(new GridFieldDataColumns()); |
|
257 | $config->addComponent(new GridFieldEditButton()); |
||
258 | 2 | $config->addComponent(new GridFieldDeleteAction()); |
|
259 | 2 | $config->addComponent(new GridFieldPageCount('toolbar-header-right')); |
|
260 | 2 | $config->addComponent($pagination = new GridFieldPaginator(25)); |
|
261 | 2 | $config->addComponent(new GridFieldDetailForm()); |
|
262 | 2 | $config->addComponent(new GridFieldButtonRow('after')); |
|
263 | 2 | $config->addComponent($export = new GridFieldExportButton('buttons-after-left')); |
|
264 | 2 | $config->addComponent($print = new GridFieldPrintButton('buttons-after-left')); |
|
265 | 2 | ||
266 | 2 | // show user form items in the summary tab |
|
267 | 2 | $summaryarray = array( |
|
268 | 'ID' => 'ID', |
||
269 | 2 | 'Created' => 'Created', |
|
270 | 'LastEdited' => 'Last Edited' |
||
271 | 2 | ); |
|
272 | foreach (EditableFormField::get()->filter(array('ParentID' => $parentID)) as $eff) { |
||
273 | if ($eff->ShowInSummary) { |
||
274 | $summaryarray[$eff->Name] = $eff->Title ?: $eff->Name; |
||
275 | } |
||
276 | } |
||
277 | |||
278 | 2 | $config->getComponentByType(GridFieldDataColumns::class)->setDisplayFields($summaryarray); |
|
279 | |||
280 | /** |
||
281 | * Support for {@link https://github.com/colymba/GridFieldBulkEditingTools} |
||
282 | */ |
||
283 | if (class_exists(BulkManager::class)) { |
||
284 | $config->addComponent(new BulkManager); |
||
285 | } |
||
286 | |||
287 | $sort->setThrowExceptionOnBadDataType(false); |
||
288 | $filter->setThrowExceptionOnBadDataType(false); |
||
289 | 3 | $pagination->setThrowExceptionOnBadDataType(false); |
|
290 | |||
291 | 3 | // attach every column to the print view form |
|
292 | $columns['Created'] = 'Created'; |
||
293 | $columns['SubmittedBy.Email'] = 'Submitter'; |
||
294 | $filter->setColumns($columns); |
||
295 | |||
296 | 2 | // print configuration |
|
297 | 3 | ||
298 | $print->setPrintHasHeader(true); |
||
299 | 3 | $print->setPrintColumns($columns); |
|
300 | |||
301 | 3 | // export configuration |
|
302 | $export->setCsvHasHeader(true); |
||
303 | $export->setExportColumns($columns); |
||
304 | |||
305 | $submissions = GridField::create( |
||
306 | 'Submissions', |
||
307 | _t(__CLASS__.'.SUBMISSIONS', 'Submissions'), |
||
308 | $this->Submissions()->sort('Created', 'DESC'), |
||
309 | $config |
||
310 | 3 | ); |
|
311 | $fields->addFieldToTab('Root.Submissions', $submissions); |
||
312 | 3 | $fields->addFieldToTab( |
|
313 | 3 | 'Root.FormOptions', |
|
314 | CheckboxField::create( |
||
315 | 3 | 'DisableSaveSubmissions', |
|
316 | 3 | _t(__CLASS__.'.SAVESUBMISSIONS', 'Disable Saving Submissions to Server') |
|
317 | 3 | ) |
|
318 | 3 | ); |
|
319 | 3 | }); |
|
320 | 3 | ||
321 | 3 | $fields = parent::getCMSFields(); |
|
322 | 3 | ||
323 | 3 | if ($this->EmailRecipients()->Count() == 0 && static::config()->recipients_warning_enabled) { |
|
324 | $fields->addFieldToTab('Root.Main', LiteralField::create( |
||
325 | 3 | 'EmailRecipientsWarning', |
|
326 | '<p class="message warning">' . _t( |
||
327 | 3 | __CLASS__.'.NORECIPIENTS', |
|
328 | 'Warning: You have not configured any recipients. Form submissions may be missed.' |
||
329 | ) |
||
330 | . '</p>' |
||
331 | ), 'Title'); |
||
332 | } |
||
333 | |||
334 | return $fields; |
||
335 | } |
||
336 | |||
405 |
This check marks private properties in classes that are never used. Those properties can be removed.