Quicklink   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 60
c 1
b 0
f 0
dl 0
loc 118
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A canView() 0 3 1
A getCMSFields() 0 45 1
A canEdit() 0 3 1
A canDelete() 0 3 1
A canCreate() 0 5 1
A fieldLabels() 0 10 1
A getLink() 0 15 4
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
/**
13
 * @method BaseHomePage Parent()
14
 * @method SiteTree InternalLink()
15
 */
16
class Quicklink extends DataObject
17
{
18
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
19
        'Name' => 'Varchar(255)',
20
        'ExternalLink' => 'Varchar(255)',
21
        'SortOrder' => 'Int',
22
    ];
23
24
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
25
        'Parent' => BaseHomePage::class,
26
        'InternalLink' => SiteTree::class,
27
    ];
28
29
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
30
        'Name' => 'Name',
31
        'InternalLink.Title' => 'Internal Link',
32
        'ExternalLink' => 'External Link',
33
    ];
34
35
    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...
36
37
    public function fieldLabels($includerelations = true)
38
    {
39
        $labels = parent::fieldLabels($includerelations);
40
        $labels['Name'] = _t(__CLASS__ . '.NameLabel', 'Name');
41
        $labels['ExternalLink'] = _t(__CLASS__ . '.ExternalLinkLabel', 'External Link');
42
        $labels['SortOrder'] = _t(__CLASS__ . '.SortOrderLabel', 'Sort Order');
43
        $labels['ParentID'] = _t(__CLASS__ . '.ParentRelationLabel', 'Parent');
44
        $labels['InternalLinkID'] = _t(__CLASS__ . '.InternalLinkLabel', 'Internal Link');
45
46
        return $labels;
47
    }
48
49
    public function getLink()
50
    {
51
        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...
52
            $url = parse_url($this->ExternalLink);
53
54
            // if no scheme set in the link, default to http
55
            if (!isset($url['scheme'])) {
56
                return 'http://' . $this->ExternalLink;
57
            }
58
59
            return $this->ExternalLink;
60
        }
61
62
        if ($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...
63
            return $this->InternalLink()->Link();
64
        }
65
    }
66
67
    public function canCreate($member = null, $context = [])
68
    {
69
        // Creating quick links should not be the same permission level as creating parent pages for them, they're
70
        // essentially content in the context of the page, so use the edit permission instead.
71
        return $this->canEdit($member);
72
    }
73
74
    public function canEdit($member = null)
75
    {
76
        return $this->Parent()->canEdit($member);
77
    }
78
79
    public function canDelete($member = null)
80
    {
81
        return $this->Parent()->canDelete($member);
82
    }
83
84
    public function canView($member = null)
85
    {
86
        return $this->Parent()->canView($member);
87
    }
88
89
    public function getCMSFields()
90
    {
91
        $fields = parent::getCMSFields();
92
93
        $fields->removeByName('ParentID');
94
95
        $externalLinkField = $fields->fieldByName('Root.Main.ExternalLink');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $externalLinkField is correct as $fields->fieldByName('Root.Main.ExternalLink') targeting SilverStripe\Forms\FieldList::fieldByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
96
97
        $fields->removeByName('ExternalLink');
98
        $fields->removeByName('InternalLinkID');
99
        $fields->removeByName('SortOrder');
100
        $externalLinkField->addExtraClass('noBorder');
101
102
        $fields->addFieldToTab('Root.Main', CompositeField::create(
103
            array(
104
                TreeDropdownField::create(
105
                    'InternalLinkID',
106
                    $this->fieldLabel('InternalLinkID'),
107
                    SiteTree::class
108
                ),
109
                $externalLinkField,
110
                $wrap = CompositeField::create(
0 ignored issues
show
Unused Code introduced by
The assignment to $wrap is dead and can be removed.
Loading history...
111
                    $extraLabel = LiteralField::create(
112
                        'NoteOverride',
113
                        sprintf('<div class="message good notice">%s</div>', _t(
114
                            __CLASS__ . '.Note',
115
                            'Note: If you specify an External Link, the Internal Link will be ignored.'
116
                        ))
117
                    )
118
                )
119
            )
120
        ));
121
        $fields->insertBefore(
122
            'Name',
123
            LiteralField::create(
124
                'Note',
125
                sprintf('<p>%s</p>', _t(
126
                    __CLASS__ . '.Note2',
127
                    'Use this to specify a link to a page either on this site '
128
                        . '(Internal Link) or another site (External Link).'
129
                ))
130
            )
131
        );
132
133
        return $fields;
134
    }
135
}
136