Passed
Pull Request — master (#40)
by Robbie
02:47
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
use SilverStripe\ORM\FieldType\DBString;
9
10
/**
11
 * Represents a generic field on a CKAN Resource, e.g. a column in a spreadsheet.
12
 * It is intentionally generic, as the resource may not be a tabular one, e.g. geospatal data to be rendered in a map.
13
 *
14
 * @method Resource Resource
15
 */
16
class ResourceField extends DataObject
17
{
18
    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...
19
20
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
21
        'Name' => 'Varchar',
22
        'Type' => 'Varchar',
23
        'ReadableName' => 'Varchar',
24
        'ShowInSummaryView' => 'Boolean',
25
        'ShowInDetailView' => 'Boolean',
26
        'RemoveDuplicates' => 'Boolean',
27
        'Order' => 'Int',
28
        'DisplayConditions' => 'Text',
29
    ];
30
31
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
32
        'Resource' => Resource::class,
33
    ];
34
35
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
36
        'Order',
37
        'ReadableName',
38
        'Name',
39
        'Type',
40
        'ShowInSummaryView',
41
        'ShowInDetailView',
42
    ];
43
44
    public function getCMSFields()
45
    {
46
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
47
            $fields->removeByName('Name');
48
49
            $fields->removeByName('Type');
50
            $fields->dataFieldByName('ReadableName')
51
                ->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...
52
53
            $summary = $fields->dataFieldByName('ShowInSummaryView');
54
            $detail = $fields->dataFieldByName('ShowInDetailView');
55
            $duplicates = $fields->dataFieldByName('RemoveDuplicates');
56
57
            $fields->removeByName(['ShowInSummaryView', 'ShowInDetailView', 'RemoveDuplicates',]);
58
            $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

58
            $fields->insertBefore(FieldGroup::create('Visibility', [$summary, $detail, $duplicates]), /** @scrutinizer ignore-type */ 'Order');
Loading history...
59
60
            $fields->removeByName('ResourceID');
61
        });
62
        return parent::getCMSFields();
63
    }
64
65
    public function onBeforeWrite()
66
    {
67
        parent::onBeforeWrite();
68
69
        if (empty($this->ReadableName) && !empty($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...
70
            $this->generateReadableName();
71
        }
72
    }
73
74
    /**
75
     * Generate a readable name from the Name
76
     *
77
     * @return $this
78
     */
79
    protected function generateReadableName()
80
    {
81
        $readableName = str_replace(['_', '-'], ' ', $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...
82
        $readableName = ucfirst(strtolower($readableName));
83
        $this->ReadableName = $readableName;
0 ignored issues
show
Bug Best Practice introduced by
The property ReadableName does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
84
        return $this;
85
    }
86
}
87