Skip to content

Commit

Permalink
ENH Update status flags generally, not just for SiteTree
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Nov 26, 2024
1 parent e925932 commit 0113c90
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 189 deletions.
109 changes: 109 additions & 0 deletions src/Extension/FluentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use SilverStripe\Security\Permission;
use SilverStripe\Versioned\Versioned;
use SilverStripe\View\HTML;
use TractorCow\Fluent\Extension\Traits\FluentBadgeTrait;
use TractorCow\Fluent\Extension\Traits\FluentObjectTrait;
use TractorCow\Fluent\Forms\CopyLocaleAction;
use TractorCow\Fluent\Forms\GroupActionMenu;
Expand Down Expand Up @@ -1286,6 +1287,114 @@ protected function findRecordInLocale($locale, $table, $id)
return $query->firstRow()->execute()->value() !== null;
}

/**
* Remove fluent status flags from site tree display
*/
protected function updateStatusFlagsForTreeTitle(array &$flags): void
{
foreach (array_keys($flags) as $key) {
if ($key === 'fluent' || str_starts_with($key, 'fluent ')) {
unset($flags[$key]);
}
}
}

/**
* Update status flags based on whether the current record is exists in the current locale.
*/
protected function updateStatusFlags(array &$flags): void
{
// If there is no current FluentState, then we shouldn't update.
if (!FluentState::singleton()->getLocale()) {
return;
}
$this->addLocaleFlags($flags);
$this->updateNoSourceFlag($flags);
}

/**
* Add a flag based on the record's status in the current locale.
*/
private function addLocaleFlags(array &$flags): void
{
$locale = Locale::getCurrentLocale();
$record = $this->getOwner();
$info = RecordLocale::create($record, $locale);

// Build new badge
if ($info->IsDraft()) {
// If the object has been localised in the current locale, show a "localised" state
$flags['fluent fluent-badge fluent-badge--default'] = [
'title' => _t(
FluentBadgeTrait::class . '.BadgeLocalised',
'Localised in {locale}',
[
'locale' => $locale->getTitle()
]
),
'text' => $locale->getLocale(),
];
} elseif ($info->getSourceLocale()) {
// If object is inheriting content from another locale show the source
$flags['fluent fluent-badge fluent-badge--localised'] = [
'title' => _t(
FluentBadgeTrait::class . '.BadgeLocalised',
'Localised in {locale}',
[
'locale' => $info->getSourceLocale()->getTitle()
]
),
'text' => $info->getSourceLocale()->getLocale(),
];
} else {
// Otherwise the object is missing a content source and needs to be remedied
// by either localising or seting up a locale fallback
$flags['fluent fluent-badge fluent-badge--invisible'] = [
'title' => _t(
FluentBadgeTrait::class . '.BaggeInvisible',
'{type} has no available content in {locale}, localise the {type} or provide a locale fallback',
[
'type' => $record->i18n_singular_name(),
'locale' => $locale->getTitle(),
]
),
'text' => $locale->getLocale(),
];
}
}

/**
* Add a flag which indicates that a record has content in other locale but the content is not being inherited
*/
protected function updateNoSourceFlag(array &$flags): void
{
if (array_key_exists('archived', $flags)) {
return;
}

$locale = FluentState::singleton()->getLocale();

if (!$locale) {
return;
}

$owner = $this->getOwner();
$info = $owner->LocaleInformation($locale);

if ($info->getSourceLocale()) {
return;
}

if (!$owner->getLocaleInstances()) {
return;
}

$flags['removedfromdraft'] = [
'text' => 'No source',
'title' => 'This page exists in a different locale but the content is not inherited',
];
}

/**
* @param $summaryColumns
* @see FluentObjectTrait::updateFluentCMSFields()
Expand Down
15 changes: 0 additions & 15 deletions src/Extension/FluentGridFieldExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\Core\Validation\ValidationResult;
use SilverStripe\Versioned\VersionedGridFieldItemRequest;
use TractorCow\Fluent\Extension\Traits\FluentAdminTrait;
use TractorCow\Fluent\Extension\Traits\FluentBadgeTrait;

/**
* Supports GridFieldDetailForm_ItemRequest with extra actions
Expand All @@ -23,19 +21,6 @@
class FluentGridFieldExtension extends Extension
{
use FluentAdminTrait;
use FluentBadgeTrait;

/**
* Push a badge to indicate the language that owns the current item
*
* @param DBField|null $badgeField
* @see VersionedGridFieldItemRequest::Breadcrumbs()
*/
protected function updateBadge(&$badgeField)
{
$record = $this->owner->getRecord();
$badgeField = $this->addFluentBadge($badgeField, $record);
}

