|
|
- <?php
-
- $requestUrl = trim(str_replace($_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']), '?');
-
- define('CONFIG_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'config.php');
- define('DATA_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'data.php');
-
- if (file_exists(CONFIG_FILE)) require_once CONFIG_FILE;
- if (!isset($config)) $config = [];
-
- $supplier = $_REQUEST['supplier'];
-
- if (!isset($config[$supplier]))
- $config[$supplier] = [];
-
- $config[$supplier] = array_merge(
- [
- 'title' => '%supplier% <small>%event%</small>',
- 'description' => '',
- 'choices' => [],
- 'start' => 'now 00:00:00',
- 'end' => '+1 year',
- 'frequency' => '1 day',
-
- ],
- $config[$supplier]
- );
-
- $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
-
- $hasEvent = isset($_REQUEST['event']);
- if (!$hasEvent) {
- $now = new \DateTime('now');
- $current = new \DateTime($config[$supplier]['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');
- header(sprintf('Location: %s?supplier=%s&event=%s', $requestUrl, $supplier, $nextEvent));
- die();
- }
-
- $event = $_REQUEST['event'];
-
- switch ($action) {
- case 'insert' :
- case 'delete' :
- $isBeginning = (!file_exists(DATA_FILE) or in_array(filesize(DATA_FILE), [ false, 0 ]));
- $output = fopen(DATA_FILE, 'a+');
- if (!$output) break;
- if (!flock($output, LOCK_EX)) break;
- if ($isBeginning)
- fwrite($output, '<?php' . PHP_EOL);
- $item = [];
- foreach (['name', 'choice', 'action'] as $field)
- $item[$field] = $_REQUEST[$field];
- $item['timestamp'] = time();
- $item['hash'] = md5(implode([ $item['name'], $item['choice'], ]));
- fprintf(
- $output,
- '$data[%s][%s][] = %s;' . PHP_EOL,
- var_export($supplier, true),
- var_export($event, true),
- str_replace(PHP_EOL, '', var_export($item, true))
- );
- flock($output, LOCK_UN);
- fclose($output);
- header(sprintf('Location: %s?supplier=%s&event=%s', $requestUrl, $supplier, $event));
- die();
- }
-
- if (!isset($data)) $data = [];
- if (file_exists(DATA_FILE)) include DATA_FILE;
-
- $items = [];
-
- $allItems = isset($data[$supplier][$event]) ? $data[$supplier][$event] : [];
-
- usort($allItems, function ($a, $b) {
- $a = intval($a['timestamp']);
- $b = intval($b['timestamp']);
- if ($a === $b)
- return 0;
- return ($a < $b) ? -1 : 1;
- });
-
- foreach ($allItems as $item) {
- if ($item['action'] === 'insert') {
- $items[] = $item;
- } elseif ($item['action'] === 'delete') {
- foreach ($items as $index => $prevItem)
- if ($prevItem['hash'] === $item['hash'])
- unset($items[$index]);
- }
- }
-
- while (preg_match('/%([^%]+)%/i', $config[$supplier]['title'], $match))
- $config[$supplier]['title'] = str_replace(
- $match[0],
- ${$match[1]},
- $config[$supplier]['title']
- );
-
-
- ?><!DOCTYPE html>
- <html lang="fr">
- <head>
- <meta charset="UTF-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1" />
- <title><?php echo strip_tags($config[$supplier]['title']); ?></title>
- </head>
- <body>
- <h1><?php echo $config[$supplier]['title']; ?></h1>
- <?php if (!empty($config[$supplier]['description'])) : ?>
- <p><?php echo $config[$supplier]['description']; ?></p>
- <?php endif; ?>
- <ul>
- <?php foreach ($items as $item) : ?>
- <li>
- <form>
- <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
- <input type="hidden" name="event" value="<?php echo $event; ?>" />
- <input type="hidden" name="name" value="<?php echo $item['name']; ?>" />
- <input type="hidden" name="choice" value="<?php echo $item['choice']; ?>" />
- <?php echo $item['name']; ?>
- <?php if (!empty($item['choice'])) : ?>
- — <?php echo $item['choice']; ?>
- <?php endif; ?>
- <button type="submit" name="action" value="delete">supprimer</button>
- </form>
- </li>
- <?php endforeach; ?>
- <li>
- <form>
- <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
- <input type="hidden" name="event" value="<?php echo $event; ?>" />
- <input type="text" name="name" required placeholder="nom" />
- <?php if (empty($config[$supplier]['choices'])) : ?>
- <input type="hidden" name="choice" value="<?php echo $item['choice']; ?>" />
- <?php else : ?>
- <select name="choice" required placeholder="choix">
- <option/>
- <?php foreach ($config[$supplier]['choices'] as $choice) : ?>
- <option><?php echo $choice; ?></option>
- <?php endforeach; ?>
- </select>
- <?php endif; ?>
- <button type="submit" name="action" value="insert">ajouter</button>
- </form>
- </li>
- </ul>
- </body>
- </html>
|