From 020da64d984852943785a5cd8af272b8e68d20ec Mon Sep 17 00:00:00 2001 From: Gabriel Jenik Date: Thu, 10 Nov 2022 07:03:06 -0300 Subject: [PATCH] Fixed issue #15195: Expiration date can be set before start date (#2714) --- application/models/Survey.php | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/application/models/Survey.php b/application/models/Survey.php index df0d2f3419f..86e9cd5c7ee 100755 --- a/application/models/Survey.php +++ b/application/models/Survey.php @@ -397,14 +397,29 @@ public function expire($surveyId = null) $dateTime = dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i:s", Yii::app()->getConfig('timeadjust')); $dateTime = dateShift($dateTime, "Y-m-d H:i:s", '-1 minute'); - if (!isset($surveyId)) { - $this->expires = $dateTime; - if ($this->scenario == 'update') { - return $this->save(); - } - } else { - self::model()->updateByPk($surveyId, array('expires' => $dateTime)); + $model = $this; + + // Set model based on surveyId, if given + // If so, set scenario as to be saved later + if (isset($surveyId)) { + $model = self::model()->findByPk($surveyId); + $model->setScenario('update'); + } + + // Avoid setting expiration date before start date + // If there is a future start date set, set the expiration date to the same date + if (!empty($model->startdate) && $dateTime < $model->startdate) { + $dateTime = $model->startdate; } + + // Set expiration date + $model->expires = $dateTime; + + // Save if scenario is update + if ($model->scenario == 'update') { + return $model->save(); + } + return null; } @@ -534,7 +549,7 @@ public function rules() 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('expires', 'checkExpireAfterStart'), + array('expires', 'checkExpireAfterStart'), ); }