Skip to content

Commit

Permalink
Fixed issue #15195: Expiration date can be set before start date (#2551)
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 Oct 6, 2022
1 parent cb1ba58 commit 53008de
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
1 change: 1 addition & 0 deletions application/controllers/admin/surveyadmin.php
Expand Up @@ -2096,6 +2096,7 @@ public function getUrlParamsJSON($iSurveyID)
*/
private function _registerScriptFiles()
{
App()->getClientScript()->registerScriptFile(App()->getConfig('adminscripts') . 'surveysettings.js', LSYii_ClientScript::POS_BEGIN);
App()->getClientScript()->registerPackage('jquery-json');
App()->getClientScript()->registerPackage('bootstrap-switch');

Expand Down
15 changes: 14 additions & 1 deletion application/models/Survey.php
Expand Up @@ -497,7 +497,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 @@ -1999,4 +2000,16 @@ public function getOwnerUserName()
return isset($this->owner["users_name"]) ? $this->owner["users_name"] : "";
}

/**
* 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 @@ -69,6 +69,10 @@
$('#".$entryData['name']."').on('submit.editLocalsettings', function(e){
e.preventDefault();
if (!validateSettingsForm($(this))) {
$('#in_survey_common').trigger('lsStopLoading'); // Remove the 'loading' state
return false;
}
var data = $(this).serializeArray();
var uri = $(this).attr('action');
$.ajax({
Expand Down
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
33 changes: 33 additions & 0 deletions assets/scripts/admin/surveysettings.js
@@ -0,0 +1,33 @@
/**
* 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.create(errorMessage, 'well-lg bg-danger');
return false;
}

0 comments on commit 53008de

Please sign in to comment.