Skip to content

Commit

Permalink
Feature: Add support for external Embargo & Expiry module. Remove fea…
Browse files Browse the repository at this point in the history
…ture from codebase.
  • Loading branch information
cpenny committed Mar 15, 2019
1 parent 0748560 commit 7fc91a4
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 1,019 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules/
.idea/
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
},
"require-dev": {
"phpunit/phpunit": "^5.7",
"squizlabs/php_codesniffer": "^3.0"
"squizlabs/php_codesniffer": "^3.0",
"silverstripe-terraformers/embargo-expiry": "~1.0.1"
},
"extra": {
"branch-alias": {
Expand All @@ -36,7 +37,8 @@
]
},
"suggest": {
"symbiote/silverstripe-queuedjobs": "Allow automated workflow transitions with queued system jobs"
"symbiote/silverstripe-queuedjobs": "Allow automated workflow reminders with queued system jobs",
"silverstripe-terraformers/embargo-expiry": "Allow automated workflow transitions with embargo & expiry dates."
},
"autoload": {
"psr-4": {
Expand Down
153 changes: 95 additions & 58 deletions src/Actions/PublishItemWorkflowAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,111 +2,85 @@

namespace Symbiote\AdvancedWorkflow\Actions;

use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\FieldGroup;
use SilverStripe\Forms\LabelField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\NumericField;
use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\Versioned;
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowAction;
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowInstance;
use Symbiote\AdvancedWorkflow\Extensions\WorkflowEmbargoExpiryExtension;
use Symbiote\AdvancedWorkflow\Jobs\WorkflowPublishTargetJob;
use Symbiote\QueuedJobs\Services\AbstractQueuedJob;
use Symbiote\QueuedJobs\Services\QueuedJobService;
use Terraformers\EmbargoExpiry\Extension\EmbargoExpiryExtension;

/**
* Publishes an item
* Publishes an item or approves it for publishing/un-publishing through queued jobs.
*
* @author [email protected]
* @license BSD License (http://silverstripe.org/bsd-license/)
* @package advancedworkflow
* @subpackage actions
* @property int $PublishDelay
*/
class PublishItemWorkflowAction extends WorkflowAction
{
/**
* @var array
*/
private static $db = array(
'PublishDelay' => 'Int',
'AllowEmbargoedEditing' => 'Boolean',
);

private static $defaults = array(
'AllowEmbargoedEditing' => true
'PublishDelay' => 'Int',
);

/**
* @var string
*/
private static $icon = 'symbiote/silverstripe-advancedworkflow:images/publish.png';

/**
* @var string
*/
private static $table_name = 'PublishItemWorkflowAction';

/**
* @param WorkflowInstance $workflow
* @return bool
* @throws \SilverStripe\ORM\ValidationException
*/
public function execute(WorkflowInstance $workflow)
{
if (!$target = $workflow->getTarget()) {
return true;
}

if (class_exists(AbstractQueuedJob::class) && $this->PublishDelay) {
$job = new WorkflowPublishTargetJob($target);
$days = $this->PublishDelay;
$after = date('Y-m-d H:i:s', strtotime("+$days days"));

// disable editing, and embargo the delay if using WorkflowEmbargoExpiryExtension
if ($target->hasExtension(WorkflowEmbargoExpiryExtension::class)) {
$target->AllowEmbargoedEditing = $this->AllowEmbargoedEditing;
$target->PublishOnDate = $after;
$target->write();
} else {
singleton(QueuedJobService::class)->queueJob($job, $after);
}
} elseif ($target->hasExtension(WorkflowEmbargoExpiryExtension::class)) {
$target->AllowEmbargoedEditing = $this->AllowEmbargoedEditing;
// setting future date stuff if needbe

// set this value regardless
$target->UnPublishOnDate = $target->DesiredUnPublishDate;
$target->DesiredUnPublishDate = '';

// Publish dates
if ($target->DesiredPublishDate) {
// Hand-off desired publish date
$target->PublishOnDate = $target->DesiredPublishDate;
$target->DesiredPublishDate = '';
$target->write();
} else {
// Ensure previously modified DesiredUnPublishDate values are written
$target->write();
if ($target->hasMethod('publishRecursive')) {
$target->publishRecursive();
}
}
if ($this->targetHasEmbargoExpiryOrDelay($target)) {
$this->queueEmbargoExpiryJobs($target);

$target->write();
} else {
if ($target->hasMethod('publishRecursive')) {
/** @var DataObject|Versioned $target */
$target->publishRecursive();
}
}

return true;
}

/**
* @return FieldList
*/
public function getCMSFields()
{
$fields = parent::getCMSFields();

if (class_exists(AbstractQueuedJob::class)) {
$fields->addFieldsToTab('Root.Main', [
CheckboxField::create(
'AllowEmbargoedEditing',
_t(
__CLASS__ . '.ALLOWEMBARGOEDEDITING',
'Allow editing while item is embargoed? (does not apply without embargo)'
)
),
if (class_exists(EmbargoExpiryExtension::class)) {
$fields->addFieldToTab(
'Root.Main',
NumericField::create(
'PublishDelay',
_t('PublishItemWorkflowAction.PUBLICATIONDELAY', 'Publication Delay')
)->setDescription(_t(
__CLASS__ . '.PublicationDelayDescription',
'Delay publiation by the specified number of days'
))
]);
);
}

return $fields;
Expand All @@ -122,4 +96,67 @@ public function canPublishTarget(DataObject $target)
{
return true;
}

/**
* @param DataObject|EmbargoExpiryExtension $target
* @return bool
*/
public function targetHasEmbargoExpiryOrDelay($target)
{
if (!$this->targetHasEmbargoExpiryModules($target)) {
return false;
}

if ($target->getDesiredPublishDateAsTimestamp() > 0
|| $target->getDesiredUnPublishDateAsTimestamp() > 0
) {
return true;
}

if ($this->actionHasPublishDelayForTarget($target)) {
return true;
}

return false;
}

/**
* @param DataObject $target
* @return bool
*/
public function actionHasPublishDelayForTarget($target)
{
if (!$this->targetHasEmbargoExpiryModules($target)) {
return false;
}

if (!$this->PublishDelay) {
return false;
}

return true;
}

/**
* @param DataObject|EmbargoExpiryExtension $target
*/
public function queueEmbargoExpiryJobs($target)
{
// Queue UnPublishJob if it's required.
if ($target->getDesiredUnPublishDateAsTimestamp() !== 0) {
$target->createOrUpdateUnPublishJob($target->getDesiredUnPublishDateAsTimestamp());
}

// Queue PublishJob if it's required, and if it is, exit early (so that we don't queue by PublishDelay).
if ($target->getDesiredPublishDateAsTimestamp() !== 0) {
$target->createOrUpdatePublishJob($target->getDesiredPublishDateAsTimestamp());

return;
}

// There was no requested PublishOnDate, so if this action has a PublishDelay, we'll use that.
if ($this->actionHasPublishDelayForTarget($target)) {
$target->createOrUpdatePublishJob(strtotime("+{$this->PublishDelay} days"));
}
}
}
111 changes: 89 additions & 22 deletions src/Actions/UnpublishItemWorkflowAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,58 @@
use SilverStripe\Forms\LabelField;
use SilverStripe\Forms\NumericField;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ValidationException;
use SilverStripe\Versioned\Versioned;
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowAction;
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowInstance;
use Symbiote\AdvancedWorkflow\Extensions\WorkflowEmbargoExpiryExtension;
use Symbiote\AdvancedWorkflow\Jobs\WorkflowPublishTargetJob;
use Symbiote\QueuedJob\Services\AbstractQueuedJob;
use Symbiote\QueuedJob\Services\QueuedJobService;
use Terraformers\EmbargoExpiry\Extension\EmbargoExpiryExtension;

/**
* Unpublishes an item
* Unpublishes an item or approves it for publishing/un-publishing through queued jobs.
*
* @author [email protected]
* @license BSD License (http://silverstripe.org/bsd-license/)
* @package advancedworkflow
* @subpackage actions
* @property int $UnpublishDelay
*/
class UnpublishItemWorkflowAction extends WorkflowAction
{
/**
* @var array
*/
private static $db = array(
'UnpublishDelay' => 'Int'
'UnpublishDelay' => 'Int',
);

/**
* @var string
*/
private static $icon = 'symbiote/silverstripe-advancedworkflow:images/unpublish.png';

/**
* @var string
*/
private static $table_name = 'UnpublishItemWorkflowAction';

/**
* @param WorkflowInstance $workflow
* @return bool
* @throws ValidationException
*/
public function execute(WorkflowInstance $workflow)
{
if (!$target = $workflow->getTarget()) {
return true;
}

if (class_exists(AbstractQueuedJob::class) && $this->UnpublishDelay) {
$job = new WorkflowPublishTargetJob($target, "unpublish");
$days = $this->UnpublishDelay;
$after = date('Y-m-d H:i:s', strtotime("+$days days"));
singleton(QueuedJobService::class)->queueJob($job, $after);
} elseif ($target->hasExtension(WorkflowEmbargoExpiryExtension::class)) {
// setting future date stuff if needbe

// set these values regardless
$target->DesiredUnPublishDate = '';
$target->DesiredPublishDate = '';
$target->write();
if ($this->targetHasEmbargoExpiryOrDelay($target)) {
$this->queueEmbargoExpiryJobs($target);

if ($target->hasMethod('doUnpublish')) {
$target->doUnpublish();
}
$target->write();
} else {
if ($target->hasMethod('doUnpublish')) {
/** @var DataObject|Versioned $target */
$target->doUnpublish();
}
}
Expand All @@ -66,7 +69,7 @@ public function getCMSFields()
{
$fields = parent::getCMSFields();

if (class_exists(AbstractQueuedJob::class)) {
if (class_exists(EmbargoExpiryExtension::class)) {
$before = _t('UnpublishItemWorkflowAction.DELAYUNPUBDAYSBEFORE', 'Delay unpublishing by ');
$after = _t('UnpublishItemWorkflowAction.DELAYUNPUBDAYSAFTER', ' days');

Expand All @@ -89,4 +92,68 @@ public function canPublishTarget(DataObject $target)
{
return false;
}

/**
* @param DataObject|EmbargoExpiryExtension $target
* @return bool
*/
public function targetHasEmbargoExpiryOrDelay($target)
{
if (!$this->targetHasEmbargoExpiryModules($target)) {
return false;
}

if ($target->getDesiredPublishDateAsTimestamp() > 0
|| $target->getDesiredUnPublishDateAsTimestamp() === 0
) {
return true;
}

if ($this->actionHasUnPublishDelayForTarget($target)) {
return true;
}

return false;
}

/**
* @param DataObject $target
* @return bool
*/
public function actionHasUnPublishDelayForTarget($target)
{
if (!$this->targetHasEmbargoExpiryModules($target)) {
return false;
}

if (!$this->UnpublishDelay) {
return false;
}

return true;
}

/**
* @param DataObject|EmbargoExpiryExtension $target
*/
public function queueEmbargoExpiryJobs($target)
{

// Queue PublishJob if it's required.
if ($target->getDesiredPublishDateAsTimestamp() !== 0) {
$target->createOrUpdatePublishJob($target->getDesiredPublishDateAsTimestamp());
}

// Queue UnPublishJob if it's required, and if it is, exit early (so that we don't queue by UnpublishDelay).
if ($target->getDesiredUnPublishDateAsTimestamp() !== 0) {
$target->createOrUpdateUnPublishJob($target->getDesiredUnPublishDateAsTimestamp());

return;
}

// There was no requested PublishOnDate, so if this action has a PublishDelay, we'll use that.
if ($this->actionHasUnPublishDelayForTarget($target)) {
$target->createOrUpdateUnPublishJob(strtotime("+{$this->UnpublishDelay} days"));
}
}
}
Loading

0 comments on commit 7fc91a4

Please sign in to comment.