Passed
Push — master ( 2f8fac...1d731f )
by
unknown
02:40
created

Quicklink::getCMSFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 29
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 45
rs 8.8571
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(__CLASS__ . '.NameLabel', 'Name');
37
        $labels['ExternalLink'] = _t(__CLASS__ . '.ExternalLinkLabel', 'External Link');
38
        $labels['SortOrder'] = _t(__CLASS__ . '.SortOrderLabel', 'Sort Order');
39
        $labels['ParentID'] = _t(__CLASS__ . '.ParentRelationLabel', 'Parent');
40
        $labels['InternalLinkID'] = _t(__CLASS__ . '.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
                        sprintf('<div class="message good notice">%s</div>', _t(
106
                            __CLASS__ . '.Note',
107
                            'Note: If you specify an External Link, the Internal Link will be ignored.'
108
                        ))
109
                    )
110
                )
111
            )
112
        ));
113
        $fields->insertBefore(
114
            'Name',
115
            LiteralField::create(
116
                'Note',
117
                sprintf('<p>%s</p>', _t(
118
                    __CLASS__ . '.Note2',
119
                    'Use this to specify a link to a page either on this site '
120
                        . '(Internal Link) or another site (External Link).'
121
                ))
122
            )
123
        );
124
125
        return $fields;
126
    }
127
}
128