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