Skip to content

Commit

Permalink
ENHANCEMENT: Added validation on Embargo extension to show message wh…
Browse files Browse the repository at this point in the history
…en an embargo date already exists on a content-object (Issue #47)
  • Loading branch information
Russell Michell committed Nov 23, 2012
1 parent db3a3d9 commit 27962c6
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 10 deletions.
86 changes: 78 additions & 8 deletions code/extensions/WorkflowEmbargoExpiryExtension.php
Expand Up @@ -31,6 +31,17 @@ class WorkflowEmbargoExpiryExtension extends DataExtension {
* @var WorkflowService * @var WorkflowService
*/ */
public $workflowService; public $workflowService;

/**
*
* @var array $extendedMethodReturn A basic extended validation routine method return format
*/
public static $extendedMethodReturn = array(
'fieldName' =>null,
'fieldField'=>null,
'fieldMsg' =>null,
'fieldValid'=>true
);


/** /**
* @param FieldList $fields * @param FieldList $fields
Expand All @@ -52,22 +63,26 @@ public function updateCMSFields(FieldList $fields) {


if ($effective) { if ($effective) {
$fields->addFieldsToTab('Root.PublishingSchedule', array( $fields->addFieldsToTab('Root.PublishingSchedule', array(
new HeaderField('PublishDateHeader', _t('REQUESTED_PUBLISH_DATE_H3', 'Expiry and Embargo'), 3), new HeaderField('PublishDateHeader', _t('WorkflowEmbargoExpiryExtension.REQUESTED_PUBLISH_DATE_H3', 'Expiry and Embargo'), 3),
new LiteralField('PublishDateIntro', $this->getIntroMessage('PublishDateIntro')), new LiteralField('PublishDateIntro', $this->getIntroMessage('PublishDateIntro')),
$dt = new Datetimefield('DesiredPublishDate', _t('AdvancedWorkflow.REQUESTED_PUBLISH_DATE', 'Requested publish date and time')), $dt = new Datetimefield('DesiredPublishDate', _t('WorkflowEmbargoExpiryExtension.REQUESTED_PUBLISH_DATE', 'Requested publish date')),
$ut = new Datetimefield('DesiredUnPublishDate', _t('AdvancedWorkflow.REQUESTED_UNPUBLISH_DATE', 'Requested un-publish date and time')), $ut = new Datetimefield('DesiredUnPublishDate', _t('WorkflowEmbargoExpiryExtension.REQUESTED_UNPUBLISH_DATE', 'Requested un-publish date')),
Datetimefield::create('PublishOnDate', _t('AdvancedWorkflow.PUBLISH_ON', 'Publish date and time'))->setDisabled(true), Datetimefield::create('PublishOnDate', _t('WorkflowEmbargoExpiryExtension.PUBLISH_ON', 'Scheduled publish date'))->setDisabled(true),
Datetimefield::create('UnPublishOnDate', _t('AdvancedWorkflow.UNPUBLISH_ON', 'Un-publish date and time'))->setDisabled(true), Datetimefield::create('UnPublishOnDate', _t('WorkflowEmbargoExpiryExtension.UNPUBLISH_ON', 'Scheduled un-publish date'))->setDisabled(true),
// 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')
)); ));
} else { } else {
$fields->addFieldsToTab('Root.PublishingSchedule', array( $fields->addFieldsToTab('Root.PublishingSchedule', array(
$dt = new Datetimefield('PublishOnDate', _t('AdvancedWorkflow.PUBLISH_ON', 'Publish date and time')), $dt = new Datetimefield('PublishOnDate', _t('WorkflowEmbargoExpiryExtension.PUBLISH_ON', 'Scheduled publish date')),
$ut = new Datetimefield('UnPublishOnDate', _t('AdvancedWorkflow.UNPUBLISH_ON', 'Un-publish date and time')), $ut = new Datetimefield('UnPublishOnDate', _t('WorkflowEmbargoExpiryExtension.UNPUBLISH_ON', 'Scheduled un-publish date')),
)); ));
} }


$dt->getDateField()->setConfig('showcalendar', true); $dt->getDateField()->setConfig('showcalendar', true);
$ut->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 // Enable a jQuery-UI timepicker widget
if(self::$showTimePicker) { if(self::$showTimePicker) {
Expand Down Expand Up @@ -157,4 +172,59 @@ public function getIntroMessage($key) {
$msg = $curr->customise($msg)->renderWith('embargoIntro'); $msg = $curr->customise($msg)->renderWith('embargoIntro');
return $msg; return $msg;
} }
}
/*
* Validate
*/
public function getCMSValidator() {
$required = new AWRequiredFields();
$required->setCaller($this);
return $required;
}

/*
* Use AWRequiredFields to peform validation at the DataExtension level. Granted, having to add self::$extendedMethodReturn everywhere you want custom validation
* is rubbish, but it works and does what it intends. If a better way is found to implement validation on a DataExtension, without access to Form,
* feel-free to re-open issues/47 and comment with a link to your own Gist! :-)
*
* @param array $data
* @return array
*/
public function extendedRequiredFieldsCheckEmbargoDates($data = null) {
$desiredEmbargo = strtotime($data['DesiredPublishDate']);
$scheduledEmbargo = strtotime($data['PublishOnDateOwner']);
$desiredExpiry = strtotime($data['DesiredUnPublishDate']);
$msg = '';
if(strlen($data['DesiredPublishDate']) && $scheduledEmbargo > time()) {
$scheduledEmbargo = date('d/m/Y H:i',$scheduledEmbargo);
$msg = _t(
'WorkflowEmbargoExpiryExtension.EMBARGO_ERROR_PT1',
"This content is already under embargo, expiring at: ")
.$scheduledEmbargo.
_t(
'WorkflowEmbargoExpiryExtension.EMBARGO_ERROR_PT2',
' please wait until this date has passed, before applying a new embargo date.'
);
self::$extendedMethodReturn['fieldName'] = 'DesiredPublishDate';
}
if(strlen($data['DesiredPublishDate']) && $desiredEmbargo < time()) {
$msg = _t(
'EMBARGO_DSIRD_ERROR',
"This date has already passed, please enter a valid future date."
);
self::$extendedMethodReturn['fieldName'] = 'DesiredPublishDate';
}
if(strlen($data['DesiredUnPublishDate']) && $desiredExpiry < time()) {
$msg = _t(
'EMBARGO_DSIRD_ERROR',
"This date has already passed, please enter a valid future date."
);
self::$extendedMethodReturn['fieldName'] = 'DesiredUnPublishDate';
}
if(strlen($msg)>0) {
self::$extendedMethodReturn['fieldValid'] = false;
self::$extendedMethodReturn['fieldMsg'] = $msg;
}
return self::$extendedMethodReturn;
}
}
11 changes: 9 additions & 2 deletions lang/en.yml
Expand Up @@ -25,7 +25,14 @@ en:
GridFieldTitleAssignedYour: Your pending items GridFieldTitleAssignedYour: Your pending items
GridFieldTitleSubmittedYour: Your submitted items GridFieldTitleSubmittedYour: Your submitted items
WorkflowEmbargoExpiryExtension: WorkflowEmbargoExpiryExtension:
REQUESTED_PUBLISH_DATE: Requested publish date
REQUESTED_UNPUBLISH_DATE: Requested un-publish date
PUBLISH_ON: Scheduled publish date
UNPUBLISH_ON: Scheduled un-publish date
REQUESTED_PUBLISH_DATE_H3: Expiry and Embargo REQUESTED_PUBLISH_DATE_H3: Expiry and Embargo
REQUESTED_PUBLISH_DATE_INTRO: Enter a date and/or time to specify embargo and expiry dates. REQUESTED_PUBLISH_DATE_INTRO: Enter a date and/or time to specify embargo and expiry dates.
WorkflowEmbargoExpiryExtension.REQUESTED_PUBLISH_DATE_INTRO_BULLET_1: These settings won't take effect until any approval actions are run REQUESTED_PUBLISH_DATE_INTRO_BULLET_1: These settings won't take effect until any approval actions are run
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 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
EMBARGO_SCHED_ERROR_PT1: 'This content is already under embargo, expiring at:'
EMBARGO_SCHED_ERROR_PT2: please wait until this date has passed, before applying a new embargo date.
EMBARGO_DSIRD_ERROR: This date has already passed, please enter a valid future date.

0 comments on commit 27962c6

Please sign in to comment.