1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\InternalExternalLink\Extensions; |
4
|
|
|
|
5
|
|
|
use SilverStripe\AssetAdmin\Forms\UploadField; |
|
|
|
|
6
|
|
|
use Page; |
|
|
|
|
7
|
|
|
use SilverStripe\Forms\FieldList; |
8
|
|
|
use SilverStripe\Forms\HeaderField; |
9
|
|
|
use SilverStripe\Forms\OptionsetField; |
10
|
|
|
use SilverStripe\Forms\Tab; |
11
|
|
|
use SilverStripe\Forms\TextField; |
12
|
|
|
|
13
|
|
|
use SilverStripe\Forms\TreeDropdownField; |
14
|
|
|
|
15
|
|
|
use SilverStripe\ORM\DataExtension; |
16
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
17
|
|
|
use SilverStripe\Assets\File; |
18
|
|
|
|
19
|
|
|
class InternalExternalLinkExtension extends DataExtension |
20
|
|
|
{ |
21
|
|
|
private static $db = [ |
22
|
|
|
'LinkType' => "Enum('Internal,External,DownloadFile', 'Internal')", |
23
|
|
|
'ExternalLink' => 'Varchar(255)', |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
private static $has_one = [ |
27
|
|
|
'InternalLink' => Page::class, |
28
|
|
|
'DownloadFile' => File::class, |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
private static $owns = [ |
32
|
|
|
'DownloadFile', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
public static $casting = [ |
36
|
|
|
'MyLink' => 'Varchar', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* use the $fieldNameAppendix if you have multiple fields |
41
|
|
|
* @param string $fieldNameAppendix - optional |
42
|
|
|
* @return string|null |
43
|
|
|
*/ |
44
|
|
|
public function MyLink($fieldNameAppendix = ''): ?string |
45
|
|
|
{ |
46
|
|
|
return $this->owner->getMyLink($fieldNameAppendix); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* use the $fieldNameAppendix if you have multiple fields |
51
|
|
|
* @param string $fieldNameAppendix - optional |
52
|
|
|
* @return string|null |
53
|
|
|
*/ |
54
|
|
|
public function getMyLink(?string $fieldNameAppendix = ''): ?string |
55
|
|
|
{ |
56
|
|
|
$linkTypeFieldName = 'LinkType' . $fieldNameAppendix; |
57
|
|
|
|
58
|
|
|
$InternalLinkMethodName = 'InternalLink' . $fieldNameAppendix; |
59
|
|
|
$internalLinkFieldName = $InternalLinkMethodName . 'ID'; |
60
|
|
|
|
61
|
|
|
$downloadLinkMethodName = 'DownloadFile' . $fieldNameAppendix; |
62
|
|
|
$downloadLinkFieldName = $downloadLinkMethodName . 'ID'; |
63
|
|
|
|
64
|
|
|
$externalLinkFieldName = 'ExternalLink' . $fieldNameAppendix; |
65
|
|
|
if ($this->owner->{$linkTypeFieldName} === 'Internal' && $this->owner->{$internalLinkFieldName}) { |
66
|
|
|
$obj = $this->owner->{$InternalLinkMethodName}(); |
67
|
|
|
if ($obj) { |
68
|
|
|
return $obj->Link(); |
69
|
|
|
} |
70
|
|
|
} elseif ($this->owner->{$linkTypeFieldName} === 'External' && $this->owner->{$externalLinkFieldName}) { |
71
|
|
|
return DBField::create_field('Varchar', $this->owner->{$externalLinkFieldName})->url(); |
72
|
|
|
}elseif ($this->owner->{$linkTypeFieldName} === 'DownloadFile' && $this->owner->{$downloadLinkFieldName}) { |
73
|
|
|
$obj = $this->owner->{$downloadLinkMethodName}(); |
74
|
|
|
if ($obj) { |
75
|
|
|
return $obj->Link(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function updateCMSFields(FieldList $fields) |
83
|
|
|
{ |
84
|
|
|
$fieldNameAppendici = $this->getFieldNameAppendici(); |
85
|
|
|
foreach($fieldNameAppendici as $appendix) { |
86
|
|
|
$js = <<<js |
87
|
|
|
var el = this; |
88
|
|
|
const val = jQuery(el).find('.form-check-input:checked').val(); |
89
|
|
|
if (val === 'Internal') { |
90
|
|
|
jQuery('#Form_ItemEditForm_InternalLink{$appendix}ID_Holder').show(); |
91
|
|
|
jQuery('#Form_ItemEditForm_ExternalLink{$appendix}_Holder').hide(); |
92
|
|
|
jQuery('#Form_ItemEditForm_DownloadFile{$appendix}_Holder').hide(); |
93
|
|
|
} else if(val === 'External') { |
94
|
|
|
jQuery('#Form_ItemEditForm_InternalLink{$appendix}ID_Holder').hide(); |
95
|
|
|
jQuery('#Form_ItemEditForm_ExternalLink{$appendix}_Holder').show(); |
96
|
|
|
jQuery('#Form_ItemEditForm_DownloadFile{$appendix}_Holder').hide(); |
97
|
|
|
} else if(val === 'DownloadFile') { |
98
|
|
|
jQuery('#Form_ItemEditForm_InternalLink{$appendix}ID_Holder').hide(); |
99
|
|
|
jQuery('#Form_ItemEditForm_ExternalLink{$appendix}_Holder').hide(); |
100
|
|
|
jQuery('#Form_ItemEditForm_DownloadFile{$appendix}_Holder').show(); |
101
|
|
|
} else { |
102
|
|
|
jQuery('#Form_ItemEditForm_InternalLink{$appendix}ID_Holder').show(); |
103
|
|
|
jQuery('#Form_ItemEditForm_ExternalLink{$appendix}_Holder').show(); |
104
|
|
|
jQuery('#Form_ItemEditForm_DownloadFile{$appendix}_Holder').show(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
js; |
108
|
|
|
// $fields->insertBefore(new Tab('Links', 'Links'), 'Settings'); |
109
|
|
|
$fields->addFieldsToTab( |
110
|
|
|
'Root.Links', |
111
|
|
|
[ |
112
|
|
|
HeaderField::create( |
113
|
|
|
'Link-Details-Heading'.$appendix, |
114
|
|
|
'Link' |
115
|
|
|
), |
116
|
|
|
OptionsetField::create( |
117
|
|
|
'LinkType'.$appendix, |
118
|
|
|
'Link Type '.$appendix, |
119
|
|
|
$this->owner->dbObject('LinkType')->enumValues() |
120
|
|
|
) |
121
|
|
|
->setAttribute('onclick', $js) |
122
|
|
|
->setAttribute('onchange', $js), |
123
|
|
|
TreeDropdownField::create( |
124
|
|
|
'InternalLink'.$appendix.'ID', |
125
|
|
|
'Internal Link '.$appendix, |
126
|
|
|
Page::class |
127
|
|
|
), |
128
|
|
|
UploadField::create( |
129
|
|
|
'DownloadFile' .$appendix, |
130
|
|
|
'Download File '.$appendix |
131
|
|
|
), |
132
|
|
|
] |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function onBeforeWrite() |
138
|
|
|
{ |
139
|
|
|
parent::onBeforeWrite(); |
140
|
|
|
$fieldNameAppendici = $this->getFieldNameAppendici(); |
141
|
|
|
foreach($fieldNameAppendici as $appendix) { |
142
|
|
|
$linkTypeField = 'LinkType'.$appendix; |
143
|
|
|
$internalLinkField = 'InternalLink'.$appendix . 'ID'; |
144
|
|
|
$externalLinkField = 'ExternalLink'.$appendix; |
145
|
|
|
|
146
|
|
|
if($this->owner->$linkTypeField === 'Internal' && ! $this->owner->$internalLinkField && $this->owner->$externalLinkField) { |
147
|
|
|
$this->owner->$linkTypeField = 'External'; |
148
|
|
|
} |
149
|
|
|
if($this->owner->LinkType === 'External' && $this->owner->$internalLinkField && ! $this->owner->$externalLinkField) { |
150
|
|
|
$this->owner->LinkType = 'External'; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
protected function getFieldNameAppendici() : array |
156
|
|
|
{ |
157
|
|
|
if($this->owner->hasMethod('getFieldNameAppendiciMore')) { |
158
|
|
|
return $this->owner->getFieldNameAppendiciMore(); |
159
|
|
|
} |
160
|
|
|
return []; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths