-
Notifications
You must be signed in to change notification settings - Fork 111
/
FluentSiteTreeExtension.php
610 lines (517 loc) · 19.6 KB
/
FluentSiteTreeExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
<?php
namespace TractorCow\Fluent\Extension;
use SilverStripe\CMS\Controllers\RootURLController;
use SilverStripe\CMS\Forms\SiteTreeURLSegmentField;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Forms\CompositeField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\Tab;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\Versioned\Versioned;
use SilverStripe\View\SSViewer;
use TractorCow\Fluent\Extension\Traits\FluentAdminTrait;
use TractorCow\Fluent\Model\Locale;
use TractorCow\Fluent\Model\RecordLocale;
use TractorCow\Fluent\State\FluentState;
// Soft dependency on CMS module
if (!class_exists(SiteTree::class)) {
return;
}
/**
* Fluent extension for SiteTree
*
* @property FluentSiteTreeExtension|SiteTree $owner
*/
class FluentSiteTreeExtension extends FluentVersionedExtension
{
use FluentAdminTrait;
/**
* Determine if status messages are enabled
*
* @config
* @var bool
*/
private static $locale_published_status_message = true;
/**
* Enable localise actions (copy to draft and copy & publish actions)
* these actions can be used to localise page content directly via main page actions
*
* @config
* @var bool
*/
private static $localise_actions_enabled = true;
/**
* Add alternate links to metatags
*
* @param string &$tags
*/
public function MetaTags(&$tags)
{
$tags .= $this->owner->renderWith('FluentSiteTree_MetaTags');
}
/**
* If this is the site home page, but still has it's own non-root url,
* make sure we treat the root as x-default.
*
* @link https://github.com/tractorcow-farm/silverstripe-fluent/blob/master/docs/en/configuration.md#default-locale-options
*
* @return bool
*/
public function getLinkToXDefault()
{
// If we disable the prefix for the default locale, this will be the default instead
if (FluentDirectorExtension::config()->get('disable_default_prefix')) {
return false;
}
// If the current domain only has one locale, there is no x-default
$localeObj = $this->getRecordLocale();
if ($localeObj && $localeObj->getIsOnlyLocale()) {
return false;
}
// Only link to x-default on home page
return $this->owner->URLSegment === RootURLController::get_homepage_link();
}
/**
* Add the current locale's URL segment to the start of the URL
*
* @param string &$base
* @param string &$action
*/
public function updateRelativeLink(&$base, &$action)
{
// Don't inject locale to subpages
if ($this->owner->ParentID && SiteTree::config()->get('nested_urls')) {
return;
}
// Get appropriate locale for this record
$localeObj = $this->getRecordLocale();
if (!$localeObj) {
return;
}
// For blank/temp pages such as Security controller fallback to querystring
if (!$this->owner->exists()) {
$base = Controller::join_links(
$base,
'?' . FluentDirectorExtension::config()->get('query_param') . '=' . urlencode($localeObj->Locale)
);
return;
}
// Check if this locale is the default for its own domain
if ($localeObj->getIsDefault()) {
// If default locale shouldn't have prefix, then don't add prefix
if (FluentDirectorExtension::config()->get('disable_default_prefix')) {
return;
}
// For all pages on a domain where there is only a single locale,
// then the domain itself is sufficient to distinguish that domain
// See https://github.com/tractorcow-farm/silverstripe-fluent/issues/75
if ($localeObj->getIsOnlyLocale()) {
return;
}
}
// Simply join locale root with base relative URL
$base = Controller::join_links($localeObj->getURLSegment(), $base);
}
/**
* Update link to include hostname if in domain mode
*
* @param string $link root-relative url (includes baseurl)
* @param string $action
* @param string $relativeLink
*/
public function updateLink(&$link, &$action, &$relativeLink)
{
// Get appropriate locale for this record
$localeObj = $this->getRecordLocale();
if (!$localeObj) {
return;
}
// Don't rewrite outside of domain mode
$domain = $localeObj->getDomain();
if (!$domain) {
return;
}
// Don't need to prepend domain if on the same domain
if (FluentState::singleton()->getDomain() === $domain->Domain) {
return;
}
// Prefix with domain
$link = Controller::join_links($domain->Link(), $link);
}
/**
* Check whether the current page is exists in the current locale.
*
* If it is invisible then we add a class to show it slightly greyed out in the site tree.
*
* @param array $flags
*/
public function updateStatusFlags(&$flags)
{
// If there is no current FluentState, then we shouldn't update.
if (!FluentState::singleton()->getLocale()) {
return;
}
$this->updateModifiedFlag($flags);
$this->updateNoSourceFlag($flags);
// If this page does not exist it should be "invisible"
if (!$this->isDraftedInLocale() && !$this->isPublishedInLocale()) {
$flags['fluentinvisible'] = [
'text' => '',
'title' => '',
];
}
}
/**
* @param FieldList $fields
*/
public function updateCMSFields(FieldList $fields)
{
// If there is no current FluentState, then we shouldn't update.
if (!FluentState::singleton()->getLocale()) {
return;
}
parent::updateCMSFields($fields);
$this->addLocaleStatusMessage($fields);
$this->addLocalePrefixToUrlSegment($fields);
}
/**
* @param FieldList $actions
*/
public function updateCMSActions(FieldList $actions)
{
// If there is no current FluentState, then we shouldn't update.
if (!FluentState::singleton()->getLocale()) {
return;
}
// Update specific sitetree publish actions
$this->updateSavePublishActions($actions);
// Update specific sitetree localise actions
$this->updateLocaliseActions($actions);
// Update information panel (shows published state)
$this->updateInformationPanel($actions);
// Update the state of publish action (if needed)
$this->updatePublishState($actions);
// Update unpublish and archive actions
$this->updateMoreOptionsActions($actions);
// Add extra fluent menu
$this->updateFluentActions($actions, $this->owner);
}
/**
* Adds a UI message to indicate whether you're editing in the default locale or not
*
* @param FieldList $fields
*/
protected function addLocaleStatusMessage(FieldList $fields)
{
// Don't display these messages if the owner class has asked us not to.
if (!$this->owner->config()->get('locale_published_status_message')) {
return;
}
// If the field is already present, don't add it a second time.
if ($fields->fieldByName('LocaleStatusMessage')) {
return;
}
// We don't need to add a status warning if a version of this Page has already been published for this Locale.
if ($this->isPublishedInLocale()) {
return;
}
$message = null;
if ($this->owner->config()->get('frontend_publish_required')) {
// If publishing is required, then we can just check whether or not this locale has been published.
if (!$this->isPublishedInLocale()) {
$message = _t(
__CLASS__ . '.LOCALESTATUSFLUENTINVISIBLE',
'This page will not be visible in this locale until it has been published.'
);
}
} else {
// If frontend publishing is *not* required, then we have multiple possibilities.
if (!$this->isDraftedInLocale()) {
$info = RecordLocale::create($this->owner, Locale::getCurrentLocale());
// Our content hasn't been drafted or published.
if ($info->getSourceLocale()) {
// If this Locale has a Fallback, then content might be getting inherited from that Fallback.
$message = _t(
__CLASS__ . '.LOCALESTATUSFLUENTINHERITED',
'Content for this page may be inherited from another locale. If you wish you make an ' .
'independent copy of this page, please use one of the "Copy" actions provided.'
);
} else {
// This locale doesn't have any content source
$message = _t(
__CLASS__ . '.LOCALESTATUSFLUENTUNKNOWN',
'No content is available for this page. Please localise this page or provide a locale fallback.'
);
}
} elseif (!$this->isPublishedInLocale()) {
// Our content has been saved to draft, but hasn't yet been published. That published content may be
// coming from a Fallback.
$message = _t(
__CLASS__ . '.LOCALESTATUSFLUENTDRAFT',
'A draft has been created for this locale, however, published content may still be ' .
'inherited from another. To publish this content for this locale, use the "Save & publish" ' .
'action provided.'
);
}
}
if ($message === null) {
return;
}
$fields->unshift(
LiteralField::create(
'LocaleStatusMessage',
sprintf(
'<p class="alert alert-info">%s</p>',
$message
)
)
);
}
/**
* Add the locale's URLSegment to the URL prefix for a page's URL segment field
*
* @param FieldList $fields
* @return $this
*/
protected function addLocalePrefixToUrlSegment(FieldList $fields)
{
// Ensure the field is available in the list
$segmentField = $fields->fieldByName('Root.Main.URLSegment');
if (!$segmentField || !($segmentField instanceof SiteTreeURLSegmentField)) {
return $this;
}
// Mock frontend and get link to parent object / page
$baseURL = FluentState::singleton()
->withState(function (FluentState $tempState) {
$tempState->setIsDomainMode(true);
$tempState->setIsFrontend(true);
// Get relative link up until the current URL segment
if (SiteTree::config()->get('nested_urls') && $this->owner->ParentID) {
$parentRelative = $this->owner->Parent()->RelativeLink();
} else {
$parentRelative = '/';
$action = null;
$this->updateRelativeLink($parentRelative, $action);
}
// Get absolute base path
$domain = Locale::getCurrentLocale()->getDomain();
if ($domain) {
$parentBase = Controller::join_links($domain->Link(), Director::baseURL());
} else {
$parentBase = Director::absoluteBaseURL();
}
// Join base / relative links
return Controller::join_links($parentBase, $parentRelative);
});
$segmentField->setURLPrefix($baseURL);
return $this;
}
/**
* @param FieldList $actions
*/
protected function updateSavePublishActions(FieldList $actions)
{
/** @var CompositeField $majorActions */
$majorActions = $actions->fieldByName('MajorActions');
// If another extension has removed this CompositeField then we don't need to update them.
if ($majorActions === null) {
return;
}
// There's no need to update actions in these ways if the Page has previously been published in this Locale.
if ($this->isPublishedInLocale()) {
return;
}
$isDraftedInLocale = $this->isDraftedInLocale();
$actionSave = $majorActions->getChildren()->fieldByName('action_save');
$actionPublish = $majorActions->getChildren()->fieldByName('action_publish');
// Make sure no other extensions have removed this field.
if ($actionSave !== null) {
// Check that the Page doesn't have a current draft.
if (!$isDraftedInLocale) {
$actionSave->addExtraClass('btn-primary font-icon-save');
$actionSave->setTitle(_t(__CLASS__ . '.LOCALECOPYTODRAFT', 'Copy to draft'));
$actionSave->removeExtraClass('btn-outline-primary font-icon-tick');
}
}
// Make sure no other extensions have removed this field.
if ($actionPublish !== null) {
$actionPublish->addExtraClass('btn-primary font-icon-rocket');
$actionPublish->removeExtraClass('btn-outline-primary font-icon-tick');
if ($isDraftedInLocale) {
$actionPublish->setTitle(_t('SilverStripe\CMS\Model\SiteTree.BUTTONSAVEPUBLISH', 'Save & publish'));
} else {
$actionPublish->setTitle(_t(__CLASS__ . '.LOCALECOPYANDPUBLISH', 'Copy & publish'));
}
}
}
/**
* Update publish action state to reflect the localised record instead of the base record
*
* @param FieldList $actions
*/
protected function updatePublishState(FieldList $actions): void
{
$owner = $this->owner;
if (!$owner->isInDB()) {
return;
}
$published = $owner->isPublishedInLocale();
if (!$published) {
return;
}
/** @var CompositeField $majorActions */
$majorActions = $actions->fieldByName('MajorActions');
if (!$majorActions) {
return;
}
$publishAction = $majorActions->fieldByName('action_publish');
if (!$publishAction) {
return;
}
// make sure that changes only on the base record
// do not trigger "need to publish" button state
// this is needed because the default interface looks
// at the base record instead of the localised page
$publishAction
->setTitle(_t('SilverStripe\\CMS\\Model\\SiteTree.BUTTONPUBLISHED', 'Published'))
->removeExtraClass(
'btn-primary font-icon-rocket btn-outline-primary font-icon-tick'
)
->addExtraClass('btn-outline-primary font-icon-tick');
if (!$owner->stagesDifferInLocale()) {
return;
}
// If staged and live is different we change the button to "Publish"
// as the page hasn't been published
$publishAction
->setTitle(_t('SilverStripe\\CMS\\Model\\SiteTree.BUTTONSAVEPUBLISH', 'Publish'))
->addExtraClass('btn-primary font-icon-rocket')
->removeExtraClass('btn-outline-primary font-icon-tick');
}
/**
* Update archive and unpublish actions to reflect the localised record instead of the base record
*
* @param FieldList $actions
*/
protected function updateMoreOptionsActions(FieldList $actions): void
{
/** @var Tab $moreOptions */
$moreOptions = $actions->fieldByName('ActionMenus.MoreOptions');
if (!$moreOptions) {
return;
}
if ($this->isPublishedInLocale()) {
return;
}
// remove unpublish action as the record is not published
$moreOptions->removeByName('action_unpublish');
// update the label on archive action as it could have "unpublish and archive" which is incorrect
$archiveAction = $moreOptions->fieldByName('action_archive');
if (!$archiveAction) {
return;
}
$archiveAction->setTitle(_t('SilverStripe\\CMS\\Controllers\\CMSMain.ARCHIVE', 'Archive'));
}
/**
* Remove "copy to draft" and "copy & publish" actions based on configuration
*
* @param FieldList $actions
*/
protected function updateLocaliseActions(FieldList $actions): void
{
$owner = $this->owner;
if ($owner->config()->get('localise_actions_enabled')) {
return;
}
if (!$owner->isInDB() || $owner->isDraftedInLocale()) {
return;
}
$actions->removeByName([
'action_save',
'action_publish',
]);
}
/**
* Information panel show published state of a base record by default
* this overrides the display with the published state of the localised record
*
* @param FieldList $actions
*/
protected function updateInformationPanel(FieldList $actions): void
{
$owner = $this->owner;
/** @var Tab $moreOptions */
$moreOptions = $actions->fieldByName('ActionMenus.MoreOptions');
if (!$moreOptions) {
return;
}
/** @var LiteralField $information */
$information = $moreOptions->fieldByName('Information');
if (!$information) {
return;
}
$liveRecord = Versioned::withVersionedMode(function () use ($owner) {
Versioned::set_stage(Versioned::LIVE);
return SiteTree::get()->byID($owner->ID);
});
$infoTemplate = SSViewer::get_templates_by_class(
$owner->ClassName,
'_Information',
SiteTree::class
);
// show published info of localised record, not base record (this is framework's default)
$information->setValue($owner->customise([
'Live' => $liveRecord,
'ExistsOnLive' => $owner->isPublishedInLocale(),
])->renderWith($infoTemplate));
}
/**
* Update modified flag to reflect localised record instead of base record
* It doesn't make sense to have modified flag if page is not localised in current locale
*
* @param array $flags
*/
protected function updateModifiedFlag(array &$flags): void
{
if (!array_key_exists('modified', $flags)) {
return;
}
if ($this->owner->isDraftedInLocale()) {
return;
}
unset($flags['modified']);
}
/**
* Add a flag which indicates that a page has content in other locale but the content is not being inherited
*
* @param array $flags
*/
protected function updateNoSourceFlag(array &$flags): void
{
$locale = FluentState::singleton()->getLocale();
if (!$locale) {
return;
}
if ($this->owner->LocaleInformation($locale)->getSourceLocale()) {
return;
}
$flags['removedfromdraft'] = [
'text' => 'No source',
'title' => 'This page exists in a different locale but the content is not inherited',
];
}
/**
* @param Form $form
* @param string $message
* @return HTTPResponse|string|DBHTMLText
*/
public function actionComplete($form, $message)
{
return null;
}
}