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.

549 lines
22 KiB

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