Skip to content

Commit

Permalink
Fixed issue #15195: Expiration date can be set before start date (#2558)
Browse files Browse the repository at this point in the history
Co-authored-by: encuestabizdevgit <devgit@encuesta.biz>
  • Loading branch information
gabrieljenik and encuestabizdevgit committed Aug 12, 2022
1 parent 4c41fab commit d84f4af
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
16 changes: 15 additions & 1 deletion application/models/Survey.php
Expand Up @@ -531,7 +531,8 @@ public function rules()
array('running', 'safe', 'on' => 'search'),
array('expires', 'date','format' => ['yyyy-M-d H:m:s.???','yyyy-M-d H:m:s','yyyy-M-d H:m'],'allowEmpty' => true),
array('startdate', 'date','format' => ['yyyy-M-d H:m:s.???','yyyy-M-d H:m:s','yyyy-M-d H:m'],'allowEmpty' => true),
array('datecreated', 'date','format' => ['yyyy-M-d H:m:s.???','yyyy-M-d H:m:s','yyyy-M-d H:m'],'allowEmpty' => true)
array('datecreated', 'date','format' => ['yyyy-M-d H:m:s.???','yyyy-M-d H:m:s','yyyy-M-d H:m'],'allowEmpty' => true),
array('expires', 'checkExpireAfterStart'),
);
}

Expand Down Expand Up @@ -2282,4 +2283,17 @@ function ($s) {
);
return $aSurveys;
}

/**
* Validates the Expiration Date is not lower than the Start Date
*/
public function checkExpireAfterStart($attributes, $params)
{
if (empty($this->startdate) || empty($this->expires)) {
return true;
}
if ($this->expires < $this->startdate) {
$this->addError('expires', gT("Expiration date can't be lower than the start date", 'unescaped'));
}
}
}
Expand Up @@ -17,6 +17,7 @@
var sAdminEmailAddressNeeded = '".gT("If you are using token functions or notifications emails you need to set an administrator email address.",'js')."'
var sURLParameters = '';
var sAddParam = '';
var expirationLowerThanStartError = '" . gT("Expiration date can't be lower than the start date") . "';
", LSYii_ClientScript::POS_BEGIN);
?>
<!-- Publication panel -->
Expand Down
Expand Up @@ -72,6 +72,9 @@
$('#".$entryData['name']."').on('submit.editLocalsettings', function(e){
e.preventDefault();
if (!validateSettingsForm($(this))) {
return false;
}
var data = $(this).serializeArray();
var uri = $(this).attr('action');
$.ajax({
Expand Down
34 changes: 34 additions & 0 deletions assets/scripts/admin/surveysettings.js
Expand Up @@ -152,6 +152,40 @@ function guidGenerator() {
return (S4() + S4() + '-' + S4() + '-' + S4() + '-' + S4() + '-' + S4() + S4() + S4());
}

/**
* Validate settings form depending on form name
*/
function validateSettingsForm($form) {
switch ($form.attr('id')) {
case 'publication':
return validateEndDateHigherThanStart(
$('#startdate_datetimepicker').data('DateTimePicker'),
$('#expires_datetimepicker').data('DateTimePicker'),
expirationLowerThanStartError
);
default:
return true;
}
}

/**
* Validates that an end date is not lower than a start date
*/
function validateEndDateHigherThanStart(startDatePicker, endDatePicker, errorMessage) {
if (!startDatePicker || !startDatePicker.date()) {
return true;
}
if (!endDatePicker || !endDatePicker.date()) {
return true;
}
const difference = endDatePicker.date().diff(startDatePicker.date());
if (difference >= 0) {
return true;
}
LS.LsGlobalNotifier.createFlash(errorMessage, 'alert-danger fade in');
return false;
}

$(document).on('ready pjax:scriptcomplete', function(){
if (window.PanelIntegrationData) {
var i10n = window.PanelIntegrationData.i10n;
Expand Down

0 comments on commit d84f4af

Please sign in to comment.