From fc40b0a5ea5c7788da4eafe3ea539528148826d6 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Wed, 20 Nov 2024 15:45:51 +1300 Subject: [PATCH] ENH Update status flags generally, not just for SiteTree --- src/Extension/FluentExtension.php | 109 +++++++++++++++++++ src/Extension/FluentGridFieldExtension.php | 15 --- src/Extension/FluentLeftAndMainExtension.php | 22 ---- src/Extension/FluentSiteTreeExtension.php | 104 ------------------ src/Extension/FluentVersionedExtension.php | 70 ++++++++++++ 5 files changed, 179 insertions(+), 141 deletions(-) diff --git a/src/Extension/FluentExtension.php b/src/Extension/FluentExtension.php index eeffef9c..b8066c86 100644 --- a/src/Extension/FluentExtension.php +++ b/src/Extension/FluentExtension.php @@ -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; @@ -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' => $locale->getSourceLocale()->getTitle() + ] + ), + 'text' => $locale->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() diff --git a/src/Extension/FluentGridFieldExtension.php b/src/Extension/FluentGridFieldExtension.php index 27a8e9e2..a4859401 100644 --- a/src/Extension/FluentGridFieldExtension.php +++ b/src/Extension/FluentGridFieldExtension.php @@ -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 @@ -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) { diff --git a/src/Extension/FluentLeftAndMainExtension.php b/src/Extension/FluentLeftAndMainExtension.php index c0320cee..5910a62d 100644 --- a/src/Extension/FluentLeftAndMainExtension.php +++ b/src/Extension/FluentLeftAndMainExtension.php @@ -7,11 +7,9 @@ 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 @@ -19,7 +17,6 @@ class FluentLeftAndMainExtension extends Extension { use FluentAdminTrait; - use FluentBadgeTrait; protected function onInit() { @@ -27,25 +24,6 @@ protected function onInit() 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 diff --git a/src/Extension/FluentSiteTreeExtension.php b/src/Extension/FluentSiteTreeExtension.php index 196a6b1e..106ed4a7 100644 --- a/src/Extension/FluentSiteTreeExtension.php +++ b/src/Extension/FluentSiteTreeExtension.php @@ -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 */ @@ -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 diff --git a/src/Extension/FluentVersionedExtension.php b/src/Extension/FluentVersionedExtension.php index 141556b4..7849e2b5 100644 --- a/src/Extension/FluentVersionedExtension.php +++ b/src/Extension/FluentVersionedExtension.php @@ -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'] = [