Passed
Pull Request — master (#36)
by
unknown
02:46
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
/**
12
 * Represents a generic field on a CKAN Resource, e.g. a column in a spreadsheet.
13
 * It is intentionally generic, as the resource may not be a tabular one, e.g. geospatal data to be rendered in a map.
14
 */
15
class ResourceField extends DataObject
16
{
17
    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...
18
19
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
20
        'Name' => 'Varchar',
21
        'Type' => 'Varchar',
22
        'ReadableName' => 'Varchar',
23
        'ShowInSummaryView' => 'Boolean',
24
        'ShowInDetailView' => 'Boolean',
25
        'RemoveDuplicates' => 'Boolean',
26
        'Order' => 'Int',
27
        'DisplayConditions' => 'Text',
28
    ];
29
30
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
31
        'Resource' => Resource::class,
32
    ];
33
34
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
35
        'Order',
36
        'ReadableName',
37
        'Name',
38
        'Type',
39
        'ShowInSummaryView',
40
        'ShowInDetailView',
41
    ];
42
43
    /**
44
     * Always display the 'ReadableName' unless it's unset, then display the name that is returned by CKAN
45
     * @return string|DBString
46
     */
47
    public function getReadableName()
48
    {
49
        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...
50
    }
51
52
    public function getCMSFields()
53
    {
54
        $fields = parent::getCMSFields();
55
56
        $fields->removeByName('Name');
57
58
        $fields->removeByName('Type');
59
        $fields->dataFieldByName('ReadableName')
60
            ->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...
61
62
        $summary = $fields->dataFieldByName('ShowInSummaryView');
63
        $detail = $fields->dataFieldByName('ShowInDetailView');
64
        $duplicates = $fields->dataFieldByName('RemoveDuplicates');
65
66
        $fields->removeByName(['ShowInSummaryView', 'ShowInDetailView', 'RemoveDuplicates',]);
67
        $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

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