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.

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