ResourceField::getCMSFields()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 57
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 1
Metric Value
cc 2
eloc 38
c 7
b 0
f 1
nc 1
nop 0
dl 0
loc 57
rs 9.312

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SilverStripe\CKANRegistry\Model;
4
5
use SilverStripe\CKANRegistry\Forms\ResultConditionsField;
6
use SilverStripe\Forms\FieldGroup;
7
use SilverStripe\Forms\FieldList;
8
use SilverStripe\Forms\NumericField;
9
use SilverStripe\Forms\ReadonlyField;
10
use SilverStripe\ORM\DataObject;
11
12
/**
13
 * Represents a generic field on a CKAN Resource, e.g. a column in a spreadsheet.
14
 * It is intentionally generic, as the resource may not be a tabular one, e.g. geospatial data to be rendered in a map.
15
 *
16
 * @property Resource Resource
17
 * @method static ResourceField create()
18
 * @property string OriginalLabel
19
 * @property string Type
20
 * @property string ReadableLabel
21
 * @property bool ShowInResultsView
22
 * @property bool ShowInDetailView
23
 * @property bool RemoveDuplicates
24
 * @property int Position
25
 * @property string DisplayConditions
26
 */
27
class ResourceField extends DataObject
28
{
29
    private static $table_name = 'CKANResourceField';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
30
31
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
32
        'OriginalLabel' => 'Varchar',
33
        'Type' => 'Varchar',
34
        'ReadableLabel' => 'Varchar',
35
        'ShowInResultsView' => 'Boolean',
36
        'ShowInDetailView' => 'Boolean(1)',
37
        'RemoveDuplicates' => 'Boolean',
38
        'Position' => 'Int',
39
        'DisplayConditions' => 'Text',
40
    ];
41
42
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
43
        'Resource' => Resource::class,
44
    ];
45
46
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
47
        'Position',
48
        'ReadableLabel',
49
        'OriginalLabel',
50
        'Type',
51
        'ShowInResultsView',
52
        'ShowInDetailView',
53
    ];
54
55
    public function getCMSFields()
56
    {
57
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
58
            $originalLabel = ReadonlyField::create('OriginalLabel')
59
                ->setDescription(_t(
60
                    __CLASS__ . '.ORIGINAL_LABEL_DESCRIPTION',
61
                    'Title of this field as provided by the CKAN resource'
62
                ));
63
            $fields->replaceField('OriginalLabel', $originalLabel);
64
65
            $readableLabel = $fields->dataFieldByName('ReadableLabel');
66
            $readableLabel->setAttribute('placeholder', $this->OriginalLabel);
67
68
            $fields->removeByName('Type');
69
70
            $positionField = NumericField::create('Position')
71
                ->setTitle(_t(__CLASS__ . '.ORDER_LABEL', 'Presented order'))
72
                ->setDescription(_t(
73
                    __CLASS__ . '.ORDER_DENOMINATOR',
74
                    'of {count} columns',
75
                    ['count' => static::get()->filter('ResourceID', $this->ResourceID)->count()]
0 ignored issues
show
Bug Best Practice introduced by
The property ResourceID does not exist on SilverStripe\CKANRegistry\Model\ResourceField. Since you implemented __get, consider adding a @property annotation.
Loading history...
76
                ))
77
                ->addExtraClass('ckan-resource__order');
78
            $fields->replaceField('Position', $positionField);
79
80
            $summary = $fields->dataFieldByName('ShowInResultsView')
81
                ->addExtraClass('visibility-options__option');
82
            $detail = $fields->dataFieldByName('ShowInDetailView')
83
                ->addExtraClass('visibility-options__option');
84
            $duplicates = $fields->dataFieldByName('RemoveDuplicates')
85
                ->setTitle(
86
                    _t(__CLASS__ . '.REMOVE_DUPLICATES_LABEL', 'Only show one value if duplicates exist')
87
                )
88
                ->addExtraClass('visibility-options__option');
89
90
            // Present the visibility fields in a group
91
            $fields->removeByName(['ShowInResultsView', 'ShowInDetailView', 'RemoveDuplicates']);
92
            $visibilityOptions = FieldGroup::create('Visibility', [$summary, $detail, $duplicates])
93
                ->addExtraClass('visibility-options');
94
            $fields->insertBefore($visibilityOptions, 'Position');
0 ignored issues
show
Bug introduced by
'Position' of type string is incompatible with the type SilverStripe\Forms\FormField expected by parameter $item of SilverStripe\Forms\FieldList::insertBefore(). ( Ignorable by Annotation )

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

94
            $fields->insertBefore($visibilityOptions, /** @scrutinizer ignore-type */ 'Position');
Loading history...
95
96
            $fields->removeByName('ResourceID');
97
98
            $fields->replaceField(
99
                'DisplayConditions',
100
                ResultConditionsField::create(
101
                    'DisplayConditions',
102
                    _t(__CLASS__ . '.RESULT_CONDITIONS', 'Result conditions')
103
                )
104
            );
105
106
            // See https://github.com/silverstripe/silverstripe-framework/issues/8696
107
            foreach ([$summary, $detail, $readableLabel, $originalLabel] as $field) {
108
                $field->setTitle(ucfirst(strtolower($field->Title())));
0 ignored issues
show
Bug introduced by
The method Title() does not exist on null. ( Ignorable by Annotation )

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

108
                $field->setTitle(ucfirst(strtolower($field->/** @scrutinizer ignore-call */ Title())));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
109
            }
110
        });
111
        return parent::getCMSFields();
112
    }
113
114
    /**
115
     * Use the readable label for GridField CRUD operation result messages
116
     *
117
     * @return string
118
     */
119
    public function getTitle()
120
    {
121
        return $this->ReadableLabel;
122
    }
123
}
124