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.

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