|
|
- <?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 = [];
-
- $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
-
- $hasSupplier = isset($_REQUEST['supplier']) and !empty($_REQUEST['supplier']);
- $supplier = $_REQUEST['supplier'];
-
- if ($hasSupplier) {
-
- if (!isset($config[$supplier]))
- $config[$supplier] = [];
-
- $config[$supplier] = array_merge(
- [
- 'title' => '%supplier% <small>%date%</small>',
- 'description' => '',
- 'choices' => [],
- 'start' => 'now 00:00:00',
- 'frequency' => '1 day',
- 'password' => '',
-
- ],
- $config[$supplier]
- );
-
- $hasPassword = !empty($config[$supplier]['password']);
-
- if ($action === 'config') {
- if ($hasPassword) {
- if (!isset($_SERVER['PHP_AUTH_USER'])) {
- header(sprintf('WWW-Authenticate: Basic realm="mon-panier-bio config %s"', $supplier));
- header('HTTP/1.0 401 Unauthorized');
- printf('Cette config est protégée par mot de passe !');
- exit;
- } elseif (
- ($_SERVER['PHP_AUTH_USER'] !== $supplier)
- or ($_SERVER['PHP_AUTH_PW'] !== $config[$supplier]['password'])
- ) {
- header('HTTP/1.0 403 Forbidden');
- printf('Cette config est protégée par mot de passe !');
- exit;
- }
- }
-
- foreach (array_keys($config[$supplier]) as $key)
- if (isset($_REQUEST[$key]))
- $config[$supplier][$key] = $_REQUEST[$key];
- }
-
- if (empty($config[$supplier]['start']))
- $config[$supplier]['start'] = 'now 00:00:00';
-
-
- if (is_string($config[$supplier]['choices']))
- $config[$supplier]['choices'] = explode(PHP_EOL, $config[$supplier]['choices']);
-
- if (!is_array($config[$supplier]['choices']))
- $config[$supplier]['choices'] = [];
-
- $config[$supplier]['choices'] = array_filter(
- $config[$supplier]['choices'],
- function ($choice) {
- return is_string($choice) and !empty(trim($choice));
- }
- );
-
- $config[$supplier]['choices'] = array_map('trim', $config[$supplier]['choices']);
- }
-
- $isConfig = false;
- if ($action === 'config') {
- $output = fopen(CONFIG_FILE, 'w+');
- if ($output) {
- if (flock($output, LOCK_EX)) {
- fwrite($output, '<?php' . PHP_EOL);
- fprintf(
- $output,
- '$config = %s;' . PHP_EOL,
- var_export($config, true)
- );
- flock($output, LOCK_UN);
- }
- fclose($output);
- }
- $isConfig = true;
- }
-
- $hasEvent = isset($_REQUEST['event']);
- if (!$isConfig and $hasSupplier) {
- 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]);
- }
- }
-
- $date = (new \IntlDateFormatter('fr_FR.UTF8', \IntlDateFormatter::FULL, \IntlDateFormatter::NONE, 'Europe/Paris'))->format(new \DateTime($event));
-
- while (preg_match('/%([^%]+)%/i', $config[$supplier]['title'], $match))
- $config[$supplier]['title'] = str_replace(
- $match[0],
- ${$match[1]},
- $config[$supplier]['title']
- );
-
- $stats = [];
- foreach ($items as $item)
- if (!empty($item['choice']))
- $stats[$item['choice']] += 1;
-
- }
-
- ?><!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>
- <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
- </head>
- <body>
- <header>
- <nav class="navbar navbar-dark bg-dark">
- <div class="container-fluid">
- <a class="navbar-brand" href="<?php echo $hasSupplier ? sprintf('%s?supplier=%s', $requestUrl, $supplier) : $requestUrl; ?>">
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="bi bi-basket d-inline-block align-text-top" viewBox="0 0 16 16">
- <path d="M5.757 1.071a.5.5 0 0 1 .172.686L3.383 6h9.234L10.07 1.757a.5.5 0 1 1 .858-.514L13.783 6H15a1 1 0 0 1 1 1v1a1 1 0 0 1-1 1v4.5a2.5 2.5 0 0 1-2.5 2.5h-9A2.5 2.5 0 0 1 1 13.5V9a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h1.217L5.07 1.243a.5.5 0 0 1 .686-.172zM2 9v4.5A1.5 1.5 0 0 0 3.5 15h9a1.5 1.5 0 0 0 1.5-1.5V9H2zM1 7v1h14V7H1zm3 3a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3A.5.5 0 0 1 4 10zm2 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3A.5.5 0 0 1 6 10zm2 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3A.5.5 0 0 1 8 10zm2 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3a.5.5 0 0 1 .5-.5zm2 0a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-1 0v-3a.5.5 0 0 1 .5-.5z"/>
- </svg>
- <?php echo $hasSupplier ? $supplier : 'Mon panier bio'; ?>
- </a>
- <?php if ($hasSupplier) : ?>
- <span class="navbar-text text-muted">
- <?php if ($isConfig) : ?>
- <a class="text-reset" href="<?php printf('%s?supplier=%s', $requestUrl, $supplier); ?>">Retour</a>
- <?php else : ?>
- <?php if ($hasPassword) : ?>
- <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-lock" viewBox="0 0 16 16">
- <path d="M8 1a2 2 0 0 1 2 2v4H6V3a2 2 0 0 1 2-2zm3 6V3a3 3 0 0 0-6 0v4a2 2 0 0 0-2 2v5a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2V9a2 2 0 0 0-2-2zM5 8h6a1 1 0 0 1 1 1v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V9a1 1 0 0 1 1-1z"/>
- </svg>
- <?php else : ?>
- <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-unlock" viewBox="0 0 16 16">
- <path d="M11 1a2 2 0 0 0-2 2v4a2 2 0 0 1 2 2v5a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V9a2 2 0 0 1 2-2h5V3a3 3 0 0 1 6 0v4a.5.5 0 0 1-1 0V3a2 2 0 0 0-2-2zM3 8a1 1 0 0 0-1 1v5a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V9a1 1 0 0 0-1-1H3z"/>
- </svg>
- <?php endif; ?>
- <a tabindex="-1" class="text-reset" href="<?php printf('%s?supplier=%s&action=config', $requestUrl, $supplier); ?>">Configuration</a>
- <?php endif; ?>
- </span>
- <?php endif; ?>
- </div>
- </nav>
- </header>
- <main>
- <?php if (!$hasSupplier) : ?>
- <section class="container-fluid">
- <div class="row my-3">
- <div class="col">
- <div class="alert alert-danger" role="alert">
- Pas de fournisseur !
- </div>
- </div>
- </div>
- </section>
- <?php else : ?>
- <?php if ($isConfig) : ?>
- <section class="container-fluid">
- <div class="row g-3">
- <div class="col">
- <h1>Configuration</h1>
- </div>
- </div>
- </section>
- <section class="container-fluid">
- <div class="row g-3">
- <form action="<?php printf('%s?supplier=%s', $requestUrl, $supplier); ?>" method="post">
- <div class="row mb-3">
- <label for="title" class="col-sm-2 col-form-label">Titre</label>
- <div class="col-sm-10">
- <input class="form-control" type="text" name="title" value="<?php echo htmlspecialchars($config[$supplier]['title']); ?>" />
- <div class="form-text">Le titre de la page. Où <kbd>%supplier%</kbd> est le fournisseur et <kbd>%date%</kbd> la date de l'événement.</div>
- </div>
- </div>
- <div class="row mb-3">
- <label for="description" class="col-sm-2 col-form-label">Description</label>
- <div class="col-sm-10">
- <textarea class="form-control js-ckeditor" name="description" rows="10"><?php echo $config[$supplier]['description']; ?></textarea>
- <div class="form-text">La description affichée sous le titre.</div>
- </div>
- </div>
- <div class="row mb-3">
- <label for="choices" class="col-sm-2 col-form-label">Choix</label>
- <div class="col-sm-10">
- <textarea class="form-control" name="choices" rows="5"><?php echo implode(PHP_EOL, $config[$supplier]['choices']); ?></textarea>
- <div class="form-text">Les différents choix possibles. Un par ligne. Ou pas.</div>
- </div>
- </div>
- <div class="row mb-3">
- <label for="start" class="col-sm-2 col-form-label">Début</label>
- <div class="col-sm-10">
- <input class="form-control" type="date" name="start" value="<?php echo $config[$supplier]['start']; ?>" />
- <div class="form-text">La date du premier événement, si nécessaire de le préciser.</div>
- </div>
- </div>
- <div class="row mb-3">
- <label for="frequency" class="col-sm-2 col-form-label">Fréquence</label>
- <div class="col-sm-10">
- <input class="form-control" type="text" name="frequency" value="<?php echo $config[$supplier]['frequency']; ?>" />
- <div class="form-text">La fréquence des événements dans le format <a class="text-reset" href="https://www.php.net/manual/fr/datetime.formats.relative.php" target="_blank">décrit sur cette page</a>.</div>
- </div>
- </div>
- <div class="row mb-3">
- <label for="password" class="col-sm-2 col-form-label">Mot de passe</label>
- <div class="col-sm-10">
- <input class="form-control" type="text" name="password" value="<?php echo $config[$supplier]['password']; ?>" />
- <div class="form-text">Ce mot de passe sera demandé pour accéder à la configuration la prochaine fois. Le nom d'utilisateur est le fournisseur courant (en l'occurrence <kbd><?php echo $supplier; ?></kbd>).</div>
- </div>
- </div>
- <div class="row">
- <div class="col mb-3">
- <button class="btn btn-primary" type="submit" name="action" value="config">Enregistrer</button>
- </div>
- </div>
- </form>
- </div>
- </section>
- <?php else : ?>
- <section class="container-fluid">
- <div class="row my-3">
- <div class="col">
- <h1><?php echo $config[$supplier]['title']; ?></h1>
- <?php if (!empty($config[$supplier]['description'])) : ?>
- <p class="lead"><?php echo $config[$supplier]['description']; ?></p>
- <?php endif; ?>
- </div>
- </div>
- </section>
- <section class="container-fluid">
- <div class="row g-3">
- <form action="<?php printf('%s?supplier=%s', $requestUrl, $supplier); ?>" method="post">
- <div class="row mb-3">
- <label for="title" class="col-sm-2 col-form-label">Nom</label>
- <div class="col-sm-10">
- <input class="form-control" type="text" name="name" required placeholder="Nom" />
- </div>
- </div>
- <?php if (!empty($config[$supplier]['choices'])) : ?>
- <div class="row mb-3">
- <label for="title" class="col-sm-2 col-form-label">Choix</label>
- <div class="col-sm-10">
- <select class="form-select" name="choice" required>
- <option/>
- <?php foreach ($config[$supplier]['choices'] as $choice) : ?>
- <option><?php echo $choice; ?></option>
- <?php endforeach; ?>
- </select>
- </div>
- </div>
- <?php endif; ?>
- <div class="row">
- <div class="col mb-3">
- <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
- <input type="hidden" name="event" value="<?php echo $event; ?>" />
- <?php if (empty($config[$supplier]['choices'])) : ?>
- <input type="hidden" name="choice" value="" />
- <?php endif; ?>
- <button class="btn btn-primary" type="submit" name="action" value="insert">Commander</button>
- </div>
- </div>
- </form>
- </div>
- </section>
- <section class="container-fluid">
- <div class="row my-3">
- <div class="col">
- <div class="table-responsive">
- <table class="table table-striped table-hover align-middle">
- <thead>
- <tr>
- <th scope="col">
- Nom
- </th>
- <?php if (!empty($config[$supplier]['choices'])) : ?>
- <th scope="col">
- Choix
- </th>
- <?php endif; ?>
- <th scope="col">
-
- </th>
- </tr>
- </thead>
- <tbody>
- <?php foreach ($items as $item) : ?>
- <tr>
- <td>
- <?php echo $item['name']; ?>
- </td>
- <?php if (!empty($config[$supplier]['choices'])) : ?>
- <td>
- <?php if (!empty($item['choice'])) : ?>
- <?php echo $item['choice']; ?>
- <?php endif; ?>
- </td>
- <?php endif; ?>
- <td>
- <form onsubmit="return confirm('Souhaitez-vous vraiment annuler cette commande ?');">
- <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']; ?>" />
- <button class="btn btn-secondary float-end" type="submit" name="action" value="delete">Annuler</button>
- </form>
- </td>
- </tr>
- <?php endforeach; ?>
- </tbody>
- <caption>
- Commandes <span class="badge bg-primary rounded-pill"><?php echo count($items); ?></span>
- <?php foreach ($stats as $choice => $count) : ?>
- /
- <?php echo $choice; ?> <span class="badge bg-secondary rounded-pill"><?php echo $count; ?></span>
- <?php endforeach; ?>
- </caption>
- </table>
- </div>
- </div>
- </div>
- </section>
- <?php endif; ?>
- <?php endif; ?>
- </main>
- <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
- <?php if ($isConfig) : ?>
- <script src="https://cdn.ckeditor.com/ckeditor5/31.0.0/classic/ckeditor.js"></script>
- <script>
- document.querySelectorAll('.js-ckeditor').forEach(function (element) {
- ClassicEditor.create(element).catch(error => { console.error(error); });
- });
- </script>
- <?php endif; ?>
- </body>
- </html>
|