Passed
Pull Request — master (#36)
by
unknown
02:09
created

ResourceField   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 19 1
A getReadableName() 0 3 2
1
<?php
2
3
namespace SilverStripe\CKANRegistry\Model;
4
5
use SilverStripe\Forms\CheckboxField;
6
use SilverStripe\Forms\FieldGroup;
7
use SilverStripe\i18n\i18n;
8
use SilverStripe\ORM\DataObject;
9
use SilverStripe\ORM\FieldType\DBString;
10
11
class ResourceField extends DataObject
12
{
13
    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...
14
15
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
16
        'Name' => 'Varchar',
17
        'Type' => 'Varchar',
18
        'ReadableName' => 'Varchar',
19
        'ShowInSummaryView' => 'Boolean',
20
        'ShowInDetailView' => 'Boolean',
21
        'RemoveDuplicates' => 'Boolean',
22
        'Order' => 'Int',
23
        'DisplayConditions' => 'Text',
24
    ];
25
26
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
27
        'Resource' => Resource::class,
28
    ];
29
30
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
31
        'Order',
32
        'ReadableName',
33
        'Name',
34
        'Type',
35
        'ShowInSummaryView',
36
        'ShowInDetailView',
37
    ];
38
39
    /**
40
     * Always display the 'ReadableName' unless it's unset, then display the name that is returned by CKAN
41
     * @return string|DBString
42
     */
43
    public function getReadableName()
44
    {
45
        return $this->getField('ReadableName') ?: $this->Name;
0 ignored issues
show
Bug Best Practice introduced by
The property Name does not exist on SilverStripe\CKANRegistry\Model\ResourceField. Since you implemented __get, consider adding a @property annotation.
Loading history...
46
    }
47
48
    public function getCMSFields()
49
    {
50
        $fields = parent::getCMSFields();
51
52
        $fields->removeByName('Name');
53
54
        $fields->removeByName('Type');
55
        $fields->dataFieldByName('ReadableName')
56
            ->setAttribute('placeholder', $this->Name);
0 ignored issues
show
Bug Best Practice introduced by
The property Name does not exist on SilverStripe\CKANRegistry\Model\ResourceField. Since you implemented __get, consider adding a @property annotation.
Loading history...
57
58
        $summary = $fields->dataFieldByName('ShowInSummaryView');
59
        $detail = $fields->dataFieldByName('ShowInDetailView');
60
        $duplicates = $fields->dataFieldByName('RemoveDuplicates');
61
62
        $fields->removeByName(['ShowInSummaryView', 'ShowInDetailView', 'RemoveDuplicates',]);
63
        $fields->insertBefore(FieldGroup::create('Visibility', [$summary, $detail, $duplicates]), 'Order');
0 ignored issues
show
Bug introduced by
'Order' 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

63
        $fields->insertBefore(FieldGroup::create('Visibility', [$summary, $detail, $duplicates]), /** @scrutinizer ignore-type */ 'Order');
Loading history...
64
65
        $fields->removeByName('ResourceID');
66
        return $fields;
67
    }
68
}
69