Browse Source

gère les exclusions de date

master
vince vince 2 years ago
parent
commit
794b7cf5e8
1 changed files with 29 additions and 12 deletions
  1. +29
    -12
      index.php

+ 29
- 12
index.php View File

@ -31,6 +31,29 @@ function generateUrl($supplier = null, $event = null) {
return sprintf('%s/%s/%s', $requestUrl, $supplier, $event); 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('CONFIG_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'config.php');
define('DATA_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'data.php'); define('DATA_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'data.php');
@ -138,25 +161,19 @@ try {
if (!$isConfig and $hasSupplier) { if (!$isConfig and $hasSupplier) {
$start = new \DateTime($config[$supplier]['start']); $start = new \DateTime($config[$supplier]['start']);
if (!$hasEvent) { if (!$hasEvent) {
$now = new \DateTime('now'); $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)); header('Location: ' . generateUrl($supplier, $nextEvent));
die(); die();
} else { } else {
$current = new \DateTimeImmutable($event); $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'); $previousEvent = $previous->format('Y-m-d');
if (false and !array_key_exists($previousEvent, $data[$supplier])) if (false and !array_key_exists($previousEvent, $data[$supplier]))
unset($previousEvent); unset($previousEvent);
$next = $current->add($frequency);
$next = findNext($current, $config[$supplier]['frequency'], $config[$supplier]['excludes']);
$nextEvent = $next->format('Y-m-d'); $nextEvent = $next->format('Y-m-d');
if (false and !array_key_exists($nextEvent, $data[$supplier])) if (false and !array_key_exists($nextEvent, $data[$supplier]))
unset($nextEvent); unset($nextEvent);
@ -336,7 +353,7 @@ if (!$isConfig and $hasSupplier) {
<label for="excludes" class="col-sm-2 col-form-label">Exceptions</label> <label for="excludes" class="col-sm-2 col-form-label">Exceptions</label>
<div class="col-sm-10"> <div class="col-sm-10">
<textarea class="form-control" name="excludes" rows="5"><?php echo implode(PHP_EOL, $config[$supplier]['excludes']); ?></textarea> <textarea class="form-control" name="excludes" rows="5"><?php echo implode(PHP_EOL, $config[$supplier]['excludes']); ?></textarea>
<div class="form-text">Les dates à exclure. Une par ligne. Ou pas.</div>
<div class="form-text">Les dates à exclure. Une par ligne. Ou pas. En tous cas le format c'est celui de l'<a href="https://fr.wikipedia.org/wiki/ISO_8601" target="_blank">ISO 8601</a> : <kbd>AAAA-MM-JJ</kbd></div>
</div> </div>
</div> </div>


Loading…
Cancel
Save