Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
Following on from Damian’s suggestions. TODO: Test and implement for …
Browse files Browse the repository at this point in the history
…GridField
  • Loading branch information
Danae Miller-Clendon committed Nov 12, 2019
1 parent 03fc69d commit ac421b7
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 87 deletions.
16 changes: 14 additions & 2 deletions src/Extension/FluentGridFieldExtension.php
Expand Up @@ -2,10 +2,13 @@

namespace TractorCow\Fluent\Extension;

use SilverStripe\Control\Controller;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\Versioned\VersionedGridFieldItemRequest;
use TractorCow\Fluent\Extension\Traits\FluentAdminTrait;
use TractorCow\Fluent\Extension\Traits\FluentBadgeTrait;
Expand All @@ -23,8 +26,8 @@ class FluentGridFieldExtension extends Extension
/**
* Push a badge to indicate the language that owns the current item
*
* @see VersionedGridFieldItemRequest::Breadcrumbs()
* @param DBField|null $badgeField
* @see VersionedGridFieldItemRequest::Breadcrumbs()
*/
public function updateBadge(&$badgeField)
{
Expand All @@ -37,8 +40,17 @@ public function updateFormActions(FieldList $actions)
$this->updateFluentActions($actions, $this->owner->getRecord());
}

/**
* @param Form $form
* @param string $message
* @return mixed
*/
public function actionComplete($form, $message)
{
// todo - Set form message
$form->sessionMessage($message, 'good', ValidationResult::CAST_HTML);

$link = $form->getController()->Link();

return $this->owner->getController()->redirect($link);
}
}
160 changes: 75 additions & 85 deletions src/Extension/Traits/FluentAdminTrait.php
Expand Up @@ -3,6 +3,7 @@
namespace TractorCow\Fluent\Extension\Traits;

use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Dev\Debug;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
Expand All @@ -14,6 +15,7 @@
use TractorCow\Fluent\Extension\FluentFilteredExtension;
use TractorCow\Fluent\Extension\FluentVersionedExtension;
use TractorCow\Fluent\Model\Delete\DeleteLocalisationPolicy;
use TractorCow\Fluent\Model\Delete\DeleteRecordPolicy;
use TractorCow\Fluent\Model\Locale;
use TractorCow\Fluent\State\FluentState;

