Plateforme web de commande de panier bio
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

557 lines
22 KiB

2 years ago
2 years ago
2 years ago
  1. <?php
  2. define('DEFAULT_TITLE', 'Mon panier bio');
  3. define('REQUEST_REGEX', '/^\/(?<supplier>[^\/]+)\/?(?<event>[^\/]+)?\/?$/');
  4. define('SUPPLIER_REGEX', '/^[A-Za-z]\w{0,31}$/');
  5. define('EVENT_REGEX', '/^\d{4}\-[01]\d\-[0123]\d$/');
  6. define('ACTION_REGEX', '/^[a-z]{1,16}$/i');
  7. $requestUrl = trim(str_replace($_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']), '?');
  8. if (preg_match(REQUEST_REGEX, $requestUrl, $match)) {
  9. $requestSupplier = array_key_exists('supplier', $match) ? $match['supplier'] : null;
  10. $requestEvent = array_key_exists('event', $match) ? $match['event'] : null;
  11. if (!is_null($requestEvent))
  12. $requestUrl = rtrim(str_replace($requestEvent, '', $requestUrl), '/');
  13. if (!is_null($requestSupplier))
  14. $requestUrl = rtrim(str_replace($requestSupplier, '', $requestUrl), '/');
  15. }
  16. function generateUrl($supplier = null, $event = null) {
  17. global $requestUrl;
  18. if (is_null($supplier))
  19. return $requestUrl;
  20. if (is_null($event))
  21. return sprintf('%s/%s', $requestUrl, $supplier);
  22. return sprintf('%s/%s/%s', $requestUrl, $supplier, $event);
  23. }
  24. function findNext($start, $frequency, $excludes = [], $vsNow = true, $maxIterations = 1000, $direction = +1) {
  25. $now = new \DateTime('now');
  26. $current = clone $start;
  27. $frequency = \DateInterval::createFromDateString($frequency);
  28. do {
  29. if ($direction === abs($direction)) {
  30. if (!$vsNow and ($maxIterations-- > 0)) {
  31. $current->add($frequency);
  32. } else {
  33. while (
  34. ($current->getTimestamp() < $now->getTimestamp())
  35. and ($maxIterations-- > 0)
  36. ) $current->add($frequency);
  37. }
  38. } else {
  39. if (!$vsNow and ($maxIterations-- > 0)) {
  40. $current->sub($frequency);
  41. } else {
  42. while (
  43. ($current->getTimestamp() > $now->getTimestamp())
  44. and ($maxIterations-- > 0)
  45. ) $current->sub($frequency);
  46. }
  47. }
  48. $nextEvent = $current->format('Y-m-d');
  49. } while (
  50. in_array($nextEvent, $excludes)
  51. and ($maxIterations > 0)
  52. );
  53. return $current;
  54. }
  55. function findPrevious($start, $frequency, $excludes = [], $vsNow = true, $maxIterations = 1000) {
  56. return findNext($start, $frequency, $excludes, $vsNow, $maxIterations, -1);
  57. }
  58. define('CONFIG_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'config.php');
  59. define('DATA_FILE', __DIR__ . DIRECTORY_SEPARATOR . 'data.php');
  60. if (file_exists(CONFIG_FILE)) require_once CONFIG_FILE;
  61. if (!isset($config)) $config = [];
  62. $action = (isset($_REQUEST['action']) and preg_match(ACTION_REGEX, $_REQUEST['action'])) ? $_REQUEST['action'] : null;
  63. $supplier = array_key_exists('supplier', $_REQUEST) ? $_REQUEST['supplier'] : $requestSupplier;
  64. $hasSupplier = is_string($supplier) and preg_match(SUPPLIER_REGEX, $supplier);
  65. if ($hasSupplier) {
  66. if (!isset($config[$supplier]))
  67. $config[$supplier] = [];
  68. $config[$supplier] = array_merge(
  69. [
  70. 'title' => '',
  71. 'subtitle' => '<small class="text-muted text-nowrap d-block d-sm-inline">%date%</small>',
  72. 'description' => '',
  73. 'choices' => [],
  74. 'start' => 'now 00:00:00',
  75. 'frequency' => '1 day',
  76. 'password' => '',
  77. 'excludes' => [],
  78. ],
  79. $config[$supplier]
  80. );
  81. $hasPassword = !empty($config[$supplier]['password']);
  82. if ($action === 'config') {
  83. if ($hasPassword) {
  84. if (!isset($_SERVER['PHP_AUTH_USER'])) {
  85. header(sprintf('WWW-Authenticate: Basic realm="Configuration de mon panier bio pour %s"', $supplier));
  86. header('HTTP/1.0 401 Unauthorized');
  87. printf('Cette configuration est protégée par mot de passe !');
  88. exit;
  89. } elseif (
  90. ($_SERVER['PHP_AUTH_USER'] !== $supplier)
  91. or ($_SERVER['PHP_AUTH_PW'] !== $config[$supplier]['password'])
  92. ) {
  93. header('HTTP/1.0 403 Forbidden');
  94. printf('Cette configuration est protégée par mot de passe !');
  95. exit;
  96. }
  97. }
  98. foreach (array_keys($config[$supplier]) as $key)
  99. if (isset($_REQUEST[$key]))
  100. $config[$supplier][$key] = (!in_array($key, ['title', 'subtitle', 'description']) ? filter_var($_REQUEST[$key], FILTER_SANITIZE_STRING) : $_REQUEST[$key]);
  101. }
  102. if (empty($config[$supplier]['start']))
  103. $config[$supplier]['start'] = 'now 00:00:00';
  104. foreach (['choices', 'excludes'] as $key) {
  105. if (is_string($config[$supplier][$key]))
  106. $config[$supplier][$key] = explode(PHP_EOL, $config[$supplier][$key]);
  107. if (!is_array($config[$supplier][$key]))
  108. $config[$supplier][$key] = [];
  109. $config[$supplier][$key] = array_filter(
  110. $config[$supplier][$key],
  111. function ($choice) {
  112. return is_string($choice) and !empty(trim($choice));
  113. }
  114. );
  115. $config[$supplier][$key] = array_map('trim', $config[$supplier][$key]);
  116. }
  117. }
  118. $isConfig = false;
  119. if ($action === 'config') {
  120. $output = fopen(CONFIG_FILE, 'w+');
  121. if ($output) {
  122. if (flock($output, LOCK_EX)) {
  123. fwrite($output, '<?php' . PHP_EOL);
  124. fprintf(
  125. $output,
  126. '$config = %s;' . PHP_EOL,
  127. var_export($config, true)
  128. );
  129. flock($output, LOCK_UN);
  130. }
  131. fclose($output);
  132. }
  133. $isConfig = true;
  134. }
  135. try {
  136. $event = array_key_exists('event', $_REQUEST) ? $_REQUEST['event'] : $requestEvent;
  137. $hasEvent = (
  138. is_string($event)
  139. and preg_match(EVENT_REGEX, $event)
  140. and ((new \DateTimeImmutable($event)) instanceof \DateTimeImmutable)
  141. );
  142. } catch (\Exception $exception) {
  143. $hasEvent = false;
  144. }
  145. if (!$isConfig and $hasSupplier) {
  146. $start = new \DateTime($config[$supplier]['start']);
  147. if (!$hasEvent) {
  148. $next = findNext($start, $config[$supplier]['frequency'], $config[$supplier]['excludes'], true);
  149. $nextEvent = $next->format('Y-m-d');
  150. header('Location: ' . generateUrl($supplier, $nextEvent));
  151. die();
  152. } else {
  153. $current = new \DateTime($event);
  154. $previous = findPrevious($current, $config[$supplier]['frequency'], $config[$supplier]['excludes'], false);
  155. $previousEvent = $previous->format('Y-m-d');
  156. if (false and !array_key_exists($previousEvent, $data[$supplier]))
  157. unset($previousEvent);
  158. $next = findNext($current, $config[$supplier]['frequency'], $config[$supplier]['excludes'], false);
  159. $nextEvent = $next->format('Y-m-d');
  160. if (false and !array_key_exists($nextEvent, $data[$supplier]))
  161. unset($nextEvent);
  162. }
  163. switch ($action) {
  164. case 'insert' :
  165. case 'delete' :
  166. $isBeginning = (!file_exists(DATA_FILE) or in_array(filesize(DATA_FILE), [ false, 0 ]));
  167. $output = fopen(DATA_FILE, 'a+');
  168. if (!$output) break;
  169. if (!flock($output, LOCK_EX)) break;
  170. if ($isBeginning)
  171. fwrite($output, '<?php' . PHP_EOL);
  172. $item = [];
  173. foreach (['name', 'choice', 'action'] as $field)
  174. $item[$field] = filter_var($_REQUEST[$field], FILTER_SANITIZE_STRING);
  175. $item['timestamp'] = time();
  176. $item['hash'] = md5(implode([ $item['name'], $item['choice'], ]));
  177. fprintf(
  178. $output,
  179. '$data[%s][%s][] = %s;' . PHP_EOL,
  180. var_export($supplier, true),
  181. var_export($event, true),
  182. str_replace(PHP_EOL, '', var_export($item, true))
  183. );
  184. flock($output, LOCK_UN);
  185. fclose($output);
  186. header('Location: ' . generateUrl($supplier, $event));
  187. die();
  188. }
  189. if (!isset($data)) $data = [];
  190. if (file_exists(DATA_FILE)) include DATA_FILE;
  191. $items = [];
  192. $allItems = isset($data[$supplier][$event]) ? $data[$supplier][$event] : [];
  193. usort($allItems, function ($a, $b) {
  194. $a = intval($a['timestamp']);
  195. $b = intval($b['timestamp']);
  196. if ($a === $b)
  197. return 0;
  198. return ($a < $b) ? -1 : 1;
  199. });
  200. foreach ($allItems as $item) {
  201. if ($item['action'] === 'insert') {
  202. $items[] = $item;
  203. } elseif ($item['action'] === 'delete') {
  204. foreach ($items as $index => $prevItem)
  205. if ($prevItem['hash'] === $item['hash'])
  206. unset($items[$index]);
  207. }
  208. }
  209. $date = (new \IntlDateFormatter('fr_FR.UTF8', \IntlDateFormatter::FULL, \IntlDateFormatter::NONE, 'Europe/Paris'))->format(new \DateTime($event));
  210. foreach (['title', 'subtitle', 'description'] as $key) {
  211. while (preg_match('/%([^%]+)%/i', $config[$supplier][$key], $match))
  212. $config[$supplier][$key] = str_replace(
  213. $match[0],
  214. ${$match[1]},
  215. $config[$supplier][$key]
  216. );
  217. }
  218. if (empty($config[$supplier]['title']))
  219. $config[$supplier]['title'] = $supplier;
  220. $stats = [];
  221. foreach ($items as $item)
  222. if (!empty($item['choice']))
  223. $stats[$item['choice']] += 1;
  224. }
  225. ?><!DOCTYPE html>
  226. <html lang="fr">
  227. <head>
  228. <meta charset="UTF-8" />
  229. <meta name="viewport" content="width=device-width, initial-scale=1" />
  230. <title><?php if ($hasSupplier) : ?><?php echo strip_tags($config[$supplier]['title']); ?><?php if (!$isConfig) : ?> — <?php echo strip_tags($config[$supplier]['subtitle']); ?><?php endif; ?><?php else : ?><?php echo DEFAULT_TITLE; ?><?php endif; ?></title>
  231. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  232. </head>
  233. <body>
  234. <header>
  235. <nav class="navbar navbar-dark bg-dark">
  236. <div class="container-fluid">
  237. <a class="navbar-brand" href="<?php echo $hasSupplier ? generateUrl($supplier) : generateUrl(); ?>">
  238. <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">
  239. <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"/>
  240. </svg>
  241. <?php echo $hasSupplier ? $supplier : DEFAULT_TITLE; ?>
  242. </a>
  243. <?php if ($hasSupplier) : ?>
  244. <span class="navbar-text text-muted">
  245. <?php if ($isConfig) : ?>
  246. <a class="text-reset" href="<?php echo generateUrl($supplier); ?>">Retour</a>
  247. <?php else : ?>
  248. <?php if ($hasPassword) : ?>
  249. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-lock" viewBox="0 0 16 16">
  250. <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"/>
  251. </svg>
  252. <?php else : ?>
  253. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-unlock" viewBox="0 0 16 16">
  254. <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"/>
  255. </svg>
  256. <?php endif; ?>
  257. <a tabindex="-1" class="text-reset" href="<?php printf('%s?action=config', generateUrl($supplier)); ?>">Configuration</a>
  258. <?php endif; ?>
  259. </span>
  260. <?php endif; ?>
  261. </div>
  262. </nav>
  263. </header>
  264. <main>
  265. <?php if (!$hasSupplier) : ?>
  266. <section class="container-fluid">
  267. <div class="row my-3">
  268. <div class="col">
  269. <div class="alert alert-danger" role="alert">
  270. Pas de fournisseur !
  271. </div>
  272. </div>
  273. </div>
  274. </section>
  275. <?php else : ?>
  276. <?php if ($isConfig) : ?>
  277. <section class="container-fluid">
  278. <div class="row my-3 g-3">
  279. <div class="col">
  280. <h1>Configuration</h1>
  281. </div>
  282. </div>
  283. </section>
  284. <section class="container-fluid">
  285. <div class="row g-3">
  286. <form action="<?php echo generateUrl($supplier); ?>" method="post">
  287. <div class="row mb-3">
  288. <label for="title" class="col-sm-2 col-form-label">Titre</label>
  289. <div class="col-sm-10">
  290. <input class="form-control" type="text" name="title" value="<?php echo htmlspecialchars($config[$supplier]['title']); ?>" placeholder="<?php echo $supplier; ?>" />
  291. <div class="form-text">Le titre de la page. Par défaut ce sera le nom du fournisseur </div>
  292. </div>
  293. </div>
  294. <div class="row mb-3">
  295. <label for="description" class="col-sm-2 col-form-label">Description</label>
  296. <div class="col-sm-10">
  297. <textarea class="form-control js-ckeditor" name="description" rows="10"><?php echo $config[$supplier]['description']; ?></textarea>
  298. <div class="form-text">La description affichée sous le titre.</div>
  299. </div>
  300. </div>
  301. <div class="row mb-3">
  302. <label for="choices" class="col-sm-2 col-form-label">Choix</label>
  303. <div class="col-sm-10">
  304. <textarea class="form-control" name="choices" rows="5"><?php echo implode(PHP_EOL, $config[$supplier]['choices']); ?></textarea>
  305. <div class="form-text">Les différents choix possibles. Un par ligne. Ou pas.</div>
  306. </div>
  307. </div>
  308. <div class="row mb-3">
  309. <label for="start" class="col-sm-2 col-form-label">Début</label>
  310. <div class="col-sm-10">
  311. <input class="form-control" type="date" name="start" value="<?php echo $config[$supplier]['start']; ?>" />
  312. <div class="form-text">La date du premier événement, si nécessaire de le préciser.</div>
  313. </div>
  314. </div>
  315. <div class="row mb-3">
  316. <label for="frequency" class="col-sm-2 col-form-label">Fréquence</label>
  317. <div class="col-sm-10">
  318. <input class="form-control" type="text" name="frequency" value="<?php echo $config[$supplier]['frequency']; ?>" />
  319. <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>
  320. </div>
  321. </div>
  322. <div class="row mb-3">
  323. <label for="excludes" class="col-sm-2 col-form-label">Exceptions</label>
  324. <div class="col-sm-10">
  325. <textarea class="form-control" name="excludes" rows="5"><?php echo implode(PHP_EOL, $config[$supplier]['excludes']); ?></textarea>
  326. <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>
  327. </div>
  328. </div>
  329. <div class="row mb-3">
  330. <label for="password" class="col-sm-2 col-form-label">Mot de passe</label>
  331. <div class="col-sm-10">
  332. <input class="form-control" type="text" name="password" value="<?php echo $config[$supplier]['password']; ?>" />
  333. <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>
  334. </div>
  335. </div>
  336. <div class="row">
  337. <div class="col mb-3">
  338. <button class="btn btn-primary" type="submit" name="action" value="config">Enregistrer</button>
  339. </div>
  340. </div>
  341. </form>
  342. </div>
  343. </section>
  344. <?php else : ?>
  345. <section class="container-fluid">
  346. <div class="row my-3">
  347. <div class="col">
  348. <h1>
  349. <div class="btn-group float-end" role="group">
  350. <?php if (isset($previousEvent)) : ?>
  351. <a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $previousEvent); ?>" title="Événement précédent">
  352. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-left" viewBox="0 0 16 16">
  353. <path fill-rule="evenodd" d="M15 8a.5.5 0 0 0-.5-.5H2.707l3.147-3.146a.5.5 0 1 0-.708-.708l-4 4a.5.5 0 0 0 0 .708l4 4a.5.5 0 0 0 .708-.708L2.707 8.5H14.5A.5.5 0 0 0 15 8z"/>
  354. </svg>
  355. </a>
  356. <?php endif; ?>
  357. <a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $event); ?>" title="Cet événement">
  358. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-link" viewBox="0 0 16 16">
  359. <path d="M6.354 5.5H4a3 3 0 0 0 0 6h3a3 3 0 0 0 2.83-4H9c-.086 0-.17.01-.25.031A2 2 0 0 1 7 10.5H4a2 2 0 1 1 0-4h1.535c.218-.376.495-.714.82-1z"/>
  360. <path d="M9 5.5a3 3 0 0 0-2.83 4h1.098A2 2 0 0 1 9 6.5h3a2 2 0 1 1 0 4h-1.535a4.02 4.02 0 0 1-.82 1H12a3 3 0 1 0 0-6H9z"/>
  361. </svg>
  362. </a>
  363. <?php if (isset($nextEvent)) : ?>
  364. <a class="btn btn-outline-primary" href="<?php echo generateUrl($supplier, $nextEvent); ?>" title="Événement suivant">
  365. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-right" viewBox="0 0 16 16">
  366. <path fill-rule="evenodd" d="M1 8a.5.5 0 0 1 .5-.5h11.793l-3.147-3.146a.5.5 0 0 1 .708-.708l4 4a.5.5 0 0 1 0 .708l-4 4a.5.5 0 0 1-.708-.708L13.293 8.5H1.5A.5.5 0 0 1 1 8z"/>
  367. </svg>
  368. </a>
  369. <?php endif; ?>
  370. </div>
  371. <?php echo $config[$supplier]['title']; ?>
  372. <?php echo $config[$supplier]['subtitle']; ?>
  373. </h1>
  374. <?php if (!empty($config[$supplier]['description'])) : ?>
  375. <p class="lead"><?php echo $config[$supplier]['description']; ?></p>
  376. <?php endif; ?>
  377. </div>
  378. </div>
  379. </section>
  380. <section class="container-fluid">
  381. <div class="row g-3">
  382. <form class="js-localremember bg-dark text-light" action="<?php echo generateUrl($supplier); ?>" method="post">
  383. <div class="row my-3">
  384. <label for="title" class="col-sm-2 col-form-label">Nom</label>
  385. <div class="col-sm-10">
  386. <input class="form-control" type="text" name="name" required placeholder="Nom" />
  387. </div>
  388. </div>
  389. <?php if (!empty($config[$supplier]['choices'])) : ?>
  390. <div class="row mb-3">
  391. <label for="title" class="col-sm-2 col-form-label">Choix</label>
  392. <div class="col-sm-10">
  393. <div class="btn-group" role="group">
  394. <?php foreach ($config[$supplier]['choices'] as $index => $choice) : ?>
  395. <input type="radio" class="btn-check" id="<?php printf('option%d', $index); ?>" autocomplete="off" name="choice" value="<?php echo $choice; ?>" />
  396. <label class="btn btn-outline-light" for="<?php printf('option%d', $index); ?>"><?php echo $choice; ?></label>
  397. <?php endforeach; ?>
  398. </div>
  399. </div>
  400. </div>
  401. <?php endif; ?>
  402. <div class="row">
  403. <div class="col mb-3">
  404. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  405. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  406. <?php if (empty($config[$supplier]['choices'])) : ?>
  407. <input type="hidden" name="choice" value="" />
  408. <?php endif; ?>
  409. <button class="btn btn-primary" type="submit" name="action" value="insert">Commander</button>
  410. </div>
  411. </div>
  412. </form>
  413. </div>
  414. </section>
  415. <section class="container-fluid">
  416. <div class="row my-3">
  417. <?php if (!empty($items)) : ?>
  418. <div class="col-12">
  419. <div class="table-responsive">
  420. <table class="table table-striped table-hover align-middle">
  421. <thead>
  422. <tr>
  423. <th scope="col">
  424. Nom
  425. </th>
  426. <?php if (!empty($config[$supplier]['choices'])) : ?>
  427. <th scope="col">
  428. Choix
  429. </th>
  430. <?php endif; ?>
  431. <th scope="col">
  432. &nbsp;
  433. </th>
  434. </tr>
  435. </thead>
  436. <tbody>
  437. <?php foreach ($items as $item) : ?>
  438. <tr>
  439. <td>
  440. <?php echo $item['name']; ?>
  441. </td>
  442. <?php if (!empty($config[$supplier]['choices'])) : ?>
  443. <td>
  444. <?php if (!empty($item['choice'])) : ?>
  445. <?php echo $item['choice']; ?>
  446. <?php endif; ?>
  447. </td>
  448. <?php endif; ?>
  449. <td>
  450. <form onsubmit="return confirm('Souhaitez-vous vraiment annuler cette commande ?');">
  451. <input type="hidden" name="supplier" value="<?php echo $supplier; ?>" />
  452. <input type="hidden" name="event" value="<?php echo $event; ?>" />
  453. <input type="hidden" name="name" value="<?php echo $item['name']; ?>" />
  454. <input type="hidden" name="choice" value="<?php echo $item['choice']; ?>" />
  455. <button class="btn btn-secondary float-end" type="submit" name="action" value="delete">Annuler</button>
  456. </form>
  457. </td>
  458. </tr>
  459. <?php endforeach; ?>
  460. </tbody>
  461. </table>
  462. </div>
  463. </div>
  464. <?php endif; ?>
  465. <div class="col-12">
  466. <ul class="list-group">
  467. <li class="list-group-item d-flex justify-content-between align-items-center">
  468. Commandes
  469. <span class="badge bg-primary rounded-pill"><?php echo count($items); ?></span>
  470. </li>
  471. <?php foreach ($stats as $choice => $count) : ?>
  472. <li class="list-group-item d-flex justify-content-between align-items-center">
  473. <?php echo $choice; ?>
  474. <span class="badge bg-secondary rounded-pill"><?php echo $count; ?></span>
  475. </li>
  476. <?php endforeach; ?>
  477. </ul>
  478. </div>
  479. </div>
  480. </section>
  481. <?php endif; ?>
  482. <?php endif; ?>
  483. </main>
  484. <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>
  485. <?php if ($isConfig) : ?>
  486. <script src="https://cdn.ckeditor.com/ckeditor5/31.0.0/classic/ckeditor.js"></script>
  487. <script>
  488. document.querySelectorAll('.js-ckeditor').forEach(function (element) {
  489. ClassicEditor.create(element).catch(error => { console.error(error); });
  490. });
  491. </script>
  492. <?php endif; ?>
  493. <script>
  494. document.querySelectorAll('.js-localremember').forEach(function (form) {
  495. const fields = [ 'name', 'choice' ];
  496. form.addEventListener('submit', function (event) {
  497. fields.forEach(function (field) {
  498. window.localStorage.setItem('mon_panier_bio_' + field, form.elements[field].value);
  499. });
  500. });
  501. fields.forEach(function (field) {
  502. if (
  503. (form.elements[field].value === '')
  504. && (window.localStorage.getItem('mon_panier_bio_' + field) !== null)
  505. ) {
  506. form.elements[field].value = window.localStorage.getItem('mon_panier_bio_' + field);
  507. }
  508. });
  509. });
  510. </script>
  511. </body>
  512. </html>