protected function updateFormActions(FieldList $actions)
{
Expand Down
22 changes: 0 additions & 22 deletions src/Extension/FluentLeftAndMainExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,23 @@
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\Form;
use SilverStripe\Model\List\ArrayList;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\View\Requirements;
use TractorCow\Fluent\Extension\Traits\FluentAdminTrait;
use TractorCow\Fluent\Extension\Traits\FluentBadgeTrait;

/**
* @extends Extension<LeftAndMain>
*/
class FluentLeftAndMainExtension extends Extension
{
use FluentAdminTrait;
use FluentBadgeTrait;

protected function onInit()
{
Requirements::javascript("tractorcow/silverstripe-fluent:client/dist/js/fluent.js");
Requirements::css("tractorcow/silverstripe-fluent:client/dist/styles/fluent.css");
}

/**
* @param ArrayList $breadcrumbs
* @see CMSMain::Breadcrumbs()
*/
protected function updateBreadcrumbs(ArrayList $breadcrumbs)
{
$record = $this->owner->currentPage();
if (!$record) {
return;
}

// Get a possibly existing badge field from the last item in the breadcrumbs list
$lastItem = $breadcrumbs->last();
$badgeField = $lastItem->hasField('Extra') ? $lastItem->getField('Extra') : null;
$newBadge = $this->addFluentBadge($badgeField, $record);

$lastItem->setField('Extra', $newBadge);
}

/**
* @param Form $form
* @param string $message
Expand Down
104 changes: 0 additions & 104 deletions src/Extension/FluentSiteTreeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,33 +158,6 @@ protected function updateLink(&$link, &$action, &$relativeLink)
$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
*/
protected function updateStatusFlags(&$flags)
{
// If there is no current FluentState, then we shouldn't update.
if (!FluentState::singleton()->getLocale()) {
return;
}

$this->updateModifiedFlag($flags);
$this->updateArchivedFlag($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
*/
Expand Down Expand Up @@ -443,83 +416,6 @@ protected function updateRestoreAction(FieldList $actions): void
$actions->removeByName('action_restore');
}

/**
* 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']);
}

/**
* Localise archived flag - remove archived flag if there is content on other locales
*
* @param array $flags
*/
protected function updateArchivedFlag(array &$flags): void
{
if (!array_key_exists('archived', $flags)) {
return;
}

$locale = FluentState::singleton()->getLocale();

if (!$locale) {
return;
}

if (!$this->owner->getLocaleInstances()) {
return;
}

unset($flags['archived']);
}

/**
* 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
{
if (array_key_exists('archived', $flags)) {
return;
}

$locale = FluentState::singleton()->getLocale();

if (!$locale) {
return;
}

$owner = $this->owner;
$info = $owner->LocaleInformation($locale);

if ($info->getSourceLocale()) {
return;
}

if (!$owner->getLocaleInstances()) {
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
Expand Down
70 changes: 70 additions & 0 deletions src/Extension/FluentVersionedExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,76 @@ public static function prepoulateIdsInLocale($locale, $dataObjectClass, $populat
}
}

/**
* Check whether the current record 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
*/
protected function updateStatusFlags(array &$flags): void
{
// If there is no current FluentState, then we shouldn't update.
if (!FluentState::singleton()->getLocale()) {
return;
}

$this->updateModifiedFlag($flags);
$this->updateArchivedFlag($flags);
parent::updateStatusFlags($flags);

// If this page does not exist it should be "invisible"
if (!$this->isDraftedInLocale() && !$this->isPublishedInLocale()) {
$flags['fluentinvisible'] = [
'text' => '',
'title' => '',
];
}
}

/**
* 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']);
}

/**
* Localise archived flag - remove archived flag if there is content on other locales
*
* @param array $flags
*/
protected function updateArchivedFlag(array &$flags): void
{
if (!array_key_exists('archived', $flags)) {
return;
}

$locale = FluentState::singleton()->getLocale();

if (!$locale) {
return;
}

if (!$this->owner->getLocaleInstances()) {
return;
}

unset($flags['archived']);
}

protected function updateLocalisationTabColumns(&$summaryColumns)
{
$summaryColumns['Status'] = [
Expand Down
Loading

0 comments on commit 0113c90

Please sign in to comment.