Expand All @@ -33,47 +35,58 @@ public abstract function actionComplete($form, $message);
*/
protected function updateFluentActions(FieldList $actions, DataObject $record)
{
// If the object exists (is not archived)
if (!$record->isArchived()) {
$locale = Locale::getCurrentLocale();

// Build root tabset that makes up the menu
$rootTabSet = TabSet::create('FluentMenu')
->setTemplate('FluentAdminTabSet');
$rootTabSet->addExtraClass('ss-ui-action-tabset action-menus fluent-actions-menu noborder');

// Add menu button
$moreOptions = Tab::create(
'FluentMenuOptions',
'Localisation'
);
$moreOptions->addExtraClass('popover-actions-simulate');
$rootTabSet->push($moreOptions);

// Add menu items
$moreOptions->push(
FormAction::create('clearFluent', "Clear from all except '{$locale->getTitle()}'")
->addExtraClass('btn-secondary')
);
$moreOptions->push(
FormAction::create('copyFluent', 'Copy to all other locales')
->addExtraClass('btn-secondary')
);
$moreOptions->push(
FormAction::create('unpublishFluent', 'Unpublish (all locales)')
->addExtraClass('btn-secondary')
);
$moreOptions->push(
FormAction::create('archiveFluent', 'Unpublish and Archive (all locales)')
->addExtraClass('btn-secondary')
);
$moreOptions->push(
FormAction::create('publishFluent', 'Save & Publish (all locales)')
->addExtraClass('btn-secondary')
);

$actions->push($rootTabSet);
$results = $record->invokeWithExtensions('isArchived');

// filter down and ensure the record exists
$results = array_filter($results, function ($v) {
return !is_null($v);
});

$isArchived = ($results) ? min($results) : false;

if ($isArchived) {
return;
}

// If there are no results, this will pass as true
$locale = Locale::getCurrentLocale();

// Build root tabset that makes up the menu
$rootTabSet = TabSet::create('FluentMenu')
->setTemplate('FluentAdminTabSet');
$rootTabSet->addExtraClass('ss-ui-action-tabset action-menus fluent-actions-menu noborder');

// Add menu button
$moreOptions = Tab::create(
'FluentMenuOptions',
'Localisation'
);
$moreOptions->addExtraClass('popover-actions-simulate');
$rootTabSet->push($moreOptions);

// Add menu items
$moreOptions->push(
FormAction::create('clearFluent', "Clear from all except '{$locale->getTitle()}'")
->addExtraClass('btn-secondary')
);
$moreOptions->push(
FormAction::create('copyFluent', 'Copy to all other locales')
->addExtraClass('btn-secondary')
);
$moreOptions->push(
FormAction::create('unpublishFluent', 'Unpublish (all locales)')
->addExtraClass('btn-secondary')
);
$moreOptions->push(
FormAction::create('archiveFluent', 'Unpublish and Archive (all locales)')
->addExtraClass('btn-secondary')
);
$moreOptions->push(
FormAction::create('publishFluent', 'Save & Publish (all locales)')
->addExtraClass('btn-secondary')
);

$actions->push($rootTabSet);
}

/**
Expand All @@ -88,6 +101,8 @@ public function clearFluent($data, $form)

$originalLocale = Locale::getCurrentLocale();

// Get the record
/** @var DataObject|SiteTree $record */
$record = $form->getRecord();

// Loop over Locales
Expand All @@ -97,35 +112,26 @@ public function clearFluent($data, $form)
if ($locale->ID != $originalLocale->ID)
foreach ([Versioned::LIVE, Versioned::DRAFT] as $stage) {

/** @var DataObject|FluentVersionedExtension|RecursivePublishable|Versioned|FluentFilteredExtension $record */
$record = Versioned::get_by_stage($record->ClassName, $stage)->byID($record->ID);
Versioned::withVersionedMode(function () use ($record, $locale, $stage) {

// Set the current locale
FluentState::singleton()->withState(function (FluentState $newState) use ($record, $locale) {
$newState->setLocale($locale->getLocale());
Versioned::set_stage($stage);

// Unpublish in localisation
/** @var DataObject|FluentVersionedExtension|RecursivePublishable|Versioned $fresh */
$fresh = $record->get()->byID($record->ID);
$fresh->doUnpublish();
/** @var DataObject|FluentVersionedExtension|RecursivePublishable|Versioned|FluentFilteredExtension $record */
$versionedRecord = Versioned::get_by_stage($record->ClassName, $stage)->byID($record->ID);

// Set the current locale
FluentState::singleton()->withState(function (FluentState $newState) use ($versionedRecord, $locale) {
$newState->setLocale($locale->getLocale());

// after loop, force delete base record with DeleteRecordPolicy
$policy = new DeleteLocalisationPolicy();
$policy->delete($record);
// after loop, force delete base record with DeleteRecordPolicy
$policy = new DeleteLocalisationPolicy();
$policy->delete($versionedRecord);
});
});

}
}

// Restore original state
FluentState::singleton()->withState(function (FluentState $newState) use ($originalLocale) {
$newState->setLocale($originalLocale->getLocale());
});

// Get the record
/** @var DataObject|SiteTree $record */
$record = $form->getRecord();

$message = _t(
__CLASS__ . '.ClearAllNotice',
"All localisations have been cleared for '{title}'.",
Expand Down Expand Up @@ -154,16 +160,8 @@ public function copyFluent($data, $form)
// Set the current locale
FluentState::singleton()->withState(function (FluentState $newState) use ($record, $locale) {
$newState->setLocale($locale->getLocale());
/** @var DataObject|FluentVersionedExtension|RecursivePublishable|Versioned $fresh */
$fresh = $record->get()->byID($record->ID);

$fresh->writeToStage(Versioned::DRAFT);

// Enable if filterable too
/** @var DataObject|FluentFilteredExtension $fresh */
if ($fresh->hasExtension(FluentFilteredExtension::class)) {
$fresh->FilteredLocales()->add($locale);
}
$record->writeToStage(Versioned::DRAFT);
});
}

Expand All @@ -190,11 +188,6 @@ public function unpublishFluent($data, $form)
// delete locale DeleteLocalisationPolicy
// delete live record

$originalStage = Versioned::get_reading_mode();

// Set Live
Versioned::set_reading_mode(Versioned::LIVE);

// Get the record
/** @var DataObject|SiteTree $record */
$record = $form->getRecord();
Expand All @@ -203,18 +196,13 @@ public function unpublishFluent($data, $form)
foreach (Locale::getCached() as $locale) {
// Set the current locale
FluentState::singleton()->withState(function (FluentState $newState) use ($record, $locale) {

// UnPublish the record
$newState->setLocale($locale->getLocale());

/** @var DataObject|FluentVersionedExtension|RecursivePublishable|Versioned $fresh */
$fresh = $record->get()->byID($record->ID);
$fresh->doUnpublish();

// Disable from filter as well
/** @var DataObject|FluentFilteredExtension $fresh */
if ($fresh->hasExtension(FluentFilteredExtension::class)) {
$fresh->FilteredLocales()->remove($locale);
}
});
}

Expand All @@ -224,9 +212,6 @@ public function unpublishFluent($data, $form)
['title' => $record->Title]
);

// Restore original reading mode
Versioned::set_reading_mode($originalStage);

return $this->actionComplete($form, $message);
}

Expand Down Expand Up @@ -261,6 +246,11 @@ public function archiveFluent($data, $form)
}

$record->doArchive();

// Use DeleteLocalisationPolicy to ensure record is properly removed from this
// Locale state
$policy = new DeleteLocalisationPolicy();
$policy->delete($record);
});
}

Expand All @@ -272,7 +262,7 @@ public function archiveFluent($data, $form)


// after loop, force delete base record with DeleteRecordPolicy
$policy = new DeleteLocalisationPolicy();
$policy = new DeleteRecordPolicy();
$policy->delete($record);

return $this->actionComplete($form, $message);
Expand Down

0 comments on commit ac421b7

Please sign in to comment.