Passed
Push — master ( d7fd6a...9124ff )
by Robbie
11:58
created

Quicklink::getCMSFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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

43
			return $this->/** @scrutinizer ignore-call */ InternalLink()->Link();
Loading history...
44
		}
45
	}
46
47
	public function canCreate($member = null) {
48
		return $this->Parent()->canCreate($member);
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on 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

48
		return $this->/** @scrutinizer ignore-call */ Parent()->canCreate($member);
Loading history...
49
	}
50
51
	public function canEdit($member = null) {
52
		return $this->Parent()->canEdit($member);
53
	}
54
55
	public function canDelete($member = null) {
56
		return $this->Parent()->canDelete($member);
57
	}
58
59
	public function canView($member = null) {
60
		return $this->Parent()->canView($member);
61
	}
62
63
	public function getCMSFields() {
64
		$fields = parent::getCMSFields();
65
66
		$fields->removeByName('ParentID');
67
68
		$externalLinkField = $fields->fieldByName('Root.Main.ExternalLink');
69
70
		$fields->removeByName('ExternalLink');
71
		$fields->removeByName('InternalLinkID');
72
		$fields->removeByName('SortOrder');
73
		$externalLinkField->addExtraClass('noBorder');
74
75
		$fields->addFieldToTab('Root.Main',CompositeField::create(
76
			array(
77
				TreeDropdownField::create(
78
					'InternalLinkID', 
79
					$this->fieldLabel('InternalLinkID'),
80
					'SiteTree'
81
				),
82
				$externalLinkField,
83
				$wrap = CompositeField::create(
0 ignored issues
show
Unused Code introduced by
The assignment to $wrap is dead and can be removed.
Loading history...
84
					$extraLabel = LiteralField::create(
85
						'NoteOverride', 
86
						_t('Quicklink.Note','<div class="message good notice">Note:  If you specify an External Link, the Internal Link will be ignored.</div>')
87
					)
88
				)
89
			)
90
		));
91
		$fields->insertBefore(
92
			LiteralField::create(
93
				'Note', 
94
				_t(
95
					'Quicklink.Note2',
96
					'<p>Use this to specify a link to a page either on this site (Internal Link) or another site (External Link).</p>'
97
				)
98
			), 
99
			'Name'
0 ignored issues
show
Bug introduced by
'Name' of type string is incompatible with the type FormField expected by parameter $item of 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

99
			/** @scrutinizer ignore-type */ 'Name'
Loading history...
100
		);
101
102
		return $fields;
103
	}
104
}
105