Passed
Pull Request — master (#64)
by Robbie
02:55
created

CKANRegistryPage::getCMSFields()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 57
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 35
nc 1
nop 0
dl 0
loc 57
rs 8.7377
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace SilverStripe\CKANRegistry\Page;
4
5
use Page;
0 ignored issues
show
Bug introduced by
The type Page was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\CKANRegistry\Forms\ResourceLocatorField;
7
use SilverStripe\CKANRegistry\Model\Resource;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\GridField\GridField;
11
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
12
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
13
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
14
use SilverStripe\Forms\GridField\GridFieldDataColumns;
15
use SilverStripe\Forms\TextField;
16
use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass;
17
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
18
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
19
20
/**
21
 * A CKANRegistryPage will render a chosen CKAN data set on the frontend, provide the user with configurable filters
22
 * and display a set of CMS configured columns.
23
 *
24
 * @method Resource DataResource
25
 */
26
class CKANRegistryPage extends Page
27
{
28
    private static $table_name = 'CKANRegistryPage';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
29
30
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
31
        'ItemsPerPage' => 'Int',
32
    ];
33
34
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
35
        'DataResource' => Resource::class,
36
    ];
37
38
    private static $defaults = [
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
39
        'ItemsPerPage' => 20,
40
    ];
41
42
    private static $singular_name = 'CKAN Registry Page';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
43
44
    private static $plural_name = 'CKAN Registry Pages';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
45
46
    public function getCMSFields()
47
    {
48
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
49
            $resource = $this->DataResource();
50
            $fields->addFieldToTab('Root.Data', ResourceLocatorField::create('DataResource'));
51
52
            if ($resource && $resource->Identifier) {
53
                $injector = Injector::inst();
54
55
                $columnsConfig = GridFieldConfig_RecordEditor::create()
56
                    ->addComponent($injector->createWithArgs(GridFieldOrderableRows::class, ['Order']));
57
58
                $resourceFields = GridField::create('DataColumns', 'Columns', $resource->Fields(), $columnsConfig);
59
                $resourceFields->addExtraClass('ckan-columns');
60
61
                // Configure inline editable checkboxes for the two boolean fields
62
                $before = null;
63
                $editableColumns = $injector->create(GridFieldEditableColumns::class);
64
                foreach ($columnsConfig->getComponents() as $component) {
65
                    if ($before !== null) {
66
                        $before = $component;
67
                        break;
68
                    }
69
                    if ($component instanceof GridFieldDataColumns) {
70
                        $before = false;
71
                        $columns = $component->getDisplayFields($resourceFields);
72
73
                        // We only want to change the labels for the GridField view, not the model's edit form
74
                        $columns['ShowInSummaryView'] = _t(__CLASS__ . '.IN_RESULTS', 'In Results');
75
                        $columns['ShowInDetailView'] = _t(__CLASS__ . '.IN_DETAIL', 'In Detail');
76
77
                        $editable = array_flip(['ShowInSummaryView', 'ShowInDetailView']);
78
                        $component->setDisplayFields(array_diff_key($columns, $editable));
79
                        // set this way so that title translations are preserved
80
                        $editableColumns->setDisplayFields(array_intersect_key($columns, $editable));
81
                    }
82
                }
83
                $columnsConfig->addComponent($editableColumns, $before);
84
                $fields->addFieldToTab('Root.Data', $resourceFields);
85
86
                $filtersConfig = GridFieldConfig_RecordEditor::create();
87
                $filtersConfig->removeComponentsByType([
88
                    GridFieldAddExistingAutocompleter::class,
89
                    GridFieldAddNewButton::class
90
                ])
91
                    ->addComponents([
92
                        $injector->create(GridFieldAddNewMultiClass::class),
93
                        $injector->createWithArgs(GridFieldOrderableRows::class, ['Order']),
94
                    ]);
95
96
                $resourceFilters = GridField::create('DataFilters', 'Filters', $resource->Filters(), $filtersConfig);
97
98
                $fields->addFieldToTab('Root.Filters', $resourceFilters);
99
            }
100
        });
101
102
        return parent::getCMSFields();
103
    }
104
105
    public function getSettingsFields()
106
    {
107
        $fields = parent::getSettingsFields();
108
109
        $fields->addFieldsToTab('Root.Settings', [
110
            TextField::create('ItemsPerPage', _t(__CLASS__ . '.ITEMS_PER_PAGE', 'Items per page')),
111
        ]);
112
113
        return $fields;
114
    }
115
}
116