Issues (50)

src/Page/CKANRegistryPage.php (9 issues)

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