Passed
Pull Request — master (#64)
by
unknown
02:07
created

CKANRegistryPage::getCMSFields()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 47
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 32
nc 1
nop 0
dl 0
loc 47
rs 8.7857
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\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
                $resourceFields = GridField::create('DataColumns', 'Columns', $resource->Fields(), $columnsConfig);
58
                // Configure inline editable checkboxes for the two boolean fields
59
                $before = null;
60
                $editableColumns = $injector->create(GridFieldEditableColumns::class);
61
                foreach ($columnsConfig->getComponents() as $component) {
62
                    if ($before !== null) {
63
                        $before = $component;
64
                        break;
65
                    }
66
                    if ($component instanceof GridFieldDataColumns) {
67
                        $before = false;
68
                        $columns = $component->getDisplayFields($resourceFields);
69
                        $editable = array_flip(['ShowInSummaryView', 'ShowInDetailView']);
70
                        $component->setDisplayFields(array_diff_key($columns, $editable));
71
                        // set this way so that title translations are preserved
72
                        $editableColumns->setDisplayFields(array_intersect_key($columns, $editable));
73
                    }
74
                }
75
                $columnsConfig->addComponent($editableColumns, $before);
76
                $fields->addFieldToTab('Root.Data', $resourceFields);
77
78
                $filtersConfig = GridFieldConfig_RecordEditor::create();
79
                $filtersConfig->removeComponentsByType([
80
                    GridFieldAddExistingAutocompleter::class,
81
                    GridFieldAddNewButton::class
82
                ])
83
                    ->addComponents([
84
                        $injector->create(GridFieldAddNewMultiClass::class),
85
                        $injector->createWithArgs(GridFieldOrderableRows::class, ['Order']),
86
                    ]);
87
                $resourceFilters = GridField::create('DataFilters', 'Filters', $resource->Filters(), $filtersConfig);
88
                $fields->addFieldToTab('Root.Filters', $resourceFilters);
89
            }
90
        });
91
92
        return parent::getCMSFields();
93
    }
94
95
    public function getSettingsFields()
96
    {
97
        $fields = parent::getSettingsFields();
98
99
        $fields->addFieldsToTab('Root.Settings', [
100
            TextField::create('ItemsPerPage', _t(__CLASS__ . '.ITEMS_PER_PAGE', 'Items per page')),
101
        ]);
102
103
        return $fields;
104
    }
105
}
106