-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add support for external Embargo & Expiry module. Remove fea…
…ture from codebase.
- Loading branch information
cpenny
committed
Mar 15, 2019
1 parent
0748560
commit 7fc91a4
Showing
9 changed files
with
367 additions
and
1,019 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/node_modules/ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
} | ||
} | ||
|
@@ -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'); | ||
|
||
|
@@ -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")); | ||
} | ||
} | ||
} |
Oops, something went wrong.