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

CKANRegistryPage::getCKANClientConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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