Skip to content

Commit

Permalink
Add migration dev task for existing Jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Penny committed Jul 19, 2022
1 parent db08b33 commit 3727f64
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/en/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ See the Security section below for more details.
You can optionally add the [Embargy & Expiry](https://github.com/silverstripe-terraformers/silverstripe-embargo-expiry)
module to your project to allow changes to be published (and/or unpublished) at future dates.

**Note:** You will need to use version 1.2.1 or greater.

#### Migrating from an older Workflow version

If you have an existing project which used the `WorkflowEmbargoExpiryExtension`, then you will need to go through a
couple of migration steps.

1) Update usages of `WorkflowEmbargoExpiryExtension` to `EmbargoExpiryExtension` (from the Terraformers module)
2) Run the `EmbargoExpiryMigrationTask` to migrate over any existing Workflow jobs to the new Terraformers' jobs
* This task will require you to define some basic configuration where you tell us what classes you have applied the
`EmbargoExpiryExtension` to. Other than that, you just need to run it
* See the class for more info

### Sending reminder emails

The workflow engine can send out email reminders if a workflow has been open for longer
Expand Down
73 changes: 73 additions & 0 deletions src/Tasks/EmbargoExpiryMigrationTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Symbiote\AdvancedWorkflow\Tasks;

use SilverStripe\Control\HTTPRequest;
use SilverStripe\Dev\BuildTask;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use Terraformers\EmbargoExpiry\Extension\EmbargoExpiryExtension;

/**
* Using this Dev Task:
*
* The main thing that you will need to do is tell us what classes you have applied the EmbargoExpiryExtension to. You
* can do this by update the `classes` config below through yml configuration, or you could copy this Dev Task into
* your project and update it from there
*
* yml example
* Symbiote\AdvancedWorkflow\Tasks\EmbargoExpiryMigrationTask
* classes:
* - SilverStripe\CMS\Model\SiteTree
* # OR you might have applied it to Page?
* - Page
* # Plus any other DataObjects you might have applied WorkflowEmbargoExpiry to
* - App\Models\MyDataObject
*/
class EmbargoExpiryMigrationTask extends BuildTask
{

protected $title = 'Migrate Embargo & Expiry Jobs to external module';
protected $description = 'Migrates existing Embargo & Expiry Jobs to the new Embargo & Expiry module';

private static string $segment = 'EmbargoExpiryMigrationTask';

private static array $classes = [];

/**
* @param HTTPRequest $request
* @return void
*/
public function run($request)
{
foreach ($this->config()->get('classes') as $className) {
if (!DataObject::singleton($className)->hasExtension(EmbargoExpiryExtension::class)) {
continue;
}

/** @var DataList|DataObject[]|EmbargoExpiryExtension[] $dataObjects */
$dataObjects = DataObject::get($className);

foreach ($dataObjects as $dataObject) {
$updated = false;

if ($dataObject->PublishOnDate) {
$dataObject->createOrUpdatePublishJob(strtotime($dataObject->PublishOnDate));

$updated = true;
}

if ($dataObject->UnPublishOnDate) {
$dataObject->createOrUpdateUnPublishJob(strtotime($dataObject->UnPublishOnDate));

$updated = true;
}

if ($updated) {
$dataObject->write();
}
}
}
}

}

0 comments on commit 3727f64

Please sign in to comment.