Passed
Pull Request — master (#13)
by Robbie
02:31
created

Quicklink::getCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 30
nc 1
nop 0
dl 0
loc 48
rs 9.125
c 0
b 0
f 0
1
<?php
2
3
namespace CWP\CWP\Model;
4
5
use CWP\CWP\PageTypes\BaseHomePage;
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\Forms\CompositeField;
8
use SilverStripe\Forms\LiteralField;
9
use SilverStripe\Forms\TreeDropdownField;
10
use SilverStripe\ORM\DataObject;
11
12
class Quicklink extends DataObject
13
{
14
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
15
        'Name' => 'Varchar(255)',
16
        'ExternalLink' => 'Varchar(255)',
17
        'SortOrder' => 'Int',
18
    ];
19
20
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
21
        'Parent' => BaseHomePage::class,
22
        'InternalLink' => SiteTree::class,
23
    ];
24
25
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
26
        'Name' => 'Name',
27
        'InternalLink.Title' => 'Internal Link',
28
        'ExternalLink' => 'External Link',
29
    ];
30
31
    private static $table_name = 'Quicklink';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
32
33
    public function fieldLabels($includerelations = true)
34
    {
35
        $labels = parent::fieldLabels($includerelations);
36
        $labels['Name'] = _t('Quicklink.NameLabel', 'Name');
37
        $labels['ExternalLink'] = _t('Quicklink.ExternalLinkLabel', 'External Link');
38
        $labels['SortOrder'] = _t('Quicklink.SortOrderLabel', 'Sort Order');
39
        $labels['ParentID'] = _t('Quicklink.ParentRelationLabel', 'Parent');
40
        $labels['InternalLinkID'] = _t('Quicklink.InternalLinkLabel', 'Internal Link');
41
42
        return $labels;
43
    }
44
45
    public function getLink()
46
    {
47
        if ($this->ExternalLink) {
0 ignored issues
show
Bug Best Practice introduced by
The property ExternalLink does not exist on CWP\CWP\Model\Quicklink. Since you implemented __get, consider adding a @property annotation.
Loading history...
48
            $url = parse_url($this->ExternalLink);
49
50
            // if no scheme set in the link, default to http
51
            if (!isset($url['scheme'])) {
52
                return 'http://' . $this->ExternalLink;
53
            }
54
55
            return $this->ExternalLink;
56
        } elseif ($this->InternalLinkID) {
0 ignored issues
show
Bug Best Practice introduced by
The property InternalLinkID does not exist on CWP\CWP\Model\Quicklink. Since you implemented __get, consider adding a @property annotation.
Loading history...
57
            return $this->InternalLink()->Link();
0 ignored issues
show
Bug introduced by
The method InternalLink() does not exist on CWP\CWP\Model\Quicklink. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

57
            return $this->/** @scrutinizer ignore-call */ InternalLink()->Link();
Loading history...
58
        }
59
    }
60
61
    public function canCreate($member = null, $context = [])
62
    {
63
        return $this->Parent()->canCreate($member, $context);
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on CWP\CWP\Model\Quicklink. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

63
        return $this->/** @scrutinizer ignore-call */ Parent()->canCreate($member, $context);
Loading history...
64
    }
65
66
    public function canEdit($member = null)
67
    {
68
        return $this->Parent()->canEdit($member);
69
    }
70
71
    public function canDelete($member = null)
72
    {
73
        return $this->Parent()->canDelete($member);
74
    }
75
76
    public function canView($member = null)
77
    {
78
        return $this->Parent()->canView($member);
79
    }
80
81
    public function getCMSFields()
82
    {
83
        $fields = parent::getCMSFields();
84
85
        $fields->removeByName('ParentID');
86
87
        $externalLinkField = $fields->fieldByName('Root.Main.ExternalLink');
88
89
        $fields->removeByName('ExternalLink');
90
        $fields->removeByName('InternalLinkID');
91
        $fields->removeByName('SortOrder');
92
        $externalLinkField->addExtraClass('noBorder');
93
94
        $fields->addFieldToTab('Root.Main', CompositeField::create(
95
            array(
96
                TreeDropdownField::create(
97
                    'InternalLinkID',
0 ignored issues
show
Bug introduced by
'InternalLinkID' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

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

97
                    /** @scrutinizer ignore-type */ 'InternalLinkID',
Loading history...
98
                    $this->fieldLabel('InternalLinkID'),
99
                    SiteTree::class
100
                ),
101
                $externalLinkField,
102
                $wrap = CompositeField::create(
0 ignored issues
show
Unused Code introduced by
The assignment to $wrap is dead and can be removed.
Loading history...
103
                    $extraLabel = LiteralField::create(
104
                        'NoteOverride',
105
                        _t(
106
                            __CLASS__ . '.Note',
107
                            // @todo remove the HTML from this translation
108
                            '<div class="message good notice">Note:  If you specify an External Link, '
109
                            . 'the Internal Link will be ignored.</div>'
110
                        )
111
                    )
112
                )
113
            )
114
        ));
115
        $fields->insertBefore(
116
            LiteralField::create(
117
                'Note',
118
                _t(
119
                    __CLASS__ . '.Note2',
120
                    // @todo remove the HTML from this translation
121
                    '<p>Use this to specify a link to a page either on this site '
122
                    . '(Internal Link) or another site (External Link).</p>'
123
                )
124
            ),
125
            'Name'
0 ignored issues
show
Bug introduced by
'Name' 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

125
            /** @scrutinizer ignore-type */ 'Name'
Loading history...
126
        );
127
128
        return $fields;
129
    }
130
}
131