Skip to content

Commit

Permalink
Merge pull request #73 from silverstripe-scienceninjas/embargoexpiry-…
Browse files Browse the repository at this point in the history
…validation

BUGFIX: When no effective workflow is enabled, PHP Notice's were thrown ...
  • Loading branch information
nyeholt committed Dec 2, 2012
2 parents e90a95f + 8cd0acf commit d2131ad
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
38 changes: 31 additions & 7 deletions code/extensions/WorkflowEmbargoExpiryExtension.php
Expand Up @@ -32,6 +32,11 @@ class WorkflowEmbargoExpiryExtension extends DataExtension {
*/
public $workflowService;

/**
* @var Is a workflow in effect?
*/
public $isWorkflowInEffect = false;

/**
*
* @var array $extendedMethodReturn A basic extended validation routine method return format
Expand All @@ -56,12 +61,9 @@ public function updateCMSFields(FieldList $fields) {
Requirements::javascript(ADVANCED_WORKFLOW_DIR . '/thirdparty/javascript/jquery-ui/timepicker/jquery-ui-timepicker-addon.js');
Requirements::javascript(ADVANCED_WORKFLOW_DIR . '/javascript/WorkflowField.js');

$this->setIsWorkflowInEffect();

// if there is a workflow applied, we can't set the publishing date directly, only the 'desired'
// publishing date
$effective = $this->workflowService->getDefinitionFor($this->owner);

if ($effective) {
if ($this->getIsWorkflowInEffect()) {
$fields->addFieldsToTab('Root.PublishingSchedule', array(
new HeaderField('PublishDateHeader', _t('WorkflowEmbargoExpiryExtension.REQUESTED_PUBLISH_DATE_H3', 'Expiry and Embargo'), 3),
new LiteralField('PublishDateIntro', $this->getIntroMessage('PublishDateIntro')),
Expand All @@ -72,17 +74,19 @@ public function updateCMSFields(FieldList $fields) {
// Readonly fields do not store any value, so add a hidden-field and set its value to the current PublishOnDate so we can perform some validation
$uth = new HiddenField('PublishOnDateOwner')
));
// Set a value to our hidden field
$uth->setValue($this->owner->PublishOnDate);
} else {
$fields->addFieldsToTab('Root.PublishingSchedule', array(
new HeaderField('PublishDateHeader', _t('WorkflowEmbargoExpiryExtension.REQUESTED_PUBLISH_DATE_H3', 'Expiry and Embargo'), 3),
new LiteralField('PublishDateIntro', $this->getIntroMessage('PublishDateIntro')),
$dt = new Datetimefield('PublishOnDate', _t('WorkflowEmbargoExpiryExtension.PUBLISH_ON', 'Scheduled publish date')),
$ut = new Datetimefield('UnPublishOnDate', _t('WorkflowEmbargoExpiryExtension.UNPUBLISH_ON', 'Scheduled un-publish date')),
));
}

$dt->getDateField()->setConfig('showcalendar', true);
$ut->getDateField()->setConfig('showcalendar', true);
// Set a value to our hidden field
$uth->setValue($this->owner->PublishOnDate);

// Enable a jQuery-UI timepicker widget
if(self::$showTimePicker) {
Expand Down Expand Up @@ -157,6 +161,10 @@ public function getIntroMessageParts($key) {
'BULLET_2'=>_t('WorkflowEmbargoExpiryExtension.REQUESTED_PUBLISH_DATE_INTRO_BULLET_2','If an embargo is already set, adding a new one prior to that date\'s passing will overwrite it')
)
);
// If there's no effective workflow, no need for the first bullet-point
if(!$this->getIsWorkflowInEffect()) {
$parts['PublishDateIntro']['BULLET_1'] = false;
}
return $parts[$key];
}

Expand Down Expand Up @@ -191,6 +199,9 @@ public function getCMSValidator() {
* @return array
*/
public function extendedRequiredFieldsCheckEmbargoDates($data = null) {
if(!$this->getIsWorkflowInEffect() || !isset($data['PublishOnDateOwner'])) {
return self::$extendedMethodReturn;
}
$desiredEmbargo = strtotime($data['DesiredPublishDate']);
$scheduledEmbargo = strtotime($data['PublishOnDateOwner']);
$desiredExpiry = strtotime($data['DesiredUnPublishDate']);
Expand Down Expand Up @@ -239,4 +250,17 @@ public function getUserDate($date) {
$member = Member::currentUser();
return $date->toString($member->getDateFormat().' '.$member->getTimeFormat());
}

/*
* Sets property as boolean true|false if an effective workflow is found or not
*/
public function setIsWorkflowInEffect() {
// if there is a workflow applied, we can't set the publishing date directly, only the 'desired' publishing date
$effective = $this->workflowService->getDefinitionFor($this->owner);
$this->isWorkflowInEffect = $effective?true:false;
}

public function getIsWorkflowInEffect() {
return $this->isWorkflowInEffect;
}
}
4 changes: 3 additions & 1 deletion templates/Includes/embargoIntro.ss
@@ -1,7 +1,9 @@

<p>$INTRO</p>
<ul>
<li>$BULLET_1</li>
<% if BULLET_1 %>
<li>$BULLET_1</li>
<% end_if %>
<li>$BULLET_2</li>
</ul>
<br/>

0 comments on commit d2131ad

Please sign in to comment.