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.

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