Passed
Pull Request — master (#122)
by
unknown
02:41
created

CKANRegistryPage::getSettingsFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\GridField\GridFieldDetailForm\ResourceFieldItemRequest;
7
use SilverStripe\CKANRegistry\Forms\GridFieldResourceTitle;
8
use SilverStripe\CKANRegistry\Forms\ResourceLocatorField;
9
use SilverStripe\CKANRegistry\Model\Resource;
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\Forms\FieldList;
12
use SilverStripe\Forms\GridField\GridField;
13
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
14
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
15
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
16
use SilverStripe\Forms\GridField\GridFieldDataColumns;
17
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
18
use SilverStripe\Forms\GridField\GridFieldDetailForm;
19
use SilverStripe\Forms\GridField\GridFieldPageCount;
20
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
21
use SilverStripe\Forms\TextField;
22
use SilverStripe\View\Requirements;
23
use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass;
24
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
25
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
26
27
/**
28
 * A CKANRegistryPage will render a chosen CKAN data set on the frontend, provide the user with configurable filters
29
 * and display a set of CMS configured columns.
30
 *
31
 * @method Resource DataResource
32
 */
33
class CKANRegistryPage extends Page
34
{
35
    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...
36
37
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
38
        'DataResource' => Resource::class,
39
    ];
40
41
    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...
42
43
    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...
44
45
    private static $icon_class = 'font-icon-p-data';
0 ignored issues
show
introduced by
The private property $icon_class is not used, and could be removed.
Loading history...
46
47
    public function getCMSFields()
48
    {
49
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
50
            $resource = $this->DataResource();
51
52
            $fields->addFieldToTab(
53
                'Root.Data',
54
                ResourceLocatorField::create('DataResource')
55
                    ->addExtraClass('form-field--no-divider')
56
            );
57
58
            if ($resource && $resource->Identifier) {
59
                $injector = Injector::inst();
60
61
                $columnsConfig = GridFieldConfig_RecordEditor::create()
62
                    ->addComponents([
63
                        $injector->createWithArgs(GridFieldOrderableRows::class, ['Position']),
64
                        $injector->createWithArgs(GridFieldResourceTitle::class, [$resource]),
65
                    ])
66
                    ->removeComponentsByType([
67
                        GridFieldAddNewButton::class,
68
                        GridFieldDeleteAction::class,
69
                        GridFieldPageCount::class,
70
                        GridFieldToolbarHeader::class,
71
                    ]);
72
73
                $columnsConfig->getComponentByType(GridFieldDetailForm::class)
74
                    ->setItemRequestClass(ResourceFieldItemRequest::class);
75
76
                $resourceFields = GridField::create('DataColumns', '', $resource->Fields(), $columnsConfig);
77
                $resourceFields->addExtraClass('ckan-columns');
78
79
                // Configure inline editable checkboxes for the two boolean fields
80
                $before = null;
81
                $editableColumns = $injector->create(GridFieldEditableColumns::class);
82
                foreach ($columnsConfig->getComponents() as $component) {
83
                    if ($before !== null) {
84
                        $before = $component;
85
                        break;
86
                    }
87
                    if ($component instanceof GridFieldDataColumns) {
88
                        $before = false;
89
                        $columns = $component->getDisplayFields($resourceFields);
90
91
                        // We only want to change the labels for the GridField view, not the model's edit form
92
                        $columns['ShowInResultsView'] = _t(__CLASS__ . '.IN_RESULTS', 'In Results');
93
                        $columns['ShowInDetailView'] = _t(__CLASS__ . '.IN_DETAIL', 'In Detail');
94
95
                        // Abbreviate the position title
96
                        $columns['Position'] = _t(__CLASS__ . '.POS', 'Pos.', 'Abbreviated version of position');
97
98
                        $editable = array_flip(['ShowInResultsView', 'ShowInDetailView']);
99
                        $component->setDisplayFields(array_diff_key($columns, $editable));
100
                        // set this way so that title translations are preserved
101
                        $editableColumns->setDisplayFields(array_intersect_key($columns, $editable));
102
                    }
103
                }
104
                $columnsConfig->addComponent($editableColumns, $before);
105
                $fields->addFieldToTab('Root.Data', $resourceFields);
106
107
                $filtersConfig = GridFieldConfig_RecordEditor::create();
108
                $filtersConfig
109
                    ->removeComponentsByType([
110
                        GridFieldAddExistingAutocompleter::class,
111
                        GridFieldAddNewButton::class
112
                    ])
113
                    ->addComponents([
114
                        $injector->create(GridFieldAddNewMultiClass::class),
115
                        $injector->createWithArgs(GridFieldOrderableRows::class, ['Order']),
116
                    ]);
117
118
                $resourceFilters = GridField::create('DataFilters', '', $resource->Filters(), $filtersConfig);
119
120
                $fields->addFieldToTab('Root.Filters', $resourceFilters);
121
            }
122
        });
123
124
        return parent::getCMSFields();
125
    }
126
}
127