From 6ef1ce274baf0952be8ae1bd4c732ea350d1d901 Mon Sep 17 00:00:00 2001 From: cpenny Date: Mon, 23 Jul 2018 07:43:50 +1200 Subject: [PATCH] Updated rollback functionality to not remote embargo/expiry --- _config/model.yml | 6 ++ .../EmbargoExpiryCMSMainExtension.php | 33 ++++--- src/Extension/EmbargoExpiryExtension.php | 93 +++++++------------ src/Job/PublishTargetJob.php | 5 +- 4 files changed, 62 insertions(+), 75 deletions(-) create mode 100644 _config/model.yml diff --git a/_config/model.yml b/_config/model.yml new file mode 100644 index 0000000..dce46c0 --- /dev/null +++ b/_config/model.yml @@ -0,0 +1,6 @@ +--- +Name: embargoexpirymodel +--- +SilverStripe\ORM\DataObject: + allow_embargoed_editing: true + enforce_sequential_dates: false diff --git a/src/Extension/EmbargoExpiryCMSMainExtension.php b/src/Extension/EmbargoExpiryCMSMainExtension.php index 078d8ec..6284f5b 100644 --- a/src/Extension/EmbargoExpiryCMSMainExtension.php +++ b/src/Extension/EmbargoExpiryCMSMainExtension.php @@ -7,10 +7,19 @@ use SilverStripe\Control\HTTPResponse_Exception; use SilverStripe\Core\Extension; use SilverStripe\Forms\Form; +use SilverStripe\ORM\DataObject; use Symfony\Component\Finder\Exception\AccessDeniedException; +/** + * Class EmbargoExpiryCMSMainExtension + * + * @package Terraformers\EmbargoExpiry\Extension + */ class EmbargoExpiryCMSMainExtension extends Extension { + /** + * @var array + */ private static $allowed_actions = array( 'removeEmbargoAction', 'removeExpiryAction', @@ -42,10 +51,7 @@ public function updateEditForm($form) */ public function removeEmbargoAction($data, $form) { - // Find the record. - $id = $data['ID']; - - $this->removeEmbargoOrExpiry($id, 'PublishOnDate'); + $this->removeEmbargoOrExpiry($data['ClassName'], $data['ID'], 'PublishOnDate', 'PublishJobID'); $this->owner->getResponse()->addHeader( 'X-Status', @@ -66,10 +72,7 @@ public function removeEmbargoAction($data, $form) */ public function removeExpiryAction($data, $form) { - // Find the record. - $id = $data['ID']; - - $this->removeEmbargoOrExpiry($id, 'UnPublishOnDate'); + $this->removeEmbargoOrExpiry($data['ClassName'], $data['ID'], 'UnPublishOnDate', 'UnPublishJobID'); $this->owner->getResponse()->addHeader( 'X-Status', @@ -80,14 +83,15 @@ public function removeExpiryAction($data, $form) } /** - * @param $id - * @param $field + * @param string $className + * @param string $id + * @param string $field * @throws HTTPResponse_Exception */ - protected function removeEmbargoOrExpiry($id, $field) + protected function removeEmbargoOrExpiry($className, $id, $dateField, $jobField) { - /** @var SiteTree|EmbargoExpiryExtension $record */ - $record = SiteTree::get()->byID($id); + /** @var DataObject|EmbargoExpiryExtension $record */ + $record = DataObject::get($className)->byID($id); if (!$record || !$record->exists()) { throw new HTTPResponse_Exception("Bad record ID #$id", 404); } @@ -97,7 +101,8 @@ protected function removeEmbargoOrExpiry($id, $field) } // Writing the record with no embargo set will automatically remove the queued jobs. - $record->$field = null; + $record->$dateField = null; + $record->$jobField = 0; $record->write(); } diff --git a/src/Extension/EmbargoExpiryExtension.php b/src/Extension/EmbargoExpiryExtension.php index 27fa5ab..9abdf15 100644 --- a/src/Extension/EmbargoExpiryExtension.php +++ b/src/Extension/EmbargoExpiryExtension.php @@ -2,6 +2,7 @@ namespace Terraformers\EmbargoExpiry\Extension; +use Exception; use SilverStripe\Core\Injector\Injector; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\DatetimeField; @@ -60,21 +61,6 @@ class EmbargoExpiryExtension extends DataExtension implements PermissionProvider */ public $isPublishJobRunning = false; - /** - * Config variable to decide whether or not pages can be edited while they are embargoed. - * - * @var bool - */ - public static $allow_embargoed_editing = false; - - /** - * Config variable that you can set to true if you want to always enforce that publish dates are before unpublish - * dates. - * - * @var bool - */ - public static $enforce_sequential_dates = false; - /** * @param FieldList $fields */ @@ -537,6 +523,8 @@ public function isEditable() */ public function addPublishingScheduleFields(FieldList $fields) { + $message = $this->getEmbargoExpiryFieldNoticeMessage(); + $fields->findOrMakeTab( 'Root.PublishingSchedule', _t(__CLASS__ . '.TAB_TITLE', 'Publishing Schedule') @@ -561,12 +549,12 @@ public function addPublishingScheduleFields(FieldList $fields) ] ); - if (($message = $this->getEmbargoExpiryFieldNoticeMessage()) !== null) { + if ($message !== null) { $fields->addFieldToTab( 'Root.PublishingSchedule', LiteralField::create( 'PublishDateIntro', - "

{$message}

" + sprintf('

%s

', $message) ), 'PublishOnDate' ); @@ -621,15 +609,26 @@ public function getEmbargoExpiryNoticeMessage($conditions) public function addEmbargoExpiryNoticeFields(FieldList $fields) { $conditions = []; + $warnings = []; if ($this->getIsPublishScheduled()) { + $time = strtotime($this->owner->PublishOnDate); $key = _t(__CLASS__ . '.EMBARGO_NAME', 'embargo'); - $conditions[$key] = $this->owner->PublishOnDate; + + $conditions[$key] = [ + 'date' => $this->owner->PublishOnDate, + 'warning' => ($time > 0 && $time < time()), + ]; } if ($this->getIsUnPublishScheduled()) { + $time = strtotime($this->owner->UnPublishOnDate); $key = _t(__CLASS__ . '.EXPIRY_NAME', 'expiry'); - $conditions[$key] = $this->owner->UnPublishOnDate; + + $conditions[$key] = [ + 'date' => $this->owner->UnPublishOnDate, + 'warning' => ($time > 0 && $time < time()), + ]; } if (count($conditions) === 0) { @@ -637,19 +636,32 @@ public function addEmbargoExpiryNoticeFields(FieldList $fields) } $message = $this->getEmbargoExpiryNoticeMessage($conditions); + $type = 'notice'; + + foreach ($conditions as $name => $data) { + $warning = ''; + + if ($data['warning']) { + $type = 'error'; + + $warning = sprintf( + '%s', + _t(__CLASS__ . '.PAST_DATE_WARNING', ' (this date is in the past, is it still valid?)') + ); + } - foreach ($conditions as $name => $date) { $message .= sprintf( - '
%s: %s', + '
%s: %s%s', ucfirst($name), - $date + $data['date'], + $warning ); } $fields->unshift( LiteralField::create( 'EmbargoExpiryNotice', - "

{$message}

" + sprintf('

%s

', $type, $message) ) ); } @@ -677,41 +689,6 @@ public function getEmbargoExpiryFieldNoticeMessage() ); } - /** - * @param string $jobType - * @throws \InvalidArgumentException - */ - public function updateVersionsTableRecord($jobType) - { - $table = $this->owner->baseTable() . '_Versions'; - - switch ($jobType) { - case static::JOB_TYPE_PUBLISH: - $dateField = 'PublishOnDate'; - $jobField = 'PublishJobID'; - break; - case static::JOB_TYPE_UNPUBLISH: - $dateField = 'UnPublishOnDate'; - $jobField = 'UnPublishJobID'; - break; - default: - throw new \InvalidArgumentException('Invalid Job type supplied.'); - } - - $sql = SQLUpdate::create($table, - [ - $dateField => $this->$dateField, - $jobField => $this->$jobField, - ], - [ - 'RecordID' => $this->owner->ID, - 'Version' => $this->owner->Version, - ] - ); - - $sql->execute(); - } - /** * A method that can be implemented on your DataObject. This method is run prior to calling publishRecursive() in * the PublishTargetJob. diff --git a/src/Job/PublishTargetJob.php b/src/Job/PublishTargetJob.php index b1cc7c9..ecc7acf 100644 --- a/src/Job/PublishTargetJob.php +++ b/src/Job/PublishTargetJob.php @@ -4,6 +4,7 @@ use SilverStripe\CMS\Model\SiteTree; use SilverStripe\ORM\DataObject; +use SilverStripe\Versioned\Versioned; use SuperClosure\SerializableClosure; use Symbiote\QueuedJobs\Services\AbstractQueuedJob; use Terraformers\EmbargoExpiry\Extension\EmbargoExpiryExtension; @@ -78,7 +79,7 @@ public function getTitle() public function process() { - /** @var SiteTree $target */ + /** @var DataObject|Versioned|EmbargoExpiryExtension $target */ $target = $this->getTarget(); $type = array_key_exists('type', $this->options) ? $this->options['type'] : null; @@ -100,13 +101,11 @@ public function process() $target->prePublishTargetJob($this->options); $target->unlinkPublishJobAndDate(); $target->writeWithoutVersion(); - $target->updateVersionsTableRecord(EmbargoExpiryExtension::JOB_TYPE_PUBLISH); $target->publishRecursive(); } elseif ($type === EmbargoExpiryExtension::JOB_TYPE_UNPUBLISH) { $target->preUnPublishTargetJob($this->options); $target->unlinkUnPublishJobAndDate(); $target->writeWithoutVersion(); - $target->updateVersionsTableRecord(EmbargoExpiryExtension::JOB_TYPE_UNPUBLISH); $target->doUnpublish(); }