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.

496 lines
20 KiB

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