1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Subsites\Pages; |
4
|
|
|
|
5
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
6
|
|
|
use SilverStripe\CMS\Model\VirtualPage; |
7
|
|
|
use SilverStripe\Control\Controller; |
8
|
|
|
use SilverStripe\Core\Config\Config; |
9
|
|
|
use SilverStripe\Forms\DropdownField; |
10
|
|
|
use SilverStripe\Forms\LabelField; |
11
|
|
|
use SilverStripe\Forms\TextareaField; |
12
|
|
|
use SilverStripe\Forms\TextField; |
13
|
|
|
use SilverStripe\ORM\ArrayList; |
14
|
|
|
use SilverStripe\ORM\DataObject; |
15
|
|
|
use SilverStripe\Subsites\Forms\SubsitesTreeDropdownField; |
16
|
|
|
use SilverStripe\Subsites\Model\Subsite; |
17
|
|
|
use SilverStripe\Subsites\State\SubsiteState; |
18
|
|
|
use SilverStripe\View\ArrayData; |
19
|
|
|
|
20
|
|
|
class SubsitesVirtualPage extends VirtualPage |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
private static $table_name = 'SubsitesVirtualPage'; |
|
|
|
|
24
|
|
|
|
25
|
|
|
private static $description = 'Displays the content of a page on another subsite'; |
|
|
|
|
26
|
|
|
|
27
|
|
|
private static $db = [ |
28
|
|
|
'CustomMetaTitle' => 'Varchar(255)', |
29
|
|
|
'CustomMetaKeywords' => 'Varchar(255)', |
30
|
|
|
'CustomMetaDescription' => 'Text', |
31
|
|
|
'CustomExtraMeta' => 'HTMLText' |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
private static $non_virtual_fields = [ |
|
|
|
|
35
|
|
|
'SubsiteID' |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
public function getCMSFields() |
39
|
|
|
{ |
40
|
|
|
$fields = parent::getCMSFields(); |
41
|
|
|
|
42
|
|
|
$subsites = DataObject::get(Subsite::class); |
43
|
|
|
if (!$subsites) { |
44
|
|
|
$subsites = new ArrayList(); |
45
|
|
|
} else { |
46
|
|
|
$subsites = ArrayList::create($subsites->toArray()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$subsites->push(new ArrayData(['Title' => 'Main site', 'ID' => 0])); |
50
|
|
|
|
51
|
|
|
$fields->addFieldToTab( |
52
|
|
|
'Root.Main', |
53
|
|
|
DropdownField::create( |
54
|
|
|
'CopyContentFromID_SubsiteID', |
|
|
|
|
55
|
|
|
_t(__CLASS__ . '.SubsiteField', 'Subsite'), |
56
|
|
|
$subsites->map('ID', 'Title') |
|
|
|
|
57
|
|
|
)->addExtraClass('subsitestreedropdownfield-chooser no-change-track'), |
58
|
|
|
'CopyContentFromID' |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
// Setup the linking to the original page. |
62
|
|
|
$pageSelectionField = new SubsitesTreeDropdownField( |
63
|
|
|
'CopyContentFromID', |
64
|
|
|
_t('SilverStripe\\CMS\\Model\\VirtualPage.CHOOSE', 'Choose a page to link to'), |
65
|
|
|
"SilverStripe\\CMS\\Model\\SiteTree", |
66
|
|
|
'ID', |
67
|
|
|
'MenuTitle' |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
if (Controller::has_curr() && Controller::curr()->getRequest()) { |
71
|
|
|
$subsiteID = Controller::curr()->getRequest()->requestVar('CopyContentFromID_SubsiteID'); |
72
|
|
|
$pageSelectionField->setSubsiteID($subsiteID); |
73
|
|
|
} |
74
|
|
|
$fields->replaceField('CopyContentFromID', $pageSelectionField); |
75
|
|
|
|
76
|
|
|
// Create links back to the original object in the CMS |
77
|
|
|
if ($this->CopyContentFromID) { |
78
|
|
|
$editLink = "admin/pages/edit/show/$this->CopyContentFromID/?SubsiteID=" . $this->CopyContentFrom()->SubsiteID; |
79
|
|
|
$linkToContent = " |
80
|
|
|
<a class=\"cmsEditlink\" href=\"$editLink\">" . |
81
|
|
|
_t('SilverStripe\\CMS\\Model\\VirtualPage.EDITCONTENT', 'Click here to edit the content') . |
82
|
|
|
'</a>'; |
83
|
|
|
$fields->removeByName('VirtualPageContentLinkLabel'); |
84
|
|
|
$fields->addFieldToTab( |
85
|
|
|
'Root.Main', |
86
|
|
|
$linkToContentLabelField = new LabelField('VirtualPageContentLinkLabel', $linkToContent), |
87
|
|
|
'Title' |
88
|
|
|
); |
89
|
|
|
$linkToContentLabelField->setAllowHTML(true); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
$fields->addFieldToTab( |
94
|
|
|
'Root.Main', |
95
|
|
|
TextField::create( |
96
|
|
|
'CustomMetaTitle', |
97
|
|
|
$this->fieldLabel('CustomMetaTitle') |
98
|
|
|
)->setDescription(_t(__CLASS__ . '.OverrideNote', 'Overrides inherited value from the source')), |
99
|
|
|
'MetaTitle' |
100
|
|
|
); |
101
|
|
|
$fields->addFieldToTab( |
102
|
|
|
'Root.Main', |
103
|
|
|
TextareaField::create( |
104
|
|
|
'CustomMetaKeywords', |
105
|
|
|
$this->fieldLabel('CustomMetaTitle') |
106
|
|
|
)->setDescription(_t(__CLASS__ . '.OverrideNote', 'Overrides inherited value from the source')), |
107
|
|
|
'MetaKeywords' |
108
|
|
|
); |
109
|
|
|
$fields->addFieldToTab( |
110
|
|
|
'Root.Main', |
111
|
|
|
TextareaField::create( |
112
|
|
|
'CustomMetaDescription', |
113
|
|
|
$this->fieldLabel('CustomMetaTitle') |
114
|
|
|
)->setDescription(_t(__CLASS__ . '.OverrideNote', 'Overrides inherited value from the source')), |
115
|
|
|
'MetaDescription' |
116
|
|
|
); |
117
|
|
|
$fields->addFieldToTab( |
118
|
|
|
'Root.Main', |
119
|
|
|
TextField::create( |
120
|
|
|
'CustomExtraMeta', |
121
|
|
|
$this->fieldLabel('CustomMetaTitle') |
122
|
|
|
)->setDescription(_t(__CLASS__ . '.OverrideNote', 'Overrides inherited value from the source')), |
123
|
|
|
'ExtraMeta' |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
return $fields; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function fieldLabels($includerelations = true) |
130
|
|
|
{ |
131
|
|
|
$labels = parent::fieldLabels($includerelations); |
132
|
|
|
$labels['CustomMetaTitle'] = _t('SilverStripe\\Subsites\\Model\\Subsite.CustomMetaTitle', 'Title'); |
133
|
|
|
$labels['CustomMetaKeywords'] = _t('SilverStripe\\Subsites\\Model\\Subsite.CustomMetaKeywords', 'Keywords'); |
134
|
|
|
$labels['CustomMetaDescription'] = _t('SilverStripe\\Subsites\\Model\\Subsite.CustomMetaDescription', 'Description'); |
135
|
|
|
$labels['CustomExtraMeta'] = _t('SilverStripe\\Subsites\\Model\\Subsite.CustomExtraMeta', 'Custom Meta Tags'); |
136
|
|
|
|
137
|
|
|
return $labels; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function getCopyContentFromID_SubsiteID() |
141
|
|
|
{ |
142
|
|
|
if ($this->CopyContentFromID) { |
143
|
|
|
return (int) $this->CopyContentFrom()->SubsiteID; |
144
|
|
|
} |
145
|
|
|
return SubsiteState::singleton()->getSubsiteId(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function getVirtualFields() |
149
|
|
|
{ |
150
|
|
|
$fields = parent::getVirtualFields(); |
151
|
|
|
foreach ($fields as $k => $v) { |
152
|
|
|
if ($v == 'SubsiteID') { |
153
|
|
|
unset($fields[$k]); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
foreach (self::$db as $field => $type) { |
158
|
|
|
if (in_array($field, $fields)) { |
159
|
|
|
unset($fields[array_search($field, $fields)]); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $fields; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function syncLinkTracking() |
167
|
|
|
{ |
168
|
|
|
$oldState = Subsite::$disable_subsite_filter; |
169
|
|
|
Subsite::$disable_subsite_filter = true; |
170
|
|
|
if ($this->CopyContentFromID) { |
171
|
|
|
$this->HasBrokenLink = DataObject::get_by_id(SiteTree::class, $this->CopyContentFromID) ? false : true; |
|
|
|
|
172
|
|
|
} |
173
|
|
|
Subsite::$disable_subsite_filter = $oldState; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function onBeforeWrite() |
177
|
|
|
{ |
178
|
|
|
parent::onBeforeWrite(); |
179
|
|
|
|
180
|
|
|
if ($this->CustomMetaTitle) { |
|
|
|
|
181
|
|
|
$this->MetaTitle = $this->CustomMetaTitle; |
|
|
|
|
182
|
|
|
} else { |
183
|
|
|
$this->MetaTitle = $this->ContentSource()->MetaTitle ?: $this->MetaTitle; |
184
|
|
|
} |
185
|
|
|
if ($this->CustomMetaKeywords) { |
|
|
|
|
186
|
|
|
$this->MetaKeywords = $this->CustomMetaKeywords; |
|
|
|
|
187
|
|
|
} else { |
188
|
|
|
$this->MetaKeywords = $this->ContentSource()->MetaKeywords ?: $this->MetaKeywords; |
189
|
|
|
} |
190
|
|
|
if ($this->CustomMetaDescription) { |
|
|
|
|
191
|
|
|
$this->MetaDescription = $this->CustomMetaDescription; |
|
|
|
|
192
|
|
|
} else { |
193
|
|
|
$this->MetaDescription = $this->ContentSource()->MetaDescription ?: $this->MetaDescription; |
194
|
|
|
} |
195
|
|
|
if ($this->CustomExtraMeta) { |
|
|
|
|
196
|
|
|
$this->ExtraMeta = $this->CustomExtraMeta; |
|
|
|
|
197
|
|
|
} else { |
198
|
|
|
$this->ExtraMeta = $this->ContentSource()->ExtraMeta ?: $this->ExtraMeta; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function validURLSegment() |
203
|
|
|
{ |
204
|
|
|
$isValid = parent::validURLSegment(); |
205
|
|
|
|
206
|
|
|
// Veto the validation rules if its false. In this case, some logic |
207
|
|
|
// needs to be duplicated from parent to find out the exact reason the validation failed. |
208
|
|
|
if (!$isValid) { |
209
|
|
|
$IDFilter = $this->ID ? "AND \"SiteTree\".\"ID\" <> $this->ID" : null; |
|
|
|
|
210
|
|
|
$parentFilter = null; |
211
|
|
|
|
212
|
|
|
if (Config::inst()->get(SiteTree::class, 'nested_urls')) { |
213
|
|
|
if ($this->ParentID) { |
|
|
|
|
214
|
|
|
$parentFilter = " AND \"SiteTree\".\"ParentID\" = $this->ParentID"; |
215
|
|
|
} else { |
216
|
|
|
$parentFilter = ' AND "SiteTree"."ParentID" = 0'; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$origDisableSubsiteFilter = Subsite::$disable_subsite_filter; |
221
|
|
|
Subsite::$disable_subsite_filter = true; |
222
|
|
|
$existingPage = DataObject::get_one( |
223
|
|
|
SiteTree::class, |
224
|
|
|
"\"URLSegment\" = '$this->URLSegment' $IDFilter $parentFilter", |
|
|
|
|
225
|
|
|
false // disable cache, it doesn't include subsite status in the key |
226
|
|
|
); |
227
|
|
|
Subsite::$disable_subsite_filter = $origDisableSubsiteFilter; |
228
|
|
|
$existingPageInSubsite = DataObject::get_one( |
229
|
|
|
SiteTree::class, |
230
|
|
|
"\"URLSegment\" = '$this->URLSegment' $IDFilter $parentFilter", |
231
|
|
|
false // disable cache, it doesn't include subsite status in the key |
232
|
|
|
); |
233
|
|
|
|
234
|
|
|
// If URL has been vetoed because of an existing page, |
235
|
|
|
// be more specific and allow same URLSegments in different subsites |
236
|
|
|
$isValid = !($existingPage && $existingPageInSubsite); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $isValid; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|