Refactor calculating max/min expiration date and enforce on poll edition
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
parent
9c969f8896
commit
5a5c233a5e
@ -137,9 +137,9 @@ if (isset($_POST['update_poll_info'])) {
|
||||
break;
|
||||
}
|
||||
} elseif ($field === 'expiration_date') {
|
||||
$expiration_date = $inputService->filterDate($_POST['expiration_date']);
|
||||
$expiration_date = $inputService->validateDate($_POST['expiration_date'], $pollService->minExpiryDate(), $pollService->maxExpiryDate());
|
||||
if ($expiration_date) {
|
||||
$poll->end_date = $expiration_date;
|
||||
$poll->end_date = $expiration_date->getTimestamp();
|
||||
$updated = true;
|
||||
}
|
||||
} elseif ($field === 'name') {
|
||||
|
@ -120,9 +120,22 @@ class InputService {
|
||||
return $this->returnIfNotBlank($comment);
|
||||
}
|
||||
|
||||
public function filterDate($date) {
|
||||
$dDate = DateTime::createFromFormat(__('Date', 'datetime_parseformat'), $date)->setTime(0, 0, 0);
|
||||
return $dDate->format('Y-m-d H:i:s');
|
||||
public function validateDate(string $date, DateTime $maxDate, DateTime $minDate): DateTime {
|
||||
$dDate = $this->parseDate($date);
|
||||
if (!$dDate) return $maxDate;
|
||||
if ($dDate < $minDate) {
|
||||
return $minDate;
|
||||
} elseif ($maxDate < $dDate) {
|
||||
return $maxDate;
|
||||
}
|
||||
return $dDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return DateTime|false
|
||||
*/
|
||||
private function parseDate(string $date) {
|
||||
return DateTime::createFromFormat(__('Date', 'datetime_parseformat'), $date)->setTime(0, 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -18,6 +18,8 @@
|
||||
*/
|
||||
namespace Framadate\Services;
|
||||
|
||||
use DateInterval;
|
||||
use DateTime;
|
||||
use Framadate\Exception\AlreadyExistsException;
|
||||
use Framadate\Exception\ConcurrentEditionException;
|
||||
use Framadate\Exception\ConcurrentVoteException;
|
||||
@ -232,18 +234,18 @@ class PollService {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int The max timestamp allowed for expiry date
|
||||
* @return DateTime The max date allowed for expiry date
|
||||
*/
|
||||
public function maxExpiryDate() {
|
||||
public function maxExpiryDate(): DateTime {
|
||||
global $config;
|
||||
return time() + (86400 * $config['default_poll_duration']);
|
||||
return (new DateTime())->add(new DateInterval('P' . $config['default_poll_duration'] . 'D'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int The min timestamp allowed for expiry date
|
||||
* @return DateTime The min date allowed for expiry date
|
||||
*/
|
||||
public function minExpiryDate() {
|
||||
return time() + 86400;
|
||||
return (new DateTime())->add(new DateInterval('P1D'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -17,6 +17,7 @@
|
||||
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
|
||||
*/
|
||||
use Framadate\Choice;
|
||||
use Framadate\Services\InputService;
|
||||
use Framadate\Services\LogService;
|
||||
use Framadate\Services\MailService;
|
||||
use Framadate\Services\PollService;
|
||||
@ -33,6 +34,7 @@ $pollService = new PollService($connect, $logService);
|
||||
$mailService = new MailService($config['use_smtp'], $config['smtp_options']);
|
||||
$purgeService = new PurgeService($connect, $logService);
|
||||
$sessionService = new SessionService();
|
||||
$inputService = new InputService();
|
||||
|
||||
if (is_file('bandeaux_local.php')) {
|
||||
include_once('bandeaux_local.php');
|
||||
@ -49,10 +51,6 @@ if (empty($form->title) || empty($form->admin_name) || (($config['use_smtp']) ?
|
||||
$smarty->display('error.tpl');
|
||||
exit;
|
||||
}
|
||||
// Min/Max archive date
|
||||
$min_expiry_time = $pollService->minExpiryDate();
|
||||
$max_expiry_time = $pollService->maxExpiryDate();
|
||||
|
||||
// The poll format is other (A) if we are in this file
|
||||
if (!isset($form->format)) {
|
||||
$form->format = 'A';
|
||||
@ -66,28 +64,8 @@ if (empty($form->title) || empty($form->admin_name) || (($config['use_smtp']) ?
|
||||
// Step 4 : Data prepare before insert in DB
|
||||
if (isset($_POST['confirmation'])) {
|
||||
// Define expiration date
|
||||
$enddate = filter_input(INPUT_POST, 'enddate', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '#^[0-9]{2}/[0-9]{2}/[0-9]{4}$#']]);
|
||||
|
||||
if (!empty($enddate)) {
|
||||
$registredate = explode('/', $enddate);
|
||||
|
||||
if (is_array($registredate) && count($registredate) === 3) {
|
||||
$time = mktime(0, 0, 0, $registredate[1], $registredate[0], $registredate[2]);
|
||||
|
||||
if ($time < $min_expiry_time) {
|
||||
$form->end_date = $min_expiry_time;
|
||||
} elseif ($max_expiry_time < $time) {
|
||||
$form->end_date = $max_expiry_time;
|
||||
} else {
|
||||
$form->end_date = $time;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($form->end_date)) {
|
||||
// By default, expiration date is 6 months after last day
|
||||
$form->end_date = $max_expiry_time;
|
||||
}
|
||||
$expiration_date = $inputService->validateDate($_POST['expiration_date'], $pollService->minExpiryDate(), $pollService->maxExpiryDate());
|
||||
$form->end_date = $expiration_date->getTimestamp();
|
||||
|
||||
// Insert poll in database
|
||||
$ids = $pollService->createPoll($form);
|
||||
@ -137,7 +115,7 @@ if (empty($form->title) || empty($form->admin_name) || (($config['use_smtp']) ?
|
||||
}
|
||||
|
||||
// Expiration date is initialised with config parameter. Value will be modified in step 4 if user has defined an other date
|
||||
$form->end_date = $max_expiry_time;
|
||||
$form->end_date = $pollService->maxExpiryDate()->format('Y-m-d H:i:s');
|
||||
|
||||
// Summary
|
||||
$summary = '<ol>';
|
||||
@ -163,7 +141,7 @@ if (empty($form->title) || empty($form->admin_name) || (($config['use_smtp']) ?
|
||||
}
|
||||
$summary .= '</ol>';
|
||||
|
||||
$end_date_str = utf8_encode(strftime($date_format['txt_date'], $max_expiry_time)); //textual date
|
||||
$end_date_str = utf8_encode(strftime($date_format['txt_date'], $pollService->maxExpiryDate()->getTimestamp())); //textual date
|
||||
|
||||
$_SESSION['form'] = serialize($form);
|
||||
|
||||
|
@ -40,10 +40,6 @@ if (is_readable('bandeaux_local.php')) {
|
||||
include_once('bandeaux_local.php');
|
||||
}
|
||||
|
||||
// Min/Max archive date
|
||||
$min_expiry_time = $pollService->minExpiryDate();
|
||||
$max_expiry_time = $pollService->maxExpiryDate();
|
||||
|
||||
$form = unserialize($_SESSION['form']);
|
||||
|
||||
// The poll format is DATE if we are in this file
|
||||
@ -175,7 +171,7 @@ switch ($step) {
|
||||
}
|
||||
$summary .= '</ul>';
|
||||
|
||||
$end_date_str = utf8_encode(strftime($date_format['txt_date'], $max_expiry_time)); // textual date
|
||||
$end_date_str = utf8_encode(strftime($date_format['txt_date'], $pollService->maxExpiryDate()->getTimestamp())); // textual date
|
||||
|
||||
$_SESSION['form'] = serialize($form);
|
||||
|
||||
@ -192,28 +188,8 @@ switch ($step) {
|
||||
// Step 4 : Data prepare before insert in DB
|
||||
|
||||
// Define expiration date
|
||||
$enddate = filter_input(INPUT_POST, 'enddate', FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '#^[0-9]{2}/[0-9]{2}/[0-9]{4}$#']]);
|
||||
|
||||
if (!empty($enddate)) {
|
||||
$registredate = explode('/', $enddate);
|
||||
|
||||
if (is_array($registredate) && count($registredate) === 3) {
|
||||
$time = mktime(0, 0, 0, $registredate[1], $registredate[0], $registredate[2]);
|
||||
|
||||
if ($time < $min_expiry_time) {
|
||||
$form->end_date = $min_expiry_time;
|
||||
} elseif ($max_expiry_time < $time) {
|
||||
$form->end_date = $max_expiry_time;
|
||||
} else {
|
||||
$form->end_date = $time;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($form->end_date)) {
|
||||
// By default, expiration date is 6 months after last day
|
||||
$form->end_date = $max_expiry_time;
|
||||
}
|
||||
$expiration_date = $inputService->validateDate($_POST['enddate'], $pollService->minExpiryDate(), $pollService->maxExpiryDate());
|
||||
$form->end_date = $expiration_date->getTimestamp();
|
||||
|
||||
// Insert poll in database
|
||||
$ids = $pollService->createPoll($form);
|
||||
|
Loading…
Reference in New Issue
Block a user