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
![]() |
|||
19 | 'Name' => 'Varchar(255)', |
||
20 | 'ExternalLink' => 'Varchar(255)', |
||
21 | 'SortOrder' => 'Int', |
||
22 | ]; |
||
23 | |||
24 | private static $has_one = [ |
||
0 ignored issues
–
show
|
|||
25 | 'Parent' => BaseHomePage::class, |
||
26 | 'InternalLink' => SiteTree::class, |
||
27 | ]; |
||
28 | |||
29 | private static $summary_fields = [ |
||
0 ignored issues
–
show
|
|||
30 | 'Name' => 'Name', |
||
31 | 'InternalLink.Title' => 'Internal Link', |
||
32 | 'ExternalLink' => 'External Link', |
||
33 | ]; |
||
34 | |||
35 | private static $table_name = 'Quicklink'; |
||
0 ignored issues
–
show
|
|||
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
The property
ExternalLink does not exist on CWP\CWP\Model\Quicklink . Since you implemented __get , consider adding a @property annotation.
![]() |
|||
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
The property
InternalLinkID does not exist on CWP\CWP\Model\Quicklink . Since you implemented __get , consider adding a @property annotation.
![]() |
|||
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
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 The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
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
|
|||
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 |