Completed
Push — master ( 61d922...1407e2 )
by
unknown
21s
created

CKANRegistryPageController::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\CKANRegistry\Page;
4
5
use PageController;
0 ignored issues
show
Bug introduced by
The type PageController 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\Model\Resource;
7
use SilverStripe\CKANRegistry\Model\ResourceField;
8
use SilverStripe\CKANRegistry\Model\ResourceFilter;
9
use SilverStripe\Control\Director;
10
use SilverStripe\ORM\DataObject;
11
use SilverStripe\View\Requirements;
12
13
class CKANRegistryPageController extends PageController
14
{
15
    private static $url_handlers = [
0 ignored issues
show
introduced by
The private property $url_handlers is not used, and could be removed.
Loading history...
16
        // Route all requests for this page, including sub-URLs, to the index action.
17
        // The frontend components should take care of handling sub-URL routing from here.
18
        'view/$Item' => 'index',
19
    ];
20
21
    protected function init()
22
    {
23
        parent::init();
24
25
        Requirements::javascript('silverstripe/admin: client/dist/js/i18n.js');
26
        Requirements::add_i18n_javascript('silverstripe/ckan-registry: client/lang');
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\View\Requir...::add_i18n_javascript() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

26
        /** @scrutinizer ignore-deprecated */ Requirements::add_i18n_javascript('silverstripe/ckan-registry: client/lang');
Loading history...
27
    }
28
29
    /**
30
     * Loads model data encapsulated as JSON in order to power front end technologies used to render that
31
     * data. Includes critical info such as the CKAN site to query (e.g. which domain, datastore, etc.)
32
     * but also can be extended to be used for configuring the component used to show this (e.g. React.js
33
     * or Vue.js component configuration).
34
     *
35
     * @param DataObject $holder
36
     * @return array
37
     */
38
    public function getCKANClientConfig(DataObject $holder = null)
39
    {
40
        if (!$holder) {
41
            $holder = $this->data();
42
        }
43
44
        /** @var Resource $resource */
45
        $resource = $holder->getComponent('DataResource');
46
47
        $config = [
48
            'spec' => [
49
                'endpoint' => $resource->Endpoint,
50
                'dataset' => $resource->DataSet,
51
                'identifier' => $resource->Identifier,
52
            ],
53
            'name' => $resource->Name,
54
            'resourceName' => $resource->ResourceName,
55
            'basePath' => $this->getBasePath($holder),
56
            'fields' => array_map(
57
                function (ResourceField $field) {
58
                    return [
59
                        'OriginalLabel' => $field->OriginalLabel,
60
                        'ReadableLabel' => $field->ReadableLabel,
61
                        'ShowInResultsView' => $field->ShowInResultsView,
62
                        'ShowInDetailView' => $field->ShowInDetailView,
63
                        'DisplayConditions' => $field->DisplayConditions,
64
                        'RemoveDuplicates' => $field->RemoveDuplicates,
65
                    ];
66
                },
67
                $resource->Fields()->filterAny([
68
                    'ShowInResultsView' => true,
69
                    'ShowInDetailView' => true,
70
                    'RemoveDuplicates' => true,
71
                ])->Sort('Position', 'ASC')->toArray()
72
            ),
73
            'filters' => array_map(
74
                function (ResourceFilter $filter) {
75
                    $explodedClassName = explode('\\', get_class($filter));
76
                    return [
77
                        'type' => array_pop($explodedClassName),
78
                    ] + $filter->getClientConfig();
79
                },
80
                $resource->Filters()->toArray()
81
            )
82
        ];
83
84
        $this->extend('updateCKANClientConfig', $config);
85
86
        return $config;
87
    }
88
89
    /**
90
     * Returns the base path for the resource's page with a leading slash
91
     *
92
     * @param DataObject $holder
93
     * @return string
94
     */
95
    public function getBasePath(DataObject $holder = null)
96
    {
97
        if (!$holder) {
98
            return '/';
99
        }
100
101
        $link = $holder->RelativeLink();
102
        return Director::baseURL() . trim($link, '/');
103
    }
104
}
105