Skip to content

Commit

Permalink
Updated rollback functionality to not remote embargo/expiry
Browse files Browse the repository at this point in the history
  • Loading branch information
cpenny authored and chrispenny committed Jul 22, 2018
1 parent 3a9902b commit 6ef1ce2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 75 deletions.
6 changes: 6 additions & 0 deletions _config/model.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
Name: embargoexpirymodel
---
SilverStripe\ORM\DataObject:
allow_embargoed_editing: true
enforce_sequential_dates: false
33 changes: 19 additions & 14 deletions src/Extension/EmbargoExpiryCMSMainExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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);
}
Expand All @@ -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();
}
Expand Down
93 changes: 35 additions & 58 deletions src/Extension/EmbargoExpiryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Terraformers\EmbargoExpiry\Extension;

use Exception;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\DatetimeField;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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')
Expand All @@ -561,12 +549,12 @@ public function addPublishingScheduleFields(FieldList $fields)
]
);

if (($message = $this->getEmbargoExpiryFieldNoticeMessage()) !== null) {
if ($message !== null) {
$fields->addFieldToTab(
'Root.PublishingSchedule',
LiteralField::create(
'PublishDateIntro',
"<h4 class=\"notice\">{$message}</h4>"
sprintf('<h4 class="notice">%s</h4>', $message)
),
'PublishOnDate'
);
Expand Down Expand Up @@ -621,35 +609,59 @@ 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) {
return;
}

$message = $this->getEmbargoExpiryNoticeMessage($conditions);
$type = 'notice';

foreach ($conditions as $name => $data) {
$warning = '';

if ($data['warning']) {
$type = 'error';

$warning = sprintf(
'<strong>%s</strong>',
_t(__CLASS__ . '.PAST_DATE_WARNING', ' (this date is in the past, is it still valid?)')
);
}

foreach ($conditions as $name => $date) {
$message .= sprintf(
'<br /><strong>%s</strong>: %s',
'<br /><strong>%s</strong>: %s%s',
ucfirst($name),
$date
$data['date'],
$warning
);
}

$fields->unshift(
LiteralField::create(
'EmbargoExpiryNotice',
"<p class=\"message notice\">{$message}</p>"
sprintf('<p class="message %s">%s</p>', $type, $message)
)
);
}
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions src/Job/PublishTargetJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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();
}

Expand Down

0 comments on commit 6ef1ce2

Please sign in to comment.