From 794b7cf5e880f748ca5119d0b03315782a731b03 Mon Sep 17 00:00:00 2001 From: vince Date: Fri, 24 Dec 2021 17:34:21 +0100 Subject: [PATCH] =?UTF-8?q?g=C3=A8re=20les=20exclusions=20de=20date?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.php | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/index.php b/index.php index dc689cc..15c099d 100644 --- a/index.php +++ b/index.php @@ -31,6 +31,29 @@ function generateUrl($supplier = null, $event = null) { return sprintf('%s/%s/%s', $requestUrl, $supplier, $event); } +function findNext($start, $frequency, $excludes = [], $maxIterations = 1000i, $direction = +1) { + $current = clone $start; + $frequency = \DateInterval::createFromDateString($frequency); + do { + while ( + ($current->getTimestamp() < $now->getTimestamp()) + and ($maxIterations-- > 0) + ) { + if ($direction === abs($direction)) $current->add($frequency); + else $current->sub($frequency); + } + $nextEvent = $current->format('Y-m-d'); + } while ( + in_array($nextEvent, $excludes) + and ($maxIterations > 0) + ); + return $current; +} + +function findPrevious($start, $frequency, $excludes = [], $maxIterations = 1000) { + return findNext($start, $frequency, $excludes, $maxIterations, -1) { +} + define('CONFIG_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'config.php'); define('DATA_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'data.php'); @@ -138,25 +161,19 @@ try { if (!$isConfig and $hasSupplier) { $start = new \DateTime($config[$supplier]['start']); if (!$hasEvent) { + $now = new \DateTime('now'); - $current = clone $start; - $frequency = \DateInterval::createFromDateString($config[$supplier]['frequency']); - $maxIterations = 1000; - while ( - ($current->getTimestamp() < $now->getTimestamp()) - and ($maxIterations-- > 0) - ) $current->add($frequency); - $nextEvent = $current->format('Y-m-d'); + $next = findNext($start, $config[$supplier]['frequency'], $config[$supplier]['excludes']); + $nextEvent = $next->format('Y-m-d'); header('Location: ' . generateUrl($supplier, $nextEvent)); die(); } else { $current = new \DateTimeImmutable($event); - $frequency = \DateInterval::createFromDateString($config[$supplier]['frequency']); - $previous = $current->sub($frequency); + $previous = findPrevious($current, $config[$supplier]['frequency'], $config[$supplier]['excludes']); $previousEvent = $previous->format('Y-m-d'); if (false and !array_key_exists($previousEvent, $data[$supplier])) unset($previousEvent); - $next = $current->add($frequency); + $next = findNext($current, $config[$supplier]['frequency'], $config[$supplier]['excludes']); $nextEvent = $next->format('Y-m-d'); if (false and !array_key_exists($nextEvent, $data[$supplier])) unset($nextEvent); @@ -336,7 +353,7 @@ if (!$isConfig and $hasSupplier) {
-
Les dates à exclure. Une par ligne. Ou pas.
+
Les dates à exclure. Une par ligne. Ou pas. En tous cas le format c'est celui de l'ISO 8601 : AAAA-MM-JJ