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.

584 lines
23 KiB

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