Completed
Push — master ( 4f056b...dcf641 )
by Robbie
23s queued 11s
created

ResourceField::generateReadableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\CKANRegistry\Model;
4
5
use SilverStripe\Forms\FieldGroup;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\ORM\DataObject;
8
9
/**
10
 * Represents a generic field on a CKAN Resource, e.g. a column in a spreadsheet.
11
 * It is intentionally generic, as the resource may not be a tabular one, e.g. geospatal data to be rendered in a map.
12
 *
13
 * @method Resource Resource
14
 * @method static ResourceField create()
15
 * @property string Name
16
 * @property string Type
17
 * @property string ReadableName
18
 * @property bool ShowInSummaryView
19
 * @property bool ShowInDetailView
20
 * @property bool RemoveDuplicates
21
 * @property int Order
22
 * @property string DisplayConditions
23
 */
24
class ResourceField extends DataObject
25
{
26
    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...
27
28
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
29
        'Name' => 'Varchar',
30
        'Type' => 'Varchar',
31
        'ReadableName' => 'Varchar',
32
        'ShowInSummaryView' => 'Boolean',
33
        'ShowInDetailView' => 'Boolean',
34
        'RemoveDuplicates' => 'Boolean',
35
        'Order' => 'Int',
36
        'DisplayConditions' => 'Text',
37
    ];
38
39
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
40
        'Resource' => Resource::class,
41
    ];
42
43
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
44
        'Order',
45
        'ReadableName',
46
        'Name',
47
        'Type',
48
        'ShowInSummaryView',
49
        'ShowInDetailView',
50
    ];
51
52
    public function getCMSFields()
53
    {
54
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
55
            $fields->removeByName('Name');
56
57
            $fields->removeByName('Type');
58
            $fields->dataFieldByName('ReadableName')
59
                ->setAttribute('placeholder', $this->Name);
60
61
            $summary = $fields->dataFieldByName('ShowInSummaryView');
62
            $detail = $fields->dataFieldByName('ShowInDetailView');
63
            $duplicates = $fields->dataFieldByName('RemoveDuplicates');
64
65
            $fields->removeByName(['ShowInSummaryView', 'ShowInDetailView', 'RemoveDuplicates',]);
66
            $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

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